github.com/partitio/terraform@v0.11.12-beta1/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.ModuleDepth = moduleDepth
   104  	opReq.Plan = plan
   105  	opReq.PlanOutPath = outPath
   106  	opReq.PlanRefresh = refresh
   107  	opReq.Type = backend.OperationTypePlan
   108  
   109  	// Perform the operation
   110  	op, err := c.RunOperation(b, opReq)
   111  	if err != nil {
   112  		diags = diags.Append(err)
   113  	}
   114  
   115  	c.showDiagnostics(diags)
   116  	if diags.HasErrors() {
   117  		return 1
   118  	}
   119  
   120  	if detailed && !op.PlanEmpty {
   121  		return 2
   122  	}
   123  
   124  	return op.ExitCode
   125  }
   126  
   127  func (c *PlanCommand) Help() string {
   128  	helpText := `
   129  Usage: terraform plan [options] [DIR-OR-PLAN]
   130  
   131    Generates an execution plan for Terraform.
   132  
   133    This execution plan can be reviewed prior to running apply to get a
   134    sense for what Terraform will do. Optionally, the plan can be saved to
   135    a Terraform plan file, and apply can take this plan file to execute
   136    this plan exactly.
   137  
   138    If a saved plan is passed as an argument, this command will output
   139    the saved plan contents. It will not modify the given plan.
   140  
   141  Options:
   142  
   143    -destroy            If set, a plan will be generated to destroy all resources
   144                        managed by the given configuration and state.
   145  
   146    -detailed-exitcode  Return detailed exit codes when the command exits. This
   147                        will change the meaning of exit codes to:
   148                        0 - Succeeded, diff is empty (no changes)
   149                        1 - Errored
   150                        2 - Succeeded, there is a diff
   151  
   152    -input=true         Ask for input for variables if not directly set.
   153  
   154    -lock=true          Lock the state file when locking is supported.
   155  
   156    -lock-timeout=0s    Duration to retry a state lock.
   157  
   158    -module-depth=n     Specifies the depth of modules to show in the output.
   159                        This does not affect the plan itself, only the output
   160                        shown. By default, this is -1, which will expand all.
   161  
   162    -no-color           If specified, output won't contain any color.
   163  
   164    -out=path           Write a plan file to the given path. This can be used as
   165                        input to the "apply" command.
   166  
   167    -parallelism=n      Limit the number of concurrent operations. Defaults to 10.
   168  
   169    -refresh=true       Update state prior to checking for differences.
   170  
   171    -state=statefile    Path to a Terraform state file to use to look
   172                        up Terraform-managed resources. By default it will
   173                        use the state "terraform.tfstate" if it exists.
   174  
   175    -target=resource    Resource to target. Operation will be limited to this
   176                        resource and its dependencies. This flag can be used
   177                        multiple times.
   178  
   179    -var 'foo=bar'      Set a variable in the Terraform configuration. This
   180                        flag can be set multiple times.
   181  
   182    -var-file=foo       Set variables in the Terraform configuration from
   183                        a file. If "terraform.tfvars" or any ".auto.tfvars"
   184                        files are present, they will be automatically loaded.
   185  `
   186  	return strings.TrimSpace(helpText)
   187  }
   188  
   189  func (c *PlanCommand) Synopsis() string {
   190  	return "Generate and show an execution plan"
   191  }