1package sql
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/tracing"
25	"net/http"
26)
27
28// SubscriptionUsagesClient is the the Azure SQL Database management API provides a RESTful set of web services that
29// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
30// and delete databases.
31type SubscriptionUsagesClient struct {
32	BaseClient
33}
34
35// NewSubscriptionUsagesClient creates an instance of the SubscriptionUsagesClient client.
36func NewSubscriptionUsagesClient(subscriptionID string) SubscriptionUsagesClient {
37	return NewSubscriptionUsagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
38}
39
40// NewSubscriptionUsagesClientWithBaseURI creates an instance of the SubscriptionUsagesClient client.
41func NewSubscriptionUsagesClientWithBaseURI(baseURI string, subscriptionID string) SubscriptionUsagesClient {
42	return SubscriptionUsagesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// Get gets a subscription usage metric.
46// Parameters:
47// locationName - the name of the region where the resource is located.
48// usageName - name of usage metric to return.
49func (client SubscriptionUsagesClient) Get(ctx context.Context, locationName string, usageName string) (result SubscriptionUsage, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionUsagesClient.Get")
52		defer func() {
53			sc := -1
54			if result.Response.Response != nil {
55				sc = result.Response.Response.StatusCode
56			}
57			tracing.EndSpan(ctx, sc, err)
58		}()
59	}
60	req, err := client.GetPreparer(ctx, locationName, usageName)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "Get", nil, "Failure preparing request")
63		return
64	}
65
66	resp, err := client.GetSender(req)
67	if err != nil {
68		result.Response = autorest.Response{Response: resp}
69		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "Get", resp, "Failure sending request")
70		return
71	}
72
73	result, err = client.GetResponder(resp)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "Get", resp, "Failure responding to request")
76	}
77
78	return
79}
80
81// GetPreparer prepares the Get request.
82func (client SubscriptionUsagesClient) GetPreparer(ctx context.Context, locationName string, usageName string) (*http.Request, error) {
83	pathParameters := map[string]interface{}{
84		"locationName":   autorest.Encode("path", locationName),
85		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
86		"usageName":      autorest.Encode("path", usageName),
87	}
88
89	const APIVersion = "2015-05-01-preview"
90	queryParameters := map[string]interface{}{
91		"api-version": APIVersion,
92	}
93
94	preparer := autorest.CreatePreparer(
95		autorest.AsGet(),
96		autorest.WithBaseURL(client.BaseURI),
97		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/usages/{usageName}", pathParameters),
98		autorest.WithQueryParameters(queryParameters))
99	return preparer.Prepare((&http.Request{}).WithContext(ctx))
100}
101
102// GetSender sends the Get request. The method will close the
103// http.Response Body if it receives an error.
104func (client SubscriptionUsagesClient) GetSender(req *http.Request) (*http.Response, error) {
105	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
106	return autorest.SendWithSender(client, req, sd...)
107}
108
109// GetResponder handles the response to the Get request. The method always
110// closes the http.Response Body.
111func (client SubscriptionUsagesClient) GetResponder(resp *http.Response) (result SubscriptionUsage, err error) {
112	err = autorest.Respond(
113		resp,
114		client.ByInspecting(),
115		azure.WithErrorUnlessStatusCode(http.StatusOK),
116		autorest.ByUnmarshallingJSON(&result),
117		autorest.ByClosing())
118	result.Response = autorest.Response{Response: resp}
119	return
120}
121
122// ListByLocation gets all subscription usage metrics in a given location.
123// Parameters:
124// locationName - the name of the region where the resource is located.
125func (client SubscriptionUsagesClient) ListByLocation(ctx context.Context, locationName string) (result SubscriptionUsageListResultPage, err error) {
126	if tracing.IsEnabled() {
127		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionUsagesClient.ListByLocation")
128		defer func() {
129			sc := -1
130			if result.sulr.Response.Response != nil {
131				sc = result.sulr.Response.Response.StatusCode
132			}
133			tracing.EndSpan(ctx, sc, err)
134		}()
135	}
136	result.fn = client.listByLocationNextResults
137	req, err := client.ListByLocationPreparer(ctx, locationName)
138	if err != nil {
139		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "ListByLocation", nil, "Failure preparing request")
140		return
141	}
142
143	resp, err := client.ListByLocationSender(req)
144	if err != nil {
145		result.sulr.Response = autorest.Response{Response: resp}
146		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "ListByLocation", resp, "Failure sending request")
147		return
148	}
149
150	result.sulr, err = client.ListByLocationResponder(resp)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "ListByLocation", resp, "Failure responding to request")
153	}
154
155	return
156}
157
158// ListByLocationPreparer prepares the ListByLocation request.
159func (client SubscriptionUsagesClient) ListByLocationPreparer(ctx context.Context, locationName string) (*http.Request, error) {
160	pathParameters := map[string]interface{}{
161		"locationName":   autorest.Encode("path", locationName),
162		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
163	}
164
165	const APIVersion = "2015-05-01-preview"
166	queryParameters := map[string]interface{}{
167		"api-version": APIVersion,
168	}
169
170	preparer := autorest.CreatePreparer(
171		autorest.AsGet(),
172		autorest.WithBaseURL(client.BaseURI),
173		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/usages", pathParameters),
174		autorest.WithQueryParameters(queryParameters))
175	return preparer.Prepare((&http.Request{}).WithContext(ctx))
176}
177
178// ListByLocationSender sends the ListByLocation request. The method will close the
179// http.Response Body if it receives an error.
180func (client SubscriptionUsagesClient) ListByLocationSender(req *http.Request) (*http.Response, error) {
181	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
182	return autorest.SendWithSender(client, req, sd...)
183}
184
185// ListByLocationResponder handles the response to the ListByLocation request. The method always
186// closes the http.Response Body.
187func (client SubscriptionUsagesClient) ListByLocationResponder(resp *http.Response) (result SubscriptionUsageListResult, err error) {
188	err = autorest.Respond(
189		resp,
190		client.ByInspecting(),
191		azure.WithErrorUnlessStatusCode(http.StatusOK),
192		autorest.ByUnmarshallingJSON(&result),
193		autorest.ByClosing())
194	result.Response = autorest.Response{Response: resp}
195	return
196}
197
198// listByLocationNextResults retrieves the next set of results, if any.
199func (client SubscriptionUsagesClient) listByLocationNextResults(ctx context.Context, lastResults SubscriptionUsageListResult) (result SubscriptionUsageListResult, err error) {
200	req, err := lastResults.subscriptionUsageListResultPreparer(ctx)
201	if err != nil {
202		return result, autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "listByLocationNextResults", nil, "Failure preparing next results request")
203	}
204	if req == nil {
205		return
206	}
207	resp, err := client.ListByLocationSender(req)
208	if err != nil {
209		result.Response = autorest.Response{Response: resp}
210		return result, autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "listByLocationNextResults", resp, "Failure sending next results request")
211	}
212	result, err = client.ListByLocationResponder(resp)
213	if err != nil {
214		err = autorest.NewErrorWithError(err, "sql.SubscriptionUsagesClient", "listByLocationNextResults", resp, "Failure responding to next results request")
215	}
216	return
217}
218
219// ListByLocationComplete enumerates all values, automatically crossing page boundaries as required.
220func (client SubscriptionUsagesClient) ListByLocationComplete(ctx context.Context, locationName string) (result SubscriptionUsageListResultIterator, err error) {
221	if tracing.IsEnabled() {
222		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionUsagesClient.ListByLocation")
223		defer func() {
224			sc := -1
225			if result.Response().Response.Response != nil {
226				sc = result.page.Response().Response.Response.StatusCode
227			}
228			tracing.EndSpan(ctx, sc, err)
229		}()
230	}
231	result.page, err = client.ListByLocation(ctx, locationName)
232	return
233}
234