1package reservations
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5//
6// Code generated by Microsoft (R) AutoRest Code Generator.
7// Changes may cause incorrect behavior and will be lost if the code is regenerated.
8
9import (
10	"context"
11	"github.com/Azure/go-autorest/autorest"
12	"github.com/Azure/go-autorest/autorest/azure"
13	"github.com/Azure/go-autorest/tracing"
14	"net/http"
15)
16
17// QuotaClient is the client for the Quota methods of the Reservations service.
18type QuotaClient struct {
19	BaseClient
20}
21
22// NewQuotaClient creates an instance of the QuotaClient client.
23func NewQuotaClient() QuotaClient {
24	return NewQuotaClientWithBaseURI(DefaultBaseURI)
25}
26
27// NewQuotaClientWithBaseURI creates an instance of the QuotaClient client using a custom endpoint.  Use this when
28// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewQuotaClientWithBaseURI(baseURI string) QuotaClient {
30	return QuotaClient{NewWithBaseURI(baseURI)}
31}
32
33// CreateOrUpdate create or update the service limits (quota) of a resource to requested value.
34// Steps:
35//
36// 1. Make the Get request to get the quota information for specific resource.
37//
38// 2. To increase the quota, update the limit field in the response from Get request to new value.
39//
40// 3. Submit the JSON to the quota request API to update the quota.
41// The Create quota request may be constructed as follows. The PUT operation can be used to update the quota.
42// Parameters:
43// subscriptionID - azure subscription id.
44// providerID - azure resource provider id.
45// location - azure region.
46// resourceName - the resource name for a resource provider, such as SKU name for Microsoft.Compute, Sku or
47// TotalLowPriorityCores for Microsoft.MachineLearningServices
48// createQuotaRequest - quota requests payload.
49func (client QuotaClient) CreateOrUpdate(ctx context.Context, subscriptionID string, providerID string, location string, resourceName string, createQuotaRequest CurrentQuotaLimitBase) (result QuotaCreateOrUpdateFuture, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaClient.CreateOrUpdate")
52		defer func() {
53			sc := -1
54			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
55				sc = result.FutureAPI.Response().StatusCode
56			}
57			tracing.EndSpan(ctx, sc, err)
58		}()
59	}
60	req, err := client.CreateOrUpdatePreparer(ctx, subscriptionID, providerID, location, resourceName, createQuotaRequest)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "CreateOrUpdate", nil, "Failure preparing request")
63		return
64	}
65
66	result, err = client.CreateOrUpdateSender(req)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "CreateOrUpdate", nil, "Failure sending request")
69		return
70	}
71
72	return
73}
74
75// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
76func (client QuotaClient) CreateOrUpdatePreparer(ctx context.Context, subscriptionID string, providerID string, location string, resourceName string, createQuotaRequest CurrentQuotaLimitBase) (*http.Request, error) {
77	pathParameters := map[string]interface{}{
78		"location":       autorest.Encode("path", location),
79		"providerId":     autorest.Encode("path", providerID),
80		"resourceName":   autorest.Encode("path", resourceName),
81		"subscriptionId": autorest.Encode("path", subscriptionID),
82	}
83
84	const APIVersion = "2019-07-19-preview"
85	queryParameters := map[string]interface{}{
86		"api-version": APIVersion,
87	}
88
89	preparer := autorest.CreatePreparer(
90		autorest.AsContentType("application/json; charset=utf-8"),
91		autorest.AsPut(),
92		autorest.WithBaseURL(client.BaseURI),
93		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Capacity/resourceProviders/{providerId}/locations/{location}/serviceLimits/{resourceName}", pathParameters),
94		autorest.WithJSON(createQuotaRequest),
95		autorest.WithQueryParameters(queryParameters))
96	return preparer.Prepare((&http.Request{}).WithContext(ctx))
97}
98
99// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
100// http.Response Body if it receives an error.
101func (client QuotaClient) CreateOrUpdateSender(req *http.Request) (future QuotaCreateOrUpdateFuture, err error) {
102	var resp *http.Response
103	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
104	if err != nil {
105		return
106	}
107	var azf azure.Future
108	azf, err = azure.NewFutureFromResponse(resp)
109	future.FutureAPI = &azf
110	future.Result = future.result
111	return
112}
113
114// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
115// closes the http.Response Body.
116func (client QuotaClient) CreateOrUpdateResponder(resp *http.Response) (result SetObject, err error) {
117	err = autorest.Respond(
118		resp,
119		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
120		autorest.ByUnmarshallingJSON(&result),
121		autorest.ByClosing())
122	result.Response = autorest.Response{Response: resp}
123	return
124}
125
126// Get gets the current service limits (quotas) and usage of a resource. The response from Get API can be leveraged to
127// submit quota update requests.
128// Parameters:
129// subscriptionID - azure subscription id.
130// providerID - azure resource provider id.
131// location - azure region.
132// resourceName - the resource name for a resource provider, such as SKU name for Microsoft.Compute, Sku or
133// TotalLowPriorityCores for Microsoft.MachineLearningServices
134func (client QuotaClient) Get(ctx context.Context, subscriptionID string, providerID string, location string, resourceName string) (result CurrentQuotaLimitBase, err error) {
135	if tracing.IsEnabled() {
136		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaClient.Get")
137		defer func() {
138			sc := -1
139			if result.Response.Response != nil {
140				sc = result.Response.Response.StatusCode
141			}
142			tracing.EndSpan(ctx, sc, err)
143		}()
144	}
145	req, err := client.GetPreparer(ctx, subscriptionID, providerID, location, resourceName)
146	if err != nil {
147		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "Get", nil, "Failure preparing request")
148		return
149	}
150
151	resp, err := client.GetSender(req)
152	if err != nil {
153		result.Response = autorest.Response{Response: resp}
154		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "Get", resp, "Failure sending request")
155		return
156	}
157
158	result, err = client.GetResponder(resp)
159	if err != nil {
160		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "Get", resp, "Failure responding to request")
161		return
162	}
163
164	return
165}
166
167// GetPreparer prepares the Get request.
168func (client QuotaClient) GetPreparer(ctx context.Context, subscriptionID string, providerID string, location string, resourceName string) (*http.Request, error) {
169	pathParameters := map[string]interface{}{
170		"location":       autorest.Encode("path", location),
171		"providerId":     autorest.Encode("path", providerID),
172		"resourceName":   autorest.Encode("path", resourceName),
173		"subscriptionId": autorest.Encode("path", subscriptionID),
174	}
175
176	const APIVersion = "2019-07-19-preview"
177	queryParameters := map[string]interface{}{
178		"api-version": APIVersion,
179	}
180
181	preparer := autorest.CreatePreparer(
182		autorest.AsGet(),
183		autorest.WithBaseURL(client.BaseURI),
184		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Capacity/resourceProviders/{providerId}/locations/{location}/serviceLimits/{resourceName}", pathParameters),
185		autorest.WithQueryParameters(queryParameters))
186	return preparer.Prepare((&http.Request{}).WithContext(ctx))
187}
188
189// GetSender sends the Get request. The method will close the
190// http.Response Body if it receives an error.
191func (client QuotaClient) GetSender(req *http.Request) (*http.Response, error) {
192	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
193}
194
195// GetResponder handles the response to the Get request. The method always
196// closes the http.Response Body.
197func (client QuotaClient) GetResponder(resp *http.Response) (result CurrentQuotaLimitBase, err error) {
198	err = autorest.Respond(
199		resp,
200		azure.WithErrorUnlessStatusCode(http.StatusOK),
201		autorest.ByUnmarshallingJSON(&result),
202		autorest.ByClosing())
203	result.Response = autorest.Response{Response: resp}
204	return
205}
206
207// List get a list of current service limits (quota) and usages of all the resources. The response from List API can be
208// leveraged to submit quota update requests.
209// Parameters:
210// subscriptionID - azure subscription id.
211// providerID - azure resource provider id.
212// location - azure region.
213func (client QuotaClient) List(ctx context.Context, subscriptionID string, providerID string, location string) (result QuotaLimitsPage, err error) {
214	if tracing.IsEnabled() {
215		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaClient.List")
216		defer func() {
217			sc := -1
218			if result.ql.Response.Response != nil {
219				sc = result.ql.Response.Response.StatusCode
220			}
221			tracing.EndSpan(ctx, sc, err)
222		}()
223	}
224	result.fn = client.listNextResults
225	req, err := client.ListPreparer(ctx, subscriptionID, providerID, location)
226	if err != nil {
227		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "List", nil, "Failure preparing request")
228		return
229	}
230
231	resp, err := client.ListSender(req)
232	if err != nil {
233		result.ql.Response = autorest.Response{Response: resp}
234		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "List", resp, "Failure sending request")
235		return
236	}
237
238	result.ql, err = client.ListResponder(resp)
239	if err != nil {
240		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "List", resp, "Failure responding to request")
241		return
242	}
243	if result.ql.hasNextLink() && result.ql.IsEmpty() {
244		err = result.NextWithContext(ctx)
245		return
246	}
247
248	return
249}
250
251// ListPreparer prepares the List request.
252func (client QuotaClient) ListPreparer(ctx context.Context, subscriptionID string, providerID string, location string) (*http.Request, error) {
253	pathParameters := map[string]interface{}{
254		"location":       autorest.Encode("path", location),
255		"providerId":     autorest.Encode("path", providerID),
256		"subscriptionId": autorest.Encode("path", subscriptionID),
257	}
258
259	const APIVersion = "2019-07-19-preview"
260	queryParameters := map[string]interface{}{
261		"api-version": APIVersion,
262	}
263
264	preparer := autorest.CreatePreparer(
265		autorest.AsGet(),
266		autorest.WithBaseURL(client.BaseURI),
267		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Capacity/resourceProviders/{providerId}/locations/{location}/serviceLimits", pathParameters),
268		autorest.WithQueryParameters(queryParameters))
269	return preparer.Prepare((&http.Request{}).WithContext(ctx))
270}
271
272// ListSender sends the List request. The method will close the
273// http.Response Body if it receives an error.
274func (client QuotaClient) ListSender(req *http.Request) (*http.Response, error) {
275	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
276}
277
278// ListResponder handles the response to the List request. The method always
279// closes the http.Response Body.
280func (client QuotaClient) ListResponder(resp *http.Response) (result QuotaLimits, err error) {
281	err = autorest.Respond(
282		resp,
283		azure.WithErrorUnlessStatusCode(http.StatusOK),
284		autorest.ByUnmarshallingJSON(&result),
285		autorest.ByClosing())
286	result.Response = autorest.Response{Response: resp}
287	return
288}
289
290// listNextResults retrieves the next set of results, if any.
291func (client QuotaClient) listNextResults(ctx context.Context, lastResults QuotaLimits) (result QuotaLimits, err error) {
292	req, err := lastResults.quotaLimitsPreparer(ctx)
293	if err != nil {
294		return result, autorest.NewErrorWithError(err, "reservations.QuotaClient", "listNextResults", nil, "Failure preparing next results request")
295	}
296	if req == nil {
297		return
298	}
299	resp, err := client.ListSender(req)
300	if err != nil {
301		result.Response = autorest.Response{Response: resp}
302		return result, autorest.NewErrorWithError(err, "reservations.QuotaClient", "listNextResults", resp, "Failure sending next results request")
303	}
304	result, err = client.ListResponder(resp)
305	if err != nil {
306		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "listNextResults", resp, "Failure responding to next results request")
307	}
308	return
309}
310
311// ListComplete enumerates all values, automatically crossing page boundaries as required.
312func (client QuotaClient) ListComplete(ctx context.Context, subscriptionID string, providerID string, location string) (result QuotaLimitsIterator, err error) {
313	if tracing.IsEnabled() {
314		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaClient.List")
315		defer func() {
316			sc := -1
317			if result.Response().Response.Response != nil {
318				sc = result.page.Response().Response.Response.StatusCode
319			}
320			tracing.EndSpan(ctx, sc, err)
321		}()
322	}
323	result.page, err = client.List(ctx, subscriptionID, providerID, location)
324	return
325}
326
327// Update update the service limits (quota) of a resource to requested value.
328// Steps:
329//
330// 1. Make the Get request to get the quota information for specific resource.
331//
332// 2. To increase the quota, update the limit field in the response from Get request to new value.
333//
334// 3. Submit the JSON to the quota request API to update the quota.
335// The Update quota request may be constructed as follows. The PATCH operation can be used to update the quota.
336// Parameters:
337// subscriptionID - azure subscription id.
338// providerID - azure resource provider id.
339// location - azure region.
340// resourceName - the resource name for a resource provider, such as SKU name for Microsoft.Compute, Sku or
341// TotalLowPriorityCores for Microsoft.MachineLearningServices
342// createQuotaRequest - quota requests payload.
343func (client QuotaClient) Update(ctx context.Context, subscriptionID string, providerID string, location string, resourceName string, createQuotaRequest CurrentQuotaLimitBase) (result QuotaUpdateFuture, err error) {
344	if tracing.IsEnabled() {
345		ctx = tracing.StartSpan(ctx, fqdn+"/QuotaClient.Update")
346		defer func() {
347			sc := -1
348			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
349				sc = result.FutureAPI.Response().StatusCode
350			}
351			tracing.EndSpan(ctx, sc, err)
352		}()
353	}
354	req, err := client.UpdatePreparer(ctx, subscriptionID, providerID, location, resourceName, createQuotaRequest)
355	if err != nil {
356		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "Update", nil, "Failure preparing request")
357		return
358	}
359
360	result, err = client.UpdateSender(req)
361	if err != nil {
362		err = autorest.NewErrorWithError(err, "reservations.QuotaClient", "Update", nil, "Failure sending request")
363		return
364	}
365
366	return
367}
368
369// UpdatePreparer prepares the Update request.
370func (client QuotaClient) UpdatePreparer(ctx context.Context, subscriptionID string, providerID string, location string, resourceName string, createQuotaRequest CurrentQuotaLimitBase) (*http.Request, error) {
371	pathParameters := map[string]interface{}{
372		"location":       autorest.Encode("path", location),
373		"providerId":     autorest.Encode("path", providerID),
374		"resourceName":   autorest.Encode("path", resourceName),
375		"subscriptionId": autorest.Encode("path", subscriptionID),
376	}
377
378	const APIVersion = "2019-07-19-preview"
379	queryParameters := map[string]interface{}{
380		"api-version": APIVersion,
381	}
382
383	preparer := autorest.CreatePreparer(
384		autorest.AsContentType("application/json; charset=utf-8"),
385		autorest.AsPatch(),
386		autorest.WithBaseURL(client.BaseURI),
387		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Capacity/resourceProviders/{providerId}/locations/{location}/serviceLimits/{resourceName}", pathParameters),
388		autorest.WithJSON(createQuotaRequest),
389		autorest.WithQueryParameters(queryParameters))
390	return preparer.Prepare((&http.Request{}).WithContext(ctx))
391}
392
393// UpdateSender sends the Update request. The method will close the
394// http.Response Body if it receives an error.
395func (client QuotaClient) UpdateSender(req *http.Request) (future QuotaUpdateFuture, err error) {
396	var resp *http.Response
397	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
398	if err != nil {
399		return
400	}
401	var azf azure.Future
402	azf, err = azure.NewFutureFromResponse(resp)
403	future.FutureAPI = &azf
404	future.Result = future.result
405	return
406}
407
408// UpdateResponder handles the response to the Update request. The method always
409// closes the http.Response Body.
410func (client QuotaClient) UpdateResponder(resp *http.Response) (result SetObject, err error) {
411	err = autorest.Respond(
412		resp,
413		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
414		autorest.ByUnmarshallingJSON(&result),
415		autorest.ByClosing())
416	result.Response = autorest.Response{Response: resp}
417	return
418}
419