Skip to content

Commit

Permalink
core: fix targeting in destroy w/ provisioners
Browse files Browse the repository at this point in the history
The `TargetTransform` was dropping provisioner nodes, which caused graph
validation to fail with messages about uninitialized provisioners when a
`terraform destroy` was attempted.

This was because `destroy` flops the dependency calculation to try and
address any nodes in the graph that "depend on" the target node. But we
still need to keep the provisioner node in the graph.

closes #1541
  • Loading branch information
phinze committed Apr 15, 2015
1 parent 4ef208d commit 66a5a39
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
42 changes: 42 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,48 @@ func TestContext2Validate_tainted(t *testing.T) {
}
}

func TestContext2Validate_targetedDestroy(t *testing.T) {
m := testModule(t, "validate-targeted")
p := testProvider("aws")
pr := testProvisioner()
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Provisioners: map[string]ResourceProvisionerFactory{
"shell": testProvisionerFuncFixed(pr),
},
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,
})

w, e := ctx.Validate()
if len(w) > 0 {
warnStr := ""
for _, v := range w {
warnStr = warnStr + " " + v
}
t.Fatalf("bad: %s", warnStr)
}
if len(e) > 0 {
t.Fatalf("bad: %s", e)
}
}

func TestContext2Validate_varRef(t *testing.T) {
m := testModule(t, "validate-variable-ref")
p := testProvider("aws")
Expand Down
13 changes: 13 additions & 0 deletions terraform/test-fixtures/validate-targeted/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "aws_instance" "foo" {
num = "2"
provisioner "shell" {
command = "echo hi"
}
}

resource "aws_instance" "bar" {
foo = "bar"
provisioner "shell" {
command = "echo hi"
}
}
6 changes: 6 additions & 0 deletions terraform/transform_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (t *TargetsTransformer) selectTargetedNodes(
continue
}

// Keep all provisioners; they'll be pruned later if necessary
if r, ok := v.(GraphNodeProvisioner); ok {
targetedNodes.Add(r)
continue
}

// For the remaining filter, we only care about addressable nodes
r, ok := v.(GraphNodeAddressable)
if !ok {
Expand Down

0 comments on commit 66a5a39

Please sign in to comment.