command: Fix terraform show not outputting child_modules properly in certain circumstances (#27352)
* Add test for module nesting without resources. * Add test * Fix showing resources when a module has no resources, only submodules.
This commit is contained in:
parent
aab0dd102d
commit
b49655724d
|
@ -198,14 +198,30 @@ func marshalRootModule(s *states.State, schemas *terraform.Schemas) (module, err
|
|||
return ret, err
|
||||
}
|
||||
|
||||
// build a map of module -> [child module addresses]
|
||||
moduleMap := make(map[string][]addrs.ModuleInstance)
|
||||
// build a map of module -> set[child module addresses]
|
||||
moduleChildSet := make(map[string]map[string]struct{})
|
||||
for _, mod := range s.Modules {
|
||||
if mod.Addr.IsRoot() {
|
||||
continue
|
||||
} else {
|
||||
parent := mod.Addr.Parent().String()
|
||||
moduleMap[parent] = append(moduleMap[parent], mod.Addr)
|
||||
for childAddr := mod.Addr; !childAddr.IsRoot(); childAddr = childAddr.Parent() {
|
||||
if _, ok := moduleChildSet[childAddr.Parent().String()]; !ok {
|
||||
moduleChildSet[childAddr.Parent().String()] = map[string]struct{}{}
|
||||
}
|
||||
moduleChildSet[childAddr.Parent().String()][childAddr.String()] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// transform the previous map into map of module -> [child module addresses]
|
||||
moduleMap := make(map[string][]addrs.ModuleInstance)
|
||||
for parent, children := range moduleChildSet {
|
||||
for child := range children {
|
||||
childModuleInstance, diags := addrs.ParseModuleInstanceStr(child)
|
||||
if diags.HasErrors() {
|
||||
return ret, diags.Err()
|
||||
}
|
||||
moduleMap[parent] = append(moduleMap[parent], childModuleInstance)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,14 +240,19 @@ func marshalModules(
|
|||
) ([]module, error) {
|
||||
var ret []module
|
||||
for _, child := range modules {
|
||||
stateMod := s.Module(child)
|
||||
// cm for child module, naming things is hard.
|
||||
cm := module{Address: stateMod.Addr.String()}
|
||||
rs, err := marshalResources(stateMod.Resources, stateMod.Addr, schemas)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
cm := module{Address: child.String()}
|
||||
|
||||
// the module may be resourceless and contain only submodules, it will then be nil here
|
||||
stateMod := s.Module(child)
|
||||
if stateMod != nil {
|
||||
rs, err := marshalResources(stateMod.Resources, stateMod.Addr, schemas)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cm.Resources = rs
|
||||
}
|
||||
cm.Resources = rs
|
||||
|
||||
if moduleMap[child.String()] != nil {
|
||||
moreChildModules, err := marshalModules(s, schemas, moduleMap[child.String()], moduleMap)
|
||||
if err != nil {
|
||||
|
|
|
@ -554,6 +554,59 @@ func TestMarshalModules_nested(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMarshalModules_parent_no_resources(t *testing.T) {
|
||||
subModule, _ := addrs.ParseModuleInstanceStr("module.child.module.submodule")
|
||||
testState := states.BuildState(func(s *states.SyncState) {
|
||||
s.SetResourceInstanceCurrent(
|
||||
addrs.Resource{
|
||||
Mode: addrs.ManagedResourceMode,
|
||||
Type: "test_instance",
|
||||
Name: "foo",
|
||||
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
||||
&states.ResourceInstanceObjectSrc{
|
||||
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
||||
Status: states.ObjectReady,
|
||||
},
|
||||
addrs.AbsProviderConfig{
|
||||
Provider: addrs.NewDefaultProvider("test"),
|
||||
Module: addrs.RootModule,
|
||||
},
|
||||
)
|
||||
s.SetResourceInstanceCurrent(
|
||||
addrs.Resource{
|
||||
Mode: addrs.ManagedResourceMode,
|
||||
Type: "test_instance",
|
||||
Name: "foo",
|
||||
}.Instance(addrs.NoKey).Absolute(subModule),
|
||||
&states.ResourceInstanceObjectSrc{
|
||||
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
||||
Status: states.ObjectReady,
|
||||
},
|
||||
addrs.AbsProviderConfig{
|
||||
Provider: addrs.NewDefaultProvider("test"),
|
||||
Module: subModule.Module(),
|
||||
},
|
||||
)
|
||||
})
|
||||
got, err := marshalRootModule(testState, testSchemas())
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err.Error())
|
||||
}
|
||||
|
||||
if len(got.ChildModules) != 1 {
|
||||
t.Fatalf("wrong result! got %d modules, expected 1", len(got.ChildModules))
|
||||
}
|
||||
|
||||
if got.ChildModules[0].Address != "module.child" {
|
||||
t.Fatalf("wrong result! got %#v\n", got)
|
||||
}
|
||||
|
||||
if got.ChildModules[0].ChildModules[0].Address != "module.child.module.submodule" {
|
||||
t.Fatalf("wrong result! got %#v\n", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testSchemas() *terraform.Schemas {
|
||||
return &terraform.Schemas{
|
||||
Providers: map[addrs.Provider]*terraform.ProviderSchema{
|
||||
|
|
Loading…
Reference in New Issue