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"
|
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
|
|
|
"github.com/hashicorp/terraform/command/arguments"
|
|
|
|
"github.com/hashicorp/terraform/command/views"
|
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
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
func (c *PlanCommand) Run(rawArgs []string) int {
|
|
|
|
// Parse and apply global view arguments
|
|
|
|
common, rawArgs := arguments.ParseView(rawArgs)
|
|
|
|
c.View.Configure(common)
|
|
|
|
|
2021-04-16 14:28:33 +02:00
|
|
|
// Propagate -no-color for the remote backend's legacy use of Ui. This
|
|
|
|
// should be removed when the remote backend is migrated to views.
|
|
|
|
c.Meta.color = !common.NoColor
|
|
|
|
c.Meta.Color = c.Meta.color
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
// Parse and validate flags
|
|
|
|
args, diags := arguments.ParsePlan(rawArgs)
|
|
|
|
|
|
|
|
// Instantiate the view, even if there are flag errors, so that we render
|
|
|
|
// diagnostics according to the desired view
|
2021-05-10 19:20:43 +02:00
|
|
|
view := views.NewPlan(args.ViewType, c.View)
|
2021-02-22 17:55:00 +01:00
|
|
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2021-02-26 22:31:12 +01:00
|
|
|
view.HelpPrompt()
|
2021-02-08 19:29:42 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
// Check for user-supplied plugin path
|
|
|
|
var err error
|
|
|
|
if c.pluginPath, err = c.loadPluginPath(); err != nil {
|
|
|
|
diags = diags.Append(err)
|
|
|
|
view.Diagnostics(diags)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: the -input flag value is needed to initialize the backend and the
|
|
|
|
// operation, but there is no clear path to pass this value down, so we
|
|
|
|
// continue to mutate the Meta object state for now.
|
|
|
|
c.Meta.input = args.InputEnabled
|
|
|
|
|
|
|
|
// FIXME: the -parallelism flag is used to control the concurrency of
|
|
|
|
// Terraform operations. At the moment, this value is used both to
|
|
|
|
// initialize the backend via the ContextOpts field inside CLIOpts, and to
|
|
|
|
// set a largely unused field on the Operation request. Again, there is no
|
|
|
|
// clear path to pass this value down, so we continue to mutate the Meta
|
|
|
|
// object state for now.
|
|
|
|
c.Meta.parallelism = args.Operation.Parallelism
|
|
|
|
|
|
|
|
diags = diags.Append(c.providerDevOverrideRuntimeWarnings())
|
|
|
|
|
|
|
|
// Prepare the backend with the backend-specific arguments
|
|
|
|
be, beDiags := c.PrepareBackend(args.State)
|
|
|
|
diags = diags.Append(beDiags)
|
2021-02-08 19:29:42 +01:00
|
|
|
if diags.HasErrors() {
|
2021-02-22 17:55:00 +01:00
|
|
|
view.Diagnostics(diags)
|
2014-06-09 20:53:41 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
// Build the operation request
|
2021-04-06 01:28:59 +02:00
|
|
|
opReq, opDiags := c.OperationRequest(be, view, args.Operation, args.OutPath)
|
2021-02-22 17:55:00 +01:00
|
|
|
diags = diags.Append(opDiags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2014-06-09 20:53:41 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-10 19:31:15 +01:00
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
// Collect variable value and add them to the operation request
|
|
|
|
diags = diags.Append(c.GatherVariables(opReq, args.Vars))
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2017-06-15 20:26:12 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
// Before we delegate to the backend, we'll print any warning diagnostics
|
|
|
|
// we've accumulated here, since the backend will start fresh with its own
|
|
|
|
// diagnostics.
|
|
|
|
view.Diagnostics(diags)
|
|
|
|
diags = nil
|
|
|
|
|
|
|
|
// Perform the operation
|
|
|
|
op, err := c.RunOperation(be, opReq)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(err)
|
|
|
|
view.Diagnostics(diags)
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return 1
|
2014-06-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
if op.Result != backend.OperationSuccess {
|
|
|
|
return op.Result.ExitStatus()
|
|
|
|
}
|
|
|
|
if args.DetailedExitCode && !op.PlanEmpty {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return op.Result.ExitStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PlanCommand) PrepareBackend(args *arguments.State) (backend.Enhanced, tfdiags.Diagnostics) {
|
|
|
|
// FIXME: we need to apply the state arguments to the meta object here
|
|
|
|
// because they are later used when initializing the backend. Carving a
|
|
|
|
// path to pass these arguments to the functions that need them is
|
|
|
|
// difficult but would make their use easier to understand.
|
|
|
|
c.Meta.applyStateArguments(args)
|
|
|
|
|
|
|
|
backendConfig, diags := c.loadBackendConfig(".")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Load the backend
|
2021-02-22 17:55:00 +01:00
|
|
|
be, beDiags := c.Backend(&BackendOpts{
|
2018-03-28 00:31:05 +02:00
|
|
|
Config: backendConfig,
|
2017-01-19 05:50:45 +01:00
|
|
|
})
|
2021-02-22 17:55:00 +01:00
|
|
|
diags = diags.Append(beDiags)
|
|
|
|
if beDiags.HasErrors() {
|
|
|
|
return nil, diags
|
2014-06-10 20:34:08 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
return be, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PlanCommand) OperationRequest(
|
|
|
|
be backend.Enhanced,
|
|
|
|
view views.Plan,
|
|
|
|
args *arguments.Operation,
|
|
|
|
planOutPath string,
|
|
|
|
) (*backend.Operation, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
2018-03-28 00:31:05 +02:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Build the operation
|
2021-02-22 17:55:00 +01:00
|
|
|
opReq := c.Operation(be)
|
|
|
|
opReq.ConfigDir = "."
|
2021-04-06 01:28:59 +02:00
|
|
|
opReq.PlanMode = args.PlanMode
|
2021-02-22 17:55:00 +01:00
|
|
|
opReq.Hooks = view.Hooks()
|
|
|
|
opReq.PlanRefresh = args.Refresh
|
|
|
|
opReq.PlanOutPath = planOutPath
|
|
|
|
opReq.Targets = args.Targets
|
2021-04-30 23:46:22 +02:00
|
|
|
opReq.ForceReplace = args.ForceReplace
|
2017-01-19 05:50:45 +01:00
|
|
|
opReq.Type = backend.OperationTypePlan
|
2021-02-22 17:55:00 +01:00
|
|
|
opReq.View = view.Operation()
|
2018-10-31 16:45:03 +01:00
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
var err error
|
2018-03-28 00:31:05 +02:00
|
|
|
opReq.ConfigLoader, err = c.initConfigLoader()
|
|
|
|
if err != nil {
|
2021-02-22 17:55:00 +01:00
|
|
|
diags = diags.Append(fmt.Errorf("Failed to initialize config loader: %s", err))
|
|
|
|
return nil, diags
|
2018-10-12 22:19:58 +02:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
return opReq, diags
|
|
|
|
}
|
2016-11-03 22:46:26 +01:00
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
func (c *PlanCommand) GatherVariables(opReq *backend.Operation, args *arguments.Vars) tfdiags.Diagnostics {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
// FIXME the arguments package currently trivially gathers variable related
|
|
|
|
// arguments in a heterogenous slice, in order to minimize the number of
|
|
|
|
// code paths gathering variables during the transition to this structure.
|
|
|
|
// Once all commands that gather variables have been converted to this
|
|
|
|
// structure, we could move the variable gathering code to the arguments
|
|
|
|
// package directly, removing this shim layer.
|
|
|
|
|
|
|
|
varArgs := args.All()
|
|
|
|
items := make([]rawFlag, len(varArgs))
|
|
|
|
for i := range varArgs {
|
|
|
|
items[i].Name = varArgs[i].Name
|
|
|
|
items[i].Value = varArgs[i].Value
|
2015-04-01 18:38:19 +02:00
|
|
|
}
|
2021-02-22 17:55:00 +01:00
|
|
|
c.Meta.variableArgs = rawFlags{items: &items}
|
|
|
|
opReq.Variables, diags = c.collectVariableValues()
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2021-02-22 17:55:00 +01:00
|
|
|
return diags
|
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 := `
|
2021-02-22 15:25:56 +01:00
|
|
|
Usage: terraform [global options] plan [options]
|
2014-07-12 05:51:26 +02:00
|
|
|
|
2020-10-24 01:55:32 +02:00
|
|
|
Generates a speculative execution plan, showing what actions Terraform
|
|
|
|
would take to apply the current configuration. This command will not
|
|
|
|
actually perform the planned actions.
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2020-10-24 01:55:32 +02:00
|
|
|
You can optionally save the plan to a file, which you can then pass to
|
|
|
|
the "apply" command to perform exactly the actions described in the plan.
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2021-04-06 01:28:59 +02:00
|
|
|
Plan Customization Options:
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2021-04-06 01:28:59 +02:00
|
|
|
The following options customize how Terraform will produce its plan. You
|
|
|
|
can also use these options when you run "terraform apply" without passing
|
|
|
|
it a saved plan, in order to plan and apply in a single command.
|
2019-12-10 20:06:06 +01:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
-destroy Select the "destroy" planning mode, which creates a plan
|
|
|
|
to destroy all objects currently managed by this
|
|
|
|
Terraform configuration instead of the usual behavior.
|
2014-07-01 18:12:35 +02:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
-refresh-only Select the "refresh only" planning mode, which checks
|
|
|
|
whether remote objects still match the outcome of the
|
|
|
|
most recent Terraform apply but does not propose any
|
|
|
|
actions to undo any changes made outside of Terraform.
|
|
|
|
|
|
|
|
-refresh=false Skip checking for external changes to remote objects
|
|
|
|
while creating the plan. This can potentially make
|
|
|
|
planning faster, but at the expense of possibly planning
|
|
|
|
against a stale record of the remote system state.
|
2021-04-30 23:46:22 +02:00
|
|
|
|
|
|
|
-replace=resource Force replacement of a particular resource instance using
|
|
|
|
its resource address. If the plan would've normally
|
|
|
|
produced an update or no-op action for this instance,
|
|
|
|
Terraform will plan to replace it instead.
|
|
|
|
|
|
|
|
-target=resource Limit the planning operation to only the given module,
|
|
|
|
resource, or resource instance and all of its
|
|
|
|
dependencies. You can use this option multiple times to
|
|
|
|
include more than one object. This is for exceptional
|
|
|
|
use only.
|
2021-04-06 01:28:59 +02:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
-var 'foo=bar' Set a value for one of the input variables in the root
|
|
|
|
module of the configuration. Use this option more than
|
|
|
|
once to set more than one variable.
|
2021-04-06 01:28:59 +02:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
-var-file=filename Load variable values from the given file, in addition
|
|
|
|
to the default files terraform.tfvars and *.auto.tfvars.
|
|
|
|
Use this option more than once to include more than one
|
|
|
|
variables file.
|
2021-04-06 01:28:59 +02:00
|
|
|
|
|
|
|
Other Options:
|
|
|
|
|
|
|
|
-compact-warnings If Terraform produces any warnings that are not
|
2021-05-07 00:22:48 +02:00
|
|
|
accompanied by errors, shows them in a more compact form
|
2021-04-06 01:28:59 +02:00
|
|
|
that includes only the summary messages.
|
|
|
|
|
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.
|
|
|
|
|
2021-05-12 18:05:03 +02:00
|
|
|
-lock=false Don't hold a state lock during the operation. This is
|
|
|
|
dangerous if others might concurrently run commands
|
|
|
|
against the same workspace.
|
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-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
|
|
|
|
2021-03-25 00:17:03 +01:00
|
|
|
-state=statefile A legacy option used for the local backend only. See the
|
|
|
|
local backend's documentation for more information.
|
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 {
|
2020-10-24 01:55:32 +02:00
|
|
|
return "Show changes required by the current configuration"
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|