From f0bfb611354428b028cc7d162516965be38da232 Mon Sep 17 00:00:00 2001 From: Samsondeen Dare Date: Tue, 17 Dec 2024 15:44:34 +0100 Subject: [PATCH] some clarity improvement --- internal/moduletest/mocking/values.go | 2 +- .../node_resource_abstract_instance.go | 48 ++++++++++++------- .../terraform/node_resource_plan_instance.go | 6 ++- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/internal/moduletest/mocking/values.go b/internal/moduletest/mocking/values.go index 1ff9f12ddb19..d374756ca35b 100644 --- a/internal/moduletest/mocking/values.go +++ b/internal/moduletest/mocking/values.go @@ -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. diff --git a/internal/terraform/node_resource_abstract_instance.go b/internal/terraform/node_resource_abstract_instance.go index 092a9c3634fe..c28799c70c5f 100644 --- a/internal/terraform/node_resource_abstract_instance.go +++ b/internal/terraform/node_resource_abstract_instance.go @@ -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, @@ -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, @@ -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 @@ -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, @@ -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, diff --git a/internal/terraform/node_resource_plan_instance.go b/internal/terraform/node_resource_plan_instance.go index af8395d7e0ab..23cd87762f96 100644 --- a/internal/terraform/node_resource_plan_instance.go +++ b/internal/terraform/node_resource_plan_instance.go @@ -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{ {