2014-06-09 20:53:41 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2018-03-28 00:31:05 +02:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2014-06-09 20:53:41 +02:00
|
|
|
)
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
// PlanCommand is a Command implementation that compares a Terraform
|
2014-06-09 20:53:41 +02:00
|
|
|
// configuration to an actual infrastructure and shows the differences.
|
2014-06-20 20:47:02 +02:00
|
|
|
type PlanCommand struct {
|
2014-07-13 05:21:46 +02:00
|
|
|
Meta
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Run(args []string) int {
|
2015-04-01 18:38:19 +02:00
|
|
|
var destroy, refresh, detailed bool
|
2014-10-12 03:47:52 +02:00
|
|
|
var outPath string
|
2014-09-25 07:54:51 +02:00
|
|
|
var moduleDepth int
|
2014-06-19 22:51:05 +02:00
|
|
|
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err := c.Meta.process(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
cmdFlags := c.Meta.flagSet("plan")
|
2014-07-01 18:12:05 +02:00
|
|
|
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
2014-06-26 18:56:29 +02:00
|
|
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
2015-04-30 21:05:39 +02:00
|
|
|
c.addModuleDepthFlag(cmdFlags, &moduleDepth)
|
2014-06-27 07:23:51 +02:00
|
|
|
cmdFlags.StringVar(&outPath, "out", "", "path")
|
2015-10-05 22:06:08 +02:00
|
|
|
cmdFlags.IntVar(
|
|
|
|
&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
|
2017-01-19 05:50:45 +01:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
|
2015-04-01 18:38:19 +02:00
|
|
|
cmdFlags.BoolVar(&detailed, "detailed-exitcode", false, "detailed-exitcode")
|
2017-02-06 16:07:32 +01:00
|
|
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
2017-04-01 22:19:59 +02:00
|
|
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
2014-06-09 20:53:41 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
configPath, err := ModulePath(cmdFlags.Args())
|
2014-06-09 20:53:41 +02:00
|
|
|
if err != nil {
|
2014-07-13 05:37:30 +02:00
|
|
|
c.Ui.Error(err.Error())
|
2014-06-09 20:53:41 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-10 19:31:15 +01:00
|
|
|
|
2017-06-15 20:26:12 +02:00
|
|
|
// Check for user-supplied plugin path
|
|
|
|
if c.pluginPath, err = c.loadPluginPath(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Check if the path is a plan
|
|
|
|
plan, err := c.Plan(configPath)
|
2016-11-04 16:30:51 +01:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
if plan != nil {
|
|
|
|
// Disable refreshing no matter what since we only want to show the plan
|
|
|
|
refresh = false
|
2016-11-04 16:30:51 +01:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Set the config path to empty for backend loading
|
|
|
|
configPath = ""
|
2015-11-10 19:31:15 +01:00
|
|
|
}
|
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
var backendConfig *configs.Backend
|
2017-01-19 05:50:45 +01:00
|
|
|
if plan == nil {
|
2018-03-28 00:31:05 +02:00
|
|
|
var configDiags tfdiags.Diagnostics
|
|
|
|
backendConfig, configDiags = c.loadBackendConfig(configPath)
|
|
|
|
diags = diags.Append(configDiags)
|
|
|
|
if configDiags.HasErrors() {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
c.showDiagnostics(diags)
|
2014-06-26 18:56:29 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Load the backend
|
2018-03-28 00:31:05 +02:00
|
|
|
b, backendDiags := c.Backend(&BackendOpts{
|
|
|
|
Config: backendConfig,
|
2017-05-01 23:47:53 +02:00
|
|
|
Plan: plan,
|
2017-01-19 05:50:45 +01:00
|
|
|
})
|
2018-03-28 00:31:05 +02:00
|
|
|
diags = diags.Append(backendDiags)
|
|
|
|
if backendDiags.HasErrors() {
|
|
|
|
c.showDiagnostics(diags)
|
2014-06-10 20:34:08 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
// Emit any diagnostics we've accumulated before we delegate to the
|
|
|
|
// backend, since the backend will handle its own diagnostics internally.
|
|
|
|
c.showDiagnostics(diags)
|
|
|
|
diags = nil
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Build the operation
|
|
|
|
opReq := c.Operation()
|
|
|
|
opReq.Destroy = destroy
|
2018-03-28 00:31:05 +02:00
|
|
|
opReq.ConfigDir = configPath
|
2017-01-19 05:50:45 +01:00
|
|
|
opReq.Plan = plan
|
|
|
|
opReq.PlanOutPath = outPath
|
2018-10-05 15:25:17 +02:00
|
|
|
opReq.PlanRefresh = refresh
|
2017-01-19 05:50:45 +01:00
|
|
|
opReq.Type = backend.OperationTypePlan
|
2018-03-28 00:31:05 +02:00
|
|
|
opReq.ConfigLoader, err = c.initConfigLoader()
|
|
|
|
if err != nil {
|
|
|
|
c.showDiagnostics(err)
|
|
|
|
return 1
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
|
|
|
// Perform the operation
|
2018-02-10 00:51:29 +01:00
|
|
|
op, err := c.RunOperation(b, opReq)
|
2017-01-19 05:50:45 +01:00
|
|
|
if err != nil {
|
2018-03-28 00:31:05 +02:00
|
|
|
c.showDiagnostics(err)
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
return 1
|
|
|
|
}
|
2016-11-03 22:46:26 +01:00
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
if op.Result != backend.OperationSuccess {
|
|
|
|
return op.Result.ExitStatus()
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
if detailed && !op.PlanEmpty {
|
2015-04-01 18:38:19 +02:00
|
|
|
return 2
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
return op.Result.ExitStatus()
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Help() string {
|
2014-06-09 20:53:41 +02:00
|
|
|
helpText := `
|
2016-12-12 19:45:26 +01:00
|
|
|
Usage: terraform plan [options] [DIR-OR-PLAN]
|
2014-07-12 05:51:26 +02:00
|
|
|
|
|
|
|
Generates an execution plan for Terraform.
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2014-07-12 05:51:26 +02:00
|
|
|
This execution plan can be reviewed prior to running apply to get a
|
|
|
|
sense for what Terraform will do. Optionally, the plan can be saved to
|
|
|
|
a Terraform plan file, and apply can take this plan file to execute
|
|
|
|
this plan exactly.
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2016-12-12 19:45:26 +01:00
|
|
|
If a saved plan is passed as an argument, this command will output
|
|
|
|
the saved plan contents. It will not modify the given plan.
|
|
|
|
|
2014-06-09 20:53:41 +02:00
|
|
|
Options:
|
|
|
|
|
2014-07-01 18:12:35 +02:00
|
|
|
-destroy If set, a plan will be generated to destroy all resources
|
|
|
|
managed by the given configuration and state.
|
|
|
|
|
2015-04-01 18:38:19 +02:00
|
|
|
-detailed-exitcode Return detailed exit codes when the command exits. This
|
|
|
|
will change the meaning of exit codes to:
|
|
|
|
0 - Succeeded, diff is empty (no changes)
|
|
|
|
1 - Errored
|
|
|
|
2 - Succeeded, there is a diff
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
2017-02-06 16:07:32 +01:00
|
|
|
-lock=true Lock the state file when locking is supported.
|
2017-02-03 20:15:08 +01:00
|
|
|
|
2017-04-01 22:19:59 +02:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
-module-depth=n Specifies the depth of modules to show in the output.
|
|
|
|
This does not affect the plan itself, only the output
|
2016-01-20 19:50:33 +01:00
|
|
|
shown. By default, this is -1, which will expand all.
|
2014-09-25 07:54:51 +02:00
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-06-27 07:23:51 +02:00
|
|
|
-out=path Write a plan file to the given path. This can be used as
|
|
|
|
input to the "apply" command.
|
|
|
|
|
2015-10-06 00:18:03 +02:00
|
|
|
-parallelism=n Limit the number of concurrent operations. Defaults to 10.
|
2015-10-05 22:06:08 +02:00
|
|
|
|
2014-06-26 18:56:29 +02:00
|
|
|
-refresh=true Update state prior to checking for differences.
|
|
|
|
|
2014-06-09 20:53:41 +02:00
|
|
|
-state=statefile Path to a Terraform state file to use to look
|
2014-07-12 05:51:26 +02:00
|
|
|
up Terraform-managed resources. By default it will
|
|
|
|
use the state "terraform.tfstate" if it exists.
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
-target=resource Resource to target. Operation will be limited to this
|
|
|
|
resource and its dependencies. This flag can be used
|
|
|
|
multiple times.
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
|
|
|
flag can be set multiple times.
|
|
|
|
|
|
|
|
-var-file=foo Set variables in the Terraform configuration from
|
2017-06-22 03:22:07 +02:00
|
|
|
a file. If "terraform.tfvars" or any ".auto.tfvars"
|
|
|
|
files are present, they will be automatically loaded.
|
2014-06-09 20:53:41 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Synopsis() string {
|
2014-07-13 04:28:38 +02:00
|
|
|
return "Generate and show an execution plan"
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|