2014-06-27 13:09:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-07-29 14:13:50 -05:00
|
|
|
|
2021-05-17 10:42:17 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-05-17 14:07:38 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/views"
|
2021-05-17 12:11:06 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2014-06-27 13:09:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// RefreshCommand is a cli.Command implementation that refreshes the state
|
|
|
|
// file.
|
|
|
|
type RefreshCommand struct {
|
2014-07-12 22:21:46 -05:00
|
|
|
Meta
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
func (c *RefreshCommand) Run(rawArgs []string) int {
|
|
|
|
// Parse and apply global view arguments
|
|
|
|
common, rawArgs := arguments.ParseView(rawArgs)
|
|
|
|
c.View.Configure(common)
|
|
|
|
|
|
|
|
// Parse and validate flags
|
|
|
|
args, diags := arguments.ParseRefresh(rawArgs)
|
|
|
|
|
|
|
|
// Instantiate the view, even if there are flag errors, so that we render
|
|
|
|
// diagnostics according to the desired view
|
|
|
|
var view views.Refresh
|
2021-05-10 12:20:43 -05:00
|
|
|
view = views.NewRefresh(args.ViewType, c.View)
|
2021-02-08 12:29:42 -06:00
|
|
|
|
|
|
|
if diags.HasErrors() {
|
2021-02-22 10:38:39 -06:00
|
|
|
view.Diagnostics(diags)
|
2021-02-26 15:31:12 -06:00
|
|
|
view.HelpPrompt()
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -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)
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// Prepare the backend with the backend-specific arguments
|
|
|
|
be, beDiags := c.PrepareBackend(args.State)
|
|
|
|
diags = diags.Append(beDiags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2017-06-15 13:26:12 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
// Build the operation request
|
|
|
|
opReq, opDiags := c.OperationRequest(be, view, args.Operation)
|
|
|
|
diags = diags.Append(opDiags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
view.Diagnostics(diags)
|
2018-03-27 17:31:05 -05:00
|
|
|
return 1
|
2017-05-01 16:47:53 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06: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)
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-23 02:33:40 -06:00
|
|
|
|
2018-03-27 17:31:05 -05: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.
|
2021-02-22 10:38:39 -06:00
|
|
|
view.Diagnostics(diags)
|
2018-03-27 17:31:05 -05:00
|
|
|
diags = nil
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
// Perform the operation
|
|
|
|
op, err := c.RunOperation(be, opReq)
|
2014-06-27 13:09:01 -05:00
|
|
|
if err != nil {
|
2021-02-22 10:38:39 -06:00
|
|
|
diags = diags.Append(err)
|
|
|
|
view.Diagnostics(diags)
|
2018-03-27 17:31:05 -05:00
|
|
|
return 1
|
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-06 18:41:48 -06:00
|
|
|
}
|
2018-11-21 08:35:27 -06:00
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
if op.State != nil {
|
|
|
|
view.Outputs(op.State.RootModule().OutputValues)
|
2018-10-12 15:19:58 -05: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-06 18:41:48 -06:00
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
return op.Result.ExitStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) 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
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
|
|
|
|
// Load the backend
|
|
|
|
be, beDiags := c.Backend(&BackendOpts{
|
|
|
|
Config: backendConfig,
|
|
|
|
})
|
|
|
|
diags = diags.Append(beDiags)
|
|
|
|
if beDiags.HasErrors() {
|
|
|
|
return nil, diags
|
2015-06-15 10:40:56 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
return be, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) OperationRequest(be backend.Enhanced, view views.Refresh, args *arguments.Operation,
|
|
|
|
) (*backend.Operation, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
// Build the operation
|
|
|
|
opReq := c.Operation(be)
|
|
|
|
opReq.ConfigDir = "."
|
|
|
|
opReq.Hooks = view.Hooks()
|
|
|
|
opReq.Targets = args.Targets
|
|
|
|
opReq.Type = backend.OperationTypeRefresh
|
|
|
|
opReq.View = view.Operation()
|
2018-03-27 17:31:05 -05:00
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
var err error
|
|
|
|
opReq.ConfigLoader, err = c.initConfigLoader()
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to initialize config loader: %s", err))
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
return opReq, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) 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
|
|
|
|
}
|
|
|
|
c.Meta.variableArgs = rawFlags{items: &items}
|
|
|
|
opReq.Variables, diags = c.collectVariableValues()
|
|
|
|
|
|
|
|
return diags
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Help() string {
|
|
|
|
helpText := `
|
2021-02-22 08:25:56 -06:00
|
|
|
Usage: terraform [global options] refresh [options]
|
2014-07-11 23:56:43 -05:00
|
|
|
|
|
|
|
Update the state file of your infrastructure with metadata that matches
|
|
|
|
the physical resources they are tracking.
|
2014-06-27 13:09:01 -05:00
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
This will not modify your infrastructure, but it can modify your
|
|
|
|
state file to update metadata. This metadata might cause new changes
|
|
|
|
to occur when you generate a plan or call apply next.
|
2014-06-27 13:09:01 -05:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2019-12-10 13:06:06 -06:00
|
|
|
-compact-warnings If Terraform produces any warnings that are not
|
|
|
|
accompanied by errors, show them in a more compact form
|
|
|
|
that includes only the summary messages.
|
|
|
|
|
2014-09-29 14:46:58 -05:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
2021-05-12 11:05:03 -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-02-03 13:15:08 -06:00
|
|
|
|
2017-04-01 15:19:59 -05:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2015-03-24 11:18:15 -05: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 13:37:27 -05: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-21 20:22:07 -05:00
|
|
|
a file. If "terraform.tfvars" or any ".auto.tfvars"
|
|
|
|
files are present, they will be automatically loaded.
|
2014-07-18 13:37:27 -05:00
|
|
|
|
2021-03-24 18:17:03 -05:00
|
|
|
-state, state-out, and -backup are legacy options supported for the local
|
|
|
|
backend only. For more information, see the local backend's documentation.
|
2014-06-27 13:09:01 -05:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Synopsis() string {
|
2020-10-23 18:55:32 -05:00
|
|
|
return "Update the state to match remote systems"
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|