diff --git a/command/apply.go b/command/apply.go index 4734b4fea..d9b3c164d 100644 --- a/command/apply.go +++ b/command/apply.go @@ -58,7 +58,7 @@ func (c *ApplyCommand) Run(args []string) int { } // Build the context based on the arguments given - ctx, err := c.Context(configPath, statePath, true) + ctx, planned, err := c.Context(configPath, statePath) if err != nil { c.Ui.Error(err.Error()) return 1 @@ -67,6 +67,15 @@ func (c *ApplyCommand) Run(args []string) int { return 1 } + // Plan if we haven't already + if !planned { + if _, err := ctx.Plan(nil); err != nil { + c.Ui.Error(fmt.Sprintf( + "Error creating plan: %s", err)) + return 1 + } + } + // Start the apply in a goroutine so that we can be interrupted. var state *terraform.State var applyErr error diff --git a/command/graph.go b/command/graph.go index 68c8bddbf..49b1a5e5b 100644 --- a/command/graph.go +++ b/command/graph.go @@ -40,7 +40,7 @@ func (c *GraphCommand) Run(args []string) int { } } - ctx, err := c.Context(path, "", false) + ctx, _, err := c.Context(path, "") if err != nil { c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) return 1 diff --git a/command/meta.go b/command/meta.go index 7b44474f6..e0d3acde7 100644 --- a/command/meta.go +++ b/command/meta.go @@ -38,7 +38,7 @@ func (m *Meta) Colorize() *colorstring.Colorize { // Context returns a Terraform Context taking into account the context // options used to initialize this meta configuration. -func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context, error) { +func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error) { opts := m.contextOpts() // First try to just read the plan directly from the path given. @@ -48,14 +48,14 @@ func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context, f.Close() if err == nil { if len(m.variables) > 0 { - return nil, fmt.Errorf( + return nil, false, fmt.Errorf( "You can't set variables with the '-var' or '-var-file' flag\n" + "when you're applying a plan file. The variables used when\n" + "the plan was created will be used. If you wish to use different\n" + "variable values, create a new plan file.") } - return plan.Context(opts), nil + return plan.Context(opts), true, nil } } @@ -73,29 +73,22 @@ func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context, } if err != nil { - return nil, fmt.Errorf("Error loading state: %s", err) + return nil, false, fmt.Errorf("Error loading state: %s", err) } } config, err := config.LoadDir(path) if err != nil { - return nil, fmt.Errorf("Error loading config: %s", err) + return nil, false, fmt.Errorf("Error loading config: %s", err) } if err := config.Validate(); err != nil { - return nil, fmt.Errorf("Error validating config: %s", err) + return nil, false, fmt.Errorf("Error validating config: %s", err) } opts.Config = config opts.State = state ctx := terraform.NewContext(opts) - - if doPlan { - if _, err := ctx.Plan(nil); err != nil { - return nil, fmt.Errorf("Error running plan: %s", err) - } - } - - return ctx, nil + return ctx, false, nil } diff --git a/command/plan.go b/command/plan.go index 540409816..b866482b6 100644 --- a/command/plan.go +++ b/command/plan.go @@ -58,7 +58,7 @@ func (c *PlanCommand) Run(args []string) int { } } - ctx, err := c.Context(path, statePath, false) + ctx, _, err := c.Context(path, statePath) if err != nil { c.Ui.Error(err.Error()) return 1 diff --git a/command/refresh.go b/command/refresh.go index 574964f75..93310efa7 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -77,7 +77,7 @@ func (c *RefreshCommand) Run(args []string) int { } // Build the context based on the arguments given - ctx, err := c.Context(configPath, statePath, false) + ctx, _, err := c.Context(configPath, statePath) if err != nil { c.Ui.Error(err.Error()) return 1