-
Notifications
You must be signed in to change notification settings - Fork 8
/
wait.go
166 lines (161 loc) · 5.54 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package instances
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)
// WaitHandler will wait for instance creation
// returned interface is nil or *ProjectInstanceUI
func (r InstanceCreateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.InstanceReadWithResponse(ctx, projectID, instanceID)
if err != nil {
return nil, false, err
}
if s.StatusCode() == http.StatusInternalServerError {
return nil, false, nil
}
if s.StatusCode() == http.StatusBadGateway {
return nil, false, nil
}
if s.HasError != nil {
return nil, false, s.HasError
}
if s.JSON200 == nil {
return nil, false, errors.New("received an empty response. JSON200 == nil")
}
if s.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_CREATE_SUCCEEDED {
return s.JSON200, true, nil
}
return s.JSON200, false, nil
})
}
// WaitHandler will wait for instance update
// returned interface is nil or *ProjectInstanceUI
func (r InstanceUpdateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
seenUpdating := false
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.InstanceReadWithResponse(ctx, projectID, instanceID)
if err != nil {
return nil, false, err
}
if s.StatusCode() == http.StatusInternalServerError {
return nil, false, nil
}
if s.StatusCode() == http.StatusBadGateway {
return nil, false, nil
}
if s.HasError != nil {
return nil, false, s.HasError
}
if s.JSON200 == nil {
return nil, false, errors.New("received an empty response. JSON200 == nil")
}
if s.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_UPDATE_SUCCEEDED ||
(seenUpdating && s.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_CREATE_SUCCEEDED) {
return s.JSON200, true, nil
}
if s.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_UPDATE_FAILED {
return s.JSON200, true, fmt.Errorf("update failed for instance %s", instanceID)
}
if s.JSON200.Status != PROJECT_INSTANCE_UI_STATUS_UPDATING {
// in some cases it takes a long time for the server to change the
// instance status to UPDATING
// the following code will wait for the status change for 5 minutes
// and continue the outer wait on change or fail
w := wait.New(func() (res interface{}, done bool, err error) {
si, err := c.InstanceReadWithResponse(ctx, projectID, instanceID)
if err != nil {
return nil, false, err
}
if si.HasError != nil {
return nil, false, s.HasError
}
if si.JSON200 == nil {
return nil, false, errors.New("received an empty response. JSON200 == nil")
}
if si.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_UPDATING ||
si.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_UPDATE_SUCCEEDED ||
si.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_UPDATE_FAILED {
return nil, true, nil
}
return nil, false, nil
})
_, err := w.SetTimeout(5 * time.Minute).WaitWithContext(ctx)
return nil, false, err
}
seenUpdating = true
return s.JSON200, false, nil
})
}
// WaitHandler will wait for instance deletion
// returned interface is nil
func (r InstanceDeleteResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.InstanceReadWithResponse(ctx, projectID, instanceID)
if err != nil {
if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) {
return nil, true, nil
}
return nil, false, err
}
if s.StatusCode() == http.StatusNotFound {
return nil, true, nil
}
if s.StatusCode() == http.StatusInternalServerError {
return nil, false, nil
}
if s.StatusCode() == http.StatusBadGateway {
return nil, false, nil
}
if s.HasError != nil {
return nil, false, s.HasError
}
if s.JSON200 == nil {
return nil, false, errors.New("received an empty response. JSON200 == nil")
}
if s.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_DELETE_SUCCEEDED {
return nil, true, nil
}
if s.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_DELETE_FAILED {
return s.JSON200, true, fmt.Errorf("deletion failed for instance %s", instanceID)
}
if s.JSON200.Status != PROJECT_INSTANCE_UI_STATUS_DELETING {
// in some cases it takes a long time for the server to change the
// instance status to status DELETING
// the following code will wait for the status change for 5 minutes
// and continue the outer wait on change or fail
w := wait.New(func() (res interface{}, done bool, err error) {
si, err := c.InstanceReadWithResponse(ctx, projectID, instanceID)
if err != nil {
if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) {
return nil, true, nil
}
return nil, false, err
}
if si.StatusCode() == http.StatusNotFound {
return nil, true, nil
}
if si.HasError != nil {
return nil, false, s.HasError
}
if si.JSON200 == nil {
return nil, false, errors.New("received an empty response. JSON200 == nil")
}
if si.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_DELETING ||
si.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_DELETE_FAILED ||
si.JSON200.Status == PROJECT_INSTANCE_UI_STATUS_DELETE_SUCCEEDED {
return nil, true, nil
}
return nil, false, nil
})
_, err := w.SetTimeout(5 * time.Minute).WaitWithContext(ctx)
return nil, false, err
}
return nil, false, nil
})
}