change state dependencies to AbsResource addrs

We need to be able to reference all possible dependencies for ordering
when the configuration is no longer present, which means that absolute
addresses must be used. Since this is only to recreate the proper
ordering for instance destruction, only resources addresses need to be
listed rather than individual instance addresses.
This commit is contained in:
James Bardin 2019-10-08 14:11:02 -04:00
parent 744b835e17
commit 2c3c011f20
6 changed files with 49 additions and 23 deletions

View File

@ -275,7 +275,8 @@ func TestRefresh_defaultState(t *testing.T) {
expected := &states.ResourceInstanceObjectSrc{ expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"), AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
Dependencies: []addrs.Referenceable{}, Dependencies: []addrs.AbsResource{},
DependsOn: []addrs.Referenceable{},
} }
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected)) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
@ -339,7 +340,8 @@ func TestRefresh_outPath(t *testing.T) {
expected := &states.ResourceInstanceObjectSrc{ expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"), AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
Dependencies: []addrs.Referenceable{}, Dependencies: []addrs.AbsResource{},
DependsOn: []addrs.Referenceable{},
} }
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected)) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
@ -568,7 +570,8 @@ func TestRefresh_backup(t *testing.T) {
expected := &states.ResourceInstanceObjectSrc{ expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"changed\"\n }"), AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"changed\"\n }"),
Dependencies: []addrs.Referenceable{}, Dependencies: []addrs.AbsResource{},
DependsOn: []addrs.Referenceable{},
} }
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected)) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
@ -632,7 +635,8 @@ func TestRefresh_disableBackup(t *testing.T) {
expected := &states.ResourceInstanceObjectSrc{ expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"), AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
Dependencies: []addrs.Referenceable{}, Dependencies: []addrs.AbsResource{},
DependsOn: []addrs.Referenceable{},
} }
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected)) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))

View File

@ -29,12 +29,17 @@ type ResourceInstanceObject struct {
// it was updated. // it was updated.
Status ObjectStatus Status ObjectStatus
// Dependencies is a set of other addresses in the same module which // Dependencies is a set of absolute address to other resources this
// this instance depended on when the given attributes were evaluated. // instance dependeded on when it was applied. This is used to construct
// This is used to construct the dependency relationships for an object // the dependency relationships for an object whose configuration is no
// whose configuration is no longer available, such as if it has been // longer available, such as if it has been removed from configuration
// removed from configuration altogether, or is now deposed. // altogether, or is now deposed.
Dependencies []addrs.Referenceable Dependencies []addrs.AbsResource
// DependsOn corresponds to the deprecated `depends_on` field in the state.
// This field contained the configuration `depends_on` values, and some of
// the references from within a single module.
DependsOn []addrs.Referenceable
} }
// ObjectStatus represents the status of a RemoteObject. // ObjectStatus represents the status of a RemoteObject.

View File

@ -53,7 +53,9 @@ type ResourceInstanceObjectSrc struct {
// ResourceInstanceObject. // ResourceInstanceObject.
Private []byte Private []byte
Status ObjectStatus Status ObjectStatus
Dependencies []addrs.Referenceable Dependencies []addrs.AbsResource
// deprecated
DependsOn []addrs.Referenceable
} }
// Decode unmarshals the raw representation of the object attributes. Pass the // Decode unmarshals the raw representation of the object attributes. Pass the

View File

@ -153,8 +153,17 @@ func (obj *ResourceInstanceObjectSrc) DeepCopy() *ResourceInstanceObjectSrc {
// Some addrs.Referencable implementations are technically mutable, but // Some addrs.Referencable implementations are technically mutable, but
// we treat them as immutable by convention and so we don't deep-copy here. // we treat them as immutable by convention and so we don't deep-copy here.
dependencies := make([]addrs.Referenceable, len(obj.Dependencies)) var dependencies []addrs.AbsResource
copy(dependencies, obj.Dependencies) if obj.Dependencies != nil {
dependencies = make([]addrs.AbsResource, len(obj.Dependencies))
copy(dependencies, obj.Dependencies)
}
var dependsOn []addrs.Referenceable
if obj.DependsOn != nil {
dependsOn = make([]addrs.Referenceable, len(obj.DependsOn))
copy(dependsOn, obj.DependsOn)
}
return &ResourceInstanceObjectSrc{ return &ResourceInstanceObjectSrc{
Status: obj.Status, Status: obj.Status,
@ -187,9 +196,9 @@ func (obj *ResourceInstanceObject) DeepCopy() *ResourceInstanceObject {
// Some addrs.Referenceable implementations are technically mutable, but // Some addrs.Referenceable implementations are technically mutable, but
// we treat them as immutable by convention and so we don't deep-copy here. // we treat them as immutable by convention and so we don't deep-copy here.
var dependencies []addrs.Referenceable var dependencies []addrs.AbsResource
if obj.Dependencies != nil { if obj.Dependencies != nil {
dependencies = make([]addrs.Referenceable, len(obj.Dependencies)) dependencies = make([]addrs.AbsResource, len(obj.Dependencies))
copy(dependencies, obj.Dependencies) copy(dependencies, obj.Dependencies)
} }

View File

@ -138,7 +138,7 @@ func TestStateDeepCopy(t *testing.T) {
SchemaVersion: 1, SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`), AttrsJSON: []byte(`{"woozles":"confuzles"}`),
Private: []byte("private data"), Private: []byte("private data"),
Dependencies: []addrs.Referenceable{}, Dependencies: []addrs.AbsResource{},
}, },
addrs.ProviderConfig{ addrs.ProviderConfig{
Type: "test", Type: "test",
@ -155,11 +155,16 @@ func TestStateDeepCopy(t *testing.T) {
SchemaVersion: 1, SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`), AttrsJSON: []byte(`{"woozles":"confuzles"}`),
Private: []byte("private data"), Private: []byte("private data"),
Dependencies: []addrs.Referenceable{addrs.Resource{ Dependencies: []addrs.AbsResource{
Mode: addrs.ManagedResourceMode, {
Type: "test_thing", Module: addrs.RootModuleInstance,
Name: "baz", Resource: addrs.Resource{
}}, Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "baz",
},
},
},
}, },
addrs.ProviderConfig{ addrs.ProviderConfig{
Type: "test", Type: "test",

View File

@ -2885,8 +2885,9 @@ func TestContext2Apply_orphanResource(t *testing.T) {
s.SetResourceMeta(zeroAddr, states.EachList, providerAddr) s.SetResourceMeta(zeroAddr, states.EachList, providerAddr)
s.SetResourceMeta(oneAddr, states.EachList, providerAddr) s.SetResourceMeta(oneAddr, states.EachList, providerAddr)
s.SetResourceInstanceCurrent(oneAddr.Instance(addrs.IntKey(0)), &states.ResourceInstanceObjectSrc{ s.SetResourceInstanceCurrent(oneAddr.Instance(addrs.IntKey(0)), &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte(`{}`), AttrsJSON: []byte(`{}`),
Dependencies: []addrs.AbsResource{},
}, providerAddr) }, providerAddr)
}) })
if !cmp.Equal(state, want) { if !cmp.Equal(state, want) {