command/format: include nested blocks in `terraform show` output (#20149)
* command/format: include nested blocks in terraform show output * command/format: fix tests
This commit is contained in:
parent
477da57a92
commit
653bb74403
|
@ -6,6 +6,8 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/addrs"
|
"github.com/hashicorp/terraform/addrs"
|
||||||
"github.com/hashicorp/terraform/configs/configschema"
|
"github.com/hashicorp/terraform/configs/configschema"
|
||||||
"github.com/hashicorp/terraform/plans"
|
"github.com/hashicorp/terraform/plans"
|
||||||
|
@ -121,7 +123,7 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
|
||||||
}
|
}
|
||||||
|
|
||||||
p.buf.WriteString(fmt.Sprintf(
|
p.buf.WriteString(fmt.Sprintf(
|
||||||
"resource %q %q {\n",
|
"resource %q %q {",
|
||||||
addr.Type,
|
addr.Type,
|
||||||
addr.Name,
|
addr.Name,
|
||||||
))
|
))
|
||||||
|
@ -134,7 +136,7 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
|
||||||
}
|
}
|
||||||
|
|
||||||
p.buf.WriteString(fmt.Sprintf(
|
p.buf.WriteString(fmt.Sprintf(
|
||||||
"data %q %q {\n",
|
"data %q %q {",
|
||||||
addr.Type,
|
addr.Type,
|
||||||
addr.Name,
|
addr.Name,
|
||||||
))
|
))
|
||||||
|
@ -150,23 +152,12 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// First get the names of all the attributes so we can show them
|
path := make(cty.Path, 0, 3)
|
||||||
// in alphabetical order.
|
bodyWritten := p.writeBlockBodyDiff(schema, val.Value, val.Value, 2, path)
|
||||||
names := make([]string, 0, len(schema.Attributes))
|
if bodyWritten {
|
||||||
for name := range schema.Attributes {
|
p.buf.WriteString("\n")
|
||||||
names = append(names, name)
|
|
||||||
}
|
}
|
||||||
sort.Strings(names)
|
|
||||||
|
|
||||||
for _, name := range names {
|
|
||||||
attr := ctyGetAttrMaybeNull(val.Value, name)
|
|
||||||
if !attr.IsNull() {
|
|
||||||
p.buf.WriteString(fmt.Sprintf(" %s = ", name))
|
|
||||||
attr := ctyGetAttrMaybeNull(val.Value, name)
|
|
||||||
p.writeValue(attr, plans.NoOp, 4)
|
|
||||||
p.buf.WriteString("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.buf.WriteString("}\n\n")
|
p.buf.WriteString("}\n\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,6 +63,14 @@ func TestState(t *testing.T) {
|
||||||
},
|
},
|
||||||
TestOutput,
|
TestOutput,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
&StateOpts{
|
||||||
|
State: nestedState(t),
|
||||||
|
Color: disabledColorize,
|
||||||
|
Schemas: testSchemas(),
|
||||||
|
},
|
||||||
|
nestedTestOutput,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -101,6 +109,17 @@ func testProviderSchema() *terraform.ProviderSchema {
|
||||||
"foo": {Type: cty.String, Optional: true},
|
"foo": {Type: cty.String, Optional: true},
|
||||||
"woozles": {Type: cty.String, Optional: true},
|
"woozles": {Type: cty.String, Optional: true},
|
||||||
},
|
},
|
||||||
|
BlockTypes: map[string]*configschema.NestedBlock{
|
||||||
|
"nested": {
|
||||||
|
Nesting: configschema.NestingList,
|
||||||
|
Block: configschema.Block{
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"compute": {Type: cty.String, Optional: true},
|
||||||
|
"value": {Type: cty.String, Optional: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
DataSources: map[string]*configschema.Block{
|
DataSources: map[string]*configschema.Block{
|
||||||
|
@ -132,3 +151,40 @@ resource "test_resource" "baz" {
|
||||||
Outputs:
|
Outputs:
|
||||||
|
|
||||||
bar = "bar value"`
|
bar = "bar value"`
|
||||||
|
|
||||||
|
const nestedTestOutput = `# test_resource.baz[0]:
|
||||||
|
resource "test_resource" "baz" {
|
||||||
|
woozles = "confuzles"
|
||||||
|
|
||||||
|
nested {
|
||||||
|
value = "42"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
func nestedState(t *testing.T) *states.State {
|
||||||
|
state := states.NewState()
|
||||||
|
|
||||||
|
rootModule := state.RootModule()
|
||||||
|
if rootModule == nil {
|
||||||
|
t.Errorf("root module is nil; want valid object")
|
||||||
|
}
|
||||||
|
|
||||||
|
rootModule.SetResourceInstanceCurrent(
|
||||||
|
addrs.Resource{
|
||||||
|
Mode: addrs.ManagedResourceMode,
|
||||||
|
Type: "test_resource",
|
||||||
|
Name: "baz",
|
||||||
|
}.Instance(addrs.IntKey(0)),
|
||||||
|
&states.ResourceInstanceObjectSrc{
|
||||||
|
Status: states.ObjectReady,
|
||||||
|
SchemaVersion: 1,
|
||||||
|
AttrsJSON: []byte(`{"woozles":"confuzles","nested": [{"value": "42"}]}`),
|
||||||
|
},
|
||||||
|
addrs.ProviderConfig{
|
||||||
|
Type: "test",
|
||||||
|
}.Absolute(addrs.RootModuleInstance),
|
||||||
|
)
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
|
|
@ -187,6 +187,6 @@ const testStateShowOutput = `
|
||||||
resource "test_instance" "foo" {
|
resource "test_instance" "foo" {
|
||||||
bar = "value"
|
bar = "value"
|
||||||
foo = "value"
|
foo = "value"
|
||||||
id = "bar"
|
id = "bar"
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
Loading…
Reference in New Issue