Skip to content

Commit

Permalink
terraform: prefix destroy resources with module path [GH-2767]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 22, 2016
1 parent 03bdf13 commit b921910
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
57 changes: 57 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,63 @@ func TestContext2Apply_destroy(t *testing.T) {
}
}

// https://github.com/hashicorp/terraform/issues/2767
func TestContext2Apply_destroyModulePrefix(t *testing.T) {
m := testModule(t, "apply-destroy-module-resource-prefix")
h := new(MockHook)
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Hooks: []Hook{h},
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})

// First plan and apply a create operation
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}

state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify that we got the apply info correct
if v := h.PreApplyInfo.HumanId(); v != "module.child.aws_instance.foo" {
t.Fatalf("bad: %s", v)
}

// Next, plan and apply a destroy operation and reset the hook
h = new(MockHook)
ctx = testContext2(t, &ContextOpts{
Destroy: true,
State: state,
Module: m,
Hooks: []Hook{h},
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})

if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}

state, err = ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

// Test that things were destroyed
if v := h.PreApplyInfo.HumanId(); v != "module.child.aws_instance.foo" {
t.Fatalf("bad: %s", v)
}
}

func TestContext2Apply_destroyNestedModule(t *testing.T) {
m := testModule(t, "apply-destroy-nested-module")
p := testProvider("aws")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resource "aws_instance" "foo" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "child" {
source = "./child"
}
3 changes: 3 additions & 0 deletions terraform/transform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,9 @@ func (n *graphNodeExpandedResourceDestroy) EvalTree() EvalNode {
Then: EvalNoop{},
},

// Load the instance info so we have the module path set
&EvalInstanceInfo{Info: info},

&EvalGetProvider{
Name: n.ProvidedBy()[0],
Output: &provider,
Expand Down

0 comments on commit b921910

Please sign in to comment.