1 package consumption
2
3
4
5
6
7
8
9 import (
10 "context"
11 "github.com/Azure/go-autorest/autorest"
12 "github.com/Azure/go-autorest/autorest/azure"
13 "github.com/Azure/go-autorest/autorest/validation"
14 "github.com/Azure/go-autorest/tracing"
15 "net/http"
16 )
17
18
19
20 type BudgetsClient struct {
21 BaseClient
22 }
23
24
25 func NewBudgetsClient(subscriptionID string) BudgetsClient {
26 return NewBudgetsClientWithBaseURI(DefaultBaseURI, subscriptionID)
27 }
28
29
30
31 func NewBudgetsClientWithBaseURI(baseURI string, subscriptionID string) BudgetsClient {
32 return BudgetsClient{NewWithBaseURI(baseURI, subscriptionID)}
33 }
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 func (client BudgetsClient) CreateOrUpdate(ctx context.Context, scope string, budgetName string, parameters Budget) (result Budget, err error) {
54 if tracing.IsEnabled() {
55 ctx = tracing.StartSpan(ctx, fqdn+"/BudgetsClient.CreateOrUpdate")
56 defer func() {
57 sc := -1
58 if result.Response.Response != nil {
59 sc = result.Response.Response.StatusCode
60 }
61 tracing.EndSpan(ctx, sc, err)
62 }()
63 }
64 if err := validation.Validate([]validation.Validation{
65 {TargetValue: parameters,
66 Constraints: []validation.Constraint{{Target: "parameters.BudgetProperties", Name: validation.Null, Rule: false,
67 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Category", Name: validation.Null, Rule: true, Chain: nil},
68 {Target: "parameters.BudgetProperties.Amount", Name: validation.Null, Rule: true, Chain: nil},
69 {Target: "parameters.BudgetProperties.TimePeriod", Name: validation.Null, Rule: true,
70 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.TimePeriod.StartDate", Name: validation.Null, Rule: true, Chain: nil}}},
71 {Target: "parameters.BudgetProperties.Filter", Name: validation.Null, Rule: false,
72 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.And", Name: validation.Null, Rule: false,
73 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.And", Name: validation.MinItems, Rule: 0, Chain: nil}}},
74 {Target: "parameters.BudgetProperties.Filter.Not", Name: validation.Null, Rule: false,
75 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Not.Dimensions", Name: validation.Null, Rule: false,
76 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Not.Dimensions.Name", Name: validation.Null, Rule: true, Chain: nil},
77 {Target: "parameters.BudgetProperties.Filter.Not.Dimensions.Operator", Name: validation.Null, Rule: true, Chain: nil},
78 {Target: "parameters.BudgetProperties.Filter.Not.Dimensions.Values", Name: validation.Null, Rule: true,
79 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Not.Dimensions.Values", Name: validation.MinItems, Rule: 0, Chain: nil}}},
80 }},
81 {Target: "parameters.BudgetProperties.Filter.Not.Tags", Name: validation.Null, Rule: false,
82 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Not.Tags.Name", Name: validation.Null, Rule: true, Chain: nil},
83 {Target: "parameters.BudgetProperties.Filter.Not.Tags.Operator", Name: validation.Null, Rule: true, Chain: nil},
84 {Target: "parameters.BudgetProperties.Filter.Not.Tags.Values", Name: validation.Null, Rule: true,
85 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Not.Tags.Values", Name: validation.MinItems, Rule: 0, Chain: nil}}},
86 }},
87 }},
88 {Target: "parameters.BudgetProperties.Filter.Dimensions", Name: validation.Null, Rule: false,
89 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Dimensions.Name", Name: validation.Null, Rule: true, Chain: nil},
90 {Target: "parameters.BudgetProperties.Filter.Dimensions.Operator", Name: validation.Null, Rule: true, Chain: nil},
91 {Target: "parameters.BudgetProperties.Filter.Dimensions.Values", Name: validation.Null, Rule: true,
92 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Dimensions.Values", Name: validation.MinItems, Rule: 0, Chain: nil}}},
93 }},
94 {Target: "parameters.BudgetProperties.Filter.Tags", Name: validation.Null, Rule: false,
95 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Tags.Name", Name: validation.Null, Rule: true, Chain: nil},
96 {Target: "parameters.BudgetProperties.Filter.Tags.Operator", Name: validation.Null, Rule: true, Chain: nil},
97 {Target: "parameters.BudgetProperties.Filter.Tags.Values", Name: validation.Null, Rule: true,
98 Chain: []validation.Constraint{{Target: "parameters.BudgetProperties.Filter.Tags.Values", Name: validation.MinItems, Rule: 0, Chain: nil}}},
99 }},
100 }},
101 }}}}}); err != nil {
102 return result, validation.NewError("consumption.BudgetsClient", "CreateOrUpdate", err.Error())
103 }
104
105 req, err := client.CreateOrUpdatePreparer(ctx, scope, budgetName, parameters)
106 if err != nil {
107 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "CreateOrUpdate", nil, "Failure preparing request")
108 return
109 }
110
111 resp, err := client.CreateOrUpdateSender(req)
112 if err != nil {
113 result.Response = autorest.Response{Response: resp}
114 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "CreateOrUpdate", resp, "Failure sending request")
115 return
116 }
117
118 result, err = client.CreateOrUpdateResponder(resp)
119 if err != nil {
120 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "CreateOrUpdate", resp, "Failure responding to request")
121 return
122 }
123
124 return
125 }
126
127
128 func (client BudgetsClient) CreateOrUpdatePreparer(ctx context.Context, scope string, budgetName string, parameters Budget) (*http.Request, error) {
129 pathParameters := map[string]interface{}{
130 "budgetName": autorest.Encode("path", budgetName),
131 "scope": scope,
132 }
133
134 const APIVersion = "2019-10-01"
135 queryParameters := map[string]interface{}{
136 "api-version": APIVersion,
137 }
138
139 preparer := autorest.CreatePreparer(
140 autorest.AsContentType("application/json; charset=utf-8"),
141 autorest.AsPut(),
142 autorest.WithBaseURL(client.BaseURI),
143 autorest.WithPathParameters("/{scope}/providers/Microsoft.Consumption/budgets/{budgetName}", pathParameters),
144 autorest.WithJSON(parameters),
145 autorest.WithQueryParameters(queryParameters))
146 return preparer.Prepare((&http.Request{}).WithContext(ctx))
147 }
148
149
150
151 func (client BudgetsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
152 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
153 }
154
155
156
157 func (client BudgetsClient) CreateOrUpdateResponder(resp *http.Response) (result Budget, err error) {
158 err = autorest.Respond(
159 resp,
160 azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
161 autorest.ByUnmarshallingJSON(&result),
162 autorest.ByClosing())
163 result.Response = autorest.Response{Response: resp}
164 return
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 func (client BudgetsClient) Delete(ctx context.Context, scope string, budgetName string) (result autorest.Response, err error) {
183 if tracing.IsEnabled() {
184 ctx = tracing.StartSpan(ctx, fqdn+"/BudgetsClient.Delete")
185 defer func() {
186 sc := -1
187 if result.Response != nil {
188 sc = result.Response.StatusCode
189 }
190 tracing.EndSpan(ctx, sc, err)
191 }()
192 }
193 req, err := client.DeletePreparer(ctx, scope, budgetName)
194 if err != nil {
195 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "Delete", nil, "Failure preparing request")
196 return
197 }
198
199 resp, err := client.DeleteSender(req)
200 if err != nil {
201 result.Response = resp
202 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "Delete", resp, "Failure sending request")
203 return
204 }
205
206 result, err = client.DeleteResponder(resp)
207 if err != nil {
208 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "Delete", resp, "Failure responding to request")
209 return
210 }
211
212 return
213 }
214
215
216 func (client BudgetsClient) DeletePreparer(ctx context.Context, scope string, budgetName string) (*http.Request, error) {
217 pathParameters := map[string]interface{}{
218 "budgetName": autorest.Encode("path", budgetName),
219 "scope": scope,
220 }
221
222 const APIVersion = "2019-10-01"
223 queryParameters := map[string]interface{}{
224 "api-version": APIVersion,
225 }
226
227 preparer := autorest.CreatePreparer(
228 autorest.AsDelete(),
229 autorest.WithBaseURL(client.BaseURI),
230 autorest.WithPathParameters("/{scope}/providers/Microsoft.Consumption/budgets/{budgetName}", pathParameters),
231 autorest.WithQueryParameters(queryParameters))
232 return preparer.Prepare((&http.Request{}).WithContext(ctx))
233 }
234
235
236
237 func (client BudgetsClient) DeleteSender(req *http.Request) (*http.Response, error) {
238 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
239 }
240
241
242
243 func (client BudgetsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
244 err = autorest.Respond(
245 resp,
246 azure.WithErrorUnlessStatusCode(http.StatusOK),
247 autorest.ByClosing())
248 result.Response = resp
249 return
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267 func (client BudgetsClient) Get(ctx context.Context, scope string, budgetName string) (result Budget, err error) {
268 if tracing.IsEnabled() {
269 ctx = tracing.StartSpan(ctx, fqdn+"/BudgetsClient.Get")
270 defer func() {
271 sc := -1
272 if result.Response.Response != nil {
273 sc = result.Response.Response.StatusCode
274 }
275 tracing.EndSpan(ctx, sc, err)
276 }()
277 }
278 req, err := client.GetPreparer(ctx, scope, budgetName)
279 if err != nil {
280 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "Get", nil, "Failure preparing request")
281 return
282 }
283
284 resp, err := client.GetSender(req)
285 if err != nil {
286 result.Response = autorest.Response{Response: resp}
287 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "Get", resp, "Failure sending request")
288 return
289 }
290
291 result, err = client.GetResponder(resp)
292 if err != nil {
293 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "Get", resp, "Failure responding to request")
294 return
295 }
296
297 return
298 }
299
300
301 func (client BudgetsClient) GetPreparer(ctx context.Context, scope string, budgetName string) (*http.Request, error) {
302 pathParameters := map[string]interface{}{
303 "budgetName": autorest.Encode("path", budgetName),
304 "scope": scope,
305 }
306
307 const APIVersion = "2019-10-01"
308 queryParameters := map[string]interface{}{
309 "api-version": APIVersion,
310 }
311
312 preparer := autorest.CreatePreparer(
313 autorest.AsGet(),
314 autorest.WithBaseURL(client.BaseURI),
315 autorest.WithPathParameters("/{scope}/providers/Microsoft.Consumption/budgets/{budgetName}", pathParameters),
316 autorest.WithQueryParameters(queryParameters))
317 return preparer.Prepare((&http.Request{}).WithContext(ctx))
318 }
319
320
321
322 func (client BudgetsClient) GetSender(req *http.Request) (*http.Response, error) {
323 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
324 }
325
326
327
328 func (client BudgetsClient) GetResponder(resp *http.Response) (result Budget, err error) {
329 err = autorest.Respond(
330 resp,
331 azure.WithErrorUnlessStatusCode(http.StatusOK),
332 autorest.ByUnmarshallingJSON(&result),
333 autorest.ByClosing())
334 result.Response = autorest.Response{Response: resp}
335 return
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 func (client BudgetsClient) List(ctx context.Context, scope string) (result BudgetsListResultPage, err error) {
353 if tracing.IsEnabled() {
354 ctx = tracing.StartSpan(ctx, fqdn+"/BudgetsClient.List")
355 defer func() {
356 sc := -1
357 if result.blr.Response.Response != nil {
358 sc = result.blr.Response.Response.StatusCode
359 }
360 tracing.EndSpan(ctx, sc, err)
361 }()
362 }
363 result.fn = client.listNextResults
364 req, err := client.ListPreparer(ctx, scope)
365 if err != nil {
366 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "List", nil, "Failure preparing request")
367 return
368 }
369
370 resp, err := client.ListSender(req)
371 if err != nil {
372 result.blr.Response = autorest.Response{Response: resp}
373 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "List", resp, "Failure sending request")
374 return
375 }
376
377 result.blr, err = client.ListResponder(resp)
378 if err != nil {
379 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "List", resp, "Failure responding to request")
380 return
381 }
382 if result.blr.hasNextLink() && result.blr.IsEmpty() {
383 err = result.NextWithContext(ctx)
384 return
385 }
386
387 return
388 }
389
390
391 func (client BudgetsClient) ListPreparer(ctx context.Context, scope string) (*http.Request, error) {
392 pathParameters := map[string]interface{}{
393 "scope": scope,
394 }
395
396 const APIVersion = "2019-10-01"
397 queryParameters := map[string]interface{}{
398 "api-version": APIVersion,
399 }
400
401 preparer := autorest.CreatePreparer(
402 autorest.AsGet(),
403 autorest.WithBaseURL(client.BaseURI),
404 autorest.WithPathParameters("/{scope}/providers/Microsoft.Consumption/budgets", pathParameters),
405 autorest.WithQueryParameters(queryParameters))
406 return preparer.Prepare((&http.Request{}).WithContext(ctx))
407 }
408
409
410
411 func (client BudgetsClient) ListSender(req *http.Request) (*http.Response, error) {
412 return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
413 }
414
415
416
417 func (client BudgetsClient) ListResponder(resp *http.Response) (result BudgetsListResult, err error) {
418 err = autorest.Respond(
419 resp,
420 azure.WithErrorUnlessStatusCode(http.StatusOK),
421 autorest.ByUnmarshallingJSON(&result),
422 autorest.ByClosing())
423 result.Response = autorest.Response{Response: resp}
424 return
425 }
426
427
428 func (client BudgetsClient) listNextResults(ctx context.Context, lastResults BudgetsListResult) (result BudgetsListResult, err error) {
429 req, err := lastResults.budgetsListResultPreparer(ctx)
430 if err != nil {
431 return result, autorest.NewErrorWithError(err, "consumption.BudgetsClient", "listNextResults", nil, "Failure preparing next results request")
432 }
433 if req == nil {
434 return
435 }
436 resp, err := client.ListSender(req)
437 if err != nil {
438 result.Response = autorest.Response{Response: resp}
439 return result, autorest.NewErrorWithError(err, "consumption.BudgetsClient", "listNextResults", resp, "Failure sending next results request")
440 }
441 result, err = client.ListResponder(resp)
442 if err != nil {
443 err = autorest.NewErrorWithError(err, "consumption.BudgetsClient", "listNextResults", resp, "Failure responding to next results request")
444 }
445 return
446 }
447
448
449 func (client BudgetsClient) ListComplete(ctx context.Context, scope string) (result BudgetsListResultIterator, err error) {
450 if tracing.IsEnabled() {
451 ctx = tracing.StartSpan(ctx, fqdn+"/BudgetsClient.List")
452 defer func() {
453 sc := -1
454 if result.Response().Response.Response != nil {
455 sc = result.page.Response().Response.Response.StatusCode
456 }
457 tracing.EndSpan(ctx, sc, err)
458 }()
459 }
460 result.page, err = client.List(ctx, scope)
461 return
462 }
463
View as plain text