forked from ionos-enterprise/ionos-enterprise-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
338 lines (296 loc) · 10.3 KB
/
request.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package profitbricks
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
resty "github.com/go-resty/resty/v2"
)
const (
RequestStatusQueued = "QUEUED"
RequestStatusRunning = "RUNNING"
RequestStatusFailed = "FAILED"
RequestStatusDone = "DONE"
)
// RequestStatus object
type RequestStatus struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata RequestStatusMetadata `json:"metadata,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// RequestStatusMetadata object
type RequestStatusMetadata struct {
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Etag string `json:"etag,omitempty"`
Targets []RequestTarget `json:"targets,omitempty"`
}
// RequestTarget object
type RequestTarget struct {
Target ResourceReference `json:"target,omitempty"`
Status string `json:"status,omitempty"`
}
// Requests object
type Requests struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Request `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
type RequestMetadata struct {
CreatedDate time.Time `json:"createdDate"`
CreatedBy string `json:"createdBy"`
Etag string `json:"etag"`
RequestStatus RequestStatus `json:"requestStatus"`
}
type RequestProperties struct {
Method string `json:"method"`
Headers interface{} `json:"headers"`
Body string `json:"body"`
URL string `json:"url"`
}
// Request object
type Request struct {
ID string `json:"id"`
Type string `json:"type"`
Href string `json:"href"`
Metadata RequestMetadata `json:"metadata"`
Properties RequestProperties `json:"properties"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// RequestListFilter is a wrapper around url.Values to provide a common
// interface to make use of the filters that the ionos API provides for the
// requests endpoint.
// Example:
// filter := NewRequestListFilter().WithUrl("volumes").WithBody("de/fra") will create a api call
// with query args like: /requests?filter.url=volumes&filter.body=de%2Ffra
type RequestListFilter struct {
url.Values
}
// NewRequestListFilter creates a new RequestListFilter
func NewRequestListFilter() *RequestListFilter {
return &RequestListFilter{Values: url.Values{}}
}
// Clone clones the RequestListFilter
func (f *RequestListFilter) Clone() *RequestListFilter {
values := make(url.Values, len(f.Values))
for k, v := range f.Values {
values[k] = v
}
return &RequestListFilter{Values: values}
}
// AddUrl adds an url filter to the request list filter
func (f *RequestListFilter) AddUrl(url string) {
f.WithUrl(url)
}
// WithUrl adds an url filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithUrl(url string) *RequestListFilter {
f.Add("filter.url", url)
return f
}
// AddCreatedDate adds a createdDate filter to the request list filter
func (f *RequestListFilter) AddCreatedDate(createdDate string) {
f.WithCreatedDate(createdDate)
}
// WithCreatedDate adds a createdDate filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithCreatedDate(createdDate string) *RequestListFilter {
f.Add("filter.createdDate", createdDate)
return f
}
// AddMethod adds a method filter to the request list filter
func (f *RequestListFilter) AddMethod(method string) {
f.WithMethod(method)
}
// WithMethod adds a method filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithMethod(method string) *RequestListFilter {
f.Add("filter.method", method)
return f
}
// AddBody adds a body filter to the request list filter
func (f *RequestListFilter) AddBody(body string) {
f.WithBody(body)
}
// WithBody adds a body filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithBody(body string) *RequestListFilter {
f.Add("filter.body", body)
return f
}
// AddRequestStatus adds a requestStatus filter to the request list filter
func (f *RequestListFilter) AddRequestStatus(requestStatus string) {
f.WithRequestStatus(requestStatus)
}
// WithRequestStatus adds a requestStatus filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithRequestStatus(requestStatus string) *RequestListFilter {
f.Add("filter.status", requestStatus)
return f
}
const timeFormat = "2006-01-02 15:04:05"
// AddCreatedAfter adds a createdAfter filter to the request list filter
func (f *RequestListFilter) AddCreatedAfter(t time.Time) {
f.WithCreatedAfter(t)
}
// WithCreatedAfter adds a createdAfter filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithCreatedAfter(t time.Time) *RequestListFilter {
f.Add("filter.createdAfter", t.Format(timeFormat))
return f
}
// AddCreatedBefore adds a createdBefore filter to the request list filter
func (f *RequestListFilter) AddCreatedBefore(t time.Time) *RequestListFilter {
f.WithCreatedBefore(t)
return f
}
// WithCreatedBefore adds a createdBefore filter to the request list filter returning the filter for chaining
func (f *RequestListFilter) WithCreatedBefore(t time.Time) *RequestListFilter {
f.Add("filter.createdBefore", t.Format(timeFormat))
return f
}
// ListRequests lists all requests
func (c *Client) ListRequests() (*Requests, error) {
url := "/requests"
ret := &Requests{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
}
// ListRequestsWithFilter lists all requests that match the given filters
func (c *Client) ListRequestsWithFilter(filter *RequestListFilter) (*Requests, error) {
path := "/requests"
ret := &Requests{}
r := c.R().SetResult(ret)
if filter != nil {
for k, v := range filter.Values {
for _, i := range v {
r.SetQueryParam(k, i)
}
}
}
return ret, c.DoWithRequest(r, resty.MethodGet, path, http.StatusOK)
}
// GetRequest gets a specific request
func (c *Client) GetRequest(reqID string) (*Request, error) {
url := "/requests/" + reqID
ret := &Request{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
}
// GetRequestStatus returns status of the request
func (c *Client) GetRequestStatus(path string) (*RequestStatus, error) {
url := path
ret := &RequestStatus{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
}
// IsRequestFinished checks the given path to a request status resource. The request is considered "done"
// if its status won't change, which is true for status FAILED and DONE. Since Failed is obviously not done,
// the method returns true and RequestFailed error in that case.
func (c *Client) IsRequestFinished(path string) (bool, error) {
request, err := c.GetRequestStatus(path)
if err != nil {
return false, err
}
switch request.Metadata.Status {
case RequestStatusDone:
return true, nil
case RequestStatusFailed:
return true, NewClientError(
RequestFailed,
fmt.Sprintf("Request %s failed: %s", request.ID, request.Metadata.Message),
)
}
return false, nil
}
// WaitTillProvisionedOrCanceled waits for a request to be completed.
// It returns an error if the request status could not be fetched, the request
// failed or the given context is canceled.
func (c *Client) WaitTillProvisionedOrCanceled(ctx context.Context, path string) error {
req := c.R()
status := &RequestStatus{}
req.SetContext(ctx).SetResult(status)
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
err := c.DoWithRequest(req, resty.MethodGet, path, http.StatusOK)
if err != nil {
return err
}
switch status.Metadata.Status {
case RequestStatusDone:
return nil
case RequestStatusFailed:
return NewClientError(
RequestFailed,
fmt.Sprintf("Request %s failed: %s", status.ID, status.Metadata.Message),
)
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
continue
}
}
}
// WaitTillProvisioned waits for a request to be completed.
// It returns an error if the request status could not be fetched, the request
// failed or a timeout of 2.5 minutes is exceeded.
func (c *Client) WaitTillProvisioned(path string) (err error) {
ctx, cancel := context.WithTimeout(context.TODO(), 150*time.Second)
defer cancel()
if err = c.WaitTillProvisionedOrCanceled(ctx, path); err != nil {
if err == context.DeadlineExceeded {
return errors.New("timeout expired while waiting for request to complete")
}
}
return
}
type RequestSelector func(Request) bool
// IsRequestStatusFinished is true if the requests Status is neither QUEUED or RUNNING.
func IsRequestStatusFinished(r Request) bool {
switch r.Metadata.RequestStatus.Metadata.Status {
case RequestStatusQueued, RequestStatusRunning:
return false
}
return true
}
// WaitTillRequestsFinished will wait until there are no more unfinished requests matching the given filter
func (c *Client) WaitTillRequestsFinished(ctx context.Context, filter *RequestListFilter) error {
return c.WaitTillMatchingRequestsFinished(ctx, filter, func(r Request) bool { return !IsRequestStatusFinished(r) })
}
// WaitTillMatchingRequestsFinished gets open requests with given filters and will
// wait for each request that is selected by the selector. The selector
// should consider filtering out requests that are finished. (e.g. using IsRequestStatusFinished)
func (c *Client) WaitTillMatchingRequestsFinished(
ctx context.Context, filter *RequestListFilter, selector RequestSelector) error {
waited := true
for waited && ctx.Err() == nil {
waited = false
requests, err := c.ListRequestsWithFilter(filter)
if err != nil {
return err
}
for _, r := range requests.Items {
if selector(r) {
waited = true
if err := c.WaitTillProvisionedOrCanceled(ctx, r.Metadata.RequestStatus.Href); err != nil {
if !IsRequestFailed(err) {
return err
}
}
}
}
if !waited {
break
}
}
return nil
}