command: plan

This commit is contained in:
Mitchell Hashimoto 2014-06-20 11:47:02 -07:00
parent d1cfb38bb8
commit bff4b8a58c
4 changed files with 24 additions and 24 deletions

View File

@ -95,15 +95,15 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 return 1
} }
diff, err := tf.Diff(state) _, err = tf.Plan(state)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error running diff: %s", err)) c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
return 1 return 1
} }
state, err = tf.Apply(state, diff) state, err = tf.Apply(state, nil)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error applying diff: %s", err)) c.Ui.Error(fmt.Sprintf("Error applying plan: %s", err))
return 1 return 1
} }

View File

@ -11,17 +11,17 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
// DiffCommand is a Command implementation that compares a Terraform // PlanCommand is a Command implementation that compares a Terraform
// configuration to an actual infrastructure and shows the differences. // configuration to an actual infrastructure and shows the differences.
type DiffCommand struct { type PlanCommand struct {
TFConfig *terraform.Config TFConfig *terraform.Config
Ui cli.Ui Ui cli.Ui
} }
func (c *DiffCommand) Run(args []string) int { func (c *PlanCommand) Run(args []string) int {
var statePath string var statePath string
cmdFlags := flag.NewFlagSet("diff", flag.ContinueOnError) cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
cmdFlags.StringVar(&statePath, "state", "", "path") cmdFlags.StringVar(&statePath, "state", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
@ -31,7 +31,7 @@ func (c *DiffCommand) Run(args []string) int {
args = cmdFlags.Args() args = cmdFlags.Args()
if len(args) != 1 { if len(args) != 1 {
c.Ui.Error( c.Ui.Error(
"The diff command expects only one argument with the path\n" + "The plan command expects only one argument with the path\n" +
"to a Terraform configuration.\n") "to a Terraform configuration.\n")
cmdFlags.Usage() cmdFlags.Usage()
return 1 return 1
@ -69,24 +69,24 @@ func (c *DiffCommand) Run(args []string) int {
return 1 return 1
} }
diff, err := tf.Diff(state) plan, err := tf.Plan(state)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error running diff: %s", err)) c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
return 1 return 1
} }
if diff.Empty() { if plan.Diff.Empty() {
c.Ui.Output("No changes. Infrastructure is up-to-date.") c.Ui.Output("No changes. Infrastructure is up-to-date.")
} else { } else {
c.Ui.Output(strings.TrimSpace(diff.String())) c.Ui.Output(strings.TrimSpace(plan.String()))
} }
return 0 return 0
} }
func (c *DiffCommand) Help() string { func (c *PlanCommand) Help() string {
helpText := ` helpText := `
Usage: terraform diff [options] [terraform.tf] Usage: terraform plan [options] [terraform.tf]
Shows the differences between the Terraform configuration and Shows the differences between the Terraform configuration and
the actual state of an infrastructure. the actual state of an infrastructure.
@ -100,6 +100,6 @@ Options:
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }
func (c *DiffCommand) Synopsis() string { func (c *PlanCommand) Synopsis() string {
return "Show changes between Terraform config and infrastructure" return "Show changes between Terraform config and infrastructure"
} }

View File

@ -10,16 +10,16 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestDiff_noState(t *testing.T) { func TestPlan_noState(t *testing.T) {
p := testProvider() p := testProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &DiffCommand{ c := &PlanCommand{
TFConfig: testTFConfig(p), TFConfig: testTFConfig(p),
Ui: ui, Ui: ui,
} }
args := []string{ args := []string{
testFixturePath("diff"), testFixturePath("plan"),
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
@ -34,7 +34,7 @@ func TestDiff_noState(t *testing.T) {
} }
} }
func TestDiff_state(t *testing.T) { func TestPlan_state(t *testing.T) {
// Write out some prior state // Write out some prior state
tf, err := ioutil.TempFile("", "tf") tf, err := ioutil.TempFile("", "tf")
if err != nil { if err != nil {
@ -60,14 +60,14 @@ func TestDiff_state(t *testing.T) {
p := testProvider() p := testProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &DiffCommand{ c := &PlanCommand{
TFConfig: testTFConfig(p), TFConfig: testTFConfig(p),
Ui: ui, Ui: ui,
} }
args := []string{ args := []string{
"-state", statePath, "-state", statePath,
testFixturePath("diff"), testFixturePath("plan"),
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())

View File

@ -30,8 +30,8 @@ func init() {
}, nil }, nil
}, },
"diff": func() (cli.Command, error) { "plan": func() (cli.Command, error) {
return &command.DiffCommand{ return &command.PlanCommand{
TFConfig: &TFConfig, TFConfig: &TFConfig,
Ui: ui, Ui: ui,
}, nil }, nil