1package storage
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/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// UsagesClient is the the Azure Storage Management API.
30type UsagesClient struct {
31	BaseClient
32}
33
34// NewUsagesClient creates an instance of the UsagesClient client.
35func NewUsagesClient(subscriptionID string) UsagesClient {
36	return NewUsagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewUsagesClientWithBaseURI creates an instance of the UsagesClient client using a custom endpoint.  Use this when
40// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewUsagesClientWithBaseURI(baseURI string, subscriptionID string) UsagesClient {
42	return UsagesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// ListByLocation gets the current usage count and the limit for the resources of the location under the subscription.
46// Parameters:
47// location - the location of the Azure Storage resource.
48func (client UsagesClient) ListByLocation(ctx context.Context, location string) (result UsageListResult, err error) {
49	if tracing.IsEnabled() {
50		ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.ListByLocation")
51		defer func() {
52			sc := -1
53			if result.Response.Response != nil {
54				sc = result.Response.Response.StatusCode
55			}
56			tracing.EndSpan(ctx, sc, err)
57		}()
58	}
59	if err := validation.Validate([]validation.Validation{
60		{TargetValue: client.SubscriptionID,
61			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
62		return result, validation.NewError("storage.UsagesClient", "ListByLocation", err.Error())
63	}
64
65	req, err := client.ListByLocationPreparer(ctx, location)
66	if err != nil {
67		err = autorest.NewErrorWithError(err, "storage.UsagesClient", "ListByLocation", nil, "Failure preparing request")
68		return
69	}
70
71	resp, err := client.ListByLocationSender(req)
72	if err != nil {
73		result.Response = autorest.Response{Response: resp}
74		err = autorest.NewErrorWithError(err, "storage.UsagesClient", "ListByLocation", resp, "Failure sending request")
75		return
76	}
77
78	result, err = client.ListByLocationResponder(resp)
79	if err != nil {
80		err = autorest.NewErrorWithError(err, "storage.UsagesClient", "ListByLocation", resp, "Failure responding to request")
81		return
82	}
83
84	return
85}
86
87// ListByLocationPreparer prepares the ListByLocation request.
88func (client UsagesClient) ListByLocationPreparer(ctx context.Context, location string) (*http.Request, error) {
89	pathParameters := map[string]interface{}{
90		"location":       autorest.Encode("path", location),
91		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
92	}
93
94	const APIVersion = "2018-11-01"
95	queryParameters := map[string]interface{}{
96		"api-version": APIVersion,
97	}
98
99	preparer := autorest.CreatePreparer(
100		autorest.AsGet(),
101		autorest.WithBaseURL(client.BaseURI),
102		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Storage/locations/{location}/usages", pathParameters),
103		autorest.WithQueryParameters(queryParameters))
104	return preparer.Prepare((&http.Request{}).WithContext(ctx))
105}
106
107// ListByLocationSender sends the ListByLocation request. The method will close the
108// http.Response Body if it receives an error.
109func (client UsagesClient) ListByLocationSender(req *http.Request) (*http.Response, error) {
110	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
111}
112
113// ListByLocationResponder handles the response to the ListByLocation request. The method always
114// closes the http.Response Body.
115func (client UsagesClient) ListByLocationResponder(resp *http.Response) (result UsageListResult, err error) {
116	err = autorest.Respond(
117		resp,
118		azure.WithErrorUnlessStatusCode(http.StatusOK),
119		autorest.ByUnmarshallingJSON(&result),
120		autorest.ByClosing())
121	result.Response = autorest.Response{Response: resp}
122	return
123}
124