-
Notifications
You must be signed in to change notification settings - Fork 188
/
raw_client.go
248 lines (199 loc) · 6.21 KB
/
raw_client.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
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package armclient
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
"time"
"github.com/Azure/azure-service-operator/hack/generated/pkg/version"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/devigned/tab"
)
type Client struct {
autorest.Client
Host string
}
var UserAgent = "aso-controller/" + version.BuildVersion
// NewClient creates a new raw client
func NewClient(authorizer autorest.Authorizer) *Client {
autorestClient := autorest.NewClientWithUserAgent(UserAgent)
// Disable retries by default
autorestClient.RetryAttempts = 0
autorestClient.Authorizer = authorizer
c := &Client{
Client: autorestClient,
Host: azure.PublicCloud.ResourceManagerEndpoint, // TODO: We need to support other endpoints
}
return c
}
// WithExponentialRetries creates a new client with exponential retries configured and returns it
func (c *Client) WithExponentialRetries(attempts int, backoff time.Duration, maxBackoff time.Duration) *Client {
// Copy the client
result := *c
result.SendDecorators = nil
// Deep copy the send decorators
result.SendDecorators = append(result.SendDecorators, c.SendDecorators...)
// There's no place to set a backoff cap on the actual client?
result.RetryAttempts = attempts
result.RetryDuration = backoff
result.SendDecorators = append(
result.SendDecorators,
autorest.DoRetryForStatusCodesWithCap(
result.RetryAttempts,
result.RetryDuration,
maxBackoff,
autorest.StatusCodesForRetry...))
return &result
}
// PutDeployment creates or updates a deployment in Azure, and updates the given Deployment
// with the current deployment state.
func (c *Client) PutDeployment(ctx context.Context, deployment *Deployment) error {
entityPath, err := deployment.GetEntityPath()
if err != nil {
return err
}
preparer := autorest.CreatePreparer(
autorest.AsContentType("application/json"),
autorest.WithJSON(deployment))
req, err := c.newRequest(ctx, http.MethodPut, entityPath)
if err != nil {
tab.For(ctx).Error(err)
return err
}
req, err = preparer.Prepare(req)
if err != nil {
tab.For(ctx).Error(err)
return err
}
// The linter below doesn't realize that the response is closed in the course of
// the autorest.Respond call below, suppressing the false positive.
// nolint:bodyclose
resp, err := c.Send(req)
if err != nil {
tab.For(ctx).Error(err)
return err
}
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
autorest.ByUnmarshallingJSON(deployment),
autorest.ByClosing())
if err != nil {
tab.For(ctx).Error(err)
return err
}
return nil
}
var zeroDuration time.Duration = 0
func (c *Client) GetResource(ctx context.Context, resourceID string, resource interface{}) (time.Duration, error) {
preparer := autorest.CreatePreparer(
autorest.AsContentType("application/json"))
req, err := c.newRequest(ctx, http.MethodGet, resourceID)
if err != nil {
return zeroDuration, err
}
req, err = preparer.Prepare(req)
if err != nil {
tab.For(ctx).Error(err)
return zeroDuration, err
}
// The linter below doesn't realize that the response is closed in the course of
// the autorest.Respond call below, suppressing the false positive.
// nolint:bodyclose
resp, err := c.Send(req)
if err != nil {
tab.For(ctx).Error(err)
return zeroDuration, err
}
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(http.StatusOK),
autorest.ByUnmarshallingJSON(resource),
autorest.ByClosing())
retryAfter := getRetryAfter(resp)
if err != nil {
tab.For(ctx).Error(err)
return retryAfter, err
}
return retryAfter, nil
}
func getRetryAfter(resp *http.Response) time.Duration {
if retryAfterStr := resp.Header.Get("Retry-After"); retryAfterStr != "" {
if retryAfterVal, parseErr := strconv.ParseInt(retryAfterStr, 10, 64); parseErr == nil {
return time.Duration(retryAfterVal) * time.Second
}
if retryAfterTime, parseErr := parseHttpDate(retryAfterStr); parseErr == nil {
result := time.Until(retryAfterTime)
if result > 0 {
return result
}
}
}
return 0
}
func parseHttpDate(s string) (time.Time, error) {
if t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", s); err == nil {
return t, nil
} else if t, err = time.Parse("Monday, 02-Jan-06 15:04:05 MST", s); err == nil {
return t, nil
} else if t, err = time.Parse("Mon Jan 2 15:04:05 2006", s); err == nil {
return t, nil
}
return time.Time{}, errors.New("unable to parse date")
}
// DeleteResource will make an HTTP DELETE call to the resourceId and attempt to fill the resource with the response.
// If the body of the response is empty, the resource will be nil.
func (c *Client) DeleteResource(ctx context.Context, resourceID string, resource interface{}) (time.Duration, error) {
preparer := autorest.CreatePreparer(
autorest.AsContentType("application/json"))
req, err := c.newRequest(ctx, http.MethodDelete, resourceID)
if err != nil {
return zeroDuration, err
}
req, err = preparer.Prepare(req)
if err != nil {
tab.For(ctx).Error(err)
return zeroDuration, err
}
// The linter below doesn't realize that the response is closed in the course of
// the autorest.Respond call below, suppressing the false positive.
// nolint:bodyclose
resp, err := c.Send(req)
if err != nil {
tab.For(ctx).Error(err)
return zeroDuration, err
}
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
autorest.ByUnmarshallingJSON(resource),
autorest.ByClosing())
retryAfter := getRetryAfter(resp)
if err != nil {
if IsNotFound(err) {
// you asked it to be gone, well, it is.
return zeroDuration, nil /* no need to retry */
}
tab.For(ctx).Error(err)
return retryAfter, err
}
return retryAfter, nil
}
func (c *Client) newRequest(ctx context.Context, method string, entityPath string) (*http.Request, error) {
return http.NewRequestWithContext(ctx, method, c.Host+strings.TrimPrefix(entityPath, "/"), nil)
}
func IsNotFound(err error) bool {
var typedError *azure.RequestError
if errors.As(err, &typedError) {
if typedError.Response != nil && typedError.Response.StatusCode == 404 {
return true
}
}
return false
}