terraform: destroy graph must connect edges for proper target ordering

This connects the destroy edges so that when a `-target` is specified on
a destroy, the proper dependencies get destroyed as well.
This commit is contained in:
Mitchell Hashimoto 2016-11-10 21:14:44 -08:00
parent 89919b605b
commit b68b95dad0
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 57 additions and 3 deletions

View File

@ -4963,6 +4963,43 @@ aws_instance.bar:
`)
}
func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) {
m := testModule(t, "apply-destroy-targeted-count")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.foo": resourceState("aws_instance", "i-bcd345"),
"aws_instance.bar": resourceState("aws_instance", "i-abc123"),
},
},
},
},
Targets: []string{"aws_instance.foo"},
Destroy: true,
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
checkStateString(t, state, `<no state>`)
}
// https://github.com/hashicorp/terraform/issues/4462
func TestContext2Apply_targetedDestroyModule(t *testing.T) {
m := testModule(t, "apply-targeted-module")

View File

@ -45,12 +45,17 @@ func (b *DestroyPlanGraphBuilder) Steps() []GraphTransformer {
State: b.State,
},
// Target
&TargetsTransformer{Targets: b.Targets},
// Attach the configuration to any resources
&AttachResourceConfigTransformer{Module: b.Module},
// Destruction ordering. We require this only so that
// targeting below will prune the correct things.
&DestroyEdgeTransformer{Module: b.Module, State: b.State},
// Target. Note we don't set "Destroy: true" here since we already
// created proper destroy ordering.
&TargetsTransformer{Targets: b.Targets},
// Single root
&RootTransformer{},
}

View File

@ -10,6 +10,11 @@ type NodePlanDestroyableResource struct {
*NodeAbstractResource
}
// GraphNodeDestroyer
func (n *NodePlanDestroyableResource) DestroyAddr() *ResourceAddress {
return n.Addr
}
// GraphNodeEvalable
func (n *NodePlanDestroyableResource) EvalTree() EvalNode {
addr := n.NodeAbstractResource.Addr

View File

@ -0,0 +1,7 @@
resource "aws_instance" "foo" {
count = 3
}
resource "aws_instance" "bar" {
instances = ["${aws_instance.foo.*.id}"]
}