Skip to content

Commit 12e76c0

Browse files
authored
Merge pull request hashicorp#9021 from kyhavlov/destroy_target_fix
core: Fixed variables not being in scope for destroy -target on modules
2 parents bba511e + 3a2819d commit 12e76c0

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎terraform/context_apply_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,68 @@ module.child:
31193119
}
31203120
}
31213121

3122+
func TestContext2Apply_destroyTargetWithModuleVariableAndCount(t *testing.T) {
3123+
m := testModule(t, "apply-destroy-mod-var-and-count")
3124+
p := testProvider("aws")
3125+
p.ApplyFn = testApplyFn
3126+
p.DiffFn = testDiffFn
3127+
3128+
var state *State
3129+
var err error
3130+
{
3131+
ctx := testContext2(t, &ContextOpts{
3132+
Module: m,
3133+
Providers: map[string]ResourceProviderFactory{
3134+
"aws": testProviderFuncFixed(p),
3135+
},
3136+
})
3137+
3138+
// First plan and apply a create operation
3139+
if _, err := ctx.Plan(); err != nil {
3140+
t.Fatalf("plan err: %s", err)
3141+
}
3142+
3143+
state, err = ctx.Apply()
3144+
if err != nil {
3145+
t.Fatalf("apply err: %s", err)
3146+
}
3147+
}
3148+
3149+
{
3150+
ctx := testContext2(t, &ContextOpts{
3151+
Destroy: true,
3152+
Module: m,
3153+
State: state,
3154+
Providers: map[string]ResourceProviderFactory{
3155+
"aws": testProviderFuncFixed(p),
3156+
},
3157+
Targets: []string{"module.child"},
3158+
})
3159+
3160+
_, err := ctx.Plan()
3161+
if err != nil {
3162+
t.Fatalf("plan err: %s", err)
3163+
}
3164+
3165+
// Destroy, targeting the module explicitly
3166+
state, err = ctx.Apply()
3167+
if err != nil {
3168+
t.Fatalf("destroy apply err: %s", err)
3169+
}
3170+
}
3171+
3172+
//Test that things were destroyed
3173+
actual := strings.TrimSpace(state.String())
3174+
expected := strings.TrimSpace(`
3175+
<no state>
3176+
module.child:
3177+
<no state>
3178+
`)
3179+
if actual != expected {
3180+
t.Fatalf("expected: \n%s\n\nbad: \n%s", expected, actual)
3181+
}
3182+
}
3183+
31223184
func TestContext2Apply_destroyWithModuleVariableAndCountNested(t *testing.T) {
31233185
m := testModule(t, "apply-destroy-mod-var-and-count-nested")
31243186
p := testProvider("aws")

‎terraform/transform_targets.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ func (t *TargetsTransformer) selectTargetedNodes(
8686
var err error
8787
if t.Destroy {
8888
deps, err = g.Descendents(v)
89+
90+
// Select any variables that we depend on in case we need them later for
91+
// interpolating in the count
92+
ancestors, _ := g.Ancestors(v)
93+
for _, a := range ancestors.List() {
94+
if _, ok := a.(*GraphNodeConfigVariableFlat); ok {
95+
deps.Add(a)
96+
}
97+
}
8998
} else {
9099
deps, err = g.Ancestors(v)
91100
}

0 commit comments

Comments
 (0)