github.com/mkuzmin/terraform@v0.3.7-0.20161118171027-ec4c00ff92a9/command/import.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "strings" 8 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 // ImportCommand is a cli.Command implementation that imports resources 13 // into the Terraform state. 14 type ImportCommand struct { 15 Meta 16 } 17 18 func (c *ImportCommand) Run(args []string) int { 19 // Get the pwd since its our default -config flag value 20 pwd, err := os.Getwd() 21 if err != nil { 22 c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) 23 return 1 24 } 25 26 var configPath string 27 args = c.Meta.process(args, true) 28 29 cmdFlags := c.Meta.flagSet("import") 30 cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism") 31 cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") 32 cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path") 33 cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path") 34 cmdFlags.StringVar(&configPath, "config", pwd, "path") 35 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 36 if err := cmdFlags.Parse(args); err != nil { 37 return 1 38 } 39 40 args = cmdFlags.Args() 41 if len(args) != 2 { 42 c.Ui.Error("The import command expects two arguments.") 43 cmdFlags.Usage() 44 return 1 45 } 46 47 // Build the context based on the arguments given 48 ctx, _, err := c.Context(contextOpts{ 49 Path: configPath, 50 PathEmptyOk: true, 51 StatePath: c.Meta.statePath, 52 Parallelism: c.Meta.parallelism, 53 }) 54 if err != nil { 55 c.Ui.Error(err.Error()) 56 return 1 57 } 58 59 // Perform the import. Note that as you can see it is possible for this 60 // API to import more than one resource at once. For now, we only allow 61 // one while we stabilize this feature. 62 newState, err := ctx.Import(&terraform.ImportOpts{ 63 Targets: []*terraform.ImportTarget{ 64 &terraform.ImportTarget{ 65 Addr: args[0], 66 ID: args[1], 67 }, 68 }, 69 }) 70 if err != nil { 71 c.Ui.Error(fmt.Sprintf("Error importing: %s", err)) 72 return 1 73 } 74 75 // Persist the final state 76 log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath()) 77 if err := c.Meta.PersistState(newState); err != nil { 78 c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err)) 79 return 1 80 } 81 82 c.Ui.Output(c.Colorize().Color(fmt.Sprintf( 83 "[reset][green]\n" + 84 "Import success! The resources imported are shown above. These are\n" + 85 "now in your Terraform state. Import does not currently generate\n" + 86 "configuration, so you must do this next. If you do not create configuration\n" + 87 "for the above resources, then the next `terraform plan` will mark\n" + 88 "them for destruction."))) 89 90 return 0 91 } 92 93 func (c *ImportCommand) Help() string { 94 helpText := ` 95 Usage: terraform import [options] ADDR ID 96 97 Import existing infrastructure into your Terraform state. 98 99 This will find and import the specified resource into your Terraform 100 state, allowing existing infrastructure to come under Terraform 101 management without having to be initially created by Terraform. 102 103 The ADDR specified is the address to import the resource to. Please 104 see the documentation online for resource addresses. The ID is a 105 resource-specific ID to identify that resource being imported. Please 106 reference the documentation for the resource type you're importing to 107 determine the ID syntax to use. It typically matches directly to the ID 108 that the provider uses. 109 110 In the current state of Terraform import, the resource is only imported 111 into your state file. Once it is imported, you must manually write 112 configuration for the new resource or Terraform will mark it for destruction. 113 Future versions of Terraform will expand the functionality of Terraform 114 import. 115 116 This command will not modify your infrastructure, but it will make 117 network requests to inspect parts of your infrastructure relevant to 118 the resource being imported. 119 120 Options: 121 122 -backup=path Path to backup the existing state file before 123 modifying. Defaults to the "-state-out" path with 124 ".backup" extension. Set to "-" to disable backup. 125 126 -config=path Path to a directory of Terraform configuration files 127 to use to configure the provider. Defaults to pwd. 128 If no config files are present, they must be provided 129 via the input prompts or env vars. 130 131 -input=true Ask for input for variables if not directly set. 132 133 -no-color If specified, output won't contain any color. 134 135 -state=path Path to read and save state (unless state-out 136 is specified). Defaults to "terraform.tfstate". 137 138 -state-out=path Path to write updated state file. By default, the 139 "-state" path will be used. 140 141 ` 142 return strings.TrimSpace(helpText) 143 } 144 145 func (c *ImportCommand) Synopsis() string { 146 return "Import existing infrastructure into Terraform" 147 }