2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-06-09 13:53:41 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/backend"
|
|
|
|
"github.com/opentofu/opentofu/internal/command/arguments"
|
|
|
|
"github.com/opentofu/opentofu/internal/command/views"
|
2024-03-07 07:55:57 -06:00
|
|
|
"github.com/opentofu/opentofu/internal/encryption"
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/tfdiags"
|
2014-06-09 13:53:41 -05:00
|
|
|
)
|
|
|
|
|
2023-12-13 10:35:41 -06:00
|
|
|
// PlanCommand is a Command implementation that compares a OpenTofu
|
2014-06-09 13:53:41 -05:00
|
|
|
// configuration to an actual infrastructure and shows the differences.
|
2014-06-20 13:47:02 -05:00
|
|
|
type PlanCommand struct {
|
2014-07-12 22:21:46 -05:00
|
|
|
Meta
|
2014-06-09 13:53:41 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
func (c *PlanCommand) Run(rawArgs []string) int {
|
2024-11-12 16:05:56 -06:00
|
|
|
ctx := c.CommandContext()
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
// Parse and apply global view arguments
|
|
|
|
common, rawArgs := arguments.ParseView(rawArgs)
|
|
|
|
c.View.Configure(common)
|
|
|
|
|
2021-10-28 15:27:39 -05:00
|
|
|
// Propagate -no-color for legacy use of Ui. The remote backend and
|
|
|
|
// cloud package use this; it should be removed when/if they are
|
|
|
|
// migrated to views.
|
2021-04-16 07:28:33 -05:00
|
|
|
c.Meta.color = !common.NoColor
|
|
|
|
c.Meta.Color = c.Meta.color
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
// Parse and validate flags
|
|
|
|
args, diags := arguments.ParsePlan(rawArgs)
|
|
|
|
|
2024-07-22 04:58:57 -05:00
|
|
|
c.View.SetShowSensitive(args.ShowSensitive)
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
// Instantiate the view, even if there are flag errors, so that we render
|
|
|
|
// diagnostics according to the desired view
|
2021-05-10 12:20:43 -05:00
|
|
|
view := views.NewPlan(args.ViewType, c.View)
|
2021-02-22 10:55:00 -06:00
|
|
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2021-02-26 15:31:12 -06:00
|
|
|
view.HelpPrompt()
|
2021-02-08 12:29:42 -06:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06: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
|
2023-12-13 10:35:41 -06:00
|
|
|
// OpenTofu operations. At the moment, this value is used both to
|
2021-02-22 10:55:00 -06:00
|
|
|
// 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())
|
|
|
|
|
2024-06-24 08:13:07 -05:00
|
|
|
// Inject variables from args into meta for static evaluation
|
|
|
|
c.GatherVariables(args.Vars)
|
|
|
|
|
2024-03-07 07:55:57 -06:00
|
|
|
// Load the encryption configuration
|
|
|
|
enc, encDiags := c.Encryption()
|
|
|
|
diags = diags.Append(encDiags)
|
|
|
|
if encDiags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
// Prepare the backend with the backend-specific arguments
|
2024-03-07 07:55:57 -06:00
|
|
|
be, beDiags := c.PrepareBackend(args.State, args.ViewType, enc)
|
2021-02-22 10:55:00 -06:00
|
|
|
diags = diags.Append(beDiags)
|
2021-02-08 12:29:42 -06:00
|
|
|
if diags.HasErrors() {
|
2021-02-22 10:55:00 -06:00
|
|
|
view.Diagnostics(diags)
|
2014-06-09 13:53:41 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
// Build the operation request
|
2024-03-07 07:55:57 -06:00
|
|
|
opReq, opDiags := c.OperationRequest(be, view, args.ViewType, args.Operation, args.OutPath, args.GenerateConfigPath, enc)
|
2021-02-22 10:55:00 -06:00
|
|
|
diags = diags.Append(opDiags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2014-06-09 13:53:41 -05:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-10 12:31:15 -06:00
|
|
|
|
2021-02-22 10:55:00 -06: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
|
2024-11-12 16:05:56 -06:00
|
|
|
op, diags := c.RunOperation(ctx, be, opReq)
|
2024-06-24 08:13:07 -05:00
|
|
|
view.Diagnostics(diags)
|
|
|
|
if diags.HasErrors() {
|
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 16:24:45 -05:00
|
|
|
return 1
|
2014-06-26 11:56:29 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
if op.Result != backend.OperationSuccess {
|
|
|
|
return op.Result.ExitStatus()
|
|
|
|
}
|
|
|
|
if args.DetailedExitCode && !op.PlanEmpty {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return op.Result.ExitStatus()
|
|
|
|
}
|
|
|
|
|
2024-03-07 07:55:57 -06:00
|
|
|
func (c *PlanCommand) PrepareBackend(args *arguments.State, viewType arguments.ViewType, enc encryption.Encryption) (backend.Enhanced, tfdiags.Diagnostics) {
|
2021-02-22 10:55:00 -06:00
|
|
|
// 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-18 22:50:45 -06:00
|
|
|
// Load the backend
|
2021-02-22 10:55:00 -06:00
|
|
|
be, beDiags := c.Backend(&BackendOpts{
|
2023-02-07 02:06:12 -06:00
|
|
|
Config: backendConfig,
|
|
|
|
ViewType: viewType,
|
2024-03-13 09:58:52 -05:00
|
|
|
}, enc.State())
|
2021-02-22 10:55:00 -06:00
|
|
|
diags = diags.Append(beDiags)
|
|
|
|
if beDiags.HasErrors() {
|
|
|
|
return nil, diags
|
2014-06-10 13:34:08 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
return be, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PlanCommand) OperationRequest(
|
|
|
|
be backend.Enhanced,
|
|
|
|
view views.Plan,
|
2023-02-07 02:06:12 -06:00
|
|
|
viewType arguments.ViewType,
|
2021-02-22 10:55:00 -06:00
|
|
|
args *arguments.Operation,
|
|
|
|
planOutPath string,
|
2023-05-12 17:05:00 -05:00
|
|
|
generateConfigOut string,
|
2024-03-07 07:55:57 -06:00
|
|
|
enc encryption.Encryption,
|
2021-02-22 10:55:00 -06:00
|
|
|
) (*backend.Operation, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
2018-03-27 17:31:05 -05:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Build the operation
|
2024-03-07 07:55:57 -06:00
|
|
|
opReq := c.Operation(be, viewType, enc)
|
2021-02-22 10:55:00 -06:00
|
|
|
opReq.ConfigDir = "."
|
2021-04-05 18:28:59 -05:00
|
|
|
opReq.PlanMode = args.PlanMode
|
2021-02-22 10:55:00 -06:00
|
|
|
opReq.Hooks = view.Hooks()
|
|
|
|
opReq.PlanRefresh = args.Refresh
|
|
|
|
opReq.PlanOutPath = planOutPath
|
2023-05-12 17:05:00 -05:00
|
|
|
opReq.GenerateConfigOut = generateConfigOut
|
2021-02-22 10:55:00 -06:00
|
|
|
opReq.Targets = args.Targets
|
2024-11-05 09:16:00 -06:00
|
|
|
opReq.Excludes = args.Excludes
|
2021-04-30 16:46:22 -05:00
|
|
|
opReq.ForceReplace = args.ForceReplace
|
2017-01-18 22:50:45 -06:00
|
|
|
opReq.Type = backend.OperationTypePlan
|
2021-02-22 10:55:00 -06:00
|
|
|
opReq.View = view.Operation()
|
2018-10-31 10:45:03 -05:00
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
var err error
|
2018-03-27 17:31:05 -05:00
|
|
|
opReq.ConfigLoader, err = c.initConfigLoader()
|
|
|
|
if err != nil {
|
2023-09-21 07:45:28 -05:00
|
|
|
diags = diags.Append(fmt.Errorf("Failed to initialize config loader: %w", err))
|
2021-02-22 10:55:00 -06:00
|
|
|
return nil, diags
|
2018-10-12 15:19:58 -05:00
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
return opReq, diags
|
|
|
|
}
|
2016-11-03 16:46:26 -05:00
|
|
|
|
2024-06-24 08:13:07 -05:00
|
|
|
func (c *PlanCommand) GatherVariables(args *arguments.Vars) {
|
2021-02-22 10:55:00 -06:00
|
|
|
// FIXME the arguments package currently trivially gathers variable related
|
2024-09-09 06:51:39 -05:00
|
|
|
// arguments in a heterogeneous slice, in order to minimize the number of
|
2021-02-22 10:55:00 -06:00
|
|
|
// 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 11:38:19 -05:00
|
|
|
}
|
2021-02-22 10:55:00 -06:00
|
|
|
c.Meta.variableArgs = rawFlags{items: &items}
|
2014-06-09 13:53:41 -05:00
|
|
|
}
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
func (c *PlanCommand) Help() string {
|
2014-06-09 13:53:41 -05:00
|
|
|
helpText := `
|
2023-09-21 07:38:46 -05:00
|
|
|
Usage: tofu [global options] plan [options]
|
2014-07-11 22:51:26 -05:00
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
Generates a speculative execution plan, showing what actions OpenTofu
|
2020-10-23 18:55:32 -05:00
|
|
|
would take to apply the current configuration. This command will not
|
|
|
|
actually perform the planned actions.
|
2014-06-09 13:53:41 -05:00
|
|
|
|
2020-10-23 18:55:32 -05: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 13:53:41 -05:00
|
|
|
|
2021-04-05 18:28:59 -05:00
|
|
|
Plan Customization Options:
|
2014-06-09 13:53:41 -05:00
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
The following options customize how OpenTofu will produce its plan. You
|
|
|
|
can also use these options when you run "tofu apply" without passing
|
2021-04-05 18:28:59 -05:00
|
|
|
it a saved plan, in order to plan and apply in a single command.
|
2019-12-10 13:06:06 -06:00
|
|
|
|
2021-05-06 17:22:48 -05:00
|
|
|
-destroy Select the "destroy" planning mode, which creates a plan
|
|
|
|
to destroy all objects currently managed by this
|
2023-09-21 07:38:46 -05:00
|
|
|
OpenTofu configuration instead of the usual behavior.
|
2014-07-01 11:12:35 -05:00
|
|
|
|
2021-05-06 17:22:48 -05:00
|
|
|
-refresh-only Select the "refresh only" planning mode, which checks
|
|
|
|
whether remote objects still match the outcome of the
|
2023-09-21 07:38:46 -05:00
|
|
|
most recent OpenTofu apply but does not propose any
|
|
|
|
actions to undo any changes made outside of OpenTofu.
|
2021-05-06 17:22:48 -05:00
|
|
|
|
|
|
|
-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 16:46:22 -05: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,
|
2023-09-21 07:38:46 -05:00
|
|
|
OpenTofu will plan to replace it instead. You can use
|
2022-03-30 03:52:49 -05:00
|
|
|
this option multiple times to replace more than one object.
|
2021-04-30 16:46:22 -05:00
|
|
|
|
|
|
|
-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
|
2024-11-05 09:16:00 -06:00
|
|
|
use only. Cannot be used alongside the -exclude flag
|
|
|
|
|
|
|
|
-exclude=resource Limit the planning operation to not operate on the given
|
|
|
|
module, resource, or resource instance and all of the
|
|
|
|
resources and modules that depend on it. You can use this
|
|
|
|
option multiple times to exclude more than one object.
|
|
|
|
This is for exceptional use only. Cannot be used alongside
|
|
|
|
the -target flag
|
2021-04-05 18:28:59 -05:00
|
|
|
|
2021-05-06 17:22:48 -05: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-05 18:28:59 -05:00
|
|
|
|
2021-05-06 17:22:48 -05: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-05 18:28:59 -05:00
|
|
|
|
|
|
|
Other Options:
|
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
-compact-warnings If OpenTofu produces any warnings that are not
|
2023-05-31 14:42:41 -05:00
|
|
|
accompanied by errors, shows them in a more compact
|
|
|
|
form that includes only the summary messages.
|
2021-04-05 18:28:59 -05:00
|
|
|
|
2024-09-23 06:31:06 -05:00
|
|
|
-consolidate-warnings If OpenTofu produces any warnings, no consolodation
|
|
|
|
will be performed. All locations, for all warnings
|
|
|
|
will be listed. Enabled by default.
|
|
|
|
|
|
|
|
-consolidate-errors If OpenTofu produces any errors, no consolodation
|
|
|
|
will be performed. All locations, for all errors
|
|
|
|
will be listed. Disabled by default
|
|
|
|
|
2023-05-31 14:42:41 -05: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
|
2015-04-01 11:38:19 -05:00
|
|
|
|
2023-05-31 14:42:41 -05:00
|
|
|
-generate-config-out=path (Experimental) If import blocks are present in
|
2023-09-21 07:38:46 -05:00
|
|
|
configuration, instructs OpenTofu to generate HCL
|
2023-05-31 14:42:41 -05:00
|
|
|
for any imported resources not already present. The
|
|
|
|
configuration is written to a new file at PATH,
|
2023-09-21 07:38:46 -05:00
|
|
|
which must not already exist. OpenTofu may still
|
2023-05-31 14:42:41 -05:00
|
|
|
attempt to write configuration if the plan errors.
|
2014-09-29 13:11:35 -05:00
|
|
|
|
2023-05-31 14:42:41 -05:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
2017-02-03 13:15:08 -06:00
|
|
|
|
2023-05-31 14:42:41 -05: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-04-01 15:19:59 -05:00
|
|
|
|
2023-05-31 14:42:41 -05:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
2014-07-12 22:21:46 -05:00
|
|
|
|
2023-05-31 14:42:41 -05:00
|
|
|
-no-color If specified, output won't contain any color.
|
2014-06-27 00:23:51 -05:00
|
|
|
|
2024-02-14 06:21:35 -06:00
|
|
|
-concise Displays plan output in a concise way, skipping the
|
|
|
|
refreshing log lines.
|
|
|
|
|
2023-05-31 14:42:41 -05:00
|
|
|
-out=path Write a plan file to the given path. This can be
|
|
|
|
used as input to the "apply" command.
|
2015-10-05 15:06:08 -05:00
|
|
|
|
2023-05-31 14:42:41 -05:00
|
|
|
-parallelism=n Limit the number of concurrent operations. Defaults
|
|
|
|
to 10.
|
|
|
|
|
|
|
|
-state=statefile A legacy option used for the local backend only.
|
|
|
|
See the local backend's documentation for more
|
|
|
|
information.
|
2024-07-22 04:58:57 -05:00
|
|
|
|
|
|
|
-show-sensitive If specified, sensitive values will be displayed.
|
2025-01-08 06:58:32 -06:00
|
|
|
|
|
|
|
-json Produce output in a machine-readable JSON format,
|
|
|
|
suitable for use in text editor integrations and
|
|
|
|
other automated systems. Always disables color.
|
2014-06-09 13:53:41 -05:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
func (c *PlanCommand) Synopsis() string {
|
2020-10-23 18:55:32 -05:00
|
|
|
return "Show changes required by the current configuration"
|
2014-06-09 13:53:41 -05:00
|
|
|
}
|