Skip to content

Commit

Permalink
Introduce exist check differently
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Hoppe committed Aug 9, 2024
1 parent d8d10da commit 8da4ee1
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions stackit/internal/resources/postgres-flex/instance/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,31 +298,6 @@ func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *

// init client
c := r.client.PostgresFlex

// check first of all if this resource is already deleted
resExists, err := c.Instance.Get(ctx, state.ProjectID.ValueString(), state.ID.ValueString())
if agg := common.Validate(&resp.Diagnostics, resExists, err, "JSON200"); agg != nil {
if validate.StatusEquals(resExists, http.StatusNotFound) {
resp.State.RemoveResource(ctx)
return
}

// STACKIT API sometimes uses uppercase .. sometimes lower .. sometimes mixed
if resExists.JSON200.Item.Status != nil && strings.ToUpper(*resExists.JSON200.Item.Status) != "DELETED" {
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("failed to read instance", agg.Error())

return
}

if err := applyClientResponse(&state, resExists.JSON200.Item); err != nil {
resp.Diagnostics.AddError("failed to process client response", err.Error())
return
}

resDelete, err := c.Instance.Delete(ctx, state.ProjectID.ValueString(), state.ID.ValueString())

if agg := common.Validate(&resp.Diagnostics, resDelete, err); agg != nil {
Expand All @@ -340,6 +315,30 @@ func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *
}
process := resDelete.WaitHandler(ctx, c.Instance, state.ProjectID.ValueString(), state.ID.ValueString()).SetTimeout(timeout)
if _, err := process.WaitWithContext(ctx); err != nil {
// check first of all if this resource is already deleted
resExists, err := c.Instance.Get(ctx, state.ProjectID.ValueString(), state.ID.ValueString())
if agg := common.Validate(&resp.Diagnostics, resExists, err, "JSON200"); agg != nil {
if validate.StatusEquals(resExists, http.StatusNotFound) {
resp.State.RemoveResource(ctx)
return
}

// STACKIT API sometimes uses uppercase .. sometimes lower .. sometimes mixed
if resExists.JSON200.Item.Status != nil && strings.ToUpper(*resExists.JSON200.Item.Status) != "DELETED" {
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("failed to read instance", agg.Error())

return
}

if err := applyClientResponse(&state, resExists.JSON200.Item); err != nil {
resp.Diagnostics.AddError("failed to process client response", err.Error())
return
}

if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) {
resp.State.RemoveResource(ctx)
return
Expand Down

0 comments on commit 8da4ee1

Please sign in to comment.