-
Notifications
You must be signed in to change notification settings - Fork 8
/
wait.go
179 lines (172 loc) · 6.17 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
167
168
169
170
171
172
173
174
175
176
177
178
179
package instances
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services/argus/v1.0/instances"
"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)
const (
connection_reset = "read: connection reset" // catch connection reset by peer error
gateway_timeout = "Gateway Timeout"
)
// Wait will wait for creation
// returned interface is nil or *instances.ProjectInstanceUI
func (*CreateResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
if strings.Contains(agg.Error(), connection_reset) ||
strings.Contains(agg.Error(), gateway_timeout) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
http.StatusForbidden,
) {
return nil, false, nil
}
return nil, false, agg
}
if s.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_CREATE_SUCCEEDED {
return s.JSON200, true, nil
}
return s.JSON200, false, nil
})
}
// Wait will wait for update
// returned interface is nil or *instances.ProjectInstanceUI
func (*UpdateResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
seenUpdating := false
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
if strings.Contains(agg.Error(), connection_reset) ||
strings.Contains(agg.Error(), gateway_timeout) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
http.StatusForbidden,
) {
return nil, false, nil
}
return nil, false, agg
}
if s.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_UPDATE_SUCCEEDED ||
(seenUpdating && s.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_CREATE_SUCCEEDED) {
return s.JSON200, true, nil
}
if s.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_UPDATE_FAILED {
return s.JSON200, true, fmt.Errorf("update failed for %s", instanceID)
}
if s.JSON200.Status != instances.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.Get(ctx, projectID, instanceID)
if agg := validate.Response(si, err, "JSON200"); agg != nil {
if strings.Contains(agg.Error(), connection_reset) ||
strings.Contains(agg.Error(), gateway_timeout) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
http.StatusForbidden,
) {
return nil, false, nil
}
return nil, false, agg
}
if si.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_UPDATING ||
si.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_UPDATE_SUCCEEDED ||
si.JSON200.Status == instances.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
})
}
// Wait will wait for deletion
// returned interface is nil
func (*DeleteResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
if strings.Contains(agg.Error(), connection_reset) ||
strings.Contains(agg.Error(), gateway_timeout) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
http.StatusForbidden,
) {
return nil, false, nil
}
if validate.StatusEquals(s, http.StatusNotFound) {
return nil, true, nil
}
return nil, false, agg
}
if s.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_DELETE_SUCCEEDED {
return nil, true, nil
}
if s.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_DELETE_FAILED {
return s.JSON200, true, fmt.Errorf("deletion failed for %s", instanceID)
}
if s.JSON200.Status != instances.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.Get(ctx, projectID, instanceID)
if agg := validate.Response(si, err, "JSON200"); agg != nil {
if strings.Contains(agg.Error(), connection_reset) ||
strings.Contains(agg.Error(), gateway_timeout) {
return nil, false, nil
}
if validate.StatusEquals(s,
http.StatusBadGateway,
http.StatusGatewayTimeout,
http.StatusInternalServerError,
http.StatusForbidden,
) {
return nil, false, nil
}
if validate.StatusEquals(s, http.StatusNotFound) {
return nil, true, nil
}
return nil, false, agg
}
if si.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_DELETING ||
si.JSON200.Status == instances.PROJECT_INSTANCE_UI_STATUS_DELETE_FAILED ||
si.JSON200.Status == instances.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
})
}