Merge pull request #24454 from hashicorp/jbardin/expand-resource
Expander.ExpandResource cannot expand all modules
This commit is contained in:
commit
c7e2bace18
|
@ -136,7 +136,7 @@ func (e *Expander) ExpandModuleResource(moduleAddr addrs.Module, resourceAddr ad
|
||||||
// (moduleInstances does plenty of allocations itself, so the benefit of
|
// (moduleInstances does plenty of allocations itself, so the benefit of
|
||||||
// pre-allocating this is marginal but it's not hard to do.)
|
// pre-allocating this is marginal but it's not hard to do.)
|
||||||
moduleInstanceAddr := make(addrs.ModuleInstance, 0, 4)
|
moduleInstanceAddr := make(addrs.ModuleInstance, 0, 4)
|
||||||
ret := e.exps.resourceInstances(moduleAddr, resourceAddr, moduleInstanceAddr)
|
ret := e.exps.moduleResourceInstances(moduleAddr, resourceAddr, moduleInstanceAddr)
|
||||||
sort.SliceStable(ret, func(i, j int) bool {
|
sort.SliceStable(ret, func(i, j int) bool {
|
||||||
return ret[i].Less(ret[j])
|
return ret[i].Less(ret[j])
|
||||||
})
|
})
|
||||||
|
@ -150,16 +150,14 @@ func (e *Expander) ExpandModuleResource(moduleAddr addrs.Module, resourceAddr ad
|
||||||
// itself must already have had their expansion registered using one of the
|
// itself must already have had their expansion registered using one of the
|
||||||
// SetModule*/SetResource* methods before calling, or this method will panic.
|
// SetModule*/SetResource* methods before calling, or this method will panic.
|
||||||
func (e *Expander) ExpandResource(resourceAddr addrs.AbsResource) []addrs.AbsResourceInstance {
|
func (e *Expander) ExpandResource(resourceAddr addrs.AbsResource) []addrs.AbsResourceInstance {
|
||||||
var ret []addrs.AbsResourceInstance
|
e.mu.RLock()
|
||||||
|
defer e.mu.RUnlock()
|
||||||
|
|
||||||
// Take the full set of expanded resource instances and filter for only
|
moduleInstanceAddr := make(addrs.ModuleInstance, 0, 4)
|
||||||
// those within our module instance.
|
ret := e.exps.resourceInstances(resourceAddr.Module, resourceAddr.Resource, moduleInstanceAddr)
|
||||||
for _, r := range e.ExpandModuleResource(resourceAddr.Module.Module(), resourceAddr.Resource) {
|
sort.SliceStable(ret, func(i, j int) bool {
|
||||||
if !r.Module.Equal(resourceAddr.Module) {
|
return ret[i].Less(ret[j])
|
||||||
continue
|
})
|
||||||
}
|
|
||||||
ret = append(ret, r)
|
|
||||||
}
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,10 +295,9 @@ func (m *expanderModule) moduleInstances(addr addrs.Module, parentAddr addrs.Mod
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *expanderModule) resourceInstances(moduleAddr addrs.Module, resourceAddr addrs.Resource, parentAddr addrs.ModuleInstance) []addrs.AbsResourceInstance {
|
func (m *expanderModule) moduleResourceInstances(moduleAddr addrs.Module, resourceAddr addrs.Resource, parentAddr addrs.ModuleInstance) []addrs.AbsResourceInstance {
|
||||||
var ret []addrs.AbsResourceInstance
|
|
||||||
|
|
||||||
if len(moduleAddr) > 0 {
|
if len(moduleAddr) > 0 {
|
||||||
|
var ret []addrs.AbsResourceInstance
|
||||||
// We need to traverse through the module levels first, so we can
|
// We need to traverse through the module levels first, so we can
|
||||||
// then iterate resource expansions in the context of each module
|
// then iterate resource expansions in the context of each module
|
||||||
// path leading to them.
|
// path leading to them.
|
||||||
|
@ -317,11 +314,37 @@ func (m *expanderModule) resourceInstances(moduleAddr addrs.Module, resourceAddr
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
moduleInstAddr := append(parentAddr, step)
|
moduleInstAddr := append(parentAddr, step)
|
||||||
ret = append(ret, inst.resourceInstances(moduleAddr[1:], resourceAddr, moduleInstAddr)...)
|
ret = append(ret, inst.moduleResourceInstances(moduleAddr[1:], resourceAddr, moduleInstAddr)...)
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return m.onlyResourceInstances(resourceAddr, parentAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *expanderModule) resourceInstances(moduleAddr addrs.ModuleInstance, resourceAddr addrs.Resource, parentAddr addrs.ModuleInstance) []addrs.AbsResourceInstance {
|
||||||
|
if len(moduleAddr) > 0 {
|
||||||
|
// We need to traverse through the module levels first, using only the
|
||||||
|
// module instances for our specific resource, as the resource may not
|
||||||
|
// yet be expanded in all module instances.
|
||||||
|
step := moduleAddr[0]
|
||||||
|
callName := step.Name
|
||||||
|
if _, ok := m.moduleCalls[addrs.ModuleCall{Name: callName}]; !ok {
|
||||||
|
// This is a bug in the caller, because it should always register
|
||||||
|
// expansions for an object and all of its ancestors before requesting
|
||||||
|
// expansion of it.
|
||||||
|
panic(fmt.Sprintf("no expansion has been registered for %s", parentAddr.Child(callName, addrs.NoKey)))
|
||||||
|
}
|
||||||
|
|
||||||
|
inst := m.childInstances[step]
|
||||||
|
moduleInstAddr := append(parentAddr, step)
|
||||||
|
return inst.resourceInstances(moduleAddr[1:], resourceAddr, moduleInstAddr)
|
||||||
|
}
|
||||||
|
return m.onlyResourceInstances(resourceAddr, parentAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *expanderModule) onlyResourceInstances(resourceAddr addrs.Resource, parentAddr addrs.ModuleInstance) []addrs.AbsResourceInstance {
|
||||||
|
var ret []addrs.AbsResourceInstance
|
||||||
exp, ok := m.resources[resourceAddr]
|
exp, ok := m.resources[resourceAddr]
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Sprintf("no expansion has been registered for %s", resourceAddr.Absolute(parentAddr)))
|
panic(fmt.Sprintf("no expansion has been registered for %s", resourceAddr.Absolute(parentAddr)))
|
||||||
|
|
Loading…
Reference in New Issue