update MaybeFixUpResourceInstanceAddressForCount

We need to fixup count by config path, not by absolute path, so update
MaybeFixUpResourceInstanceAddressForCount to work with
addrs.ConfigResource.
This commit is contained in:
James Bardin 2020-03-13 13:05:37 -04:00
parent f0e175a835
commit 3729e6a705
2 changed files with 57 additions and 26 deletions

View File

@ -70,6 +70,17 @@ func (s *State) Module(addr addrs.ModuleInstance) *Module {
return s.Modules[addr.String()] return s.Modules[addr.String()]
} }
// ModuleInstances returns the set of Module states that matches the given path.
func (s *State) ModuleInstances(addr addrs.Module) []*Module {
var ms []*Module
for _, m := range s.Modules {
if m.Addr.Module().Equal(addr) {
ms = append(ms, m)
}
}
return ms
}
// RemoveModule removes the module with the given address from the state, // RemoveModule removes the module with the given address from the state,
// unless it is the root module. The root module cannot be deleted, and so // unless it is the root module. The root module cannot be deleted, and so
// this method will panic if that is attempted. // this method will panic if that is attempted.
@ -133,6 +144,18 @@ func (s *State) Resource(addr addrs.AbsResource) *Resource {
return ms.Resource(addr.Resource) return ms.Resource(addr.Resource)
} }
// Resources returns the set of resources that match the given configuration path.
func (s *State) Resources(addr addrs.ConfigResource) []*Resource {
var ret []*Resource
for _, m := range s.ModuleInstances(addr.Module) {
r := m.Resource(addr.Resource)
if r != nil {
ret = append(ret, r)
}
}
return ret
}
// ResourceInstance returns the state for the resource instance with the given // ResourceInstance returns the state for the resource instance with the given
// address, or nil if no such resource is tracked in the state. // address, or nil if no such resource is tracked in the state.
func (s *State) ResourceInstance(addr addrs.AbsResourceInstance) *ResourceInstance { func (s *State) ResourceInstance(addr addrs.AbsResourceInstance) *ResourceInstance {

View File

@ -246,20 +246,25 @@ func (s *SyncState) RemoveResourceIfEmpty(addr addrs.AbsResource) bool {
// The state is modified in-place if necessary, moving a resource instance // The state is modified in-place if necessary, moving a resource instance
// between the two addresses. The return value is true if a change was made, // between the two addresses. The return value is true if a change was made,
// and false otherwise. // and false otherwise.
func (s *SyncState) MaybeFixUpResourceInstanceAddressForCount(addr addrs.AbsResource, countEnabled bool) bool { func (s *SyncState) MaybeFixUpResourceInstanceAddressForCount(addr addrs.ConfigResource, countEnabled bool) bool {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
ms := s.state.Module(addr.Module) // get all modules instances that may match this state
if ms == nil { modules := s.state.ModuleInstances(addr.Module)
if len(modules) == 0 {
return false return false
} }
changed := false
for _, ms := range modules {
relAddr := addr.Resource relAddr := addr.Resource
rs := ms.Resource(relAddr) rs := ms.Resource(relAddr)
if rs == nil { if rs == nil {
return false continue
} }
huntKey := addrs.NoKey huntKey := addrs.NoKey
replaceKey := addrs.InstanceKey(addrs.IntKey(0)) replaceKey := addrs.InstanceKey(addrs.IntKey(0))
if !countEnabled { if !countEnabled {
@ -268,18 +273,21 @@ func (s *SyncState) MaybeFixUpResourceInstanceAddressForCount(addr addrs.AbsReso
is, exists := rs.Instances[huntKey] is, exists := rs.Instances[huntKey]
if !exists { if !exists {
return false continue
} }
if _, exists := rs.Instances[replaceKey]; exists { if _, exists := rs.Instances[replaceKey]; exists {
// If the replacement key also exists then we'll do nothing and keep both. // If the replacement key also exists then we'll do nothing and keep both.
return false continue
} }
// If we get here then we need to "rename" from hunt to replace // If we get here then we need to "rename" from hunt to replace
rs.Instances[replaceKey] = is rs.Instances[replaceKey] = is
delete(rs.Instances, huntKey) delete(rs.Instances, huntKey)
return true changed = true
}
return changed
} }
// SetResourceInstanceCurrent saves the given instance object as the current // SetResourceInstanceCurrent saves the given instance object as the current