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// GroupUsersClient is the apiManagement Client
30type GroupUsersClient struct {
31	BaseClient
32}
33
34// NewGroupUsersClient creates an instance of the GroupUsersClient client.
35func NewGroupUsersClient(subscriptionID string) GroupUsersClient {
36	return NewGroupUsersClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewGroupUsersClientWithBaseURI creates an instance of the GroupUsersClient 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 NewGroupUsersClientWithBaseURI(baseURI string, subscriptionID string) GroupUsersClient {
42	return GroupUsersClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// Add adds a user to the specified group.
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// serviceName - the name of the API Management service.
49// groupID - group identifier. Must be unique in the current API Management service instance.
50// UID - user identifier. Must be unique in the current API Management service instance.
51func (client GroupUsersClient) Add(ctx context.Context, resourceGroupName string, serviceName string, groupID string, UID string) (result ErrorBodyContract, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUsersClient.Add")
54		defer func() {
55			sc := -1
56			if result.Response.Response != nil {
57				sc = result.Response.Response.StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	if err := validation.Validate([]validation.Validation{
63		{TargetValue: serviceName,
64			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
65				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
66				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
67		{TargetValue: groupID,
68			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
69				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
70				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}},
71		{TargetValue: UID,
72			Constraints: []validation.Constraint{{Target: "UID", Name: validation.MaxLength, Rule: 256, Chain: nil},
73				{Target: "UID", Name: validation.MinLength, Rule: 1, Chain: nil},
74				{Target: "UID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
75		return result, validation.NewError("apimanagement.GroupUsersClient", "Add", err.Error())
76	}
77
78	req, err := client.AddPreparer(ctx, resourceGroupName, serviceName, groupID, UID)
79	if err != nil {
80		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "Add", nil, "Failure preparing request")
81		return
82	}
83
84	resp, err := client.AddSender(req)
85	if err != nil {
86		result.Response = autorest.Response{Response: resp}
87		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "Add", resp, "Failure sending request")
88		return
89	}
90
91	result, err = client.AddResponder(resp)
92	if err != nil {
93		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "Add", resp, "Failure responding to request")
94		return
95	}
96
97	return
98}
99
100// AddPreparer prepares the Add request.
101func (client GroupUsersClient) AddPreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, UID string) (*http.Request, error) {
102	pathParameters := map[string]interface{}{
103		"groupId":           autorest.Encode("path", groupID),
104		"resourceGroupName": autorest.Encode("path", resourceGroupName),
105		"serviceName":       autorest.Encode("path", serviceName),
106		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
107		"uid":               autorest.Encode("path", UID),
108	}
109
110	const APIVersion = "2016-07-07"
111	queryParameters := map[string]interface{}{
112		"api-version": APIVersion,
113	}
114
115	preparer := autorest.CreatePreparer(
116		autorest.AsPut(),
117		autorest.WithBaseURL(client.BaseURI),
118		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users/{uid}", pathParameters),
119		autorest.WithQueryParameters(queryParameters))
120	return preparer.Prepare((&http.Request{}).WithContext(ctx))
121}
122
123// AddSender sends the Add request. The method will close the
124// http.Response Body if it receives an error.
125func (client GroupUsersClient) AddSender(req *http.Request) (*http.Response, error) {
126	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
127}
128
129// AddResponder handles the response to the Add request. The method always
130// closes the http.Response Body.
131func (client GroupUsersClient) AddResponder(resp *http.Response) (result ErrorBodyContract, err error) {
132	err = autorest.Respond(
133		resp,
134		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusMethodNotAllowed),
135		autorest.ByUnmarshallingJSON(&result),
136		autorest.ByClosing())
137	result.Response = autorest.Response{Response: resp}
138	return
139}
140
141// ListByGroup lists a collection of the members of the group, specified by its identifier.
142// Parameters:
143// resourceGroupName - the name of the resource group.
144// serviceName - the name of the API Management service.
145// groupID - group identifier. Must be unique in the current API Management service instance.
146// filter - | Field            | Supported operators    | Supported functions               |
147// |------------------|------------------------|-----------------------------------|
148// | id               | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
149// | firstName        | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
150// | lastName         | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
151// | email            | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
152// | state            | eq                     | N/A                               |
153// | registrationDate | ge, le, eq, ne, gt, lt | N/A                               |
154// | note             | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
155// top - number of records to return.
156// skip - number of records to skip.
157func (client GroupUsersClient) ListByGroup(ctx context.Context, resourceGroupName string, serviceName string, groupID string, filter string, top *int32, skip *int32) (result UserCollectionPage, err error) {
158	if tracing.IsEnabled() {
159		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUsersClient.ListByGroup")
160		defer func() {
161			sc := -1
162			if result.uc.Response.Response != nil {
163				sc = result.uc.Response.Response.StatusCode
164			}
165			tracing.EndSpan(ctx, sc, err)
166		}()
167	}
168	if err := validation.Validate([]validation.Validation{
169		{TargetValue: serviceName,
170			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
171				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
172				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
173		{TargetValue: groupID,
174			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
175				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
176				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}},
177		{TargetValue: top,
178			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
179				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
180		{TargetValue: skip,
181			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
182				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
183		return result, validation.NewError("apimanagement.GroupUsersClient", "ListByGroup", err.Error())
184	}
185
186	result.fn = client.listByGroupNextResults
187	req, err := client.ListByGroupPreparer(ctx, resourceGroupName, serviceName, groupID, filter, top, skip)
188	if err != nil {
189		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "ListByGroup", nil, "Failure preparing request")
190		return
191	}
192
193	resp, err := client.ListByGroupSender(req)
194	if err != nil {
195		result.uc.Response = autorest.Response{Response: resp}
196		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "ListByGroup", resp, "Failure sending request")
197		return
198	}
199
200	result.uc, err = client.ListByGroupResponder(resp)
201	if err != nil {
202		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "ListByGroup", resp, "Failure responding to request")
203		return
204	}
205	if result.uc.hasNextLink() && result.uc.IsEmpty() {
206		err = result.NextWithContext(ctx)
207		return
208	}
209
210	return
211}
212
213// ListByGroupPreparer prepares the ListByGroup request.
214func (client GroupUsersClient) ListByGroupPreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, filter string, top *int32, skip *int32) (*http.Request, error) {
215	pathParameters := map[string]interface{}{
216		"groupId":           autorest.Encode("path", groupID),
217		"resourceGroupName": autorest.Encode("path", resourceGroupName),
218		"serviceName":       autorest.Encode("path", serviceName),
219		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
220	}
221
222	const APIVersion = "2016-07-07"
223	queryParameters := map[string]interface{}{
224		"api-version": APIVersion,
225	}
226	if len(filter) > 0 {
227		queryParameters["$filter"] = autorest.Encode("query", filter)
228	}
229	if top != nil {
230		queryParameters["$top"] = autorest.Encode("query", *top)
231	}
232	if skip != nil {
233		queryParameters["$skip"] = autorest.Encode("query", *skip)
234	}
235
236	preparer := autorest.CreatePreparer(
237		autorest.AsGet(),
238		autorest.WithBaseURL(client.BaseURI),
239		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users", pathParameters),
240		autorest.WithQueryParameters(queryParameters))
241	return preparer.Prepare((&http.Request{}).WithContext(ctx))
242}
243
244// ListByGroupSender sends the ListByGroup request. The method will close the
245// http.Response Body if it receives an error.
246func (client GroupUsersClient) ListByGroupSender(req *http.Request) (*http.Response, error) {
247	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
248}
249
250// ListByGroupResponder handles the response to the ListByGroup request. The method always
251// closes the http.Response Body.
252func (client GroupUsersClient) ListByGroupResponder(resp *http.Response) (result UserCollection, err error) {
253	err = autorest.Respond(
254		resp,
255		azure.WithErrorUnlessStatusCode(http.StatusOK),
256		autorest.ByUnmarshallingJSON(&result),
257		autorest.ByClosing())
258	result.Response = autorest.Response{Response: resp}
259	return
260}
261
262// listByGroupNextResults retrieves the next set of results, if any.
263func (client GroupUsersClient) listByGroupNextResults(ctx context.Context, lastResults UserCollection) (result UserCollection, err error) {
264	req, err := lastResults.userCollectionPreparer(ctx)
265	if err != nil {
266		return result, autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "listByGroupNextResults", nil, "Failure preparing next results request")
267	}
268	if req == nil {
269		return
270	}
271	resp, err := client.ListByGroupSender(req)
272	if err != nil {
273		result.Response = autorest.Response{Response: resp}
274		return result, autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "listByGroupNextResults", resp, "Failure sending next results request")
275	}
276	result, err = client.ListByGroupResponder(resp)
277	if err != nil {
278		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "listByGroupNextResults", resp, "Failure responding to next results request")
279	}
280	return
281}
282
283// ListByGroupComplete enumerates all values, automatically crossing page boundaries as required.
284func (client GroupUsersClient) ListByGroupComplete(ctx context.Context, resourceGroupName string, serviceName string, groupID string, filter string, top *int32, skip *int32) (result UserCollectionIterator, err error) {
285	if tracing.IsEnabled() {
286		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUsersClient.ListByGroup")
287		defer func() {
288			sc := -1
289			if result.Response().Response.Response != nil {
290				sc = result.page.Response().Response.Response.StatusCode
291			}
292			tracing.EndSpan(ctx, sc, err)
293		}()
294	}
295	result.page, err = client.ListByGroup(ctx, resourceGroupName, serviceName, groupID, filter, top, skip)
296	return
297}
298
299// Remove remove existing user from existing group.
300// Parameters:
301// resourceGroupName - the name of the resource group.
302// serviceName - the name of the API Management service.
303// groupID - group identifier. Must be unique in the current API Management service instance.
304// UID - user identifier. Must be unique in the current API Management service instance.
305func (client GroupUsersClient) Remove(ctx context.Context, resourceGroupName string, serviceName string, groupID string, UID string) (result ErrorBodyContract, err error) {
306	if tracing.IsEnabled() {
307		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUsersClient.Remove")
308		defer func() {
309			sc := -1
310			if result.Response.Response != nil {
311				sc = result.Response.Response.StatusCode
312			}
313			tracing.EndSpan(ctx, sc, err)
314		}()
315	}
316	if err := validation.Validate([]validation.Validation{
317		{TargetValue: serviceName,
318			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
319				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
320				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
321		{TargetValue: groupID,
322			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
323				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
324				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}},
325		{TargetValue: UID,
326			Constraints: []validation.Constraint{{Target: "UID", Name: validation.MaxLength, Rule: 256, Chain: nil},
327				{Target: "UID", Name: validation.MinLength, Rule: 1, Chain: nil},
328				{Target: "UID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
329		return result, validation.NewError("apimanagement.GroupUsersClient", "Remove", err.Error())
330	}
331
332	req, err := client.RemovePreparer(ctx, resourceGroupName, serviceName, groupID, UID)
333	if err != nil {
334		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "Remove", nil, "Failure preparing request")
335		return
336	}
337
338	resp, err := client.RemoveSender(req)
339	if err != nil {
340		result.Response = autorest.Response{Response: resp}
341		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "Remove", resp, "Failure sending request")
342		return
343	}
344
345	result, err = client.RemoveResponder(resp)
346	if err != nil {
347		err = autorest.NewErrorWithError(err, "apimanagement.GroupUsersClient", "Remove", resp, "Failure responding to request")
348		return
349	}
350
351	return
352}
353
354// RemovePreparer prepares the Remove request.
355func (client GroupUsersClient) RemovePreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, UID string) (*http.Request, error) {
356	pathParameters := map[string]interface{}{
357		"groupId":           autorest.Encode("path", groupID),
358		"resourceGroupName": autorest.Encode("path", resourceGroupName),
359		"serviceName":       autorest.Encode("path", serviceName),
360		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
361		"uid":               autorest.Encode("path", UID),
362	}
363
364	const APIVersion = "2016-07-07"
365	queryParameters := map[string]interface{}{
366		"api-version": APIVersion,
367	}
368
369	preparer := autorest.CreatePreparer(
370		autorest.AsDelete(),
371		autorest.WithBaseURL(client.BaseURI),
372		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users/{uid}", pathParameters),
373		autorest.WithQueryParameters(queryParameters))
374	return preparer.Prepare((&http.Request{}).WithContext(ctx))
375}
376
377// RemoveSender sends the Remove request. The method will close the
378// http.Response Body if it receives an error.
379func (client GroupUsersClient) RemoveSender(req *http.Request) (*http.Response, error) {
380	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
381}
382
383// RemoveResponder handles the response to the Remove request. The method always
384// closes the http.Response Body.
385func (client GroupUsersClient) RemoveResponder(resp *http.Response) (result ErrorBodyContract, err error) {
386	err = autorest.Respond(
387		resp,
388		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent, http.StatusMethodNotAllowed),
389		autorest.ByUnmarshallingJSON(&result),
390		autorest.ByClosing())
391	result.Response = autorest.Response{Response: resp}
392	return
393}
394