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