-
Notifications
You must be signed in to change notification settings - Fork 8
/
wait.go
60 lines (54 loc) · 1.75 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package project
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services/kubernetes/v1.0/generated/project"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
"github.com/pkg/errors"
)
func (r CreateProjectResponse) WaitHandler(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.GetProjectWithResponse(ctx, projectID)
if err != nil {
return nil, false, errors.Wrap(err, "failed during create request preparation")
}
if resp.HasError != nil {
if strings.Contains(resp.HasError.Error(), "project has no assigned namespace") {
return nil, false, nil
}
return nil, false, err
}
switch *resp.JSON200.State {
case project.STATE_FAILED:
fallthrough
case project.STATE_DELETING:
return nil, false, fmt.Errorf("received state: %s for project ID: %s",
*resp.JSON200.State,
*resp.JSON200.ProjectID,
)
case project.STATE_CREATED:
return nil, true, nil
}
return nil, false, nil
})
}
func (r DeleteProjectResponse) WaitHandler(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.GetProjectWithResponse(ctx, projectID)
if err != nil {
if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) {
return nil, true, nil
}
return nil, false, errors.Wrap(err, "failed during delete request preparation")
}
if resp.HasError != nil {
if strings.Contains(resp.HasError.Error(), http.StatusText(http.StatusNotFound)) {
return nil, true, nil
}
return nil, false, err
}
return nil, false, nil
})
}