diff --git a/go.mod b/go.mod index fb026504..ca5aae2c 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/Masterminds/semver v1.5.0 - github.com/SchwarzIT/community-stackit-go-client v1.30.5 + github.com/SchwarzIT/community-stackit-go-client v1.30.6 github.com/go-test/deep v1.0.3 github.com/google/uuid v1.3.0 github.com/hashicorp/terraform-plugin-framework v1.2.0 diff --git a/go.sum b/go.sum index ffbb180f..aa0adf9c 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,8 @@ github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugX github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ= github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= -github.com/SchwarzIT/community-stackit-go-client v1.30.5 h1:6IVN7wt30gw7CsPKcEsJ4sWsMD5w37++K1fyU55qspc= -github.com/SchwarzIT/community-stackit-go-client v1.30.5/go.mod h1:hlTfBNOKE1fokWE8g3KrI0AHo0SqzTKkS+LrIdhH8Qg= +github.com/SchwarzIT/community-stackit-go-client v1.30.6 h1:FHfESqJni4kdR4419PT+9/8pDZSW1cq5Nfl4GpLc/I0= +github.com/SchwarzIT/community-stackit-go-client v1.30.6/go.mod h1:hlTfBNOKE1fokWE8g3KrI0AHo0SqzTKkS+LrIdhH8Qg= github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= diff --git a/stackit/internal/resources/postgres-flex/instance/actions.go b/stackit/internal/resources/postgres-flex/instance/actions.go index dea1da6b..b76d774b 100644 --- a/stackit/internal/resources/postgres-flex/instance/actions.go +++ b/stackit/internal/resources/postgres-flex/instance/actions.go @@ -298,10 +298,11 @@ func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp * // init client c := r.client.PostgresFlex - resDelete, err := c.Instance.Delete(ctx, state.ProjectID.ValueString(), state.ID.ValueString()) + res, err := c.Instance.Delete(ctx, state.ProjectID.ValueString(), state.ID.ValueString()) - if agg := common.Validate(&resp.Diagnostics, resDelete, err); agg != nil { - if validate.StatusEquals(resDelete, http.StatusNotFound) { + if agg := common.Validate(&resp.Diagnostics, res, err); agg != nil { + if validate.StatusEquals(res, http.StatusNotFound) || + (validate.StatusEquals(res, http.StatusBadRequest) && res.JSON400 != nil && strings.Contains(*res.JSON400.Message, "already marked for deletion")) { resp.State.RemoveResource(ctx) return } @@ -309,36 +310,14 @@ func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp * return } - timeout, d := state.Timeouts.Delete(ctx, 1*time.Hour) + timeout, d := state.Timeouts.Delete(ctx, 30*time.Minute) + if resp.Diagnostics.Append(d...); resp.Diagnostics.HasError() { return } - 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 - } + process := res.WaitHandler(ctx, c.Instance, state.ProjectID.ValueString(), state.ID.ValueString()).SetTimeout(timeout) + if _, err := process.WaitWithContext(ctx); err != nil { if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) { resp.State.RemoveResource(ctx) return