Skip to content

Commit 0a8cebd

Browse files
committed
command/plan: refresh by default
1 parent fa3ac80 commit 0a8cebd

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎command/plan.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ type PlanCommand struct {
1919
}
2020

2121
func (c *PlanCommand) Run(args []string) int {
22+
var refresh bool
2223
var statePath string
2324

2425
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
26+
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
2527
cmdFlags.StringVar(&statePath, "state", "", "path")
2628
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
2729
if err := cmdFlags.Parse(args); err != nil {
@@ -66,6 +68,14 @@ func (c *PlanCommand) Run(args []string) int {
6668
return 1
6769
}
6870

71+
if refresh {
72+
state, err = tf.Refresh(b, state)
73+
if err != nil {
74+
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
75+
return 1
76+
}
77+
}
78+
6979
plan, err := tf.Plan(b, state, nil)
7080
if err != nil {
7181
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
@@ -90,6 +100,8 @@ Usage: terraform plan [options] [terraform.tf]
90100
91101
Options:
92102
103+
-refresh=true Update state prior to checking for differences.
104+
93105
-state=statefile Path to a Terraform state file to use to look
94106
up Terraform-managed resources.
95107

‎command/plan_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ func TestPlan_noState(t *testing.T) {
2525
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
2626
}
2727

28+
// Verify that refresh was called
29+
if !p.RefreshCalled {
30+
t.Fatal("refresh should be called")
31+
}
32+
2833
// Verify that the provider was called with the existing state
2934
expectedState := &terraform.ResourceState{
3035
Type: "test_instance",
@@ -34,6 +39,27 @@ func TestPlan_noState(t *testing.T) {
3439
}
3540
}
3641

42+
func TestPlan_refresh(t *testing.T) {
43+
p := testProvider()
44+
ui := new(cli.MockUi)
45+
c := &PlanCommand{
46+
TFConfig: testTFConfig(p),
47+
Ui: ui,
48+
}
49+
50+
args := []string{
51+
"-refresh=false",
52+
testFixturePath("plan"),
53+
}
54+
if code := c.Run(args); code != 0 {
55+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
56+
}
57+
58+
if p.RefreshCalled {
59+
t.Fatal("refresh should not be called")
60+
}
61+
}
62+
3763
func TestPlan_state(t *testing.T) {
3864
// Write out some prior state
3965
tf, err := ioutil.TempFile("", "tf")

0 commit comments

Comments
 (0)