github.com/bfallik/terraform@v0.7.1-0.20160814101525-d3a4714efbf5/command/import.go (about)

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