From 4cd96ecfde09c51e93399dc7c204eaa0af93140c Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Tue, 15 Mar 2016 10:03:01 -0500 Subject: [PATCH] core: Don't filter ignore_changes on create The ignore_changes diff filter was stripping out attributes on Create but the diff was still making it down to the provider, so Create would end up missing attributes, causing a full failure if any required attributes were being ignored. This should fix `ignore_changes` problems with Required attributes. Refs #5627 --- terraform/context_apply_test.go | 41 +++++++++++++++++++ terraform/eval_ignore_changes.go | 6 +++ .../apply-ignore-changes-create/main.tf | 7 ++++ 3 files changed, 54 insertions(+) create mode 100644 terraform/test-fixtures/apply-ignore-changes-create/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index a815f3928aaa..361f27379af0 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -4084,3 +4084,44 @@ aws_instance.ifailedprovisioners: (1 tainted) t.Fatalf("expected state: \n%s\ngot: \n%s", expected, actual) } } + +func TestContext2Apply_ignoreChangesCreate(t *testing.T) { + m := testModule(t, "apply-ignore-changes-create") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if p, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } else { + t.Logf(p.String()) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + mod := state.RootModule() + if len(mod.Resources) != 1 { + t.Fatalf("bad: %s", state) + } + + actual := strings.TrimSpace(state.String()) + // Expect no changes from original state + expected := strings.TrimSpace(` +aws_instance.foo: + ID = foo + required_field = set + type = aws_instance +`) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} diff --git a/terraform/eval_ignore_changes.go b/terraform/eval_ignore_changes.go index cc2261313714..3ae1d4995f19 100644 --- a/terraform/eval_ignore_changes.go +++ b/terraform/eval_ignore_changes.go @@ -22,6 +22,12 @@ func (n *EvalIgnoreChanges) Eval(ctx EvalContext) (interface{}, error) { diff := *n.Diff ignoreChanges := n.Resource.Lifecycle.IgnoreChanges + // If we're just creating the resource, we shouldn't alter the + // Diff at all + if diff.ChangeType() == DiffCreate { + return nil, nil + } + for _, ignoredName := range ignoreChanges { for name := range diff.Attributes { if strings.HasPrefix(name, ignoredName) { diff --git a/terraform/test-fixtures/apply-ignore-changes-create/main.tf b/terraform/test-fixtures/apply-ignore-changes-create/main.tf new file mode 100644 index 000000000000..d470660ec1cc --- /dev/null +++ b/terraform/test-fixtures/apply-ignore-changes-create/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" { + required_field = "set" + + lifecycle { + ignore_changes = ["required_field"] + } +}