9 "github.com/zclconf/go-cty/cty"
11 "github.com/hashicorp/terraform/addrs"
12 "github.com/hashicorp/terraform/configs/configschema"
13 "github.com/hashicorp/terraform/plans"
14 "github.com/hashicorp/terraform/states"
15 "github.com/hashicorp/terraform/terraform"
16 "github.com/mitchellh/colorstring"
19 // StateOpts are the options for formatting a state.
20 type StateOpts struct {
21 // State is the state to format. This is required.
24 // Schemas are used to decode attributes. This is required.
25 Schemas *terraform.Schemas
27 // Color is the colorizer. This is optional.
28 Color *colorstring.Colorize
31 // State takes a state and returns a string
32 func State(opts *StateOpts) string {
33 if opts.Color == nil {
34 panic("colorize not given")
37 if opts.Schemas == nil {
38 panic("schemas not given")
42 if len(s.Modules) == 0 {
43 return "The state file is empty. No resources are represented."
46 buf := bytes.NewBufferString("[reset]")
47 p := blockBodyDiffPrinter{
53 // Format all the modules
54 for _, m := range s.Modules {
55 formatStateModule(p, m, opts.Schemas)
58 // Write the outputs for the root module
61 if m.OutputValues != nil {
62 if len(m.OutputValues) > 0 {
63 p.buf.WriteString("Outputs:\n\n")
67 ks := make([]string, 0, len(m.OutputValues))
68 for k := range m.OutputValues {
73 // Output each output k/v pair
74 for _, k := range ks {
75 v := m.OutputValues[k]
76 p.buf.WriteString(fmt.Sprintf("%s = ", k))
77 p.writeValue(v.Value, plans.NoOp, 0)
78 p.buf.WriteString("\n\n")
82 return opts.Color.Color(strings.TrimSpace(p.buf.String()))
86 func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraform.Schemas) {
87 // First get the names of all the resources so we can show them
88 // in alphabetical order.
89 names := make([]string, 0, len(m.Resources))
90 for name := range m.Resources {
91 names = append(names, name)
95 // Go through each resource and begin building up the output.
96 for _, key := range names {
97 for k, v := range m.Resources[key].Instances {
98 addr := m.Resources[key].Addr
101 if v.Current.Status == 'T' {
102 taintStr = "(tainted)"
104 p.buf.WriteString(fmt.Sprintf("# %s: %s\n", addr.Absolute(m.Addr).Instance(k), taintStr))
106 var schema *configschema.Block
107 provider := m.Resources[key].ProviderConfig.ProviderConfig.StringCompact()
108 if _, exists := schemas.Providers[provider]; !exists {
109 // This should never happen in normal use because we should've
110 // loaded all of the schemas and checked things prior to this
111 // point. We can't return errors here, but since this is UI code
112 // we will try to do _something_ reasonable.
113 p.buf.WriteString(fmt.Sprintf("# missing schema for provider %q\n\n", provider))
118 case addrs.ManagedResourceMode:
119 schema, _ = schemas.ResourceTypeConfig(
125 p.buf.WriteString(fmt.Sprintf(
126 "# missing schema for provider %q resource type %s\n\n", provider, addr.Type))
130 p.buf.WriteString(fmt.Sprintf(
135 case addrs.DataResourceMode:
136 schema, _ = schemas.ResourceTypeConfig(
142 p.buf.WriteString(fmt.Sprintf(
143 "# missing schema for provider %q data source %s\n\n", provider, addr.Type))
147 p.buf.WriteString(fmt.Sprintf(
153 // should never happen, since the above is exhaustive
154 p.buf.WriteString(addr.String())
157 val, err := v.Current.Decode(schema.ImpliedType())
159 fmt.Println(err.Error())
163 path := make(cty.Path, 0, 3)
164 bodyWritten := p.writeBlockBodyDiff(schema, val.Value, val.Value, 2, path)
166 p.buf.WriteString("\n")
169 p.buf.WriteString("}\n\n")
172 p.buf.WriteString("[reset]\n")
175 func formatNestedList(indent string, outputList []interface{}) string {
176 outputBuf := new(bytes.Buffer)
177 outputBuf.WriteString(fmt.Sprintf("%s[", indent))
179 lastIdx := len(outputList) - 1
181 for i, value := range outputList {
182 outputBuf.WriteString(fmt.Sprintf("\n%s%s%s", indent, " ", value))
184 outputBuf.WriteString(",")
188 outputBuf.WriteString(fmt.Sprintf("\n%s]", indent))
189 return strings.TrimPrefix(outputBuf.String(), "\n")
192 func formatListOutput(indent, outputName string, outputList []interface{}) string {
195 outputBuf := new(bytes.Buffer)
197 if outputName != "" {
198 outputBuf.WriteString(fmt.Sprintf("%s%s = [", indent, outputName))
202 lastIdx := len(outputList) - 1
204 for i, value := range outputList {
205 switch typedValue := value.(type) {
207 outputBuf.WriteString(fmt.Sprintf("\n%s%s%s", indent, keyIndent, value))
209 outputBuf.WriteString(fmt.Sprintf("\n%s%s", indent,
210 formatNestedList(indent+keyIndent, typedValue)))
211 case map[string]interface{}:
212 outputBuf.WriteString(fmt.Sprintf("\n%s%s", indent,
213 formatNestedMap(indent+keyIndent, typedValue)))
217 outputBuf.WriteString(",")
221 if outputName != "" {
222 if len(outputList) > 0 {
223 outputBuf.WriteString(fmt.Sprintf("\n%s]", indent))
225 outputBuf.WriteString("]")
229 return strings.TrimPrefix(outputBuf.String(), "\n")
232 func formatNestedMap(indent string, outputMap map[string]interface{}) string {
233 ks := make([]string, 0, len(outputMap))
234 for k, _ := range outputMap {
239 outputBuf := new(bytes.Buffer)
240 outputBuf.WriteString(fmt.Sprintf("%s{", indent))
242 lastIdx := len(outputMap) - 1
243 for i, k := range ks {
245 outputBuf.WriteString(fmt.Sprintf("\n%s%s = %v", indent+" ", k, v))
248 outputBuf.WriteString(",")
252 outputBuf.WriteString(fmt.Sprintf("\n%s}", indent))
254 return strings.TrimPrefix(outputBuf.String(), "\n")
257 func formatMapOutput(indent, outputName string, outputMap map[string]interface{}) string {
258 ks := make([]string, 0, len(outputMap))
259 for k, _ := range outputMap {
266 outputBuf := new(bytes.Buffer)
267 if outputName != "" {
268 outputBuf.WriteString(fmt.Sprintf("%s%s = {", indent, outputName))
272 for _, k := range ks {
274 outputBuf.WriteString(fmt.Sprintf("\n%s%s%s = %v", indent, keyIndent, k, v))
277 if outputName != "" {
278 if len(outputMap) > 0 {
279 outputBuf.WriteString(fmt.Sprintf("\n%s}", indent))
281 outputBuf.WriteString("}")
285 return strings.TrimPrefix(outputBuf.String(), "\n")