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