-
Notifications
You must be signed in to change notification settings - Fork 31
/
client.go
470 lines (436 loc) · 15 KB
/
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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package azure
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
)
const (
requestFormat = `HTTP Request Begin %[1]s %[2]s ===================================================
%[3]s
===================================================== HTTP Request End %[1]s %[2]s
`
responseFormat = `HTTP Response Begin %[1]s [%[2]s ===================================================
%[3]s
===================================================== HTTP Response End %[1]s %[2]s
`
)
type AzureDeleter interface {
Delete(ctx context.Context, id, apiVersion, asyncStyle string, queryParams map[string]any) error
}
type AzureClient interface {
AzureDeleter
CanCreate(ctx context.Context, id, path, apiVersion, readMethod string, isSingletonResource, hasDefaultBody bool, isDefaultResponse func(map[string]any) bool) error
Get(ctx context.Context, id string, apiVersion string) (any, error)
Head(ctx context.Context, id string, apiVersion string) error
Patch(ctx context.Context, id string, bodyProps map[string]interface{}, queryParameters map[string]interface{}, asyncStyle string) (map[string]interface{}, bool, error)
Post(ctx context.Context, id string, bodyProps map[string]interface{}, queryParameters map[string]interface{}) (any, error)
Put(ctx context.Context, id string, bodyProps map[string]interface{}, queryParameters map[string]interface{}, asyncStyle string) (map[string]interface{}, bool, error)
}
type azureClientImpl struct {
environment azure.Environment
client autorest.Client
}
func NewAzureClient(environment azure.Environment, authorizer autorest.Authorizer, userAgent string) AzureClient {
autorest.Count429AsRetry = false
client := autorest.NewClientWithUserAgent(userAgent)
client.Authorizer = authorizer
// Log requests
client.RequestInspector = withInspection()
// Log responses
client.ResponseInspector = byInspecting()
return &azureClientImpl{environment: environment, client: client}
}
func (a *azureClientImpl) Delete(ctx context.Context, id, apiVersion, asyncStyle string, queryParams map[string]any) error {
queryParameters := map[string]interface{}{
"api-version": apiVersion,
}
for k, v := range queryParams {
queryParameters[k] = v
}
preparer := autorest.CreatePreparer(
autorest.AsDelete(),
autorest.WithBaseURL(a.environment.ResourceManagerEndpoint),
autorest.WithPath(id),
autorest.WithQueryParameters(queryParameters))
prepReq, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return err
}
resp, err := autorest.SendWithSender(
a.client,
prepReq,
azure.DoRetryWithRegistration(a.client),
)
if err != nil {
return err
}
// Some APIs are explicitly marked `x-ms-long-running-operation` and we should only do the
// Future+WaitForCompletion+GetResult steps in that case.
if asyncStyle != "" {
future, err := azure.NewFutureFromResponse(resp)
if err != nil {
return err
}
err = future.WaitForCompletionRef(ctx, a.client)
if err != nil {
if detailed, ok := err.(autorest.DetailedError); ok {
if resp.StatusCode == 202 && detailed.StatusCode == 404 && detailed.Original != nil &&
strings.Contains(detailed.Original.Error(), "ResourceNotFound") {
// Consider this specific error to be a success of deletion.
// Work around https://github.com/pulumi/pulumi-azure-nextgen/issues/120
// Upstream fix is tracked in https://github.com/Azure/go-autorest/issues/596
return nil
}
}
return err
}
resp, err = future.GetResult(a.client)
if err != nil {
return err
}
}
err = autorest.Respond(
resp,
a.client.ByInspecting(),
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent, http.StatusNotFound),
autorest.ByClosing(),
)
if err != nil {
return err
}
return nil
}
func (a *azureClientImpl) Post(
ctx context.Context,
id string,
bodyProps map[string]interface{},
queryParameters map[string]interface{}) (any, error) {
preparer := autorest.CreatePreparer(
autorest.AsContentType("application/json; charset=utf-8"),
autorest.AsPost(),
autorest.WithBaseURL(a.environment.ResourceManagerEndpoint),
autorest.WithPath(id),
autorest.WithJSON(bodyProps),
autorest.WithQueryParameters(queryParameters))
prepReq, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return nil, err
}
var resp *http.Response
resp, err = autorest.SendWithSender(
a.client,
prepReq,
azure.DoRetryWithRegistration(a.client),
)
if err != nil {
return nil, err
}
var outputs any
err = autorest.Respond(
resp,
a.client.ByInspecting(),
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
autorest.ByUnmarshallingJSON(&outputs),
autorest.ByClosing())
if err != nil {
return nil, err
}
return outputs, nil
}
func (a *azureClientImpl) Get(ctx context.Context, id string, apiVersion string) (any, error) {
queryParameters := map[string]interface{}{
"api-version": apiVersion,
}
preparer := autorest.CreatePreparer(
autorest.AsGet(),
autorest.WithBaseURL(a.environment.ResourceManagerEndpoint),
autorest.WithPath(id),
autorest.WithQueryParameters(queryParameters))
prepReq, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return nil, err
}
resp, err := autorest.SendWithSender(
a.client,
prepReq,
azure.DoRetryWithRegistration(a.client),
)
if err != nil {
return nil, err
}
var outputs any
err = autorest.Respond(
resp,
a.client.ByInspecting(),
azure.WithErrorUnlessStatusCode(http.StatusOK),
forceRequestErrorForStatusNotFound,
autorest.ByUnmarshallingJSON(&outputs),
autorest.ByClosing())
if err != nil {
return nil, err
}
return outputs, nil
}
func (a *azureClientImpl) Head(ctx context.Context, id string, apiVersion string) error {
queryParameters := map[string]interface{}{
"api-version": apiVersion,
}
preparer := autorest.CreatePreparer(
autorest.AsHead(),
autorest.WithBaseURL(a.environment.ResourceManagerEndpoint),
autorest.WithPath(id),
autorest.WithQueryParameters(queryParameters))
prepReq, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return err
}
resp, err := autorest.SendWithSender(
a.client,
prepReq,
azure.DoRetryWithRegistration(a.client),
)
if err != nil {
return err
}
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
forceRequestErrorForStatusNotFound,
autorest.ByClosing())
return err
}
// If a Status Code is 404, always return a request error 'StatusNotFound'. This doesn't work out-of-the-box
// in case the service returns an invalid response that failed to parse into an 'autorest.ServiceError'.
// The parsing error (e.g., 'json.UnmarshalTypeError') would mask the 404 response and the provider wouldn't
// be able to make the right decision for a missing resource.
func forceRequestErrorForStatusNotFound(r autorest.Responder) autorest.Responder {
return autorest.ResponderFunc(func(resp *http.Response) error {
err := r.Respond(resp)
if err == nil || !autorest.ResponseHasStatusCode(resp, http.StatusNotFound) {
return err
}
if _, ok := err.(*azure.RequestError); ok {
return err
}
return &azure.RequestError{
DetailedError: autorest.DetailedError{
Original: err,
StatusCode: http.StatusNotFound,
},
}
})
}
func (a *azureClientImpl) Patch(
ctx context.Context,
id string,
bodyProps map[string]interface{},
queryParameters map[string]interface{},
asyncStyle string,
) (map[string]interface{}, bool, error) {
return a.update(ctx, id, bodyProps, queryParameters, autorest.AsPatch(), asyncStyle)
}
func (a *azureClientImpl) Put(
ctx context.Context,
id string,
bodyProps map[string]interface{},
queryParameters map[string]interface{},
asyncStyle string,
) (map[string]interface{}, bool, error) {
return a.update(ctx, id, bodyProps, queryParameters, autorest.AsPut(), asyncStyle)
}
func (a *azureClientImpl) update(
ctx context.Context,
id string,
bodyProps map[string]interface{},
queryParameters map[string]interface{},
op autorest.PrepareDecorator,
asyncStyle string,
) (map[string]interface{}, bool, error) {
decorators := []autorest.PrepareDecorator{
autorest.AsContentType("application/json; charset=utf-8"),
op,
autorest.WithBaseURL(a.environment.ResourceManagerEndpoint),
autorest.WithPath(id),
autorest.WithQueryParameters(queryParameters),
}
if bodyProps != nil {
decorators = append(decorators, autorest.WithJSON(bodyProps))
}
prepReq, err := autorest.Prepare((&http.Request{}).WithContext(ctx), decorators...)
if err != nil {
return nil, false, err
}
var resp *http.Response
resp, err = autorest.SendWithSender(
a.client,
prepReq,
azure.DoRetryWithRegistration(a.client),
)
if err != nil {
return nil, false, err
}
// Some APIs are explicitly marked `x-ms-long-running-operation` and we are only supposed to do the
// Future+WaitForCompletion+GetResult steps in that case. However, if we get 202, we don't want to
// consider this a failure - so try following the awaiting protocol in case the service hasn't marked
// its API as long-running by an oversight.
created := false
if asyncStyle != "" || resp.StatusCode == http.StatusAccepted {
// We have now created a resource. It is very important to ensure that from this point on,
// any other error below returns the ID using the `pulumirpc.ErrorResourceInitFailed` error
// details annotation. Otherwise, the resource is leaked. We ensure that we wrap any await
// errors as a partial error to the RPC.
created = resp.StatusCode < 400
// Ignore the style value for now, let go-autorest handle the headers.
future, err := azure.NewFutureFromResponse(resp)
if err != nil {
return nil, created, err
}
err = future.WaitForCompletionRef(ctx, a.client)
if err != nil {
return nil, created, err
}
resp, err = future.GetResult(a.client)
if err != nil {
return nil, created, err
}
}
var outputs map[string]interface{}
err = autorest.Respond(
resp,
a.client.ByInspecting(),
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
autorest.ByUnmarshallingJSON(&outputs),
autorest.ByClosing())
if err != nil {
return nil, created, err
}
return outputs, true, nil
}
// CanCreate asserts that a resource with a given ID and API version can be created
// or returns an error otherwise.
func (a *azureClientImpl) CanCreate(ctx context.Context, id, path, apiVersion, readMethod string, isSingletonResource, hasDefaultBody bool, isDefaultResponse func(map[string]any) bool) error {
queryParameters := map[string]interface{}{
"api-version": apiVersion,
}
op := autorest.AsGet()
switch readMethod {
case "HEAD":
op = autorest.AsHead()
case "POST":
op = autorest.AsPost()
}
preparer := autorest.CreatePreparer(
op,
autorest.WithBaseURL(a.environment.ResourceManagerEndpoint),
autorest.WithPath(id+path),
autorest.WithQueryParameters(queryParameters))
prepReq, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return err
}
resp, err := autorest.SendWithSender(
a.client,
prepReq,
azure.DoRetryWithRegistration(a.client),
)
if err != nil {
return err
}
switch {
case http.StatusOK == resp.StatusCode && isSingletonResource:
// Singleton resources always exist, so OK is expected.
return nil
case http.StatusOK == resp.StatusCode && hasDefaultBody:
// This resource is automatically created with a parent and set to its default state. It can be deleted though.
// Validate that its current shape is in the default state to avoid unintended adoption and destructive
// actions.
// NOTE: We may reconsider and relax this restriction when we get more examples of such resources.
// The difference between "take any singleton resource as-is" and "require default body for deletable resources"
// isn't very principled but is based on what subjectively feels best for the current examples.
var outputs map[string]interface{}
err = autorest.Respond(
resp,
a.client.ByInspecting(),
autorest.ByUnmarshallingJSON(&outputs),
autorest.ByClosing())
if err != nil {
return err
}
if !isDefaultResponse(outputs) {
return fmt.Errorf("cannot create already existing subresource '%s'", id)
}
return nil
case http.StatusNoContent == resp.StatusCode:
if readMethod == "HEAD" {
return fmt.Errorf("cannot create already existing resource '%s'", id)
}
// A few "linking" resources, like private endpoint connections, return 204 as "does not exist" status code.
// Treat them as such unless it's a HEAD method treated above.
return nil
case http.StatusOK == resp.StatusCode:
// Usually, 200 means that the resource already exists and we shouldn't try to create it.
// However, unfortunately, some APIs return 200 with an empty body for non-existing resources.
// Our strategy here is to try to parse the response body and see if it's a valid non-empty JSON.
// If it is, we assume the resource exists.
var outputs map[string]interface{}
err = autorest.Respond(
resp,
a.client.ByInspecting(),
autorest.ByUnmarshallingJSON(&outputs),
autorest.ByClosing())
if err == nil && len(outputs) > 0 {
return fmt.Errorf("cannot create already existing resource '%s'", id)
}
return nil
case http.StatusNotFound == resp.StatusCode:
return nil
default:
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("cannot check existence of resource '%s': status code %d, %s", id, resp.StatusCode, body)
}
}
// withInspection is a copy of autorest's LoggingInspector.WithInspector. It uses our glog wrapper
// instead of a go logger which gets complicated in the presence of log redirection via the host.
func withInspection() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
var body, b bytes.Buffer
if r.Body != nil {
defer r.Body.Close()
r.Body = io.NopCloser(io.TeeReader(r.Body, &body))
if err := r.Write(&b); err != nil {
return nil, fmt.Errorf("failed to write response: %v", err)
}
logging.V(9).Infof(requestFormat, r.Method, r.URL, b.String())
r.Body = io.NopCloser(&body)
} else {
logging.V(9).Infof(requestFormat, r.Method, r.URL, "Empty body")
}
return p.Prepare(r)
})
}
}
// byInspecting is acopy of autorest's LoggingInspector.ByInspecting(). It uses our glog wrapper
// instead of a go logger which gets complicated in the presence of log redirection via the host.
func byInspecting() autorest.RespondDecorator {
return func(r autorest.Responder) autorest.Responder {
return autorest.ResponderFunc(func(resp *http.Response) error {
var body, b bytes.Buffer
defer resp.Body.Close()
resp.Body = io.NopCloser(io.TeeReader(resp.Body, &body))
if err := resp.Write(&b); err != nil {
return fmt.Errorf("failed to write response: %v", err)
}
logging.V(9).Infof(responseFormat, resp.Request.Method, resp.Request.URL, b.String())
resp.Body = io.NopCloser(&body)
return r.Respond(resp)
})
}
}