2016-05-04 19:06:16 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-11-02 18:30:28 +01:00
|
|
|
"os"
|
2016-05-04 19:06:16 +02:00
|
|
|
"strings"
|
2016-05-04 19:32:08 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2016-05-04 19:06:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImportCommand is a cli.Command implementation that imports resources
|
|
|
|
// into the Terraform state.
|
|
|
|
type ImportCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ImportCommand) Run(args []string) int {
|
2016-11-02 19:11:42 +01:00
|
|
|
// Get the pwd since its our default -config flag value
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var configPath string
|
2016-05-04 19:06:16 +02:00
|
|
|
args = c.Meta.process(args, true)
|
|
|
|
|
|
|
|
cmdFlags := c.Meta.flagSet("import")
|
|
|
|
cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism")
|
2016-11-02 19:08:16 +01:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
2016-05-04 19:06:16 +02:00
|
|
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
|
2016-11-02 19:11:42 +01:00
|
|
|
cmdFlags.StringVar(&configPath, "config", pwd, "path")
|
2016-11-23 10:44:52 +01:00
|
|
|
cmdFlags.StringVar(&c.Meta.provider, "provider", "", "provider")
|
2016-05-04 19:06:16 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 2 {
|
|
|
|
c.Ui.Error("The import command expects two arguments.")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the context based on the arguments given
|
|
|
|
ctx, _, err := c.Context(contextOpts{
|
2016-11-02 19:11:42 +01:00
|
|
|
Path: configPath,
|
2016-11-02 18:30:28 +01:00
|
|
|
PathEmptyOk: true,
|
2016-05-04 19:06:16 +02:00
|
|
|
StatePath: c.Meta.statePath,
|
|
|
|
Parallelism: c.Meta.parallelism,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:32:08 +02:00
|
|
|
// Perform the import. Note that as you can see it is possible for this
|
|
|
|
// API to import more than one resource at once. For now, we only allow
|
|
|
|
// one while we stabilize this feature.
|
|
|
|
newState, err := ctx.Import(&terraform.ImportOpts{
|
|
|
|
Targets: []*terraform.ImportTarget{
|
|
|
|
&terraform.ImportTarget{
|
2016-11-23 10:44:52 +01:00
|
|
|
Addr: args[0],
|
|
|
|
ID: args[1],
|
|
|
|
Provider: c.Meta.provider,
|
2016-05-04 19:32:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-05-04 19:06:16 +02:00
|
|
|
if err != nil {
|
2016-05-04 19:32:08 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error importing: %s", err))
|
2016-05-04 19:06:16 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:32:08 +02:00
|
|
|
// Persist the final state
|
2016-05-04 19:06:16 +02:00
|
|
|
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
|
|
|
if err := c.Meta.PersistState(newState); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-05-04 21:05:42 +02:00
|
|
|
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][green]\n" +
|
|
|
|
"Import success! The resources imported are shown above. These are\n" +
|
|
|
|
"now in your Terraform state. Import does not currently generate\n" +
|
|
|
|
"configuration, so you must do this next. If you do not create configuration\n" +
|
|
|
|
"for the above resources, then the next `terraform plan` will mark\n" +
|
|
|
|
"them for destruction.")))
|
|
|
|
|
2016-05-04 19:06:16 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ImportCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform import [options] ADDR ID
|
|
|
|
|
|
|
|
Import existing infrastructure into your Terraform state.
|
|
|
|
|
|
|
|
This will find and import the specified resource into your Terraform
|
|
|
|
state, allowing existing infrastructure to come under Terraform
|
|
|
|
management without having to be initially created by Terraform.
|
|
|
|
|
|
|
|
The ADDR specified is the address to import the resource to. Please
|
|
|
|
see the documentation online for resource addresses. The ID is a
|
|
|
|
resource-specific ID to identify that resource being imported. Please
|
|
|
|
reference the documentation for the resource type you're importing to
|
|
|
|
determine the ID syntax to use. It typically matches directly to the ID
|
|
|
|
that the provider uses.
|
|
|
|
|
|
|
|
In the current state of Terraform import, the resource is only imported
|
|
|
|
into your state file. Once it is imported, you must manually write
|
|
|
|
configuration for the new resource or Terraform will mark it for destruction.
|
|
|
|
Future versions of Terraform will expand the functionality of Terraform
|
|
|
|
import.
|
|
|
|
|
|
|
|
This command will not modify your infrastructure, but it will make
|
|
|
|
network requests to inspect parts of your infrastructure relevant to
|
|
|
|
the resource being imported.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-backup=path Path to backup the existing state file before
|
|
|
|
modifying. Defaults to the "-state-out" path with
|
|
|
|
".backup" extension. Set to "-" to disable backup.
|
|
|
|
|
2016-11-02 19:11:42 +01:00
|
|
|
-config=path Path to a directory of Terraform configuration files
|
|
|
|
to use to configure the provider. Defaults to pwd.
|
|
|
|
If no config files are present, they must be provided
|
|
|
|
via the input prompts or env vars.
|
|
|
|
|
2016-05-04 19:06:16 +02:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
|
|
|
-state=path Path to read and save state (unless state-out
|
|
|
|
is specified). Defaults to "terraform.tfstate".
|
|
|
|
|
|
|
|
-state-out=path Path to write updated state file. By default, the
|
|
|
|
"-state" path will be used.
|
|
|
|
|
2016-11-23 10:44:52 +01:00
|
|
|
-provider=provider Provider used for import. Defaults to: ""
|
|
|
|
|
2016-05-04 19:06:16 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ImportCommand) Synopsis() string {
|
|
|
|
return "Import existing infrastructure into Terraform"
|
|
|
|
}
|