From 3a2819de25514dde1ea716dd867ae0fead21f9bc Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Fri, 23 Sep 2016 16:07:41 -0400 Subject: [PATCH] core: Fixed variables not being in scope for destroy -target on modules --- terraform/context_apply_test.go | 62 +++++++++++++++++++++++++++++++++ terraform/transform_targets.go | 9 +++++ 2 files changed, 71 insertions(+) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index d0d9bcc2b..74fcf33bd 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -3119,6 +3119,68 @@ module.child: } } +func TestContext2Apply_destroyTargetWithModuleVariableAndCount(t *testing.T) { + m := testModule(t, "apply-destroy-mod-var-and-count") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + var state *State + var err error + { + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + // First plan and apply a create operation + if _, err := ctx.Plan(); err != nil { + t.Fatalf("plan err: %s", err) + } + + state, err = ctx.Apply() + if err != nil { + t.Fatalf("apply err: %s", err) + } + } + + { + ctx := testContext2(t, &ContextOpts{ + Destroy: true, + Module: m, + State: state, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Targets: []string{"module.child"}, + }) + + _, err := ctx.Plan() + if err != nil { + t.Fatalf("plan err: %s", err) + } + + // Destroy, targeting the module explicitly + state, err = ctx.Apply() + if err != nil { + t.Fatalf("destroy apply err: %s", err) + } + } + + //Test that things were destroyed + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(` + +module.child: + + `) + if actual != expected { + t.Fatalf("expected: \n%s\n\nbad: \n%s", expected, actual) + } +} + func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) { m := testModule(t, "apply-destroy-mod-var-and-count-nested") p := testProvider("aws") diff --git a/terraform/transform_targets.go b/terraform/transform_targets.go index 4e99badda..84393796e 100644 --- a/terraform/transform_targets.go +++ b/terraform/transform_targets.go @@ -86,6 +86,15 @@ func (t *TargetsTransformer) selectTargetedNodes( var err error if t.Destroy { deps, err = g.Descendents(v) + + // Select any variables that we depend on in case we need them later for + // interpolating in the count + ancestors, _ := g.Ancestors(v) + for _, a := range ancestors.List() { + if _, ok := a.(*GraphNodeConfigVariableFlat); ok { + deps.Add(a) + } + } } else { deps, err = g.Ancestors(v) }