command/plan: add -destroy flag to plan a destroy

This commit is contained in:
Mitchell Hashimoto 2014-07-01 09:12:05 -07:00
parent 935573d01c
commit 64363da9dd
4 changed files with 59 additions and 3 deletions

View File

@ -42,6 +42,21 @@ func testPlanFile(t *testing.T, plan *terraform.Plan) string {
return path return path
} }
func testReadPlan(t *testing.T, path string) *terraform.Plan {
f, err := os.Open(path)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
p, err := terraform.ReadPlan(f)
if err != nil {
t.Fatalf("err: %s", err)
}
return p
}
func testStateFile(t *testing.T, s *terraform.State) string { func testStateFile(t *testing.T, s *terraform.State) string {
path := testTempFile(t) path := testTempFile(t)

View File

@ -20,10 +20,11 @@ type PlanCommand struct {
} }
func (c *PlanCommand) Run(args []string) int { func (c *PlanCommand) Run(args []string) int {
var refresh bool var destroy, refresh bool
var outPath, statePath string var outPath, statePath string
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError) cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh") cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
cmdFlags.StringVar(&outPath, "out", "", "path") cmdFlags.StringVar(&outPath, "out", "", "path")
cmdFlags.StringVar(&statePath, "state", "", "path") cmdFlags.StringVar(&statePath, "state", "", "path")
@ -80,8 +81,9 @@ func (c *PlanCommand) Run(args []string) int {
} }
plan, err := tf.Plan(&terraform.PlanOpts{ plan, err := tf.Plan(&terraform.PlanOpts{
Config: b, Config: b,
State: state, Destroy: destroy,
State: state,
}) })
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err)) c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))

View File

@ -10,6 +10,43 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestPlan_destroy(t *testing.T) {
originalState := &terraform.State{
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
ID: "bar",
Type: "test_instance",
},
},
}
outPath := testTempFile(t)
statePath := testStateFile(t, originalState)
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
TFConfig: testTFConfig(p),
Ui: ui,
}
args := []string{
"-destroy",
"-out", outPath,
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
plan := testReadPlan(t, outPath)
for _, r := range plan.Diff.Resources {
if !r.Destroy {
t.Fatalf("bad: %#v", r)
}
}
}
func TestPlan_noState(t *testing.T) { func TestPlan_noState(t *testing.T) {
p := testProvider() p := testProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)

View File

@ -253,6 +253,8 @@ func (t *Terraform) planWalkFn(result *Plan, opts *PlanOpts) depgraph.WalkFunc {
if r.State.ID != "" { if r.State.ID != "" {
log.Printf("[DEBUG] %s: Making for destroy", r.Id) log.Printf("[DEBUG] %s: Making for destroy", r.Id)
diff = &ResourceDiff{Destroy: true} diff = &ResourceDiff{Destroy: true}
} else {
log.Printf("[DEBUG] %s: Not marking for destroy, no ID", r.Id)
} }
} else if r.Config == nil { } else if r.Config == nil {
log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id) log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)