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// UserGroupClient is the apiManagement Client
30type UserGroupClient struct {
31	BaseClient
32}
33
34// NewUserGroupClient creates an instance of the UserGroupClient client.
35func NewUserGroupClient(subscriptionID string) UserGroupClient {
36	return NewUserGroupClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewUserGroupClientWithBaseURI creates an instance of the UserGroupClient client.
40func NewUserGroupClientWithBaseURI(baseURI string, subscriptionID string) UserGroupClient {
41	return UserGroupClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// List lists all user groups.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// serviceName - the name of the API Management service.
48// userID - user identifier. Must be unique in the current API Management service instance.
49// filter - |   Field     |     Usage     |     Supported operators     |     Supported functions
50// |</br>|-------------|-------------|-------------|-------------|</br>| name | filter | ge, le, eq, ne, gt, lt
51// | substringof, contains, startswith, endswith | </br>| displayName | filter | ge, le, eq, ne, gt, lt |
52// substringof, contains, startswith, endswith | </br>| description | filter | ge, le, eq, ne, gt, lt |
53// substringof, contains, startswith, endswith | </br>
54// top - number of records to return.
55// skip - number of records to skip.
56func (client UserGroupClient) List(ctx context.Context, resourceGroupName string, serviceName string, userID string, filter string, top *int32, skip *int32) (result GroupCollectionPage, err error) {
57	if tracing.IsEnabled() {
58		ctx = tracing.StartSpan(ctx, fqdn+"/UserGroupClient.List")
59		defer func() {
60			sc := -1
61			if result.gc.Response.Response != nil {
62				sc = result.gc.Response.Response.StatusCode
63			}
64			tracing.EndSpan(ctx, sc, err)
65		}()
66	}
67	if err := validation.Validate([]validation.Validation{
68		{TargetValue: serviceName,
69			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
70				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
71				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
72		{TargetValue: userID,
73			Constraints: []validation.Constraint{{Target: "userID", Name: validation.MaxLength, Rule: 80, Chain: nil},
74				{Target: "userID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
75		{TargetValue: top,
76			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
77				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
78		{TargetValue: skip,
79			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
80				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
81		return result, validation.NewError("apimanagement.UserGroupClient", "List", err.Error())
82	}
83
84	result.fn = client.listNextResults
85	req, err := client.ListPreparer(ctx, resourceGroupName, serviceName, userID, filter, top, skip)
86	if err != nil {
87		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupClient", "List", nil, "Failure preparing request")
88		return
89	}
90
91	resp, err := client.ListSender(req)
92	if err != nil {
93		result.gc.Response = autorest.Response{Response: resp}
94		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupClient", "List", resp, "Failure sending request")
95		return
96	}
97
98	result.gc, err = client.ListResponder(resp)
99	if err != nil {
100		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupClient", "List", resp, "Failure responding to request")
101	}
102
103	return
104}
105
106// ListPreparer prepares the List request.
107func (client UserGroupClient) ListPreparer(ctx context.Context, resourceGroupName string, serviceName string, userID string, filter string, top *int32, skip *int32) (*http.Request, error) {
108	pathParameters := map[string]interface{}{
109		"resourceGroupName": autorest.Encode("path", resourceGroupName),
110		"serviceName":       autorest.Encode("path", serviceName),
111		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
112		"userId":            autorest.Encode("path", userID),
113	}
114
115	const APIVersion = "2019-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}/users/{userId}/groups", pathParameters),
133		autorest.WithQueryParameters(queryParameters))
134	return preparer.Prepare((&http.Request{}).WithContext(ctx))
135}
136
137// ListSender sends the List request. The method will close the
138// http.Response Body if it receives an error.
139func (client UserGroupClient) ListSender(req *http.Request) (*http.Response, error) {
140	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
141	return autorest.SendWithSender(client, req, sd...)
142}
143
144// ListResponder handles the response to the List request. The method always
145// closes the http.Response Body.
146func (client UserGroupClient) ListResponder(resp *http.Response) (result GroupCollection, err error) {
147	err = autorest.Respond(
148		resp,
149		client.ByInspecting(),
150		azure.WithErrorUnlessStatusCode(http.StatusOK),
151		autorest.ByUnmarshallingJSON(&result),
152		autorest.ByClosing())
153	result.Response = autorest.Response{Response: resp}
154	return
155}
156
157// listNextResults retrieves the next set of results, if any.
158func (client UserGroupClient) listNextResults(ctx context.Context, lastResults GroupCollection) (result GroupCollection, err error) {
159	req, err := lastResults.groupCollectionPreparer(ctx)
160	if err != nil {
161		return result, autorest.NewErrorWithError(err, "apimanagement.UserGroupClient", "listNextResults", nil, "Failure preparing next results request")
162	}
163	if req == nil {
164		return
165	}
166	resp, err := client.ListSender(req)
167	if err != nil {
168		result.Response = autorest.Response{Response: resp}
169		return result, autorest.NewErrorWithError(err, "apimanagement.UserGroupClient", "listNextResults", resp, "Failure sending next results request")
170	}
171	result, err = client.ListResponder(resp)
172	if err != nil {
173		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupClient", "listNextResults", resp, "Failure responding to next results request")
174	}
175	return
176}
177
178// ListComplete enumerates all values, automatically crossing page boundaries as required.
179func (client UserGroupClient) ListComplete(ctx context.Context, resourceGroupName string, serviceName string, userID string, filter string, top *int32, skip *int32) (result GroupCollectionIterator, err error) {
180	if tracing.IsEnabled() {
181		ctx = tracing.StartSpan(ctx, fqdn+"/UserGroupClient.List")
182		defer func() {
183			sc := -1
184			if result.Response().Response.Response != nil {
185				sc = result.page.Response().Response.Response.StatusCode
186			}
187			tracing.EndSpan(ctx, sc, err)
188		}()
189	}
190	result.page, err = client.List(ctx, resourceGroupName, serviceName, userID, filter, top, skip)
191	return
192}
193