restore saved dependencies on delete error

Is a resource delete action fails and the provider returned a new state,
we need to ensure the stored dependencies are retained.
This commit is contained in:
James Bardin 2021-04-08 09:57:14 -04:00
parent b7fb533bd2
commit 4bfabbaee4
2 changed files with 15 additions and 3 deletions

View File

@ -12467,9 +12467,10 @@ func TestContext2Apply_errorRestoreStatus(t *testing.T) {
state := states.BuildState(func(s *states.SyncState) { state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(addr, &states.ResourceInstanceObjectSrc{ s.SetResourceInstanceCurrent(addr, &states.ResourceInstanceObjectSrc{
Status: states.ObjectTainted, Status: states.ObjectTainted,
AttrsJSON: []byte(`{"test_string":"foo"}`), AttrsJSON: []byte(`{"test_string":"foo"}`),
Private: []byte("private"), Private: []byte("private"),
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.b")},
}, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`)) }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`))
}) })
@ -12506,6 +12507,10 @@ func TestContext2Apply_errorRestoreStatus(t *testing.T) {
t.Fatal("resource should still be tainted in the state") t.Fatal("resource should still be tainted in the state")
} }
if len(res.Current.Dependencies) != 1 || !res.Current.Dependencies[0].Equal(mustConfigResourceAddr("test_object.b")) {
t.Fatalf("incorrect dependencies, got %q", res.Current.Dependencies)
}
if string(res.Current.Private) != "private" { if string(res.Current.Private) != "private" {
t.Fatalf("incorrect private data, got %q", res.Current.Private) t.Fatalf("incorrect private data, got %q", res.Current.Private)
} }

View File

@ -2102,6 +2102,13 @@ func (n *NodeAbstractResourceInstance) apply(
Private: resp.Private, Private: resp.Private,
CreateBeforeDestroy: createBeforeDestroy, CreateBeforeDestroy: createBeforeDestroy,
} }
// if the resource was being deleted, the dependencies are not going to
// be recalculated and we need to restore those as well.
if change.Action == plans.Delete {
newState.Dependencies = state.Dependencies
}
return newState, diags return newState, diags
case !newVal.IsNull(): case !newVal.IsNull():