2018-08-01 00:26:51 +02:00
|
|
|
package states
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/hcl2shim"
|
2018-08-01 00:26:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns a rather-odd string representation of the entire state.
|
|
|
|
//
|
|
|
|
// This is intended to match the behavior of the older terraform.State.String
|
|
|
|
// method that is used in lots of existing tests. It should not be used in
|
|
|
|
// new tests: instead, use "cmp" to directly compare the state data structures
|
|
|
|
// and print out a diff if they do not match.
|
|
|
|
//
|
|
|
|
// This method should never be used in non-test code, whether directly by call
|
|
|
|
// or indirectly via a %s or %q verb in package fmt.
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
|
2018-09-07 03:58:14 +02:00
|
|
|
// sort the modules by name for consistent output
|
|
|
|
modules := make([]string, 0, len(s.Modules))
|
|
|
|
for m := range s.Modules {
|
|
|
|
modules = append(modules, m)
|
|
|
|
}
|
|
|
|
sort.Strings(modules)
|
|
|
|
|
2018-08-01 00:26:51 +02:00
|
|
|
var buf bytes.Buffer
|
2018-09-07 03:58:14 +02:00
|
|
|
for _, name := range modules {
|
|
|
|
m := s.Modules[name]
|
2018-08-01 00:26:51 +02:00
|
|
|
mStr := m.testString()
|
|
|
|
|
|
|
|
// If we're the root module, we just write the output directly.
|
|
|
|
if m.Addr.IsRoot() {
|
|
|
|
buf.WriteString(mStr + "\n")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to build out a string that resembles the not-quite-standard
|
|
|
|
// format that terraform.State.String used to use, where there's a
|
|
|
|
// "module." prefix but then just a chain of all of the module names
|
|
|
|
// without any further "module." portions.
|
|
|
|
buf.WriteString("module")
|
|
|
|
for _, step := range m.Addr {
|
|
|
|
buf.WriteByte('.')
|
|
|
|
buf.WriteString(step.Name)
|
|
|
|
if step.InstanceKey != addrs.NoKey {
|
|
|
|
buf.WriteString(step.InstanceKey.String())
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 20:48:31 +02:00
|
|
|
buf.WriteString(":\n")
|
2018-08-01 00:26:51 +02:00
|
|
|
|
|
|
|
s := bufio.NewScanner(strings.NewReader(mStr))
|
|
|
|
for s.Scan() {
|
|
|
|
text := s.Text()
|
|
|
|
if text != "" {
|
|
|
|
text = " " + text
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s\n", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// testString is used to produce part of the output of State.String. It should
|
|
|
|
// never be used directly.
|
2020-11-30 23:20:50 +01:00
|
|
|
func (ms *Module) testString() string {
|
2018-08-01 00:26:51 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2020-11-30 23:20:50 +01:00
|
|
|
if len(ms.Resources) == 0 {
|
2018-08-01 00:26:51 +02:00
|
|
|
buf.WriteString("<no state>")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use AbsResourceInstance here, even though everything belongs to
|
|
|
|
// the same module, just because we have a sorting behavior defined
|
|
|
|
// for those but not for just ResourceInstance.
|
2020-11-30 23:20:50 +01:00
|
|
|
addrsOrder := make([]addrs.AbsResourceInstance, 0, len(ms.Resources))
|
|
|
|
for _, rs := range ms.Resources {
|
2018-08-01 00:26:51 +02:00
|
|
|
for ik := range rs.Instances {
|
2020-03-13 18:51:55 +01:00
|
|
|
addrsOrder = append(addrsOrder, rs.Addr.Instance(ik))
|
2018-08-01 00:26:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(addrsOrder, func(i, j int) bool {
|
|
|
|
return addrsOrder[i].Less(addrsOrder[j])
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, fakeAbsAddr := range addrsOrder {
|
|
|
|
addr := fakeAbsAddr.Resource
|
2020-11-30 23:20:50 +01:00
|
|
|
rs := ms.Resource(addr.ContainingResource())
|
|
|
|
is := ms.ResourceInstance(addr)
|
2018-08-01 00:26:51 +02:00
|
|
|
|
|
|
|
// Here we need to fake up a legacy-style address as the old state
|
|
|
|
// types would've used, since that's what our tests against those
|
|
|
|
// old types expect. The significant difference is that instancekey
|
|
|
|
// is dot-separated rather than using index brackets.
|
|
|
|
k := addr.ContainingResource().String()
|
|
|
|
if addr.Key != addrs.NoKey {
|
|
|
|
switch tk := addr.Key.(type) {
|
|
|
|
case addrs.IntKey:
|
|
|
|
k = fmt.Sprintf("%s.%d", k, tk)
|
|
|
|
default:
|
|
|
|
// No other key types existed for the legacy types, so we
|
|
|
|
// can do whatever we want here. We'll just use our standard
|
|
|
|
// syntax for these.
|
|
|
|
k = k + tk.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
id := LegacyInstanceObjectID(is.Current)
|
2018-08-01 00:26:51 +02:00
|
|
|
|
|
|
|
taintStr := ""
|
|
|
|
if is.Current != nil && is.Current.Status == ObjectTainted {
|
|
|
|
taintStr = " (tainted)"
|
|
|
|
}
|
|
|
|
|
|
|
|
deposedStr := ""
|
|
|
|
if len(is.Deposed) > 0 {
|
|
|
|
deposedStr = fmt.Sprintf(" (%d deposed)", len(is.Deposed))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s:%s%s\n", k, taintStr, deposedStr))
|
|
|
|
buf.WriteString(fmt.Sprintf(" ID = %s\n", id))
|
|
|
|
buf.WriteString(fmt.Sprintf(" provider = %s\n", rs.ProviderConfig.String()))
|
|
|
|
|
|
|
|
// Attributes were a flatmap before, but are not anymore. To preserve
|
|
|
|
// our old output as closely as possible we need to do a conversion
|
|
|
|
// to flatmap. Normally we'd want to do this with schema for
|
|
|
|
// accuracy, but for our purposes here it only needs to be approximate.
|
|
|
|
// This should produce an identical result for most cases, though
|
|
|
|
// in particular will differ in a few cases:
|
|
|
|
// - The keys used for elements in a set will be different
|
|
|
|
// - Values for attributes of type cty.DynamicPseudoType will be
|
|
|
|
// misinterpreted (but these weren't possible in old world anyway)
|
|
|
|
var attributes map[string]string
|
|
|
|
if obj := is.Current; obj != nil {
|
|
|
|
switch {
|
|
|
|
case obj.AttrsFlat != nil:
|
|
|
|
// Easy (but increasingly unlikely) case: the state hasn't
|
|
|
|
// actually been upgraded to the new form yet.
|
|
|
|
attributes = obj.AttrsFlat
|
|
|
|
case obj.AttrsJSON != nil:
|
|
|
|
ty, err := ctyjson.ImpliedType(obj.AttrsJSON)
|
|
|
|
if err == nil {
|
|
|
|
val, err := ctyjson.Unmarshal(obj.AttrsJSON, ty)
|
|
|
|
if err == nil {
|
|
|
|
attributes = hcl2shim.FlatmapValueFromHCL2(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attrKeys := make([]string, 0, len(attributes))
|
2018-11-19 23:59:38 +01:00
|
|
|
for ak, val := range attributes {
|
2018-08-01 00:26:51 +02:00
|
|
|
if ak == "id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:59:38 +01:00
|
|
|
// don't show empty containers in the output
|
|
|
|
if val == "0" && (strings.HasSuffix(ak, ".#") || strings.HasSuffix(ak, ".%")) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-01 00:26:51 +02:00
|
|
|
attrKeys = append(attrKeys, ak)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(attrKeys)
|
|
|
|
|
|
|
|
for _, ak := range attrKeys {
|
|
|
|
av := attributes[ak]
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CAUTION: Since deposed keys are now random strings instead of
|
|
|
|
// incrementing integers, this result will not be deterministic
|
|
|
|
// if there is more than one deposed object.
|
2018-09-14 18:31:46 +02:00
|
|
|
i := 1
|
|
|
|
for _, t := range is.Deposed {
|
2018-10-17 23:01:15 +02:00
|
|
|
id := LegacyInstanceObjectID(t)
|
2018-08-01 00:26:51 +02:00
|
|
|
taintStr := ""
|
|
|
|
if t.Status == ObjectTainted {
|
|
|
|
taintStr = " (tainted)"
|
|
|
|
}
|
2018-09-14 18:31:46 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(" Deposed ID %d = %s%s\n", i, id, taintStr))
|
|
|
|
i++
|
2018-08-01 00:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if obj := is.Current; obj != nil && len(obj.Dependencies) > 0 {
|
2020-12-01 00:10:41 +01:00
|
|
|
buf.WriteString("\n Dependencies:\n")
|
2018-08-01 00:26:51 +02:00
|
|
|
for _, dep := range obj.Dependencies {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s\n", dep.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:20:50 +01:00
|
|
|
if len(ms.OutputValues) > 0 {
|
2018-08-01 00:26:51 +02:00
|
|
|
buf.WriteString("\nOutputs:\n\n")
|
|
|
|
|
2020-11-30 23:20:50 +01:00
|
|
|
ks := make([]string, 0, len(ms.OutputValues))
|
|
|
|
for k := range ms.OutputValues {
|
2018-08-01 00:26:51 +02:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
for _, k := range ks {
|
2020-11-30 23:20:50 +01:00
|
|
|
v := ms.OutputValues[k]
|
2018-08-01 00:26:51 +02:00
|
|
|
lv := hcl2shim.ConfigValueFromHCL2(v.Value)
|
|
|
|
switch vTyped := lv.(type) {
|
|
|
|
case string:
|
|
|
|
buf.WriteString(fmt.Sprintf("%s = %s\n", k, vTyped))
|
|
|
|
case []interface{}:
|
|
|
|
buf.WriteString(fmt.Sprintf("%s = %s\n", k, vTyped))
|
|
|
|
case map[string]interface{}:
|
|
|
|
var mapKeys []string
|
|
|
|
for key := range vTyped {
|
|
|
|
mapKeys = append(mapKeys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(mapKeys)
|
|
|
|
|
|
|
|
var mapBuf bytes.Buffer
|
|
|
|
mapBuf.WriteString("{")
|
|
|
|
for _, key := range mapKeys {
|
|
|
|
mapBuf.WriteString(fmt.Sprintf("%s:%s ", key, vTyped[key]))
|
|
|
|
}
|
|
|
|
mapBuf.WriteString("}")
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s = %s\n", k, mapBuf.String()))
|
2018-09-14 01:08:00 +02:00
|
|
|
default:
|
|
|
|
buf.WriteString(fmt.Sprintf("%s = %#v\n", k, lv))
|
2018-08-01 00:26:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
// LegacyInstanceObjectID is a helper for extracting an object id value from
|
2018-08-01 00:26:51 +02:00
|
|
|
// an instance object in a way that approximates how we used to do this
|
|
|
|
// for the old state types. ID is no longer first-class, so this is preserved
|
|
|
|
// only for compatibility with old tests that include the id as part of their
|
|
|
|
// expected value.
|
2018-10-17 23:01:15 +02:00
|
|
|
func LegacyInstanceObjectID(obj *ResourceInstanceObjectSrc) string {
|
2018-08-01 00:26:51 +02:00
|
|
|
if obj == nil {
|
|
|
|
return "<not created>"
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj.AttrsJSON != nil {
|
|
|
|
type WithID struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
var withID WithID
|
|
|
|
err := json.Unmarshal(obj.AttrsJSON, &withID)
|
|
|
|
if err == nil {
|
|
|
|
return withID.ID
|
|
|
|
}
|
|
|
|
} else if obj.AttrsFlat != nil {
|
|
|
|
if flatID, exists := obj.AttrsFlat["id"]; exists {
|
|
|
|
return flatID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For resource types created after we removed id as special there may
|
|
|
|
// not actually be one at all. This is okay because older tests won't
|
|
|
|
// encounter this, and new tests shouldn't be using ids.
|
|
|
|
return "<none>"
|
|
|
|
}
|