github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/refresh.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/backend"
     8  	"github.com/hashicorp/terraform/config"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/hashicorp/terraform/tfdiags"
    11  )
    12  
    13  // RefreshCommand is a cli.Command implementation that refreshes the state
    14  // file.
    15  type RefreshCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *RefreshCommand) Run(args []string) int {
    20  	args, err := c.Meta.process(args, true)
    21  	if err != nil {
    22  		return 1
    23  	}
    24  
    25  	cmdFlags := c.Meta.flagSet("refresh")
    26  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    27  	cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism")
    28  	cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
    29  	cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
    30  	cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
    31  	cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
    32  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    33  	if err := cmdFlags.Parse(args); err != nil {
    34  		return 1
    35  	}
    36  
    37  	configPath, err := ModulePath(cmdFlags.Args())
    38  	if err != nil {
    39  		c.Ui.Error(err.Error())
    40  		return 1
    41  	}
    42  
    43  	var diags tfdiags.Diagnostics
    44  
    45  	// Load the module
    46  	mod, diags := c.Module(configPath)
    47  	if diags.HasErrors() {
    48  		c.showDiagnostics(diags)
    49  		return 1
    50  	}
    51  
    52  	// Check for user-supplied plugin path
    53  	if c.pluginPath, err = c.loadPluginPath(); err != nil {
    54  		c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
    55  		return 1
    56  	}
    57  
    58  	var conf *config.Config
    59  	if mod != nil {
    60  		conf = mod.Config()
    61  	}
    62  
    63  	// Load the backend
    64  	b, err := c.Backend(&BackendOpts{
    65  		Config: conf,
    66  	})
    67  	if err != nil {
    68  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    69  		return 1
    70  	}
    71  
    72  	// Build the operation
    73  	opReq := c.Operation()
    74  	opReq.Type = backend.OperationTypeRefresh
    75  	opReq.Module = mod
    76  
    77  	op, err := c.RunOperation(b, opReq)
    78  	if err != nil {
    79  		diags = diags.Append(err)
    80  	}
    81  
    82  	c.showDiagnostics(diags)
    83  	if diags.HasErrors() {
    84  		return 1
    85  	}
    86  
    87  	// Output the outputs
    88  	if outputs := outputsAsString(op.State, terraform.RootModulePath, nil, true); outputs != "" {
    89  		c.Ui.Output(c.Colorize().Color(outputs))
    90  	}
    91  
    92  	return 0
    93  }
    94  
    95  func (c *RefreshCommand) Help() string {
    96  	helpText := `
    97  Usage: terraform refresh [options] [dir]
    98  
    99    Update the state file of your infrastructure with metadata that matches
   100    the physical resources they are tracking.
   101  
   102    This will not modify your infrastructure, but it can modify your
   103    state file to update metadata. This metadata might cause new changes
   104    to occur when you generate a plan or call apply next.
   105  
   106  Options:
   107  
   108    -backup=path        Path to backup the existing state file before
   109                        modifying. Defaults to the "-state-out" path with
   110                        ".backup" extension. Set to "-" to disable backup.
   111  
   112    -input=true         Ask for input for variables if not directly set.
   113  
   114    -lock=true          Lock the state file when locking is supported.
   115  
   116    -lock-timeout=0s    Duration to retry a state lock.
   117  
   118    -no-color           If specified, output won't contain any color.
   119  
   120    -state=path         Path to read and save state (unless state-out
   121                        is specified). Defaults to "terraform.tfstate".
   122  
   123    -state-out=path     Path to write updated state file. By default, the
   124                        "-state" path will be used.
   125  
   126    -target=resource    Resource to target. Operation will be limited to this
   127                        resource and its dependencies. This flag can be used
   128                        multiple times.
   129  
   130    -var 'foo=bar'      Set a variable in the Terraform configuration. This
   131                        flag can be set multiple times.
   132  
   133    -var-file=foo       Set variables in the Terraform configuration from
   134                        a file. If "terraform.tfvars" or any ".auto.tfvars"
   135                        files are present, they will be automatically loaded.
   136  
   137  `
   138  	return strings.TrimSpace(helpText)
   139  }
   140  
   141  func (c *RefreshCommand) Synopsis() string {
   142  	return "Update local state file against real resources"
   143  }