mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-17 20:22:58 -06:00
36c4d4c241
Previously we had three different layers all thinking they were responsible for substituting a default value for an unset root module variable: - the local backend, via logic in backend.ParseVariableValues - the context.Plan function (and other similar functions) trying to preprocess the input variables using terraform.mergeDefaultInputVariableValues . - the newer prepareFinalInputVariableValue, which aims to centralize all of the variable preparation logic so it can be common to both root and child module variables. The second of these was also trying to handle type constraint checking, which is also the responsibility of the central function and not something we need to handle so early. Only the last of these consistently handles both root and child module variables, and so is the one we ought to keep. The others are now redundant and are causing prepareFinalInputVariableValue to get a slightly corrupted view of the caller's chosen variable values. To rectify that, here we remove the two redundant layers altogether and have unset root variables pass through as cty.NilVal all the way to the central prepareFinalInputVariableValue function, which will then handle them in a suitable way which properly respects the "nullable" setting. This commit includes some test changes in the terraform package to make those tests no longer rely on the mergeDefaultInputVariableValues logic we've removed, and to instead explicitly set cty.NilVal for all unset variables to comply with our intended contract for PlanOpts.SetVariables, and similar. (This is so that we can more easily catch bugs in callers where they _don't_ correctly handle input variables; it allows us to distinguish between the caller explicitly marking a variable as unset vs. not describing it at all, where the latter is a bug in the caller.)
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/configs"
|
|
"github.com/hashicorp/terraform/internal/states"
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
|
)
|
|
|
|
// ImportOpts are used as the configuration for Import.
|
|
type ImportOpts struct {
|
|
// Targets are the targets to import
|
|
Targets []*ImportTarget
|
|
|
|
// SetVariables are the variables set outside of the configuration,
|
|
// such as on the command line, in variables files, etc.
|
|
SetVariables InputValues
|
|
}
|
|
|
|
// ImportTarget is a single resource to import.
|
|
type ImportTarget struct {
|
|
// Addr is the address for the resource instance that the new object should
|
|
// be imported into.
|
|
Addr addrs.AbsResourceInstance
|
|
|
|
// ID is the ID of the resource to import. This is resource-specific.
|
|
ID string
|
|
|
|
// ProviderAddr is the address of the provider that should handle the import.
|
|
ProviderAddr addrs.AbsProviderConfig
|
|
}
|
|
|
|
// Import takes already-created external resources and brings them
|
|
// under Terraform management. Import requires the exact type, name, and ID
|
|
// of the resources to import.
|
|
//
|
|
// This operation is idempotent. If the requested resource is already
|
|
// imported, no changes are made to the state.
|
|
//
|
|
// Further, this operation also gracefully handles partial state. If during
|
|
// an import there is a failure, all previously imported resources remain
|
|
// imported.
|
|
func (c *Context) Import(config *configs.Config, prevRunState *states.State, opts *ImportOpts) (*states.State, tfdiags.Diagnostics) {
|
|
var diags tfdiags.Diagnostics
|
|
|
|
// Hold a lock since we can modify our own state here
|
|
defer c.acquireRun("import")()
|
|
|
|
// Don't modify our caller's state
|
|
state := prevRunState.DeepCopy()
|
|
|
|
log.Printf("[DEBUG] Building and walking import graph")
|
|
|
|
variables := opts.SetVariables
|
|
|
|
// Initialize our graph builder
|
|
builder := &ImportGraphBuilder{
|
|
ImportTargets: opts.Targets,
|
|
Config: config,
|
|
RootVariableValues: variables,
|
|
Plugins: c.plugins,
|
|
}
|
|
|
|
// Build the graph
|
|
graph, graphDiags := builder.Build(addrs.RootModuleInstance)
|
|
diags = diags.Append(graphDiags)
|
|
if graphDiags.HasErrors() {
|
|
return state, diags
|
|
}
|
|
|
|
// Walk it
|
|
walker, walkDiags := c.walk(graph, walkImport, &graphWalkOpts{
|
|
Config: config,
|
|
InputState: state,
|
|
})
|
|
diags = diags.Append(walkDiags)
|
|
if walkDiags.HasErrors() {
|
|
return state, diags
|
|
}
|
|
|
|
newState := walker.State.Close()
|
|
return newState, diags
|
|
}
|