command: also print plan for destroy

This commit is contained in:
David Glasser 2017-06-12 18:30:57 -07:00 committed by Martin Atkins
parent 039d36bf91
commit 14af879fe0
3 changed files with 49 additions and 50 deletions

View File

@ -121,10 +121,11 @@ type Operation struct {
// The options below are more self-explanatory and affect the runtime // The options below are more self-explanatory and affect the runtime
// behavior of the operation. // behavior of the operation.
Destroy bool Destroy bool
Targets []string Targets []string
Variables map[string]interface{} Variables map[string]interface{}
AutoApprove bool AutoApprove bool
DestroyForce bool
// Input/output/control options. // Input/output/control options.
UIIn terraform.UIInput UIIn terraform.UIInput

View File

@ -98,18 +98,46 @@ func (b *Local) opApply(
trivialPlan := plan.Diff == nil || plan.Diff.Empty() trivialPlan := plan.Diff == nil || plan.Diff.Empty()
hasUI := op.UIOut != nil && op.UIIn != nil hasUI := op.UIOut != nil && op.UIIn != nil
if hasUI && !op.AutoApprove && !trivialPlan { if hasUI && ((op.Destroy && !op.DestroyForce) ||
op.UIOut.Output(strings.TrimSpace(approvePlanHeader) + "\n") (!op.Destroy && !op.AutoApprove && !trivialPlan)) {
op.UIOut.Output(format.Plan(&format.PlanOpts{ var desc, query string
Plan: plan, if op.Destroy {
Color: b.Colorize(), // Default destroy message
ModuleDepth: -1, desc = "Terraform will delete all your managed infrastructure.\n" +
})) "There is no undo. Only 'yes' will be accepted to confirm."
desc := "Terraform will apply the plan described above.\n" +
"Only 'yes' will be accepted to approve." // If targets are specified, list those to user
if op.Targets != nil {
var descBuffer bytes.Buffer
descBuffer.WriteString("Terraform will delete the following infrastructure:\n")
for _, target := range op.Targets {
descBuffer.WriteString("\t")
descBuffer.WriteString(target)
descBuffer.WriteString("\n")
}
descBuffer.WriteString("There is no undo. Only 'yes' will be accepted to confirm")
desc = descBuffer.String()
}
query = "Do you really want to destroy?"
} else {
desc = "Terraform will apply the plan described above.\n" +
"Only 'yes' will be accepted to approve."
query = "Do you want to apply the plan above?"
}
if !trivialPlan {
// Display the plan of what we are going to apply/destroy.
op.UIOut.Output(strings.TrimSpace(approvePlanHeader) + "\n")
op.UIOut.Output(format.Plan(&format.PlanOpts{
Plan: plan,
Color: b.Colorize(),
ModuleDepth: -1,
}))
}
v, err := op.UIIn.Input(&terraform.InputOpts{ v, err := op.UIIn.Input(&terraform.InputOpts{
Id: "approve", Id: "approve",
Query: "Do you want to apply the plan above?", Query: query,
Description: desc, Description: desc,
}) })
if err != nil { if err != nil {
@ -117,7 +145,11 @@ func (b *Local) opApply(
return return
} }
if v != "yes" { if v != "yes" {
runningOp.Err = errors.New("Apply cancelled.") if op.Destroy {
runningOp.Err = errors.New("Destroy cancelled.")
} else {
runningOp.Err = errors.New("Apply cancelled.")
}
return return
} }
} }

View File

@ -161,41 +161,6 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 return 1
} }
// If we're not forcing and we're destroying, verify with the
// user at this point.
if !destroyForce && c.Destroy {
// Default destroy message
desc := "Terraform will delete all your managed infrastructure.\n" +
"There is no undo. Only 'yes' will be accepted to confirm."
// If targets are specified, list those to user
if c.Meta.targets != nil {
var descBuffer bytes.Buffer
descBuffer.WriteString("Terraform will delete the following infrastructure:\n")
for _, target := range c.Meta.targets {
descBuffer.WriteString("\t")
descBuffer.WriteString(target)
descBuffer.WriteString("\n")
}
descBuffer.WriteString("There is no undo. Only 'yes' will be accepted to confirm")
desc = descBuffer.String()
}
v, err := c.UIInput().Input(&terraform.InputOpts{
Id: "destroy",
Query: "Do you really want to destroy?",
Description: desc,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Error asking for confirmation: %s", err))
return 1
}
if v != "yes" {
c.Ui.Output("Destroy cancelled.")
return 1
}
}
// Build the operation // Build the operation
opReq := c.Operation() opReq := c.Operation()
opReq.Destroy = c.Destroy opReq.Destroy = c.Destroy
@ -204,6 +169,7 @@ func (c *ApplyCommand) Run(args []string) int {
opReq.PlanRefresh = refresh opReq.PlanRefresh = refresh
opReq.Type = backend.OperationTypeApply opReq.Type = backend.OperationTypeApply
opReq.AutoApprove = autoApprove opReq.AutoApprove = autoApprove
opReq.DestroyForce = destroyForce
// Perform the operation // Perform the operation
ctx, ctxCancel := context.WithCancel(context.Background()) ctx, ctxCancel := context.WithCancel(context.Background())