Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: Fix terraform show not outputting child_modules properly in certain circumstances #27352

Merged
merged 3 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions command/jsonstate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand Down
53 changes: 53 additions & 0 deletions command/jsonstate/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down