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{}
addr := m.Resources[key].Addr
resAddr := addr.Resource
taintStr := ""
if v.Current != nil && v.Current.Status == 'T' {
@ -114,11 +115,11 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
}
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 {
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.
@ -150,44 +151,44 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
continue
}
switch addr.Mode {
switch resAddr.Mode {
case addrs.ManagedResourceMode:
schema, _ = schemas.ResourceTypeConfig(
provider,
addr.Mode,
addr.Type,
resAddr.Mode,
resAddr.Type,
)
if schema == nil {
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
}
p.buf.WriteString(fmt.Sprintf(
"resource %q %q {",
addr.Type,
addr.Name,
resAddr.Type,
resAddr.Name,
))
case addrs.DataResourceMode:
schema, _ = schemas.ResourceTypeConfig(
provider,
addr.Mode,
addr.Type,
resAddr.Mode,
resAddr.Type,
)
if schema == nil {
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
}
p.buf.WriteString(fmt.Sprintf(
"data %q %q {",
addr.Type,
addr.Name,
resAddr.Type,
resAddr.Name,
))
default:
// should never happen, since the above is exhaustive
p.buf.WriteString(addr.String())
p.buf.WriteString(resAddr.String())
}
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 k, ri := range r.Instances {
resAddr := r.Addr.Resource
current := resource{
Address: r.Addr.Absolute(module).Instance(k).String(),
Type: r.Addr.Type,
Name: r.Addr.Name,
Address: r.Addr.Instance(k).String(),
Type: resAddr.Type,
Name: resAddr.Name,
ProviderName: r.ProviderConfig.Provider.LegacyString(),
}
switch r.Addr.Mode {
switch resAddr.Mode {
case addrs.ManagedResourceMode:
current.Mode = "managed"
case addrs.DataResourceMode:
current.Mode = "data"
default:
return ret, fmt.Errorf("resource %s has an unsupported mode %s",
r.Addr.String(),
r.Addr.Mode.String(),
resAddr.String(),
resAddr.Mode.String(),
)
}
@ -280,8 +282,8 @@ func marshalResources(resources map[string]*states.Resource, module addrs.Module
schema, _ := schemas.ResourceTypeConfig(
r.ProviderConfig.Provider,
r.Addr.Mode,
r.Addr.Type,
resAddr.Mode,
resAddr.Type,
)
// 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
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())
if err != nil {

View File

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

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 {
var ret []addrs.AbsResourceInstance
for key := range rs.Instances {
ret = append(ret, rs.Addr.Instance(key).Absolute(moduleAddr))
ret = append(ret, rs.Addr.Instance(key))
}
return ret
}

View File

@ -226,7 +226,7 @@ func (c *StateMvCommand) Run(args []string) int {
ssFrom.RemoveResource(addrFrom)
// 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
}

View File

@ -47,10 +47,10 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
}
for _, res := range newMod.Resources {
resType := res.Addr.Type
resType := res.Addr.Resource.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 {
resState := &terraform.ResourceState{
@ -103,7 +103,7 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
idx = "." + key.String()
}
mod.Resources[res.Addr.String()+idx] = resState
mod.Resources[res.Addr.Resource.String()+idx] = resState
}
// add any deposed instances