terraform: Set Destroy: true to PlanOpts to delete everything

This commit is contained in:
Mitchell Hashimoto 2014-06-29 16:28:50 -07:00
parent 5c836ab861
commit acc6686cca
2 changed files with 57 additions and 1 deletions

View File

@ -256,7 +256,12 @@ func (t *Terraform) planWalkFn(result *Plan, opts *PlanOpts) depgraph.WalkFunc {
h.PreDiff(r.Id, r.State)
}
if r.Config == nil {
if opts.Destroy {
if r.State.ID != "" {
log.Printf("[DEBUG] %s: Making for destroy", r.Id)
diff = &ResourceDiff{Destroy: true}
}
} else if r.Config == nil {
log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)
// This is an orphan (no config), so we mark it to be destroyed

View File

@ -188,6 +188,43 @@ func TestTerraformPlan_computed(t *testing.T) {
}
}
func TestTerraformPlan_destroy(t *testing.T) {
c := testConfig(t, "plan-good")
tf := testTerraform2(t, nil)
s := &State{
Resources: map[string]*ResourceState{
"aws_instance.one": &ResourceState{
ID: "bar",
Type: "aws_instance",
},
"aws_instance.two": &ResourceState{
ID: "baz",
Type: "aws_instance",
},
},
}
plan, err := tf.Plan(&PlanOpts{
Destroy: true,
Config: c,
State: s,
})
if err != nil {
t.Fatalf("err: %s", err)
}
if len(plan.Diff.Resources) != 2 {
t.Fatalf("bad: %#v", plan.Diff.Resources)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanDestroyStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestTerraformPlan_hook(t *testing.T) {
c := testConfig(t, "plan-good")
h := new(MockHook)
@ -606,6 +643,20 @@ STATE:
<no state>
`
const testTerraformPlanDestroyStr = `
DIFF:
DESTROY: aws_instance.one
DESTROY: aws_instance.two
STATE:
aws_instance.one:
ID = bar
aws_instance.two:
ID = baz
`
const testTerraformPlanOrphanStr = `
DIFF: