Skip to content

Private State is lost after a failed Deletion #863

@AmadeusK525

Description

@AmadeusK525

Module version

v1.3.4

Relevant provider source code

func (r *MyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	result, err, d := performOperation(...)
	resp.Diagnostics.Append(d...)
	if err != nil {
	        return
	}

	d = applyState(&resp.State, result)
	resp.Diagnostics.Append(d...)
	if resp.Diagnostics.HasError() {
	        return
	}

	d = resp.Private.SetKey(ctx, createResultKey, result)
	resp.Diagnostics.Append(d...)
}

func (r *MyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
        // Do stuff, has access to 'createResultKey' via req.Private.GetKey
}

func (r *MyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
        // Do stuff, has access to 'createResultKey' via req.Private.GetKey
}

func (r *MyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
        // This works fine on the first call to Delete
        createResult, d := req.Private.GetKey(ctx, createResultKey)
        resp.Diagnostics.Append(d...)
        if resp.Diagnostics.HasError() {
                return
        }
        
        result, err, d := performOperation(...)
        resp.Diagnostics.Append(d...)
        if err != nil {
                return
        }
        
        // Add a dummy error to signal that 'Delete' failed.
        resp.Diagnostics.AddError("dummy error", "this is a forced error")
}

Terraform Configuration Files

...

Debug Output

Expected Behavior

When saving private state resources, they should be accessible by the CRUD methods and should not be dropped/altered if the operation that modified them return with error diagnostics (just like normal states) for further use/re-use

Actual Behavior

If the Delete (I haven't tested other CRUD methods to see if the behavior is the same) call is made and fails (thus, the resource was NOT deleted), the Private state is completely wiped and can't be accessed in further calls to the CRUD methods

Steps to Reproduce

  • Create a dummy Resource and save some dummy data on the Private state in the CRU calls
  • In the Delete call, retrieve that Private State
  • Check if everything went correctly (it should have)
  • Force fail the Delete call
  • Make the Delete call again and see that retrieving the Private State will no longer work

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions