Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command/plan: user friendly error if plan file given to plan command #10639

Merged
merged 2 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *PlanCommand) Run(args []string) int {
// This is going to keep track of shadow errors
var shadowErr error

ctx, _, err := c.Context(contextOpts{
ctx, planned, err := c.Context(contextOpts{
Destroy: destroy,
Path: path,
StatePath: c.Meta.statePath,
Expand All @@ -71,6 +71,17 @@ func (c *PlanCommand) Run(args []string) int {
c.Ui.Error(err.Error())
return 1
}
if planned {
c.Ui.Output(c.Colorize().Color(
"[reset][bold][yellow]" +
"The plan command received a saved plan file as input. This command\n" +
"will output the saved plan. This will not modify the already-existing\n" +
"plan. If you wish to generate a new plan, please pass in a configuration\n" +
"directory as an argument.\n\n"))

// Disable refreshing no matter what since we only want to show the plan
refresh = false
}

err = terraform.SetDebugInfo(DefaultDataDir)
if err != nil {
Expand Down Expand Up @@ -171,7 +182,7 @@ func (c *PlanCommand) Run(args []string) int {

func (c *PlanCommand) Help() string {
helpText := `
Usage: terraform plan [options] [dir]
Usage: terraform plan [options] [DIR-OR-PLAN]

Generates an execution plan for Terraform.

Expand All @@ -180,6 +191,9 @@ Usage: terraform plan [options] [dir]
a Terraform plan file, and apply can take this plan file to execute
this plan exactly.

If a saved plan is passed as an argument, this command will output
the saved plan contents. It will not modify the given plan.

Options:

-destroy If set, a plan will be generated to destroy all resources
Expand Down
27 changes: 27 additions & 0 deletions command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ func TestPlan(t *testing.T) {
}
}

func TestPlan_plan(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)

planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
})

p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

args := []string{planPath}
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_destroy(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
Expand Down
7 changes: 6 additions & 1 deletion website/source/docs/commands/plan.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ to `terraform apply` to ensure only the pre-planned actions are executed.

## Usage

Usage: `terraform plan [options] [dir]`
Usage: `terraform plan [options] [dir-or-plan]`

By default, `plan` requires no flags and looks in the current directory
for the configuration and state file to refresh.

If the command is given an existing saved plan as an argument, the
command will output the contents of the saved plan. In this scenario,
the `plan` command will not modify the given plan. This can be used to
inspect a planfile.

The command-line flags are all optional. The list of available flags are:

* `-destroy` - If set, generates a plan to destroy all the known resources.
Expand Down