1package sql
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// SubscriptionUsagesClient is the the Azure SQL Database management API provides a RESTful set of web services that
18// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
19// and delete databases.
20type SubscriptionUsagesClient struct {
21	BaseClient
22}
23
24// NewSubscriptionUsagesClient creates an instance of the SubscriptionUsagesClient client.
25func NewSubscriptionUsagesClient(subscriptionID string) SubscriptionUsagesClient {
26	return NewSubscriptionUsagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
27}
28
29// NewSubscriptionUsagesClientWithBaseURI creates an instance of the SubscriptionUsagesClient client using a custom
30// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
31// stack).
32func NewSubscriptionUsagesClientWithBaseURI(baseURI string, subscriptionID string) SubscriptionUsagesClient {
33	return SubscriptionUsagesClient{NewWithBaseURI(baseURI, subscriptionID)}
34}
35
36// Get gets a subscription usage metric.
37// Parameters:
38// locationName - the name of the region where the resource is located.
39// usageName - name of usage metric to return.
40func (client SubscriptionUsagesClient) Get(ctx context.Context, locationName string, usageName string) (result SubscriptionUsage, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionUsagesClient.Get")
43		defer func() {
44			sc := -1
45			if result.Response.Response != nil {
46				sc = result.Response.Response.StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	req, err := client.GetPreparer(ctx, locationName, usageName)
52	if err != nil {
53		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "Get", nil, "Failure preparing request")
54		return
55	}
56
57	resp, err := client.GetSender(req)
58	if err != nil {
59		result.Response = autorest.Response{Response: resp}
60		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "Get", resp, "Failure sending request")
61		return
62	}
63
64	result, err = client.GetResponder(resp)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "Get", resp, "Failure responding to request")
67		return
68	}
69
70	return
71}
72
73// GetPreparer prepares the Get request.
74func (client SubscriptionUsagesClient) GetPreparer(ctx context.Context, locationName string, usageName string) (*http.Request, error) {
75	pathParameters := map[string]interface{}{
76		"locationName":   autorest.Encode("path", locationName),
77		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
78		"usageName":      autorest.Encode("path", usageName),
79	}
80
81	const APIVersion = "2015-05-01-preview"
82	queryParameters := map[string]interface{}{
83		"api-version": APIVersion,
84	}
85
86	preparer := autorest.CreatePreparer(
87		autorest.AsGet(),
88		autorest.WithBaseURL(client.BaseURI),
89		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/usages/{usageName}", pathParameters),
90		autorest.WithQueryParameters(queryParameters))
91	return preparer.Prepare((&http.Request{}).WithContext(ctx))
92}
93
94// GetSender sends the Get request. The method will close the
95// http.Response Body if it receives an error.
96func (client SubscriptionUsagesClient) GetSender(req *http.Request) (*http.Response, error) {
97	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
98}
99
100// GetResponder handles the response to the Get request. The method always
101// closes the http.Response Body.
102func (client SubscriptionUsagesClient) GetResponder(resp *http.Response) (result SubscriptionUsage, err error) {
103	err = autorest.Respond(
104		resp,
105		azure.WithErrorUnlessStatusCode(http.StatusOK),
106		autorest.ByUnmarshallingJSON(&result),
107		autorest.ByClosing())
108	result.Response = autorest.Response{Response: resp}
109	return
110}
111
112// ListByLocation gets all subscription usage metrics in a given location.
113// Parameters:
114// locationName - the name of the region where the resource is located.
115func (client SubscriptionUsagesClient) ListByLocation(ctx context.Context, locationName string) (result SubscriptionUsageListResultPage, err error) {
116	if tracing.IsEnabled() {
117		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionUsagesClient.ListByLocation")
118		defer func() {
119			sc := -1
120			if result.sulr.Response.Response != nil {
121				sc = result.sulr.Response.Response.StatusCode
122			}
123			tracing.EndSpan(ctx, sc, err)
124		}()
125	}
126	result.fn = client.listByLocationNextResults
127	req, err := client.ListByLocationPreparer(ctx, locationName)
128	if err != nil {
129		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "ListByLocation", nil, "Failure preparing request")
130		return
131	}
132
133	resp, err := client.ListByLocationSender(req)
134	if err != nil {
135		result.sulr.Response = autorest.Response{Response: resp}
136		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "ListByLocation", resp, "Failure sending request")
137		return
138	}
139
140	result.sulr, err = client.ListByLocationResponder(resp)
141	if err != nil {
142		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "ListByLocation", resp, "Failure responding to request")
143		return
144	}
145	if result.sulr.hasNextLink() && result.sulr.IsEmpty() {
146		err = result.NextWithContext(ctx)
147		return
148	}
149
150	return
151}
152
153// ListByLocationPreparer prepares the ListByLocation request.
154func (client SubscriptionUsagesClient) ListByLocationPreparer(ctx context.Context, locationName string) (*http.Request, error) {
155	pathParameters := map[string]interface{}{
156		"locationName":   autorest.Encode("path", locationName),
157		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
158	}
159
160	const APIVersion = "2015-05-01-preview"
161	queryParameters := map[string]interface{}{
162		"api-version": APIVersion,
163	}
164
165	preparer := autorest.CreatePreparer(
166		autorest.AsGet(),
167		autorest.WithBaseURL(client.BaseURI),
168		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/usages", pathParameters),
169		autorest.WithQueryParameters(queryParameters))
170	return preparer.Prepare((&http.Request{}).WithContext(ctx))
171}
172
173// ListByLocationSender sends the ListByLocation request. The method will close the
174// http.Response Body if it receives an error.
175func (client SubscriptionUsagesClient) ListByLocationSender(req *http.Request) (*http.Response, error) {
176	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
177}
178
179// ListByLocationResponder handles the response to the ListByLocation request. The method always
180// closes the http.Response Body.
181func (client SubscriptionUsagesClient) ListByLocationResponder(resp *http.Response) (result SubscriptionUsageListResult, err error) {
182	err = autorest.Respond(
183		resp,
184		azure.WithErrorUnlessStatusCode(http.StatusOK),
185		autorest.ByUnmarshallingJSON(&result),
186		autorest.ByClosing())
187	result.Response = autorest.Response{Response: resp}
188	return
189}
190
191// listByLocationNextResults retrieves the next set of results, if any.
192func (client SubscriptionUsagesClient) listByLocationNextResults(ctx context.Context, lastResults SubscriptionUsageListResult) (result SubscriptionUsageListResult, err error) {
193	req, err := lastResults.subscriptionUsageListResultPreparer(ctx)
194	if err != nil {
195		return result, autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "listByLocationNextResults", nil, "Failure preparing next results request")
196	}
197	if req == nil {
198		return
199	}
200	resp, err := client.ListByLocationSender(req)
201	if err != nil {
202		result.Response = autorest.Response{Response: resp}
203		return result, autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "listByLocationNextResults", resp, "Failure sending next results request")
204	}
205	result, err = client.ListByLocationResponder(resp)
206	if err != nil {
207		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "listByLocationNextResults", resp, "Failure responding to next results request")
208	}
209	return
210}
211
212// ListByLocationComplete enumerates all values, automatically crossing page boundaries as required.
213func (client SubscriptionUsagesClient) ListByLocationComplete(ctx context.Context, locationName string) (result SubscriptionUsageListResultIterator, err error) {
214	if tracing.IsEnabled() {
215		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionUsagesClient.ListByLocation")
216		defer func() {
217			sc := -1
218			if result.Response().Response.Response != nil {
219				sc = result.page.Response().Response.Response.StatusCode
220			}
221			tracing.EndSpan(ctx, sc, err)
222		}()
223	}
224	result.page, err = client.ListByLocation(ctx, locationName)
225	return
226}
227