1package apimanagement
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	"net/http"
26)
27
28// QuotaByCounterKeysClient is the apiManagement Client
29type QuotaByCounterKeysClient struct {
30	BaseClient
31}
32
33// NewQuotaByCounterKeysClient creates an instance of the QuotaByCounterKeysClient client.
34func NewQuotaByCounterKeysClient(subscriptionID string) QuotaByCounterKeysClient {
35	return NewQuotaByCounterKeysClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewQuotaByCounterKeysClientWithBaseURI creates an instance of the QuotaByCounterKeysClient client.
39func NewQuotaByCounterKeysClientWithBaseURI(baseURI string, subscriptionID string) QuotaByCounterKeysClient {
40	return QuotaByCounterKeysClient{NewWithBaseURI(baseURI, subscriptionID)}
41}
42
43// ListByService lists a collection of current quota counter periods associated with the counter-key configured in the
44// policy on the specified service instance. The api does not support paging yet.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// serviceName - the name of the API Management service.
48// quotaCounterKey - quota counter key identifier.This is the result of expression defined in counter-key
49// attribute of the quota-by-key policy.For Example, if you specify counter-key="boo" in the policy, then it’s
50// accessible by "boo" counter key. But if it’s defined as counter-key="@("b"+"a")" then it will be accessible
51// by "ba" key
52func (client QuotaByCounterKeysClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string) (result QuotaCounterCollection, err error) {
53	if err := validation.Validate([]validation.Validation{
54		{TargetValue: serviceName,
55			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
56				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
57				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
58		return result, validation.NewError("apimanagement.QuotaByCounterKeysClient", "ListByService", err.Error())
59	}
60
61	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, quotaCounterKey)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "ListByService", nil, "Failure preparing request")
64		return
65	}
66
67	resp, err := client.ListByServiceSender(req)
68	if err != nil {
69		result.Response = autorest.Response{Response: resp}
70		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "ListByService", resp, "Failure sending request")
71		return
72	}
73
74	result, err = client.ListByServiceResponder(resp)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "ListByService", resp, "Failure responding to request")
77	}
78
79	return
80}
81
82// ListByServicePreparer prepares the ListByService request.
83func (client QuotaByCounterKeysClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string) (*http.Request, error) {
84	pathParameters := map[string]interface{}{
85		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
86		"resourceGroupName": autorest.Encode("path", resourceGroupName),
87		"serviceName":       autorest.Encode("path", serviceName),
88		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
89	}
90
91	const APIVersion = "2017-03-01"
92	queryParameters := map[string]interface{}{
93		"api-version": APIVersion,
94	}
95
96	preparer := autorest.CreatePreparer(
97		autorest.AsGet(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}", pathParameters),
100		autorest.WithQueryParameters(queryParameters))
101	return preparer.Prepare((&http.Request{}).WithContext(ctx))
102}
103
104// ListByServiceSender sends the ListByService request. The method will close the
105// http.Response Body if it receives an error.
106func (client QuotaByCounterKeysClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
107	return autorest.SendWithSender(client, req,
108		azure.DoRetryWithRegistration(client.Client))
109}
110
111// ListByServiceResponder handles the response to the ListByService request. The method always
112// closes the http.Response Body.
113func (client QuotaByCounterKeysClient) ListByServiceResponder(resp *http.Response) (result QuotaCounterCollection, err error) {
114	err = autorest.Respond(
115		resp,
116		client.ByInspecting(),
117		azure.WithErrorUnlessStatusCode(http.StatusOK),
118		autorest.ByUnmarshallingJSON(&result),
119		autorest.ByClosing())
120	result.Response = autorest.Response{Response: resp}
121	return
122}
123
124// Update updates all the quota counter values specified with the existing quota counter key to a value in the
125// specified service instance. This should be used for reset of the quota counter values.
126// Parameters:
127// resourceGroupName - the name of the resource group.
128// serviceName - the name of the API Management service.
129// quotaCounterKey - quota counter key identifier.This is the result of expression defined in counter-key
130// attribute of the quota-by-key policy.For Example, if you specify counter-key="boo" in the policy, then it’s
131// accessible by "boo" counter key. But if it’s defined as counter-key="@("b"+"a")" then it will be accessible
132// by "ba" key
133// parameters - the value of the quota counter to be applied to all quota counter periods.
134func (client QuotaByCounterKeysClient) Update(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, parameters QuotaCounterValueContractProperties) (result autorest.Response, err error) {
135	if err := validation.Validate([]validation.Validation{
136		{TargetValue: serviceName,
137			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
138				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
139				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
140		return result, validation.NewError("apimanagement.QuotaByCounterKeysClient", "Update", err.Error())
141	}
142
143	req, err := client.UpdatePreparer(ctx, resourceGroupName, serviceName, quotaCounterKey, parameters)
144	if err != nil {
145		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "Update", nil, "Failure preparing request")
146		return
147	}
148
149	resp, err := client.UpdateSender(req)
150	if err != nil {
151		result.Response = resp
152		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "Update", resp, "Failure sending request")
153		return
154	}
155
156	result, err = client.UpdateResponder(resp)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "apimanagement.QuotaByCounterKeysClient", "Update", resp, "Failure responding to request")
159	}
160
161	return
162}
163
164// UpdatePreparer prepares the Update request.
165func (client QuotaByCounterKeysClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, quotaCounterKey string, parameters QuotaCounterValueContractProperties) (*http.Request, error) {
166	pathParameters := map[string]interface{}{
167		"quotaCounterKey":   autorest.Encode("path", quotaCounterKey),
168		"resourceGroupName": autorest.Encode("path", resourceGroupName),
169		"serviceName":       autorest.Encode("path", serviceName),
170		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
171	}
172
173	const APIVersion = "2017-03-01"
174	queryParameters := map[string]interface{}{
175		"api-version": APIVersion,
176	}
177
178	preparer := autorest.CreatePreparer(
179		autorest.AsContentType("application/json; charset=utf-8"),
180		autorest.AsPatch(),
181		autorest.WithBaseURL(client.BaseURI),
182		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/quotas/{quotaCounterKey}", pathParameters),
183		autorest.WithJSON(parameters),
184		autorest.WithQueryParameters(queryParameters))
185	return preparer.Prepare((&http.Request{}).WithContext(ctx))
186}
187
188// UpdateSender sends the Update request. The method will close the
189// http.Response Body if it receives an error.
190func (client QuotaByCounterKeysClient) UpdateSender(req *http.Request) (*http.Response, error) {
191	return autorest.SendWithSender(client, req,
192		azure.DoRetryWithRegistration(client.Client))
193}
194
195// UpdateResponder handles the response to the Update request. The method always
196// closes the http.Response Body.
197func (client QuotaByCounterKeysClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
198	err = autorest.Respond(
199		resp,
200		client.ByInspecting(),
201		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
202		autorest.ByClosing())
203	result.Response = resp
204	return
205}
206