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