diff --git a/stackit/internal/resources/postgres-flex/instance/actions.go b/stackit/internal/resources/postgres-flex/instance/actions.go index 64ecd663..dea1da6b 100644 --- a/stackit/internal/resources/postgres-flex/instance/actions.go +++ b/stackit/internal/resources/postgres-flex/instance/actions.go @@ -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 { @@ -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