mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
a3403f2766
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.
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package terraform
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/states"
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
)
|
|
|
|
// ImportOpts are used as the configuration for Import.
|
|
type ImportOpts struct {
|
|
// Targets are the targets to import
|
|
Targets []*ImportTarget
|
|
|
|
// Config is optional, and specifies a config tree that will be loaded
|
|
// into the graph and evaluated. This is the source for provider
|
|
// configurations.
|
|
Config *configs.Config
|
|
}
|
|
|
|
// 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(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")()
|
|
|
|
// Copy our own state
|
|
c.state = c.state.DeepCopy()
|
|
|
|
// If no module is given, default to the module configured with
|
|
// the Context.
|
|
config := opts.Config
|
|
if config == nil {
|
|
config = c.config
|
|
}
|
|
|
|
// Initialize our graph builder
|
|
builder := &ImportGraphBuilder{
|
|
ImportTargets: opts.Targets,
|
|
Config: config,
|
|
Components: c.components,
|
|
Schemas: c.schemas,
|
|
}
|
|
|
|
// Build the graph!
|
|
graph, graphDiags := builder.Build(addrs.RootModuleInstance)
|
|
diags = diags.Append(graphDiags)
|
|
if graphDiags.HasErrors() {
|
|
return c.state, diags
|
|
}
|
|
|
|
// Walk it
|
|
_, walkDiags := c.walk(graph, walkImport)
|
|
diags = diags.Append(walkDiags)
|
|
if walkDiags.HasErrors() {
|
|
return c.state, diags
|
|
}
|
|
|
|
return c.state, diags
|
|
}
|