Skip to content

Commit

Permalink
add wait for user deletion in postgres-flex resource (#142)
Browse files Browse the repository at this point in the history
Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
  • Loading branch information
do87 and Dean Oren committed Mar 16, 2023
1 parent 9c04f7b commit be85375
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/services/postgres-flex/v1.0/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ output-options:
tidy:
- replace: "instance."
all: true
- from: include/users/wait.go
to: users/wait.go
tidy:
- replace: "users."
all: true
- from: include/instance/helper.go
to: instance/helper.go
tidy:
Expand Down
41 changes: 41 additions & 0 deletions pkg/services/postgres-flex/v1.0/generated/users/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package users

import (
"context"
"net/http"
"strings"

"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

const ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers"

// WaitHandler will wait for user deletion
// returned value for deletion wait will always be nil
func (r DeleteUserResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID, userID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
s, err := c.GetUsersWithResponse(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200.Items"); agg != nil {
if strings.Contains(agg.Error(), ClientTimeoutErr) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
) {
return nil, false, nil
}
return nil, false, agg
}
for _, v := range *s.JSON200.Items {
if v.ID == nil || *v.ID != userID {
continue
}
// user was found
return nil, false, nil
}
return nil, true, nil
})
}
10 changes: 10 additions & 0 deletions pkg/services/postgres-flex/v1.0/include/users/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// this file is only used to prevent wait.go
// from showing errors

package users

import "github.com/SchwarzIT/community-stackit-go-client/pkg/services/postgres-flex/v1.0/generated/users"

type DeleteUserResponse struct {
users.ClientWithResponsesInterface
}
42 changes: 42 additions & 0 deletions pkg/services/postgres-flex/v1.0/include/users/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package users

import (
"context"
"net/http"
"strings"

"github.com/SchwarzIT/community-stackit-go-client/pkg/services/postgres-flex/v1.0/generated/users"
"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

const ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers"

// WaitHandler will wait for user deletion
// returned value for deletion wait will always be nil
func (r DeleteUserResponse) WaitHandler(ctx context.Context, c *users.ClientWithResponses, projectID, instanceID, userID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
s, err := c.GetUsersWithResponse(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200.Items"); agg != nil {
if strings.Contains(agg.Error(), ClientTimeoutErr) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
) {
return nil, false, nil
}
return nil, false, agg
}
for _, v := range *s.JSON200.Items {
if v.ID == nil || *v.ID != userID {
continue
}
// user was found
return nil, false, nil
}
return nil, true, nil
})
}

0 comments on commit be85375

Please sign in to comment.