Skip to content

Commit

Permalink
some clarity improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Dec 17, 2024
1 parent c77010e commit f0bfb61
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/moduletest/mocking/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func makeUnknown(target *configschema.Attribute, _ cty.Value, _ cty.Path) (cty.V
type MockedData struct {
Value cty.Value
Range hcl.Range
ComputedAsUnknown bool // If true, computed values are replaced with unknown.
ComputedAsUnknown bool // If true, computed values are replaced with unknown, otherwise they are replaced with generated values.
}

// NewMockedData creates a new MockedData struct with the given value and range.
Expand Down
48 changes: 32 additions & 16 deletions internal/terraform/node_resource_abstract_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,11 @@ func (n *NodeAbstractResourceInstance) plan(
if priorVal.IsNull() {
// Then we are actually creating something, so let's populate the
// computed values from our override value.
override, overrideDiags := mocking.PlanComputedValuesForResource(proposedNewVal, getMockedData(n.override, true), schema)
override, overrideDiags := mocking.PlanComputedValuesForResource(proposedNewVal, &mocking.MockedData{
Value: n.override.Values,
Range: n.override.Range,
ComputedAsUnknown: n.override.IgnoreValues,
}, schema)
resp = providers.PlanResourceChangeResponse{
PlannedState: override,
Diagnostics: overrideDiags,
Expand Down Expand Up @@ -1090,7 +1094,11 @@ func (n *NodeAbstractResourceInstance) plan(
if n.override != nil {
// In this case, we are always creating the resource so we don't
// do any validation, and just call out to the mocking library.
override, overrideDiags := mocking.PlanComputedValuesForResource(proposedNewVal, getMockedData(n.override, true), schema)
override, overrideDiags := mocking.PlanComputedValuesForResource(proposedNewVal, &mocking.MockedData{
Value: n.override.Values,
Range: n.override.Range,
ComputedAsUnknown: n.override.IgnoreValues,
}, schema)
resp = providers.PlanResourceChangeResponse{
PlannedState: override,
Diagnostics: overrideDiags,
Expand Down Expand Up @@ -1242,18 +1250,18 @@ func (n *NodeAbstractResourceInstance) plan(
return plan, state, deferred, keyData, diags
}

func getMockedData(override *configs.Override, isPlan bool) *mocking.MockedData {
if override == nil {
return nil
}
return &mocking.MockedData{
Value: override.Values,
Range: override.Range,
// Apply never ignores computed values. This attribute only matters
// when we are planning.
ComputedAsUnknown: override.IgnoreValues && isPlan,
}
}
// func getMockedData(override *configs.Override, isPlan bool) *mocking.MockedData {
// if override == nil {
// return nil
// }
// return &mocking.MockedData{
// Value: override.Values,
// Range: override.Range,
// // Apply never ignores computed values. This attribute only matters
// // when we are planning.
// ComputedAsUnknown: override.IgnoreValues && isPlan,
// }
// }

func (n *NodeAbstractResource) processIgnoreChanges(prior, config cty.Value, schema *configschema.Block) (cty.Value, tfdiags.Diagnostics) {
// ignore_changes only applies when an object already exists, since we
Expand Down Expand Up @@ -1551,7 +1559,11 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal

var resp providers.ReadDataSourceResponse
if n.override != nil {
override, overrideDiags := mocking.ComputedValuesForDataSource(configVal, getMockedData(n.override, false), schema)
override, overrideDiags := mocking.ComputedValuesForDataSource(configVal, &mocking.MockedData{
Value: n.override.Values,
Range: n.override.Range,
ComputedAsUnknown: false,
}, schema)
resp = providers.ReadDataSourceResponse{
State: override,
Diagnostics: overrideDiags,
Expand Down Expand Up @@ -2506,7 +2518,11 @@ func (n *NodeAbstractResourceInstance) apply(
// values the first time the object is created. Otherwise, we're happy
// to just apply whatever the user asked for.
if change.Action == plans.Create {
override, overrideDiags := mocking.ApplyComputedValuesForResource(unmarkedAfter, getMockedData(n.override, false), schema)
override, overrideDiags := mocking.ApplyComputedValuesForResource(unmarkedAfter, &mocking.MockedData{
Value: n.override.Values,
Range: n.override.Range,
ComputedAsUnknown: false,
}, schema)
resp = providers.ApplyResourceChangeResponse{
NewState: override,
Diagnostics: overrideDiags,
Expand Down
6 changes: 5 additions & 1 deletion internal/terraform/node_resource_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,11 @@ func (n *NodePlannableResourceInstance) importState(ctx EvalContext, addr addrs.

// Let's pretend we're reading the value as a data source so we
// pre-compute values now as if the resource has already been created.
override, overrideDiags := mocking.ComputedValuesForDataSource(configVal, getMockedData(n.override, false), schema)
override, overrideDiags := mocking.ComputedValuesForDataSource(configVal, &mocking.MockedData{
Value: n.override.Values,
Range: n.override.Range,
ComputedAsUnknown: false,
}, schema)
resp = providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
Expand Down

0 comments on commit f0bfb61

Please sign in to comment.