From acc6686cca8e924ebf5314d776811956f8f6e9be Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 29 Jun 2014 16:28:50 -0700 Subject: [PATCH] terraform: Set Destroy: true to PlanOpts to delete everything --- terraform/terraform.go | 7 ++++- terraform/terraform_test.go | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/terraform/terraform.go b/terraform/terraform.go index 5dc852ce03..978eac343d 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -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 diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index ae8496995a..4998b787b3 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -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: ` +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: