github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/plan.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/config/module"
    10  	"github.com/hashicorp/terraform/tfdiags"
    11  )
    12  
    13  // PlanCommand is a Command implementation that compares a Terraform
    14  // configuration to an actual infrastructure and shows the differences.
    15  type PlanCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *PlanCommand) Run(args []string) int {
    20  	var destroy, refresh, detailed bool
    21  	var outPath string
    22  	var moduleDepth int
    23  
    24  	args, err := c.Meta.process(args, true)
    25  	if err != nil {
    26  		return 1
    27  	}
    28  
    29  	cmdFlags := c.Meta.flagSet("plan")
    30  	cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
    31  	cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
    32  	c.addModuleDepthFlag(cmdFlags, &moduleDepth)
    33  	cmdFlags.StringVar(&outPath, "out", "", "path")
    34  	cmdFlags.IntVar(
    35  		&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
    36  	cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
    37  	cmdFlags.BoolVar(&detailed, "detailed-exitcode", false, "detailed-exitcode")
    38  	cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
    39  	cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
    40  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    41  	if err := cmdFlags.Parse(args); err != nil {
    42  		return 1
    43  	}
    44  
    45  	configPath, err := ModulePath(cmdFlags.Args())
    46  	if err != nil {
    47  		c.Ui.Error(err.Error())
    48  		return 1
    49  	}
    50  
    51  	// Check for user-supplied plugin path
    52  	if c.pluginPath, err = c.loadPluginPath(); err != nil {
    53  		c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
    54  		return 1
    55  	}
    56  
    57  	// Check if the path is a plan
    58  	plan, err := c.Plan(configPath)
    59  	if err != nil {
    60  		c.Ui.Error(err.Error())
    61  		return 1
    62  	}
    63  	if plan != nil {
    64  		// Disable refreshing no matter what since we only want to show the plan
    65  		refresh = false
    66  
    67  		// Set the config path to empty for backend loading
    68  		configPath = ""
    69  	}
    70  
    71  	var diags tfdiags.Diagnostics
    72  
    73  	// Load the module if we don't have one yet (not running from plan)
    74  	var mod *module.Tree
    75  	if plan == nil {
    76  		var modDiags tfdiags.Diagnostics
    77  		mod, modDiags = c.Module(configPath)
    78  		diags = diags.Append(modDiags)
    79  		if modDiags.HasErrors() {
    80  			c.showDiagnostics(diags)
    81  			return 1
    82  		}
    83  	}
    84  
    85  	var conf *config.Config
    86  	if mod != nil {
    87  		conf = mod.Config()
    88  	}
    89  	// Load the backend
    90  	b, err := c.Backend(&BackendOpts{
    91  		Config: conf,
    92  		Plan:   plan,
    93  	})
    94  	if err != nil {
    95  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    96  		return 1
    97  	}
    98  
    99  	// Build the operation
   100  	opReq := c.Operation()
   101  	opReq.Destroy = destroy
   102  	opReq.Module = mod
   103  	opReq.Plan = plan
   104  	opReq.PlanRefresh = refresh
   105  	opReq.PlanOutPath = outPath
   106  	opReq.Type = backend.OperationTypePlan
   107  
   108  	// Perform the operation
   109  	op, err := c.RunOperation(b, opReq)
   110  	if err != nil {
   111  		diags = diags.Append(err)
   112  	}
   113  
   114  	c.showDiagnostics(diags)
   115  	if diags.HasErrors() {
   116  		return 1
   117  	}
   118  
   119  	if detailed && !op.PlanEmpty {
   120  		return 2
   121  	}
   122  
   123  	return 0
   124  }
   125  
   126  func (c *PlanCommand) Help() string {
   127  	helpText := `
   128  Usage: terraform plan [options] [DIR-OR-PLAN]
   129  
   130    Generates an execution plan for Terraform.
   131  
   132    This execution plan can be reviewed prior to running apply to get a
   133    sense for what Terraform will do. Optionally, the plan can be saved to
   134    a Terraform plan file, and apply can take this plan file to execute
   135    this plan exactly.
   136  
   137    If a saved plan is passed as an argument, this command will output
   138    the saved plan contents. It will not modify the given plan.
   139  
   140  Options:
   141  
   142    -destroy            If set, a plan will be generated to destroy all resources
   143                        managed by the given configuration and state.
   144  
   145    -detailed-exitcode  Return detailed exit codes when the command exits. This
   146                        will change the meaning of exit codes to:
   147                        0 - Succeeded, diff is empty (no changes)
   148                        1 - Errored
   149                        2 - Succeeded, there is a diff
   150  
   151    -input=true         Ask for input for variables if not directly set.
   152  
   153    -lock=true          Lock the state file when locking is supported.
   154  
   155    -lock-timeout=0s    Duration to retry a state lock.
   156  
   157    -module-depth=n     Specifies the depth of modules to show in the output.
   158                        This does not affect the plan itself, only the output
   159                        shown. By default, this is -1, which will expand all.
   160  
   161    -no-color           If specified, output won't contain any color.
   162  
   163    -out=path           Write a plan file to the given path. This can be used as
   164                        input to the "apply" command.
   165  
   166    -parallelism=n      Limit the number of concurrent operations. Defaults to 10.
   167  
   168    -refresh=true       Update state prior to checking for differences.
   169  
   170    -state=statefile    Path to a Terraform state file to use to look
   171                        up Terraform-managed resources. By default it will
   172                        use the state "terraform.tfstate" if it exists.
   173  
   174    -target=resource    Resource to target. Operation will be limited to this
   175                        resource and its dependencies. This flag can be used
   176                        multiple times.
   177  
   178    -var 'foo=bar'      Set a variable in the Terraform configuration. This
   179                        flag can be set multiple times.
   180  
   181    -var-file=foo       Set variables in the Terraform configuration from
   182                        a file. If "terraform.tfvars" or any ".auto.tfvars"
   183                        files are present, they will be automatically loaded.
   184  `
   185  	return strings.TrimSpace(helpText)
   186  }
   187  
   188  func (c *PlanCommand) Synopsis() string {
   189  	return "Generate and show an execution plan"
   190  }