From 0a8cebdb82023aa105609942f7de7e076619a55f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Jun 2014 09:56:29 -0700 Subject: [PATCH] command/plan: refresh by default --- command/plan.go | 12 ++++++++++++ command/plan_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/command/plan.go b/command/plan.go index 2437e9b8d..bd26a5d51 100644 --- a/command/plan.go +++ b/command/plan.go @@ -19,9 +19,11 @@ type PlanCommand struct { } func (c *PlanCommand) Run(args []string) int { + var refresh bool var statePath string cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError) + cmdFlags.BoolVar(&refresh, "refresh", true, "refresh") cmdFlags.StringVar(&statePath, "state", "", "path") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { @@ -66,6 +68,14 @@ func (c *PlanCommand) Run(args []string) int { return 1 } + if refresh { + state, err = tf.Refresh(b, state) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err)) + return 1 + } + } + plan, err := tf.Plan(b, state, nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error running plan: %s", err)) @@ -90,6 +100,8 @@ Usage: terraform plan [options] [terraform.tf] Options: + -refresh=true Update state prior to checking for differences. + -state=statefile Path to a Terraform state file to use to look up Terraform-managed resources. diff --git a/command/plan_test.go b/command/plan_test.go index bb790ec2d..06e416407 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -25,6 +25,11 @@ func TestPlan_noState(t *testing.T) { t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) } + // Verify that refresh was called + if !p.RefreshCalled { + t.Fatal("refresh should be called") + } + // Verify that the provider was called with the existing state expectedState := &terraform.ResourceState{ Type: "test_instance", @@ -34,6 +39,27 @@ func TestPlan_noState(t *testing.T) { } } +func TestPlan_refresh(t *testing.T) { + p := testProvider() + ui := new(cli.MockUi) + c := &PlanCommand{ + TFConfig: testTFConfig(p), + Ui: ui, + } + + args := []string{ + "-refresh=false", + testFixturePath("plan"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + if p.RefreshCalled { + t.Fatal("refresh should not be called") + } +} + func TestPlan_state(t *testing.T) { // Write out some prior state tf, err := ioutil.TempFile("", "tf")