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