From 3e281e1edac45ac0675b7818f6266c0f9ecc8d2d Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 20 Sep 2022 10:59:15 -0400 Subject: [PATCH] default optional+computed to prior value --- internal/terraform/context_plan_test.go | 7 ++++++- internal/terraform/node_resource_abstract_instance.go | 2 +- .../testdata/plan-ignore-changes-wildcard/main.tf | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/terraform/context_plan_test.go b/internal/terraform/context_plan_test.go index 8fc9522fe2..ba34e20b81 100644 --- a/internal/terraform/context_plan_test.go +++ b/internal/terraform/context_plan_test.go @@ -4543,6 +4543,11 @@ func TestContext2Plan_ignoreChangesWildcard(t *testing.T) { t.Error("computed id set in plan config") } + foo := req.Config.GetAttr("foo") + if foo.IsNull() { + t.Error(`missing "foo" during plan, was set to "bar" in state and config`) + } + return testDiffFn(req) } @@ -4552,7 +4557,7 @@ func TestContext2Plan_ignoreChangesWildcard(t *testing.T) { mustResourceInstanceAddr("aws_instance.foo").Resource, &states.ResourceInstanceObjectSrc{ Status: states.ObjectReady, - AttrsJSON: []byte(`{"id":"bar","ami":"ami-abcd1234","instance":"t2.micro","type":"aws_instance"}`), + AttrsJSON: []byte(`{"id":"bar","ami":"ami-abcd1234","instance":"t2.micro","type":"aws_instance","foo":"bar"}`), }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`), ) diff --git a/internal/terraform/node_resource_abstract_instance.go b/internal/terraform/node_resource_abstract_instance.go index 55b543da95..5df414da89 100644 --- a/internal/terraform/node_resource_abstract_instance.go +++ b/internal/terraform/node_resource_abstract_instance.go @@ -1165,7 +1165,7 @@ func (n *NodeAbstractResource) processIgnoreChanges(prior, config cty.Value, sch // to the provider as if they were included in the configuration. ret, _ := cty.Transform(prior, func(path cty.Path, v cty.Value) (cty.Value, error) { attr := schema.AttributeByPath(path) - if attr != nil && attr.Computed { + if attr != nil && attr.Computed && !attr.Optional { return cty.NullVal(v.Type()), nil } diff --git a/internal/terraform/testdata/plan-ignore-changes-wildcard/main.tf b/internal/terraform/testdata/plan-ignore-changes-wildcard/main.tf index d4e55a8858..ac594a9eb8 100644 --- a/internal/terraform/testdata/plan-ignore-changes-wildcard/main.tf +++ b/internal/terraform/testdata/plan-ignore-changes-wildcard/main.tf @@ -5,6 +5,7 @@ variable "bar" {} resource "aws_instance" "foo" { ami = "${var.foo}" instance = "${var.bar}" + foo = "bar" lifecycle { ignore_changes = all