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	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// TagResourceClient is the apiManagement Client
30type TagResourceClient struct {
31	BaseClient
32}
33
34// NewTagResourceClient creates an instance of the TagResourceClient client.
35func NewTagResourceClient(subscriptionID string) TagResourceClient {
36	return NewTagResourceClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewTagResourceClientWithBaseURI creates an instance of the TagResourceClient client.
40func NewTagResourceClientWithBaseURI(baseURI string, subscriptionID string) TagResourceClient {
41	return TagResourceClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// ListByService lists a collection of resources associated with tags.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// serviceName - the name of the API Management service.
48// filter - |   Field     |     Usage     |     Supported operators     |     Supported functions
49// |</br>|-------------|-------------|-------------|-------------|</br>| aid | filter | ge, le, eq, ne, gt, lt
50// | substringof, contains, startswith, endswith | </br>| name | filter | ge, le, eq, ne, gt, lt | substringof,
51// contains, startswith, endswith | </br>| displayName | filter | ge, le, eq, ne, gt, lt | substringof,
52// contains, startswith, endswith | </br>| apiName | filter | ge, le, eq, ne, gt, lt | substringof, contains,
53// startswith, endswith | </br>| apiRevision | filter | ge, le, eq, ne, gt, lt | substringof, contains,
54// startswith, endswith | </br>| path | filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith,
55// endswith | </br>| description | filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith,
56// endswith | </br>| serviceUrl | filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith
57// | </br>| method | filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith | </br>|
58// urlTemplate | filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith | </br>| terms |
59// filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith | </br>| state | filter | eq |
60// | </br>| isCurrent | filter | eq |     | </br>
61// top - number of records to return.
62// skip - number of records to skip.
63func (client TagResourceClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, filter string, top *int32, skip *int32) (result TagResourceCollectionPage, err error) {
64	if tracing.IsEnabled() {
65		ctx = tracing.StartSpan(ctx, fqdn+"/TagResourceClient.ListByService")
66		defer func() {
67			sc := -1
68			if result.trc.Response.Response != nil {
69				sc = result.trc.Response.Response.StatusCode
70			}
71			tracing.EndSpan(ctx, sc, err)
72		}()
73	}
74	if err := validation.Validate([]validation.Validation{
75		{TargetValue: serviceName,
76			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
77				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
78				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
79		{TargetValue: top,
80			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
81				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
82		{TargetValue: skip,
83			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
84				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
85		return result, validation.NewError("apimanagement.TagResourceClient", "ListByService", err.Error())
86	}
87
88	result.fn = client.listByServiceNextResults
89	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, filter, top, skip)
90	if err != nil {
91		err = autorest.NewErrorWithError(err, "apimanagement.TagResourceClient", "ListByService", nil, "Failure preparing request")
92		return
93	}
94
95	resp, err := client.ListByServiceSender(req)
96	if err != nil {
97		result.trc.Response = autorest.Response{Response: resp}
98		err = autorest.NewErrorWithError(err, "apimanagement.TagResourceClient", "ListByService", resp, "Failure sending request")
99		return
100	}
101
102	result.trc, err = client.ListByServiceResponder(resp)
103	if err != nil {
104		err = autorest.NewErrorWithError(err, "apimanagement.TagResourceClient", "ListByService", resp, "Failure responding to request")
105	}
106
107	return
108}
109
110// ListByServicePreparer prepares the ListByService request.
111func (client TagResourceClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, filter string, top *int32, skip *int32) (*http.Request, error) {
112	pathParameters := map[string]interface{}{
113		"resourceGroupName": autorest.Encode("path", resourceGroupName),
114		"serviceName":       autorest.Encode("path", serviceName),
115		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
116	}
117
118	const APIVersion = "2019-01-01"
119	queryParameters := map[string]interface{}{
120		"api-version": APIVersion,
121	}
122	if len(filter) > 0 {
123		queryParameters["$filter"] = autorest.Encode("query", filter)
124	}
125	if top != nil {
126		queryParameters["$top"] = autorest.Encode("query", *top)
127	}
128	if skip != nil {
129		queryParameters["$skip"] = autorest.Encode("query", *skip)
130	}
131
132	preparer := autorest.CreatePreparer(
133		autorest.AsGet(),
134		autorest.WithBaseURL(client.BaseURI),
135		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tagResources", pathParameters),
136		autorest.WithQueryParameters(queryParameters))
137	return preparer.Prepare((&http.Request{}).WithContext(ctx))
138}
139
140// ListByServiceSender sends the ListByService request. The method will close the
141// http.Response Body if it receives an error.
142func (client TagResourceClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
143	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
144	return autorest.SendWithSender(client, req, sd...)
145}
146
147// ListByServiceResponder handles the response to the ListByService request. The method always
148// closes the http.Response Body.
149func (client TagResourceClient) ListByServiceResponder(resp *http.Response) (result TagResourceCollection, err error) {
150	err = autorest.Respond(
151		resp,
152		client.ByInspecting(),
153		azure.WithErrorUnlessStatusCode(http.StatusOK),
154		autorest.ByUnmarshallingJSON(&result),
155		autorest.ByClosing())
156	result.Response = autorest.Response{Response: resp}
157	return
158}
159
160// listByServiceNextResults retrieves the next set of results, if any.
161func (client TagResourceClient) listByServiceNextResults(ctx context.Context, lastResults TagResourceCollection) (result TagResourceCollection, err error) {
162	req, err := lastResults.tagResourceCollectionPreparer(ctx)
163	if err != nil {
164		return result, autorest.NewErrorWithError(err, "apimanagement.TagResourceClient", "listByServiceNextResults", nil, "Failure preparing next results request")
165	}
166	if req == nil {
167		return
168	}
169	resp, err := client.ListByServiceSender(req)
170	if err != nil {
171		result.Response = autorest.Response{Response: resp}
172		return result, autorest.NewErrorWithError(err, "apimanagement.TagResourceClient", "listByServiceNextResults", resp, "Failure sending next results request")
173	}
174	result, err = client.ListByServiceResponder(resp)
175	if err != nil {
176		err = autorest.NewErrorWithError(err, "apimanagement.TagResourceClient", "listByServiceNextResults", resp, "Failure responding to next results request")
177	}
178	return
179}
180
181// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
182func (client TagResourceClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, filter string, top *int32, skip *int32) (result TagResourceCollectionIterator, err error) {
183	if tracing.IsEnabled() {
184		ctx = tracing.StartSpan(ctx, fqdn+"/TagResourceClient.ListByService")
185		defer func() {
186			sc := -1
187			if result.Response().Response.Response != nil {
188				sc = result.page.Response().Response.Response.StatusCode
189			}
190			tracing.EndSpan(ctx, sc, err)
191		}()
192	}
193	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, filter, top, skip)
194	return
195}
196