fixup states.Resource change throughout packages

This commit is contained in:
James Bardin 2020-03-16 16:50:48 -04:00
parent d905b990a5
commit a8b9547e0d
6 changed files with 61 additions and 48 deletions

View File

@ -107,6 +107,7 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
instances := []obj{} instances := []obj{}
addr := m.Resources[key].Addr addr := m.Resources[key].Addr
resAddr := addr.Resource
taintStr := "" taintStr := ""
if v.Current != nil && v.Current.Status == 'T' { if v.Current != nil && v.Current.Status == 'T' {
@ -114,11 +115,11 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
} }
instances = append(instances, instances = append(instances,
obj{fmt.Sprintf("# %s:%s\n", addr.Absolute(m.Addr).Instance(k), taintStr), v.Current}) obj{fmt.Sprintf("# %s:%s\n", addr.Instance(k), taintStr), v.Current})
for dk, v := range v.Deposed { for dk, v := range v.Deposed {
instances = append(instances, instances = append(instances,
obj{fmt.Sprintf("# %s: (deposed object %s)\n", addr.Absolute(m.Addr).Instance(k), dk), v}) obj{fmt.Sprintf("# %s: (deposed object %s)\n", addr.Instance(k), dk), v})
} }
// Sort the instances for consistent output. // Sort the instances for consistent output.
@ -150,44 +151,44 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
continue continue
} }
switch addr.Mode { switch resAddr.Mode {
case addrs.ManagedResourceMode: case addrs.ManagedResourceMode:
schema, _ = schemas.ResourceTypeConfig( schema, _ = schemas.ResourceTypeConfig(
provider, provider,
addr.Mode, resAddr.Mode,
addr.Type, resAddr.Type,
) )
if schema == nil { if schema == nil {
p.buf.WriteString(fmt.Sprintf( p.buf.WriteString(fmt.Sprintf(
"# missing schema for provider %q resource type %s\n\n", provider, addr.Type)) "# missing schema for provider %q resource type %s\n\n", provider, resAddr.Type))
continue continue
} }
p.buf.WriteString(fmt.Sprintf( p.buf.WriteString(fmt.Sprintf(
"resource %q %q {", "resource %q %q {",
addr.Type, resAddr.Type,
addr.Name, resAddr.Name,
)) ))
case addrs.DataResourceMode: case addrs.DataResourceMode:
schema, _ = schemas.ResourceTypeConfig( schema, _ = schemas.ResourceTypeConfig(
provider, provider,
addr.Mode, resAddr.Mode,
addr.Type, resAddr.Type,
) )
if schema == nil { if schema == nil {
p.buf.WriteString(fmt.Sprintf( p.buf.WriteString(fmt.Sprintf(
"# missing schema for provider %q data source %s\n\n", provider, addr.Type)) "# missing schema for provider %q data source %s\n\n", provider, resAddr.Type))
continue continue
} }
p.buf.WriteString(fmt.Sprintf( p.buf.WriteString(fmt.Sprintf(
"data %q %q {", "data %q %q {",
addr.Type, resAddr.Type,
addr.Name, resAddr.Name,
)) ))
default: default:
// should never happen, since the above is exhaustive // should never happen, since the above is exhaustive
p.buf.WriteString(addr.String()) p.buf.WriteString(resAddr.String())
} }
val, err := instance.Decode(schema.ImpliedType()) val, err := instance.Decode(schema.ImpliedType())

View File

