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// UserGroupsClient is the apiManagement Client
30type UserGroupsClient struct {
31	BaseClient
32}
33
34// NewUserGroupsClient creates an instance of the UserGroupsClient client.
35func NewUserGroupsClient(subscriptionID string) UserGroupsClient {
36	return NewUserGroupsClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewUserGroupsClientWithBaseURI creates an instance of the UserGroupsClient 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 NewUserGroupsClientWithBaseURI(baseURI string, subscriptionID string) UserGroupsClient {
42	return UserGroupsClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// ListByUser lists all user groups.
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// serviceName - the name of the API Management service.
49// UID - user identifier. Must be unique in the current API Management service instance.
50// filter - | Field       | Supported operators    | Supported functions                         |
51// |-------------|------------------------|---------------------------------------------|
52// | id          | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
53// | name        | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
54// | description | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
55// top - number of records to return.
56// skip - number of records to skip.
57func (client UserGroupsClient) ListByUser(ctx context.Context, resourceGroupName string, serviceName string, UID string, filter string, top *int32, skip *int32) (result GroupCollectionPage, err error) {
58	if tracing.IsEnabled() {
59		ctx = tracing.StartSpan(ctx, fqdn+"/UserGroupsClient.ListByUser")
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: UID,
74			Constraints: []validation.Constraint{{Target: "UID", Name: validation.MaxLength, Rule: 256, Chain: nil},
75				{Target: "UID", Name: validation.MinLength, Rule: 1, Chain: nil},
76				{Target: "UID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}},
77		{TargetValue: top,
78			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
79				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
80		{TargetValue: skip,
81			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
82				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
83		return result, validation.NewError("apimanagement.UserGroupsClient", "ListByUser", err.Error())
84	}
85
86	result.fn = client.listByUserNextResults
87	req, err := client.ListByUserPreparer(ctx, resourceGroupName, serviceName, UID, filter, top, skip)
88	if err != nil {
89		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupsClient", "ListByUser", nil, "Failure preparing request")
90		return
91	}
92
93	resp, err := client.ListByUserSender(req)
94	if err != nil {
95		result.gc.Response = autorest.Response{Response: resp}
96		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupsClient", "ListByUser", resp, "Failure sending request")
97		return
98	}
99
100	result.gc, err = client.ListByUserResponder(resp)
101	if err != nil {
102		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupsClient", "ListByUser", resp, "Failure responding to request")
103		return
104	}
105	if result.gc.hasNextLink() && result.gc.IsEmpty() {
106		err = result.NextWithContext(ctx)
107		return
108	}
109
110	return
111}
112
113// ListByUserPreparer prepares the ListByUser request.
114func (client UserGroupsClient) ListByUserPreparer(ctx context.Context, resourceGroupName string, serviceName string, UID string, filter string, top *int32, skip *int32) (*http.Request, error) {
115	pathParameters := map[string]interface{}{
116		"resourceGroupName": autorest.Encode("path", resourceGroupName),
117		"serviceName":       autorest.Encode("path", serviceName),
118		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
119		"uid":               autorest.Encode("path", UID),
120	}
121
122	const APIVersion = "2016-07-07"
123	queryParameters := map[string]interface{}{
124		"api-version": APIVersion,
125	}
126	if len(filter) > 0 {
127		queryParameters["$filter"] = autorest.Encode("query", filter)
128	}
129	if top != nil {
130		queryParameters["$top"] = autorest.Encode("query", *top)
131	}
132	if skip != nil {
133		queryParameters["$skip"] = autorest.Encode("query", *skip)
134	}
135
136	preparer := autorest.CreatePreparer(
137		autorest.AsGet(),
138		autorest.WithBaseURL(client.BaseURI),
139		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/users/{uid}/groups", pathParameters),
140		autorest.WithQueryParameters(queryParameters))
141	return preparer.Prepare((&http.Request{}).WithContext(ctx))
142}
143
144// ListByUserSender sends the ListByUser request. The method will close the
145// http.Response Body if it receives an error.
146func (client UserGroupsClient) ListByUserSender(req *http.Request) (*http.Response, error) {
147	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
148}
149
150// ListByUserResponder handles the response to the ListByUser request. The method always
151// closes the http.Response Body.
152func (client UserGroupsClient) ListByUserResponder(resp *http.Response) (result GroupCollection, err error) {
153	err = autorest.Respond(
154		resp,
155		azure.WithErrorUnlessStatusCode(http.StatusOK),
156		autorest.ByUnmarshallingJSON(&result),
157		autorest.ByClosing())
158	result.Response = autorest.Response{Response: resp}
159	return
160}
161
162// listByUserNextResults retrieves the next set of results, if any.
163func (client UserGroupsClient) listByUserNextResults(ctx context.Context, lastResults GroupCollection) (result GroupCollection, err error) {
164	req, err := lastResults.groupCollectionPreparer(ctx)
165	if err != nil {
166		return result, autorest.NewErrorWithError(err, "apimanagement.UserGroupsClient", "listByUserNextResults", nil, "Failure preparing next results request")
167	}
168	if req == nil {
169		return
170	}
171	resp, err := client.ListByUserSender(req)
172	if err != nil {
173		result.Response = autorest.Response{Response: resp}
174		return result, autorest.NewErrorWithError(err, "apimanagement.UserGroupsClient", "listByUserNextResults", resp, "Failure sending next results request")
175	}
176	result, err = client.ListByUserResponder(resp)
177	if err != nil {
178		err = autorest.NewErrorWithError(err, "apimanagement.UserGroupsClient", "listByUserNextResults", resp, "Failure responding to next results request")
179	}
180	return
181}
182
183// ListByUserComplete enumerates all values, automatically crossing page boundaries as required.
184func (client UserGroupsClient) ListByUserComplete(ctx context.Context, resourceGroupName string, serviceName string, UID string, filter string, top *int32, skip *int32) (result GroupCollectionIterator, err error) {
185	if tracing.IsEnabled() {
186		ctx = tracing.StartSpan(ctx, fqdn+"/UserGroupsClient.ListByUser")
187		defer func() {
188			sc := -1
189			if result.Response().Response.Response != nil {
190				sc = result.page.Response().Response.Response.StatusCode
191			}
192			tracing.EndSpan(ctx, sc, err)
193		}()
194	}
195	result.page, err = client.ListByUser(ctx, resourceGroupName, serviceName, UID, filter, top, skip)
196	return
197}
198