core: Fixed variables not being in scope for destroy -target on modules

This commit is contained in:
Kyle Havlovitz 2016-09-23 16:07:41 -04:00
parent 74eb2e74c1
commit 3a2819de25
2 changed files with 71 additions and 0 deletions

View File

@ -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(`
<no state>
module.child:
<no state>
`)
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")

View File

@ -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)
}