states.Resource needs to record its module too

The ModuleInstance is known while building the state resource, but it's
not recorded. Since a resource may be retrieved via a ConfigResource
address, we need to know from which module instance it was loaded.
This commit is contained in:
James Bardin 2020-03-13 13:51:55 -04:00
parent 3729e6a705
commit ea51b790bc
6 changed files with 9 additions and 8 deletions

View File

@ -58,7 +58,7 @@ func (ms *Module) SetResourceMeta(addr addrs.Resource, eachMode EachMode, provid
rs := ms.Resource(addr) rs := ms.Resource(addr)
if rs == nil { if rs == nil {
rs = &Resource{ rs = &Resource{
Addr: addr, Addr: addr.Absolute(ms.Addr),
Instances: map[addrs.InstanceKey]*ResourceInstance{}, Instances: map[addrs.InstanceKey]*ResourceInstance{},
} }
ms.Resources[addr.String()] = rs ms.Resources[addr.String()] = rs
@ -295,7 +295,7 @@ func (ms *Module) RemoveLocalValue(name string) {
func (ms *Module) PruneResourceHusks() { func (ms *Module) PruneResourceHusks() {
for _, rs := range ms.Resources { for _, rs := range ms.Resources {
if len(rs.Instances) == 0 { if len(rs.Instances) == 0 {
ms.RemoveResource(rs.Addr) ms.RemoveResource(rs.Addr.Resource)
} }
} }
} }

View File

@ -10,9 +10,9 @@ import (
// Resource represents the state of a resource. // Resource represents the state of a resource.
type Resource struct { type Resource struct {
// Addr is the module-relative address for the resource this state object // Addr is the absolute address for the resource this state object
// belongs to. // belongs to.
Addr addrs.Resource Addr addrs.AbsResource
// EachMode is the multi-instance mode currently in use for this resource, // EachMode is the multi-instance mode currently in use for this resource,
// or NoEach if this is a single-instance resource. This dictates what // or NoEach if this is a single-instance resource. This dictates what

View File

@ -91,7 +91,7 @@ func (m *Module) testString() string {
addrsOrder := make([]addrs.AbsResourceInstance, 0, len(m.Resources)) addrsOrder := make([]addrs.AbsResourceInstance, 0, len(m.Resources))
for _, rs := range m.Resources { for _, rs := range m.Resources {
for ik := range rs.Instances { for ik := range rs.Instances {
addrsOrder = append(addrsOrder, rs.Addr.Instance(ik).Absolute(addrs.RootModuleInstance)) addrsOrder = append(addrsOrder, rs.Addr.Instance(ik))
} }
} }

View File

@ -67,7 +67,8 @@ func TestState(t *testing.T) {
Mode: addrs.ManagedResourceMode, Mode: addrs.ManagedResourceMode,
Type: "test_thing", Type: "test_thing",
Name: "baz", Name: "baz",
}, }.Absolute(addrs.RootModuleInstance),
EachMode: EachList, EachMode: EachList,
Instances: map[addrs.InstanceKey]*ResourceInstance{ Instances: map[addrs.InstanceKey]*ResourceInstance{
addrs.IntKey(0): { addrs.IntKey(0): {

View File

@ -371,7 +371,7 @@ func writeStateV4(file *File, w io.Writer) tfdiags.Diagnostics {
for _, ms := range file.State.Modules { for _, ms := range file.State.Modules {
moduleAddr := ms.Addr moduleAddr := ms.Addr
for _, rs := range ms.Resources { for _, rs := range ms.Resources {
resourceAddr := rs.Addr resourceAddr := rs.Addr.Resource
var mode string var mode string
switch resourceAddr.Mode { switch resourceAddr.Mode {

View File

@ -472,7 +472,7 @@ func (s *SyncState) RemovePlannedResourceInstanceObjects() {
moduleAddr := ms.Addr moduleAddr := ms.Addr
for _, rs := range ms.Resources { for _, rs := range ms.Resources {
resAddr := rs.Addr resAddr := rs.Addr.Resource
for ik, is := range rs.Instances { for ik, is := range rs.Instances {
instAddr := resAddr.Instance(ik) instAddr := resAddr.Instance(ik)