Skip to content

Commit 48a999f

Browse files
authored
Merge pull request #31885 from hashicorp/backport/jbardin/ignore-changes-all-computed/centrally-factual-colt
Backport of filter computed attrs from `ignore_changes=all` into v1.3
2 parents a310613 + 40e85cd commit 48a999f

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

internal/terraform/context_plan_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,15 +4536,28 @@ func TestContext2Plan_ignoreChanges(t *testing.T) {
45364536
func TestContext2Plan_ignoreChangesWildcard(t *testing.T) {
45374537
m := testModule(t, "plan-ignore-changes-wildcard")
45384538
p := testProvider("aws")
4539-
p.PlanResourceChangeFn = testDiffFn
4539+
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
4540+
// computed attributes should not be set in config
4541+
id := req.Config.GetAttr("id")
4542+
if !id.IsNull() {
4543+
t.Error("computed id set in plan config")
4544+
}
4545+
4546+
foo := req.Config.GetAttr("foo")
4547+
if foo.IsNull() {
4548+
t.Error(`missing "foo" during plan, was set to "bar" in state and config`)
4549+
}
4550+
4551+
return testDiffFn(req)
4552+
}
45404553

45414554
state := states.NewState()
45424555
root := state.EnsureModule(addrs.RootModuleInstance)
45434556
root.SetResourceInstanceCurrent(
45444557
mustResourceInstanceAddr("aws_instance.foo").Resource,
45454558
&states.ResourceInstanceObjectSrc{
45464559
Status: states.ObjectReady,
4547-
AttrsJSON: []byte(`{"id":"bar","ami":"ami-abcd1234","instance":"t2.micro","type":"aws_instance"}`),
4560+
AttrsJSON: []byte(`{"id":"bar","ami":"ami-abcd1234","instance":"t2.micro","type":"aws_instance","foo":"bar"}`),
45484561
},
45494562
mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`),
45504563
)

internal/terraform/node_resource_abstract_instance.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ func (n *NodeAbstractResourceInstance) plan(
777777
// starting values.
778778
// Here we operate on the marked values, so as to revert any changes to the
779779
// marks as well as the value.
780-
configValIgnored, ignoreChangeDiags := n.processIgnoreChanges(priorVal, origConfigVal)
780+
configValIgnored, ignoreChangeDiags := n.processIgnoreChanges(priorVal, origConfigVal, schema)
781781
diags = diags.Append(ignoreChangeDiags)
782782
if ignoreChangeDiags.HasErrors() {
783783
return plan, state, keyData, diags
@@ -881,7 +881,7 @@ func (n *NodeAbstractResourceInstance) plan(
881881
// providers that we must accommodate the behavior for now, so for
882882
// ignore_changes to work at all on these values, we will revert the
883883
// ignored values once more.
884-
plannedNewVal, ignoreChangeDiags = n.processIgnoreChanges(unmarkedPriorVal, plannedNewVal)
884+
plannedNewVal, ignoreChangeDiags = n.processIgnoreChanges(unmarkedPriorVal, plannedNewVal, schema)
885885
diags = diags.Append(ignoreChangeDiags)
886886
if ignoreChangeDiags.HasErrors() {
887887
return plan, state, keyData, diags
@@ -1145,7 +1145,7 @@ func (n *NodeAbstractResourceInstance) plan(
11451145
return plan, state, keyData, diags
11461146
}
11471147

1148-
func (n *NodeAbstractResource) processIgnoreChanges(prior, config cty.Value) (cty.Value, tfdiags.Diagnostics) {
1148+
func (n *NodeAbstractResource) processIgnoreChanges(prior, config cty.Value, schema *configschema.Block) (cty.Value, tfdiags.Diagnostics) {
11491149
// ignore_changes only applies when an object already exists, since we
11501150
// can't ignore changes to a thing we've not created yet.
11511151
if prior.IsNull() {
@@ -1158,9 +1158,23 @@ func (n *NodeAbstractResource) processIgnoreChanges(prior, config cty.Value) (ct
11581158
if len(ignoreChanges) == 0 && !ignoreAll {
11591159
return config, nil
11601160
}
1161+
11611162
if ignoreAll {
1162-
return prior, nil
1163+
// If we are trying to ignore all attribute changes, we must filter
1164+
// computed attributes out from the prior state to avoid sending them
1165+
// to the provider as if they were included in the configuration.
1166+
ret, _ := cty.Transform(prior, func(path cty.Path, v cty.Value) (cty.Value, error) {
1167+
attr := schema.AttributeByPath(path)
1168+
if attr != nil && attr.Computed && !attr.Optional {
1169+
return cty.NullVal(v.Type()), nil
1170+
}
1171+
1172+
return v, nil
1173+
})
1174+
1175+
return ret, nil
11631176
}
1177+
11641178
if prior.IsNull() || config.IsNull() {
11651179
// Ignore changes doesn't apply when we're creating for the first time.
11661180
// Proposed should never be null here, but if it is then we'll just let it be.

internal/terraform/testdata/plan-ignore-changes-wildcard/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ variable "bar" {}
55
resource "aws_instance" "foo" {
66
ami = "${var.foo}"
77
instance = "${var.bar}"
8+
foo = "bar"
89

910
lifecycle {
1011
ignore_changes = all

0 commit comments

Comments
 (0)