Skip to content

Commit

Permalink
Introduce better handling (#254)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Hoppe <robert.hoppe@mail.schwarz>
  • Loading branch information
roberth1988 and Robert Hoppe authored Aug 9, 2024
1 parent d9e9ae2 commit 3612173
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
37 changes: 8 additions & 29 deletions stackit/internal/resources/postgres-flex/instance/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,47 +298,26 @@ 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
}
resp.Diagnostics.AddError("failed to delete instance", agg.Error())
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
Expand Down

0 comments on commit 3612173

Please sign in to comment.