2017-01-19 05:49:42 +01:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-30 19:08:59 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-09-24 18:30:39 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-09-25 00:00:07 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-09-24 18:30:39 +02:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2018-09-25 00:00:07 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/colorstring"
|
2017-01-19 05:49:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// StateOpts are the options for formatting a state.
|
|
|
|
type StateOpts struct {
|
|
|
|
// State is the state to format. This is required.
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
State *states.State
|
2017-01-19 05:49:42 +01:00
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
// Schemas are used to decode attributes. This is required.
|
|
|
|
Schemas *terraform.Schemas
|
|
|
|
|
2017-01-19 05:49:42 +01:00
|
|
|
// Color is the colorizer. This is optional.
|
|
|
|
Color *colorstring.Colorize
|
|
|
|
}
|
|
|
|
|
|
|
|
// State takes a state and returns a string
|
|
|
|
func State(opts *StateOpts) string {
|
|
|
|
if opts.Color == nil {
|
|
|
|
panic("colorize not given")
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
if opts.Schemas == nil {
|
|
|
|
panic("schemas not given")
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:49:42 +01:00
|
|
|
s := opts.State
|
|
|
|
if len(s.Modules) == 0 {
|
|
|
|
return "The state file is empty. No resources are represented."
|
|
|
|
}
|
|
|
|
|
2018-10-19 15:38:11 +02:00
|
|
|
buf := bytes.NewBufferString("[reset]")
|
2018-09-24 18:30:39 +02:00
|
|
|
p := blockBodyDiffPrinter{
|
2018-10-19 15:38:11 +02:00
|
|
|
buf: buf,
|
2018-09-24 18:30:39 +02:00
|
|
|
color: opts.Color,
|
|
|
|
action: plans.NoOp,
|
|
|
|
}
|
2017-01-19 05:49:42 +01:00
|
|
|
|
|
|
|
// Format all the modules
|
|
|
|
for _, m := range s.Modules {
|
2018-09-25 00:00:07 +02:00
|
|
|
formatStateModule(p, m, opts.Schemas)
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the outputs for the root module
|
|
|
|
m := s.RootModule()
|
2018-09-24 18:30:39 +02:00
|
|
|
|
|
|
|
if m.OutputValues != nil {
|
|
|
|
if len(m.OutputValues) > 0 {
|
2018-09-26 19:11:18 +02:00
|
|
|
p.buf.WriteString("Outputs:\n\n")
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
2017-01-19 05:49:42 +01:00
|
|
|
|
|
|
|
// Sort the outputs
|
2018-09-24 18:30:39 +02:00
|
|
|
ks := make([]string, 0, len(m.OutputValues))
|
|
|
|
for k := range m.OutputValues {
|
2017-01-19 05:49:42 +01:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
// Output each output k/v pair
|
|
|
|
for _, k := range ks {
|
2018-09-24 18:30:39 +02:00
|
|
|
v := m.OutputValues[k]
|
2018-09-25 00:00:07 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf("%s = ", k))
|
2018-09-24 18:30:39 +02:00
|
|
|
p.writeValue(v.Value, plans.NoOp, 0)
|
2019-05-16 15:52:06 +02:00
|
|
|
p.buf.WriteString("\n")
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
trimmedOutput := strings.TrimSpace(p.buf.String())
|
|
|
|
trimmedOutput += "[reset]"
|
|
|
|
|
|
|
|
return opts.Color.Color(trimmedOutput)
|
2018-09-24 18:30:39 +02:00
|
|
|
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:38:11 +02:00
|
|
|
func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraform.Schemas) {
|
2017-01-19 05:49:42 +01:00
|
|
|
// First get the names of all the resources so we can show them
|
|
|
|
// in alphabetical order.
|
|
|
|
names := make([]string, 0, len(m.Resources))
|
2018-09-24 18:30:39 +02:00
|
|
|
for name := range m.Resources {
|
2017-01-19 05:49:42 +01:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
// Go through each resource and begin building up the output.
|
2018-09-25 00:00:07 +02:00
|
|
|
for _, key := range names {
|
2018-10-19 15:38:11 +02:00
|
|
|
for k, v := range m.Resources[key].Instances {
|
2019-07-19 22:39:16 +02:00
|
|
|
// keep these in order to keep the current object first, and
|
|
|
|
// provide deterministic output for the deposed objects
|
|
|
|
type obj struct {
|
|
|
|
header string
|
|
|
|
instance *states.ResourceInstanceObjectSrc
|
|
|
|
}
|
|
|
|
instances := []obj{}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
addr := m.Resources[key].Addr
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr := addr.Resource
|
2018-10-19 15:38:11 +02:00
|
|
|
|
|
|
|
taintStr := ""
|
2019-07-19 22:39:16 +02:00
|
|
|
if v.Current != nil && v.Current.Status == 'T' {
|
2019-05-16 15:52:06 +02:00
|
|
|
taintStr = " (tainted)"
|
2018-09-25 00:00:07 +02:00
|
|
|
}
|
2019-07-19 22:39:16 +02:00
|
|
|
|
|
|
|
instances = append(instances,
|
2020-03-16 21:50:48 +01:00
|
|
|
obj{fmt.Sprintf("# %s:%s\n", addr.Instance(k), taintStr), v.Current})
|
2019-07-19 22:39:16 +02:00
|
|
|
|
|
|
|
for dk, v := range v.Deposed {
|
|
|
|
instances = append(instances,
|
2020-03-16 21:50:48 +01:00
|
|
|
obj{fmt.Sprintf("# %s: (deposed object %s)\n", addr.Instance(k), dk), v})
|
2018-10-11 19:51:54 +02:00
|
|
|
}
|
2018-09-25 00:00:07 +02:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
// Sort the instances for consistent output.
|
|
|
|
// Starting the sort from the second index, so the current instance
|
|
|
|
// is always first.
|
|
|
|
sort.Slice(instances[1:], func(i, j int) bool {
|
|
|
|
return instances[i+1].header < instances[j+1].header
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, obj := range instances {
|
|
|
|
header := obj.header
|
|
|
|
instance := obj.instance
|
|
|
|
p.buf.WriteString(header)
|
|
|
|
if instance == nil {
|
|
|
|
// this shouldn't happen, but there's nothing to do here so
|
|
|
|
// don't panic below.
|
2018-10-11 19:51:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
var schema *configschema.Block
|
2020-02-03 14:18:04 +01:00
|
|
|
|
2020-02-13 21:32:58 +01:00
|
|
|
provider := m.Resources[key].ProviderConfig.Provider
|
2019-07-19 22:39:16 +02:00
|
|
|
if _, exists := schemas.Providers[provider]; !exists {
|
|
|
|
// This should never happen in normal use because we should've
|
|
|
|
// loaded all of the schemas and checked things prior to this
|
|
|
|
// point. We can't return errors here, but since this is UI code
|
|
|
|
// we will try to do _something_ reasonable.
|
2020-02-04 18:07:59 +01:00
|
|
|
p.buf.WriteString(fmt.Sprintf("# missing schema for provider %q\n\n", provider.LegacyString()))
|
2018-10-11 19:51:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-16 21:50:48 +01:00
|
|
|
switch resAddr.Mode {
|
2019-07-19 22:39:16 +02:00
|
|
|
case addrs.ManagedResourceMode:
|
|
|
|
schema, _ = schemas.ResourceTypeConfig(
|
|
|
|
provider,
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Mode,
|
|
|
|
resAddr.Type,
|
2019-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
if schema == nil {
|
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
2020-03-16 21:50:48 +01:00
|
|
|
"# missing schema for provider %q resource type %s\n\n", provider, resAddr.Type))
|
2019-07-19 22:39:16 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-09-25 00:00:07 +02:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"resource %q %q {",
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Type,
|
|
|
|
resAddr.Name,
|
2019-07-19 22:39:16 +02:00
|
|
|
))
|
|
|
|
case addrs.DataResourceMode:
|
|
|
|
schema, _ = schemas.ResourceTypeConfig(
|
|
|
|
provider,
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Mode,
|
|
|
|
resAddr.Type,
|
2019-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
if schema == nil {
|
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
2020-03-16 21:50:48 +01:00
|
|
|
"# missing schema for provider %q data source %s\n\n", provider, resAddr.Type))
|
2019-07-19 22:39:16 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-10-19 15:38:11 +02:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"data %q %q {",
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Type,
|
|
|
|
resAddr.Name,
|
2019-07-19 22:39:16 +02:00
|
|
|
))
|
|
|
|
default:
|
|
|
|
// should never happen, since the above is exhaustive
|
2020-03-16 21:50:48 +01:00
|
|
|
p.buf.WriteString(resAddr.String())
|
2019-07-19 22:39:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val, err := instance.Decode(schema.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
path := make(cty.Path, 0, 3)
|
|
|
|
bodyWritten := p.writeBlockBodyDiff(schema, val.Value, val.Value, 2, path)
|
|
|
|
if bodyWritten {
|
|
|
|
p.buf.WriteString("\n")
|
|
|
|
}
|
2019-01-30 19:08:59 +01:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString("}\n\n")
|
|
|
|
}
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 15:52:06 +02:00
|
|
|
p.buf.WriteString("\n")
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatNestedList(indent string, outputList []interface{}) string {
|
|
|
|
outputBuf := new(bytes.Buffer)
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("%s[", indent))
|
|
|
|
|
|
|
|
lastIdx := len(outputList) - 1
|
|
|
|
|
|
|
|
for i, value := range outputList {
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s%s%s", indent, " ", value))
|
|
|
|
if i != lastIdx {
|
|
|
|
outputBuf.WriteString(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s]", indent))
|
|
|
|
return strings.TrimPrefix(outputBuf.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatListOutput(indent, outputName string, outputList []interface{}) string {
|
|
|
|
keyIndent := ""
|
|
|
|
|
|
|
|
outputBuf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if outputName != "" {
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("%s%s = [", indent, outputName))
|
|
|
|
keyIndent = " "
|
|
|
|
}
|
|
|
|
|
|
|
|
lastIdx := len(outputList) - 1
|
|
|
|
|
|
|
|
for i, value := range outputList {
|
|
|
|
switch typedValue := value.(type) {
|
|
|
|
case string:
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s%s%s", indent, keyIndent, value))
|
|
|
|
case []interface{}:
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s%s", indent,
|
|
|
|
formatNestedList(indent+keyIndent, typedValue)))
|
|
|
|
case map[string]interface{}:
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s%s", indent,
|
|
|
|
formatNestedMap(indent+keyIndent, typedValue)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastIdx != i {
|
|
|
|
outputBuf.WriteString(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if outputName != "" {
|
|
|
|
if len(outputList) > 0 {
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s]", indent))
|
|
|
|
} else {
|
|
|
|
outputBuf.WriteString("]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimPrefix(outputBuf.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatNestedMap(indent string, outputMap map[string]interface{}) string {
|
|
|
|
ks := make([]string, 0, len(outputMap))
|
2019-05-16 15:52:06 +02:00
|
|
|
for k := range outputMap {
|
2017-01-19 05:49:42 +01:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
outputBuf := new(bytes.Buffer)
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("%s{", indent))
|
|
|
|
|
|
|
|
lastIdx := len(outputMap) - 1
|
|
|
|
for i, k := range ks {
|
|
|
|
v := outputMap[k]
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s%s = %v", indent+" ", k, v))
|
|
|
|
|
|
|
|
if lastIdx != i {
|
|
|
|
outputBuf.WriteString(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s}", indent))
|
|
|
|
|
|
|
|
return strings.TrimPrefix(outputBuf.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatMapOutput(indent, outputName string, outputMap map[string]interface{}) string {
|
|
|
|
ks := make([]string, 0, len(outputMap))
|
2019-05-16 15:52:06 +02:00
|
|
|
for k := range outputMap {
|
2017-01-19 05:49:42 +01:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
keyIndent := ""
|
|
|
|
|
|
|
|
outputBuf := new(bytes.Buffer)
|
|
|
|
if outputName != "" {
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("%s%s = {", indent, outputName))
|
|
|
|
keyIndent = " "
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range ks {
|
|
|
|
v := outputMap[k]
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s%s%s = %v", indent, keyIndent, k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
if outputName != "" {
|
|
|
|
if len(outputMap) > 0 {
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("\n%s}", indent))
|
|
|
|
} else {
|
|
|
|
outputBuf.WriteString("}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimPrefix(outputBuf.String(), "\n")
|
|
|
|
}
|