@ -255,22 +255,24 @@ func marshalResources(resources map[string]*states.Resource, module addrs.Module
for _, r := range resources { for _, r := range resources {
for k, ri := range r.Instances { for k, ri := range r.Instances {
resAddr := r.Addr.Resource
current := resource{ current := resource{
Address: r.Addr.Absolute(module).Instance(k).String(), Address: r.Addr.Instance(k).String(),
Type: r.Addr.Type, Type: resAddr.Type,
Name: r.Addr.Name, Name: resAddr.Name,
ProviderName: r.ProviderConfig.Provider.LegacyString(), ProviderName: r.ProviderConfig.Provider.LegacyString(),
} }
switch r.Addr.Mode { switch resAddr.Mode {
case addrs.ManagedResourceMode: case addrs.ManagedResourceMode:
current.Mode = "managed" current.Mode = "managed"
case addrs.DataResourceMode: case addrs.DataResourceMode:
current.Mode = "data" current.Mode = "data"
default: default:
return ret, fmt.Errorf("resource %s has an unsupported mode %s", return ret, fmt.Errorf("resource %s has an unsupported mode %s",
r.Addr.String(), resAddr.String(),
r.Addr.Mode.String(), resAddr.Mode.String(),
) )
} }
@ -280,8 +282,8 @@ func marshalResources(resources map[string]*states.Resource, module addrs.Module
schema, _ := schemas.ResourceTypeConfig( schema, _ := schemas.ResourceTypeConfig(
r.ProviderConfig.Provider, r.ProviderConfig.Provider,
r.Addr.Mode, resAddr.Mode,
r.Addr.Type, resAddr.Type,
) )
// It is possible that the only instance is deposed // It is possible that the only instance is deposed
@ -289,7 +291,7 @@ func marshalResources(resources map[string]*states.Resource, module addrs.Module
current.SchemaVersion = ri.Current.SchemaVersion current.SchemaVersion = ri.Current.SchemaVersion
if schema == nil { if schema == nil {
return nil, fmt.Errorf("no schema found for %s", r.Addr.String()) return nil, fmt.Errorf("no schema found for %s", resAddr.String())
} }
riObj, err := ri.Current.Decode(schema.ImpliedType()) riObj, err := ri.Current.Decode(schema.ImpliedType())
if err != nil { if err != nil {

View File

@ -186,10 +186,12 @@ func TestMarshalResources(t *testing.T) {
"single resource": { "single resource": {
map[string]*states.Resource{ map[string]*states.Resource{
"test_thing.baz": { "test_thing.baz": {
Addr: addrs.Resource{ Addr: addrs.AbsResource{
Mode: addrs.ManagedResourceMode, Resource: addrs.Resource{
Type: "test_thing", Mode: addrs.ManagedResourceMode,
Name: "bar", Type: "test_thing",
Name: "bar",
},
}, },
EachMode: states.NoEach, EachMode: states.NoEach,
Instances: map[addrs.InstanceKey]*states.ResourceInstance{ Instances: map[addrs.InstanceKey]*states.ResourceInstance{
@ -228,10 +230,12 @@ func TestMarshalResources(t *testing.T) {
"resource with count": { "resource with count": {
map[string]*states.Resource{ map[string]*states.Resource{
"test_thing.bar": { "test_thing.bar": {
Addr: addrs.Resource{ Addr: addrs.AbsResource{
Mode: addrs.ManagedResourceMode, Resource: addrs.Resource{
Type: "test_thing", Mode: addrs.ManagedResourceMode,
Name: "bar", Type: "test_thing",
Name: "bar",
},
}, },
EachMode: states.EachList, EachMode: states.EachList,
Instances: map[addrs.InstanceKey]*states.ResourceInstance{ Instances: map[addrs.InstanceKey]*states.ResourceInstance{
@ -270,10 +274,12 @@ func TestMarshalResources(t *testing.T) {
"resource with for_each": { "resource with for_each": {
map[string]*states.Resource{ map[string]*states.Resource{
"test_thing.bar": { "test_thing.bar": {
Addr: addrs.Resource{ Addr: addrs.AbsResource{
Mode: addrs.ManagedResourceMode, Resource: addrs.Resource{
Type: "test_thing", Mode: addrs.ManagedResourceMode,
Name: "bar", Type: "test_thing",
Name: "bar",
},
}, },
EachMode: states.EachMap, EachMode: states.EachMap,
Instances: map[addrs.InstanceKey]*states.ResourceInstance{ Instances: map[addrs.InstanceKey]*states.ResourceInstance{
@ -312,10 +318,12 @@ func TestMarshalResources(t *testing.T) {
"deposed resource": { "deposed resource": {
map[string]*states.Resource{ map[string]*states.Resource{
"test_thing.baz": { "test_thing.baz": {
Addr: addrs.Resource{ Addr: addrs.AbsResource{
Mode: addrs.ManagedResourceMode, Resource: addrs.Resource{
Type: "test_thing", Mode: addrs.ManagedResourceMode,
Name: "bar", Type: "test_thing",
Name: "bar",
},
}, },
EachMode: states.NoEach, EachMode: states.NoEach,
Instances: map[addrs.InstanceKey]*states.ResourceInstance{ Instances: map[addrs.InstanceKey]*states.ResourceInstance{
@ -356,10 +364,12 @@ func TestMarshalResources(t *testing.T) {
"deposed and current resource": { "deposed and current resource": {
map[string]*states.Resource{ map[string]*states.Resource{
"test_thing.baz": { "test_thing.baz": {
Addr: addrs.Resource{ Addr: addrs.AbsResource{
Mode: addrs.ManagedResourceMode, Resource: addrs.Resource{
Type: "test_thing", Mode: addrs.ManagedResourceMode,
Name: "bar", Type: "test_thing",
Name: "bar",
},
}, },
EachMode: states.NoEach, EachMode: states.NoEach,
Instances: map[addrs.InstanceKey]*states.ResourceInstance{ Instances: map[addrs.InstanceKey]*states.ResourceInstance{

View File

@ -192,7 +192,7 @@ func (c *StateMeta) collectModuleResourceInstances(ms *states.Module) []addrs.Ab
func (c *StateMeta) collectResourceInstances(moduleAddr addrs.ModuleInstance, rs *states.Resource) []addrs.AbsResourceInstance { func (c *StateMeta) collectResourceInstances(moduleAddr addrs.ModuleInstance, rs *states.Resource) []addrs.AbsResourceInstance {
var ret []addrs.AbsResourceInstance var ret []addrs.AbsResourceInstance
for key := range rs.Instances { for key := range rs.Instances {
ret = append(ret, rs.Addr.Instance(key).Absolute(moduleAddr)) ret = append(ret, rs.Addr.Instance(key))
} }
return ret return ret
} }

View File

@ -226,7 +226,7 @@ func (c *StateMvCommand) Run(args []string) int {
ssFrom.RemoveResource(addrFrom) ssFrom.RemoveResource(addrFrom)
// Update the address before adding it to the state. // Update the address before adding it to the state.
rs.Addr = addrTo.Resource rs.Addr = addrTo
stateTo.Module(addrTo.Module).Resources[addrTo.Resource.String()] = rs stateTo.Module(addrTo.Module).Resources[addrTo.Resource.String()] = rs
} }

View File

@ -47,10 +47,10 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
} }
for _, res := range newMod.Resources { for _, res := range newMod.Resources {
resType := res.Addr.Type resType := res.Addr.Resource.Type
providerType := res.ProviderConfig.Provider.Type providerType := res.ProviderConfig.Provider.Type
resource := getResource(providers, providerType, res.Addr) resource := getResource(providers, providerType, res.Addr.Resource)
for key, i := range res.Instances { for key, i := range res.Instances {
resState := &terraform.ResourceState{ resState := &terraform.ResourceState{
@ -103,7 +103,7 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
idx = "." + key.String() idx = "." + key.String()
} }
mod.Resources[res.Addr.String()+idx] = resState mod.Resources[res.Addr.Resource.String()+idx] = resState
} }
// add any deposed instances // add any deposed instances