diff --git a/states/module.go b/states/module.go index a7a09f5a9..fa9e71993 100644 --- a/states/module.go +++ b/states/module.go @@ -102,7 +102,7 @@ func (ms *Module) SetResourceInstanceCurrent(addr addrs.ResourceInstance, obj *R return } // check for an existing resource, now that we've ensured that rs.Instances is more than 0/not nil - is := rs.Instances[addr.Key] + is := rs.Instance(addr.Key) if is == nil { // if there is no instance, but the resource exists and has other instances, // be chill, just return @@ -130,14 +130,12 @@ func (ms *Module) SetResourceInstanceCurrent(addr addrs.ResourceInstance, obj *R rs = ms.Resource(addr.Resource) } // Get our instance from the resource; it could be there or not at this point - is := rs.Instances[addr.Key] + is := rs.Instance(addr.Key) if is == nil { // if we don't have a resource, create one and add to the instances - is = NewResourceInstance() - rs.Instances[addr.Key] = is + is = rs.CreateInstance(addr.Key) // update the resource meta because we have a new instance, so EachMode may have changed ms.SetResourceMeta(addr.Resource, eachModeForInstanceKey(addr.Key), provider) - } is.Current = obj } diff --git a/states/resource.go b/states/resource.go index 7f58543c4..da883ddab 100644 --- a/states/resource.go +++ b/states/resource.go @@ -39,6 +39,13 @@ func (rs *Resource) Instance(key addrs.InstanceKey) *ResourceInstance { return rs.Instances[key] } +// CreateInstance creates an instance and adds it to the resource +func (rs *Resource) CreateInstance(key addrs.InstanceKey) *ResourceInstance { + is := NewResourceInstance() + rs.Instances[key] = is + return is +} + // EnsureInstance returns the state for the instance with the given key, // creating a new empty state for it if one doesn't already exist. //