4 "github.com/hashicorp/terraform/addrs"
5 "github.com/hashicorp/terraform/configs"
6 "github.com/hashicorp/terraform/states"
7 "github.com/hashicorp/terraform/tfdiags"
10 // ImportOpts are used as the configuration for Import.
11 type ImportOpts struct {
12 // Targets are the targets to import
13 Targets []*ImportTarget
15 // Config is optional, and specifies a config tree that will be loaded
16 // into the graph and evaluated. This is the source for provider
18 Config *configs.Config
21 // ImportTarget is a single resource to import.
22 type ImportTarget struct {
23 // Addr is the address for the resource instance that the new object should
25 Addr addrs.AbsResourceInstance
27 // ID is the ID of the resource to import. This is resource-specific.
30 // ProviderAddr is the address of the provider that should handle the import.
31 ProviderAddr addrs.AbsProviderConfig
34 // Import takes already-created external resources and brings them
35 // under Terraform management. Import requires the exact type, name, and ID
36 // of the resources to import.
38 // This operation is idempotent. If the requested resource is already
39 // imported, no changes are made to the state.
41 // Further, this operation also gracefully handles partial state. If during
42 // an import there is a failure, all previously imported resources remain
44 func (c *Context) Import(opts *ImportOpts) (*states.State, tfdiags.Diagnostics) {
45 var diags tfdiags.Diagnostics
47 // Hold a lock since we can modify our own state here
48 defer c.acquireRun("import")()
51 c.state = c.state.DeepCopy()
53 // If no module is given, default to the module configured with
60 // Initialize our graph builder
61 builder := &ImportGraphBuilder{
62 ImportTargets: opts.Targets,
64 Components: c.components,
69 graph, graphDiags := builder.Build(addrs.RootModuleInstance)
70 diags = diags.Append(graphDiags)
71 if graphDiags.HasErrors() {
76 _, walkDiags := c.walk(graph, walkImport)
77 diags = diags.Append(walkDiags)
78 if walkDiags.HasErrors() {