terraform: destroy plans work with modules

This commit is contained in:
Mitchell Hashimoto 2014-09-25 20:44:34 -07:00
parent e5e51d7b17
commit 77b1c7daa0
5 changed files with 103 additions and 12 deletions

View File

@ -703,21 +703,34 @@ func (c *walkContext) planDestroyWalkFn() depgraph.WalkFunc {
result.init()
return func(n *depgraph.Noun) error {
rn, ok := n.Meta.(*GraphNodeResource)
if !ok {
return nil
}
switch m := n.Meta.(type) {
case *GraphNodeModule:
// Build another walkContext for this module and walk it.
wc := c.Context.walkContext(c.Operation, m.Path)
r := rn.Resource
// Set the graph to specifically walk this subgraph
wc.graph = m.Graph
// Preserve the meta
wc.Meta = c.Meta
return wc.Walk()
case *GraphNodeResource:
r := m.Resource
if r.State != nil && r.State.ID != "" {
log.Printf("[DEBUG] %s: Making for destroy", r.Id)
l.Lock()
defer l.Unlock()
result.Diff.RootModule().Resources[r.Id] = &InstanceDiff{Destroy: true}
md := result.Diff.ModuleByPath(c.Path)
if md == nil {
md = result.Diff.AddModule(c.Path)
}
md.Resources[r.Id] = &InstanceDiff{Destroy: true}
} else {
log.Printf("[DEBUG] %s: Not marking for destroy, no ID", r.Id)
}
}
return nil
}

View File

@ -2117,6 +2117,56 @@ func TestContextPlan_destroy(t *testing.T) {
}
}
func TestContextPlan_moduleDestroy(t *testing.T) {
m := testModule(t, "plan-module-destroy")
p := testProvider("aws")
p.DiffFn = testDiffFn
s := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
},
},
},
},
&ModuleState{
Path: []string{"root", "child"},
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
},
},
},
},
},
}
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: s,
})
plan, err := ctx.Plan(&PlanOpts{Destroy: true})
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleDestroyStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_diffVar(t *testing.T) {
m := testModule(t, "plan-diffvar")
p := testProvider("aws")

View File

@ -528,6 +528,24 @@ STATE:
<no state>
`
const testTerraformPlanModuleDestroyStr = `
DIFF:
DESTROY: aws_instance.foo
module.child:
DESTROY: aws_instance.foo
STATE:
aws_instance.foo:
ID = bar
module.child:
aws_instance.foo:
ID = bar
`
const testTerraformPlanModuleInputStr = `
DIFF:

View File

@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
num = "2"
}

View File

@ -0,0 +1,7 @@
module "child" {
source = "./child"
}
resource "aws_instance" "foo" {
num = "2"
}