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