-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add wait for user deletion in postgres-flex resource (#142)
Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |