/
client.go
344 lines (327 loc) · 16.4 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
//go:build go1.18
// +build go1.18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
package armquota
import (
"context"
"errors"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"net/http"
"net/url"
"strings"
)
// Client contains the methods for the Quota group.
// Don't use this type directly, use NewClient() instead.
type Client struct {
internal *arm.Client
}
// NewClient creates a new instance of Client with the specified values.
// - credential - used to authorize requests. Usually a credential from azidentity.
// - options - pass nil to accept the default values.
func NewClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*Client, error) {
cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
if err != nil {
return nil, err
}
client := &Client{
internal: cl,
}
return client, nil
}
// BeginCreateOrUpdate - Create or update the quota limit for the specified resource with the requested value. To update the
// quota, follow these steps:
// 1. Use the GET operation for quotas and usages to determine how much quota remains for the specific resource and to calculate
// the new quota limit. These steps are detailed in this example
// [https://techcommunity.microsoft.com/t5/azure-governance-and-management/using-the-new-quota-rest-api/ba-p/2183670].
// 2. Use this PUT operation to update the quota limit. Please check the URI in location header for the detailed status of
// the request.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-02-01
// - resourceName - Resource name for a given resource provider. For example:
// - SKU name for Microsoft.Compute
// - SKU or TotalLowPriorityCores for Microsoft.MachineLearningServices For Microsoft.Network PublicIPAddresses.
// - scope - The target Azure resource URI. For example, /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/qms-test/providers/Microsoft.Batch/batchAccounts/testAccount/.
// This is the target Azure
// resource URI for the List GET operation. If a {resourceName} is added after /quotas, then it's the target Azure resource
// URI in the GET operation for the specific resource.
// - createQuotaRequest - Quota request payload.
// - options - ClientBeginCreateOrUpdateOptions contains the optional parameters for the Client.BeginCreateOrUpdate method.
func (client *Client) BeginCreateOrUpdate(ctx context.Context, resourceName string, scope string, createQuotaRequest CurrentQuotaLimitBase, options *ClientBeginCreateOrUpdateOptions) (*runtime.Poller[ClientCreateOrUpdateResponse], error) {
if options == nil || options.ResumeToken == "" {
resp, err := client.createOrUpdate(ctx, resourceName, scope, createQuotaRequest, options)
if err != nil {
return nil, err
}
poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ClientCreateOrUpdateResponse]{
FinalStateVia: runtime.FinalStateViaOriginalURI,
Tracer: client.internal.Tracer(),
})
return poller, err
} else {
return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ClientCreateOrUpdateResponse]{
Tracer: client.internal.Tracer(),
})
}
}
// CreateOrUpdate - Create or update the quota limit for the specified resource with the requested value. To update the quota,
// follow these steps:
// 1. Use the GET operation for quotas and usages to determine how much quota remains for the specific resource and to calculate
// the new quota limit. These steps are detailed in this example
// [https://techcommunity.microsoft.com/t5/azure-governance-and-management/using-the-new-quota-rest-api/ba-p/2183670].
// 2. Use this PUT operation to update the quota limit. Please check the URI in location header for the detailed status of
// the request.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-02-01
func (client *Client) createOrUpdate(ctx context.Context, resourceName string, scope string, createQuotaRequest CurrentQuotaLimitBase, options *ClientBeginCreateOrUpdateOptions) (*http.Response, error) {
var err error
const operationName = "Client.BeginCreateOrUpdate"
ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
defer func() { endSpan(err) }()
req, err := client.createOrUpdateCreateRequest(ctx, resourceName, scope, createQuotaRequest, options)
if err != nil {
return nil, err
}
httpResp, err := client.internal.Pipeline().Do(req)
if err != nil {
return nil, err
}
if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
err = runtime.NewResponseError(httpResp)
return nil, err
}
return httpResp, nil
}
// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *Client) createOrUpdateCreateRequest(ctx context.Context, resourceName string, scope string, createQuotaRequest CurrentQuotaLimitBase, options *ClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
urlPath := "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}"
if resourceName == "" {
return nil, errors.New("parameter resourceName cannot be empty")
}
urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
if err != nil {
return nil, err
}
reqQP := req.Raw().URL.Query()
reqQP.Set("api-version", "2023-02-01")
req.Raw().URL.RawQuery = reqQP.Encode()
req.Raw().Header["Accept"] = []string{"application/json"}
if err := runtime.MarshalAsJSON(req, createQuotaRequest); err != nil {
return nil, err
}
return req, nil
}
// Get - Get the quota limit of a resource. The response can be used to determine the remaining quota to calculate a new quota
// limit that can be submitted with a PUT request.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-02-01
// - resourceName - Resource name for a given resource provider. For example:
// - SKU name for Microsoft.Compute
// - SKU or TotalLowPriorityCores for Microsoft.MachineLearningServices For Microsoft.Network PublicIPAddresses.
// - scope - The target Azure resource URI. For example, /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/qms-test/providers/Microsoft.Batch/batchAccounts/testAccount/.
// This is the target Azure
// resource URI for the List GET operation. If a {resourceName} is added after /quotas, then it's the target Azure resource
// URI in the GET operation for the specific resource.
// - options - ClientGetOptions contains the optional parameters for the Client.Get method.
func (client *Client) Get(ctx context.Context, resourceName string, scope string, options *ClientGetOptions) (ClientGetResponse, error) {
var err error
const operationName = "Client.Get"
ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
defer func() { endSpan(err) }()
req, err := client.getCreateRequest(ctx, resourceName, scope, options)
if err != nil {
return ClientGetResponse{}, err
}
httpResp, err := client.internal.Pipeline().Do(req)
if err != nil {
return ClientGetResponse{}, err
}
if !runtime.HasStatusCode(httpResp, http.StatusOK) {
err = runtime.NewResponseError(httpResp)
return ClientGetResponse{}, err
}
resp, err := client.getHandleResponse(httpResp)
return resp, err
}
// getCreateRequest creates the Get request.
func (client *Client) getCreateRequest(ctx context.Context, resourceName string, scope string, options *ClientGetOptions) (*policy.Request, error) {
urlPath := "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}"
if resourceName == "" {
return nil, errors.New("parameter resourceName cannot be empty")
}
urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
if err != nil {
return nil, err
}
reqQP := req.Raw().URL.Query()
reqQP.Set("api-version", "2023-02-01")
req.Raw().URL.RawQuery = reqQP.Encode()
req.Raw().Header["Accept"] = []string{"application/json"}
return req, nil
}
// getHandleResponse handles the Get response.
func (client *Client) getHandleResponse(resp *http.Response) (ClientGetResponse, error) {
result := ClientGetResponse{}
if val := resp.Header.Get("ETag"); val != "" {
result.ETag = &val
}
if err := runtime.UnmarshalAsJSON(resp, &result.CurrentQuotaLimitBase); err != nil {
return ClientGetResponse{}, err
}
return result, nil
}
// NewListPager - Get a list of current quota limits of all resources for the specified scope. The response from this GET
// operation can be leveraged to submit requests to update a quota.
//
// Generated from API version 2023-02-01
// - scope - The target Azure resource URI. For example, /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/qms-test/providers/Microsoft.Batch/batchAccounts/testAccount/.
// This is the target Azure
// resource URI for the List GET operation. If a {resourceName} is added after /quotas, then it's the target Azure resource
// URI in the GET operation for the specific resource.
// - options - ClientListOptions contains the optional parameters for the Client.NewListPager method.
func (client *Client) NewListPager(scope string, options *ClientListOptions) *runtime.Pager[ClientListResponse] {
return runtime.NewPager(runtime.PagingHandler[ClientListResponse]{
More: func(page ClientListResponse) bool {
return page.NextLink != nil && len(*page.NextLink) > 0
},
Fetcher: func(ctx context.Context, page *ClientListResponse) (ClientListResponse, error) {
ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "Client.NewListPager")
nextLink := ""
if page != nil {
nextLink = *page.NextLink
}
resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
return client.listCreateRequest(ctx, scope, options)
}, nil)
if err != nil {
return ClientListResponse{}, err
}
return client.listHandleResponse(resp)
},
Tracer: client.internal.Tracer(),
})
}
// listCreateRequest creates the List request.
func (client *Client) listCreateRequest(ctx context.Context, scope string, options *ClientListOptions) (*policy.Request, error) {
urlPath := "/{scope}/providers/Microsoft.Quota/quotas"
urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
if err != nil {
return nil, err
}
reqQP := req.Raw().URL.Query()
reqQP.Set("api-version", "2023-02-01")
req.Raw().URL.RawQuery = reqQP.Encode()
req.Raw().Header["Accept"] = []string{"application/json"}
return req, nil
}
// listHandleResponse handles the List response.
func (client *Client) listHandleResponse(resp *http.Response) (ClientListResponse, error) {
result := ClientListResponse{}
if val := resp.Header.Get("ETag"); val != "" {
result.ETag = &val
}
if err := runtime.UnmarshalAsJSON(resp, &result.Limits); err != nil {
return ClientListResponse{}, err
}
return result, nil
}
// BeginUpdate - Update the quota limit for a specific resource to the specified value:
// 1. Use the Usages-GET and Quota-GET operations to determine the remaining quota for the specific resource and to calculate
// the new quota limit. These steps are detailed in this example
// [https://techcommunity.microsoft.com/t5/azure-governance-and-management/using-the-new-quota-rest-api/ba-p/2183670].
// 2. Use this PUT operation to update the quota limit. Please check the URI in location header for the detailed status of
// the request.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-02-01
// - resourceName - Resource name for a given resource provider. For example:
// - SKU name for Microsoft.Compute
// - SKU or TotalLowPriorityCores for Microsoft.MachineLearningServices For Microsoft.Network PublicIPAddresses.
// - scope - The target Azure resource URI. For example, /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/qms-test/providers/Microsoft.Batch/batchAccounts/testAccount/.
// This is the target Azure
// resource URI for the List GET operation. If a {resourceName} is added after /quotas, then it's the target Azure resource
// URI in the GET operation for the specific resource.
// - createQuotaRequest - Quota requests payload.
// - options - ClientBeginUpdateOptions contains the optional parameters for the Client.BeginUpdate method.
func (client *Client) BeginUpdate(ctx context.Context, resourceName string, scope string, createQuotaRequest CurrentQuotaLimitBase, options *ClientBeginUpdateOptions) (*runtime.Poller[ClientUpdateResponse], error) {
if options == nil || options.ResumeToken == "" {
resp, err := client.update(ctx, resourceName, scope, createQuotaRequest, options)
if err != nil {
return nil, err
}
poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[ClientUpdateResponse]{
FinalStateVia: runtime.FinalStateViaOriginalURI,
Tracer: client.internal.Tracer(),
})
return poller, err
} else {
return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[ClientUpdateResponse]{
Tracer: client.internal.Tracer(),
})
}
}
// Update - Update the quota limit for a specific resource to the specified value:
// 1. Use the Usages-GET and Quota-GET operations to determine the remaining quota for the specific resource and to calculate
// the new quota limit. These steps are detailed in this example
// [https://techcommunity.microsoft.com/t5/azure-governance-and-management/using-the-new-quota-rest-api/ba-p/2183670].
// 2. Use this PUT operation to update the quota limit. Please check the URI in location header for the detailed status of
// the request.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-02-01
func (client *Client) update(ctx context.Context, resourceName string, scope string, createQuotaRequest CurrentQuotaLimitBase, options *ClientBeginUpdateOptions) (*http.Response, error) {
var err error
const operationName = "Client.BeginUpdate"
ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
defer func() { endSpan(err) }()
req, err := client.updateCreateRequest(ctx, resourceName, scope, createQuotaRequest, options)
if err != nil {
return nil, err
}
httpResp, err := client.internal.Pipeline().Do(req)
if err != nil {
return nil, err
}
if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
err = runtime.NewResponseError(httpResp)
return nil, err
}
return httpResp, nil
}
// updateCreateRequest creates the Update request.
func (client *Client) updateCreateRequest(ctx context.Context, resourceName string, scope string, createQuotaRequest CurrentQuotaLimitBase, options *ClientBeginUpdateOptions) (*policy.Request, error) {
urlPath := "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}"
if resourceName == "" {
return nil, errors.New("parameter resourceName cannot be empty")
}
urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
if err != nil {
return nil, err
}
reqQP := req.Raw().URL.Query()
reqQP.Set("api-version", "2023-02-01")
req.Raw().URL.RawQuery = reqQP.Encode()
req.Raw().Header["Accept"] = []string{"application/json"}
if err := runtime.MarshalAsJSON(req, createQuotaRequest); err != nil {
return nil, err
}
return req, nil
}