github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/context_import.go (about) 1 package terraform 2 3 import ( 4 "github.com/hashicorp/terraform/config/module" 5 ) 6 7 // ImportOpts are used as the configuration for Import. 8 type ImportOpts struct { 9 // Targets are the targets to import 10 Targets []*ImportTarget 11 12 // Module is optional, and specifies a config module that is loaded 13 // into the graph and evaluated. The use case for this is to provide 14 // provider configuration. 15 Module *module.Tree 16 } 17 18 // ImportTarget is a single resource to import. 19 type ImportTarget struct { 20 // Addr is the full resource address of the resource to import. 21 // Example: "module.foo.aws_instance.bar" 22 Addr string 23 24 // ID is the ID of the resource to import. This is resource-specific. 25 ID string 26 27 // Provider string 28 Provider string 29 } 30 31 // Import takes already-created external resources and brings them 32 // under Terraform management. Import requires the exact type, name, and ID 33 // of the resources to import. 34 // 35 // This operation is idempotent. If the requested resource is already 36 // imported, no changes are made to the state. 37 // 38 // Further, this operation also gracefully handles partial state. If during 39 // an import there is a failure, all previously imported resources remain 40 // imported. 41 func (c *Context) Import(opts *ImportOpts) (*State, error) { 42 // Hold a lock since we can modify our own state here 43 defer c.acquireRun("import")() 44 45 // Copy our own state 46 c.state = c.state.DeepCopy() 47 48 // If no module is given, default to the module configured with 49 // the Context. 50 module := opts.Module 51 if module == nil { 52 module = c.module 53 } 54 55 // Initialize our graph builder 56 builder := &ImportGraphBuilder{ 57 ImportTargets: opts.Targets, 58 Module: module, 59 Providers: c.components.ResourceProviders(), 60 } 61 62 // Build the graph! 63 graph, err := builder.Build(RootModulePath) 64 if err != nil { 65 return c.state, err 66 } 67 68 // Walk it 69 if _, err := c.walk(graph, nil, walkImport); err != nil { 70 return c.state, err 71 } 72 73 // Clean the state 74 c.state.prune() 75 76 return c.state, nil 77 }