2014-07-13 01:32:48 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/colorstring"
|
|
|
|
)
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
// FormatPlanOpts are the options for formatting a plan.
|
|
|
|
type FormatPlanOpts struct {
|
|
|
|
// Plan is the plan to format. This is required.
|
|
|
|
Plan *terraform.Plan
|
|
|
|
|
|
|
|
// Color is the colorizer. This is optional.
|
|
|
|
Color *colorstring.Colorize
|
|
|
|
|
|
|
|
// ModuleDepth is the depth of the modules to expand. By default this
|
|
|
|
// is zero which will not expand modules at all.
|
|
|
|
ModuleDepth int
|
|
|
|
}
|
|
|
|
|
2014-07-13 01:32:48 +02:00
|
|
|
// FormatPlan takes a plan and returns a
|
2014-09-25 07:54:51 +02:00
|
|
|
func FormatPlan(opts *FormatPlanOpts) string {
|
|
|
|
p := opts.Plan
|
2014-07-13 04:47:31 +02:00
|
|
|
if p.Diff == nil || p.Diff.Empty() {
|
|
|
|
return "This plan does nothing."
|
|
|
|
}
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
if opts.Color == nil {
|
|
|
|
opts.Color = &colorstring.Colorize{
|
2014-07-13 01:32:48 +02:00
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Reset: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2014-09-25 07:54:51 +02:00
|
|
|
for _, m := range p.Diff.Modules {
|
|
|
|
if len(m.Path)-1 <= opts.ModuleDepth || opts.ModuleDepth == -1 {
|
|
|
|
formatPlanModuleExpand(buf, m, opts)
|
|
|
|
} else {
|
|
|
|
formatPlanModuleSingle(buf, m, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatPlanModuleExpand will output the given module and all of its
|
|
|
|
// resources.
|
|
|
|
func formatPlanModuleExpand(
|
|
|
|
buf *bytes.Buffer, m *terraform.ModuleDiff, opts *FormatPlanOpts) {
|
|
|
|
// Ignore empty diffs
|
|
|
|
if m.Empty() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var moduleName string
|
|
|
|
if !m.IsRoot() {
|
|
|
|
moduleName = fmt.Sprintf("module.%s", strings.Join(m.Path[1:], "."))
|
|
|
|
}
|
2014-07-13 01:32:48 +02:00
|
|
|
|
|
|
|
// We want to output the resources in sorted order to make things
|
|
|
|
// easier to scan through, so get all the resource names and sort them.
|
2014-09-25 07:54:51 +02:00
|
|
|
names := make([]string, 0, len(m.Resources))
|
|
|
|
for name, _ := range m.Resources {
|
2014-07-13 01:32:48 +02:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
// Go through each sorted name and start building the output
|
|
|
|
for _, name := range names {
|
2014-09-25 07:54:51 +02:00
|
|
|
rdiff := m.Resources[name]
|
|
|
|
if rdiff.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-30 20:46:08 +02:00
|
|
|
dataSource := strings.HasPrefix(name, "data.")
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
if moduleName != "" {
|
|
|
|
name = moduleName + "." + name
|
|
|
|
}
|
2014-07-13 01:32:48 +02:00
|
|
|
|
|
|
|
// Determine the color for the text (green for adding, yellow
|
|
|
|
// for change, red for delete), and symbol, and output the
|
|
|
|
// resource header.
|
|
|
|
color := "yellow"
|
|
|
|
symbol := "~"
|
2016-05-08 09:03:41 +02:00
|
|
|
oldValues := true
|
2014-09-25 07:54:51 +02:00
|
|
|
switch rdiff.ChangeType() {
|
|
|
|
case terraform.DiffDestroyCreate:
|
2014-07-27 04:54:09 +02:00
|
|
|
color = "green"
|
|
|
|
symbol = "-/+"
|
2014-09-25 07:54:51 +02:00
|
|
|
case terraform.DiffCreate:
|
2014-07-13 01:32:48 +02:00
|
|
|
color = "green"
|
|
|
|
symbol = "+"
|
2016-05-08 09:03:41 +02:00
|
|
|
oldValues = false
|
2016-05-08 09:16:35 +02:00
|
|
|
|
|
|
|
// If we're "creating" a data resource then we'll present it
|
|
|
|
// to the user as a "read" operation, so it's clear that this
|
|
|
|
// operation won't change anything outside of the Terraform state.
|
|
|
|
// Unfortunately by the time we get here we only have the name
|
|
|
|
// to work with, so we need to cheat and exploit knowledge of the
|
|
|
|
// naming scheme for data resources.
|
2016-06-30 20:46:08 +02:00
|
|
|
if dataSource {
|
2016-05-08 09:16:35 +02:00
|
|
|
symbol = "<="
|
|
|
|
color = "cyan"
|
|
|
|
}
|
2014-09-25 07:54:51 +02:00
|
|
|
case terraform.DiffDestroy:
|
2014-07-13 01:32:48 +02:00
|
|
|
color = "red"
|
|
|
|
symbol = "-"
|
|
|
|
}
|
2014-09-25 07:54:51 +02:00
|
|
|
|
2016-11-28 22:49:10 +01:00
|
|
|
var extraAttr []string
|
2016-04-21 21:55:29 +02:00
|
|
|
if rdiff.DestroyTainted {
|
2016-11-28 22:49:10 +01:00
|
|
|
extraAttr = append(extraAttr, "tainted")
|
|
|
|
}
|
|
|
|
if rdiff.DestroyDeposed {
|
|
|
|
extraAttr = append(extraAttr, "deposed")
|
|
|
|
}
|
|
|
|
var extraStr string
|
|
|
|
if len(extraAttr) > 0 {
|
|
|
|
extraStr = fmt.Sprintf(" (%s)", strings.Join(extraAttr, ", "))
|
2016-04-21 21:55:29 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
buf.WriteString(opts.Color.Color(fmt.Sprintf(
|
2016-04-21 21:55:29 +02:00
|
|
|
"[%s]%s %s%s\n",
|
2016-11-28 22:49:10 +01:00
|
|
|
color, symbol, name, extraStr)))
|
2014-07-13 01:32:48 +02:00
|
|
|
|
|
|
|
// Get all the attributes that are changing, and sort them. Also
|
|
|
|
// determine the longest key so that we can align them all.
|
|
|
|
keyLen := 0
|
|
|
|
keys := make([]string, 0, len(rdiff.Attributes))
|
|
|
|
for key, _ := range rdiff.Attributes {
|
|
|
|
// Skip the ID since we do that specially
|
|
|
|
if key == "id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
if len(key) > keyLen {
|
|
|
|
keyLen = len(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
// Go through and output each attribute
|
|
|
|
for _, attrK := range keys {
|
|
|
|
attrDiff := rdiff.Attributes[attrK]
|
|
|
|
|
|
|
|
v := attrDiff.New
|
|
|
|
if attrDiff.NewComputed {
|
|
|
|
v = "<computed>"
|
|
|
|
}
|
|
|
|
|
2016-05-28 02:00:59 +02:00
|
|
|
if attrDiff.Sensitive {
|
|
|
|
v = "<sensitive>"
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMsg := ""
|
2014-07-13 01:32:48 +02:00
|
|
|
if attrDiff.RequiresNew && rdiff.Destroy {
|
2016-05-28 02:00:59 +02:00
|
|
|
updateMsg = opts.Color.Color(" [red](forces new resource)")
|
|
|
|
} else if attrDiff.Sensitive && oldValues {
|
|
|
|
updateMsg = opts.Color.Color(" [yellow](attribute changed)")
|
2014-07-13 01:32:48 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 09:03:41 +02:00
|
|
|
if oldValues {
|
2016-05-28 02:00:59 +02:00
|
|
|
var u string
|
|
|
|
if attrDiff.Sensitive {
|
|
|
|
u = "<sensitive>"
|
|
|
|
} else {
|
|
|
|
u = attrDiff.Old
|
|
|
|
}
|
2016-05-08 09:03:41 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
" %s:%s %#v => %#v%s\n",
|
|
|
|
attrK,
|
|
|
|
strings.Repeat(" ", keyLen-len(attrK)),
|
2016-05-28 02:00:59 +02:00
|
|
|
u,
|
2016-05-08 09:03:41 +02:00
|
|
|
v,
|
2016-05-28 02:00:59 +02:00
|
|
|
updateMsg))
|
2016-05-08 09:03:41 +02:00
|
|
|
} else {
|
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
" %s:%s %#v%s\n",
|
|
|
|
attrK,
|
|
|
|
strings.Repeat(" ", keyLen-len(attrK)),
|
|
|
|
v,
|
2016-05-28 02:00:59 +02:00
|
|
|
updateMsg))
|
2016-05-08 09:03:41 +02:00
|
|
|
}
|
2014-07-13 01:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the reset color so we don't overload the user's terminal
|
2014-09-25 07:54:51 +02:00
|
|
|
buf.WriteString(opts.Color.Color("[reset]\n"))
|
2014-07-13 01:32:48 +02:00
|
|
|
}
|
2014-09-25 07:54:51 +02:00
|
|
|
}
|
2014-07-13 01:32:48 +02:00
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
// formatPlanModuleSingle will output the given module and all of its
|
|
|
|
// resources.
|
|
|
|
func formatPlanModuleSingle(
|
|
|
|
buf *bytes.Buffer, m *terraform.ModuleDiff, opts *FormatPlanOpts) {
|
|
|
|
// Ignore empty diffs
|
|
|
|
if m.Empty() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleName := fmt.Sprintf("module.%s", strings.Join(m.Path[1:], "."))
|
|
|
|
|
|
|
|
// Determine the color for the text (green for adding, yellow
|
|
|
|
// for change, red for delete), and symbol, and output the
|
|
|
|
// resource header.
|
|
|
|
color := "yellow"
|
|
|
|
symbol := "~"
|
|
|
|
switch m.ChangeType() {
|
|
|
|
case terraform.DiffCreate:
|
|
|
|
color = "green"
|
|
|
|
symbol = "+"
|
|
|
|
case terraform.DiffDestroy:
|
|
|
|
color = "red"
|
|
|
|
symbol = "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(opts.Color.Color(fmt.Sprintf(
|
|
|
|
"[%s]%s %s\n",
|
|
|
|
color, symbol, moduleName)))
|
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
" %d resource(s)",
|
|
|
|
len(m.Resources)))
|
|
|
|
buf.WriteString(opts.Color.Color("[reset]\n"))
|
2014-07-13 01:32:48 +02:00
|
|
|
}
|