command/plan: refresh by default

This commit is contained in:
Mitchell Hashimoto 2014-06-26 09:56:29 -07:00
parent fa3ac806cc
commit 0a8cebdb82
2 changed files with 38 additions and 0 deletions

View File

@ -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.

View File

@ -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")