2017-01-19 05:49:42 +01:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
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)
|
2018-12-04 20:37:22 +01:00
|
|
|
p.buf.WriteString("\n\n")
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
return opts.Color.Color(strings.TrimSpace(p.buf.String()))
|
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 {
|
2018-09-25 00:00:07 +02:00
|
|
|
addr := m.Resources[key].Addr
|
2018-10-19 15:38:11 +02:00
|
|
|
|
|
|
|
taintStr := ""
|
2018-09-25 00:00:07 +02:00
|
|
|
if v.Current.Status == 'T' {
|
|
|
|
taintStr = "(tainted)"
|
|
|
|
}
|
2018-10-19 15:38:11 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf("# %s: %s\n", addr.Absolute(m.Addr).Instance(k), taintStr))
|
2018-09-25 00:00:07 +02:00
|
|
|
|
|
|
|
var schema *configschema.Block
|
|
|
|
provider := m.Resources[key].ProviderConfig.ProviderConfig.StringCompact()
|
2018-10-11 19:51:54 +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.
|
|
|
|
p.buf.WriteString(fmt.Sprintf("# missing schema for provider %q\n\n", provider))
|
|
|
|
continue
|
|
|
|
}
|
2018-09-25 00:00:07 +02:00
|
|
|
|
|
|
|
switch addr.Mode {
|
|
|
|
case addrs.ManagedResourceMode:
|
2018-10-11 19:51:54 +02:00
|
|
|
if _, exists := schemas.Providers[provider].ResourceTypes[addr.Type]; !exists {
|
2018-10-19 15:38:11 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"# missing schema for provider %q resource type %s\n\n", provider, addr.Type))
|
2018-10-11 19:51:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"resource %q %q {\n",
|
|
|
|
addr.Type,
|
|
|
|
addr.Name,
|
|
|
|
))
|
|
|
|
schema = schemas.Providers[provider].ResourceTypes[addr.Type]
|
|
|
|
case addrs.DataResourceMode:
|
2018-10-11 19:51:54 +02:00
|
|
|
if _, exists := schemas.Providers[provider].ResourceTypes[addr.Type]; !exists {
|
2018-10-19 15:38:11 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"# missing schema for provider %q data source %s\n\n", provider, addr.Type))
|
2018-10-11 19:51:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"data %q %q {\n",
|
|
|
|
addr.Type,
|
|
|
|
addr.Name,
|
|
|
|
))
|
|
|
|
schema = schemas.Providers[provider].DataSources[addr.Type]
|
|
|
|
default:
|
|
|
|
// should never happen, since the above is exhaustive
|
|
|
|
p.buf.WriteString(addr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := v.Current.Decode(schema.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
break
|
|
|
|
}
|
2018-10-19 15:38:11 +02:00
|
|
|
|
|
|
|
// First get the names of all the attributes so we can show them
|
|
|
|
// in alphabetical order.
|
|
|
|
names := make([]string, 0, len(schema.Attributes))
|
2018-09-25 00:00:07 +02:00
|
|
|
for name := range schema.Attributes {
|
2018-10-19 15:38:11 +02:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
for _, name := range names {
|
2018-09-25 00:00:07 +02:00
|
|
|
attr := ctyGetAttrMaybeNull(val.Value, name)
|
|
|
|
if !attr.IsNull() {
|
2018-10-10 20:22:09 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(" %s = ", name))
|
2018-09-25 00:00:07 +02:00
|
|
|
attr := ctyGetAttrMaybeNull(val.Value, name)
|
2018-10-10 20:22:09 +02:00
|
|
|
p.writeValue(attr, plans.NoOp, 4)
|
2018-09-25 00:00:07 +02:00
|
|
|
p.buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.buf.WriteString("}\n\n")
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-25 00:00:07 +02:00
|
|
|
p.buf.WriteString("[reset]\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))
|
|
|
|
for k, _ := range outputMap {
|
|
|
|
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))
|
|
|
|
for k, _ := range outputMap {
|
|
|
|
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")
|
|
|
|
}
|