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