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// GroupUserClient is the apiManagement Client
30type GroupUserClient struct {
31	BaseClient
32}
33
34// NewGroupUserClient creates an instance of the GroupUserClient client.
35func NewGroupUserClient(subscriptionID string) GroupUserClient {
36	return NewGroupUserClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewGroupUserClientWithBaseURI creates an instance of the GroupUserClient client.
40func NewGroupUserClientWithBaseURI(baseURI string, subscriptionID string) GroupUserClient {
41	return GroupUserClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// CheckEntityExists checks that user entity specified by identifier is associated with the group entity.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// serviceName - the name of the API Management service.
48// groupID - group identifier. Must be unique in the current API Management service instance.
49// userID - user identifier. Must be unique in the current API Management service instance.
50func (client GroupUserClient) CheckEntityExists(ctx context.Context, resourceGroupName string, serviceName string, groupID string, userID string) (result autorest.Response, err error) {
51	if tracing.IsEnabled() {
52		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUserClient.CheckEntityExists")
53		defer func() {
54			sc := -1
55			if result.Response != nil {
56				sc = result.Response.StatusCode
57			}
58			tracing.EndSpan(ctx, sc, err)
59		}()
60	}
61	if err := validation.Validate([]validation.Validation{
62		{TargetValue: serviceName,
63			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
64				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
65				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
66		{TargetValue: groupID,
67			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
68				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
69		{TargetValue: userID,
70			Constraints: []validation.Constraint{{Target: "userID", Name: validation.MaxLength, Rule: 80, Chain: nil},
71				{Target: "userID", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
72		return result, validation.NewError("apimanagement.GroupUserClient", "CheckEntityExists", err.Error())
73	}
74
75	req, err := client.CheckEntityExistsPreparer(ctx, resourceGroupName, serviceName, groupID, userID)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "CheckEntityExists", nil, "Failure preparing request")
78		return
79	}
80
81	resp, err := client.CheckEntityExistsSender(req)
82	if err != nil {
83		result.Response = resp
84		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "CheckEntityExists", resp, "Failure sending request")
85		return
86	}
87
88	result, err = client.CheckEntityExistsResponder(resp)
89	if err != nil {
90		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "CheckEntityExists", resp, "Failure responding to request")
91	}
92
93	return
94}
95
96// CheckEntityExistsPreparer prepares the CheckEntityExists request.
97func (client GroupUserClient) CheckEntityExistsPreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, userID string) (*http.Request, error) {
98	pathParameters := map[string]interface{}{
99		"groupId":           autorest.Encode("path", groupID),
100		"resourceGroupName": autorest.Encode("path", resourceGroupName),
101		"serviceName":       autorest.Encode("path", serviceName),
102		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
103		"userId":            autorest.Encode("path", userID),
104	}
105
106	const APIVersion = "2019-01-01"
107	queryParameters := map[string]interface{}{
108		"api-version": APIVersion,
109	}
110
111	preparer := autorest.CreatePreparer(
112		autorest.AsHead(),
113		autorest.WithBaseURL(client.BaseURI),
114		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users/{userId}", pathParameters),
115		autorest.WithQueryParameters(queryParameters))
116	return preparer.Prepare((&http.Request{}).WithContext(ctx))
117}
118
119// CheckEntityExistsSender sends the CheckEntityExists request. The method will close the
120// http.Response Body if it receives an error.
121func (client GroupUserClient) CheckEntityExistsSender(req *http.Request) (*http.Response, error) {
122	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
123	return autorest.SendWithSender(client, req, sd...)
124}
125
126// CheckEntityExistsResponder handles the response to the CheckEntityExists request. The method always
127// closes the http.Response Body.
128func (client GroupUserClient) CheckEntityExistsResponder(resp *http.Response) (result autorest.Response, err error) {
129	err = autorest.Respond(
130		resp,
131		client.ByInspecting(),
132		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent, http.StatusNotFound),
133		autorest.ByClosing())
134	result.Response = resp
135	return
136}
137
138// Create add existing user to existing group
139// Parameters:
140// resourceGroupName - the name of the resource group.
141// serviceName - the name of the API Management service.
142// groupID - group identifier. Must be unique in the current API Management service instance.
143// userID - user identifier. Must be unique in the current API Management service instance.
144func (client GroupUserClient) Create(ctx context.Context, resourceGroupName string, serviceName string, groupID string, userID string) (result UserContract, err error) {
145	if tracing.IsEnabled() {
146		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUserClient.Create")
147		defer func() {
148			sc := -1
149			if result.Response.Response != nil {
150				sc = result.Response.Response.StatusCode
151			}
152			tracing.EndSpan(ctx, sc, err)
153		}()
154	}
155	if err := validation.Validate([]validation.Validation{
156		{TargetValue: serviceName,
157			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
158				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
159				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
160		{TargetValue: groupID,
161			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
162				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
163		{TargetValue: userID,
164			Constraints: []validation.Constraint{{Target: "userID", Name: validation.MaxLength, Rule: 80, Chain: nil},
165				{Target: "userID", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
166		return result, validation.NewError("apimanagement.GroupUserClient", "Create", err.Error())
167	}
168
169	req, err := client.CreatePreparer(ctx, resourceGroupName, serviceName, groupID, userID)
170	if err != nil {
171		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "Create", nil, "Failure preparing request")
172		return
173	}
174
175	resp, err := client.CreateSender(req)
176	if err != nil {
177		result.Response = autorest.Response{Response: resp}
178		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "Create", resp, "Failure sending request")
179		return
180	}
181
182	result, err = client.CreateResponder(resp)
183	if err != nil {
184		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "Create", resp, "Failure responding to request")
185	}
186
187	return
188}
189
190// CreatePreparer prepares the Create request.
191func (client GroupUserClient) CreatePreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, userID string) (*http.Request, error) {
192	pathParameters := map[string]interface{}{
193		"groupId":           autorest.Encode("path", groupID),
194		"resourceGroupName": autorest.Encode("path", resourceGroupName),
195		"serviceName":       autorest.Encode("path", serviceName),
196		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
197		"userId":            autorest.Encode("path", userID),
198	}
199
200	const APIVersion = "2019-01-01"
201	queryParameters := map[string]interface{}{
202		"api-version": APIVersion,
203	}
204
205	preparer := autorest.CreatePreparer(
206		autorest.AsPut(),
207		autorest.WithBaseURL(client.BaseURI),
208		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users/{userId}", pathParameters),
209		autorest.WithQueryParameters(queryParameters))
210	return preparer.Prepare((&http.Request{}).WithContext(ctx))
211}
212
213// CreateSender sends the Create request. The method will close the
214// http.Response Body if it receives an error.
215func (client GroupUserClient) CreateSender(req *http.Request) (*http.Response, error) {
216	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
217	return autorest.SendWithSender(client, req, sd...)
218}
219
220// CreateResponder handles the response to the Create request. The method always
221// closes the http.Response Body.
222func (client GroupUserClient) CreateResponder(resp *http.Response) (result UserContract, err error) {
223	err = autorest.Respond(
224		resp,
225		client.ByInspecting(),
226		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
227		autorest.ByUnmarshallingJSON(&result),
228		autorest.ByClosing())
229	result.Response = autorest.Response{Response: resp}
230	return
231}
232
233// Delete remove existing user from existing group.
234// Parameters:
235// resourceGroupName - the name of the resource group.
236// serviceName - the name of the API Management service.
237// groupID - group identifier. Must be unique in the current API Management service instance.
238// userID - user identifier. Must be unique in the current API Management service instance.
239func (client GroupUserClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, groupID string, userID string) (result autorest.Response, err error) {
240	if tracing.IsEnabled() {
241		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUserClient.Delete")
242		defer func() {
243			sc := -1
244			if result.Response != nil {
245				sc = result.Response.StatusCode
246			}
247			tracing.EndSpan(ctx, sc, err)
248		}()
249	}
250	if err := validation.Validate([]validation.Validation{
251		{TargetValue: serviceName,
252			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
253				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
254				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
255		{TargetValue: groupID,
256			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
257				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
258		{TargetValue: userID,
259			Constraints: []validation.Constraint{{Target: "userID", Name: validation.MaxLength, Rule: 80, Chain: nil},
260				{Target: "userID", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
261		return result, validation.NewError("apimanagement.GroupUserClient", "Delete", err.Error())
262	}
263
264	req, err := client.DeletePreparer(ctx, resourceGroupName, serviceName, groupID, userID)
265	if err != nil {
266		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "Delete", nil, "Failure preparing request")
267		return
268	}
269
270	resp, err := client.DeleteSender(req)
271	if err != nil {
272		result.Response = resp
273		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "Delete", resp, "Failure sending request")
274		return
275	}
276
277	result, err = client.DeleteResponder(resp)
278	if err != nil {
279		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "Delete", resp, "Failure responding to request")
280	}
281
282	return
283}
284
285// DeletePreparer prepares the Delete request.
286func (client GroupUserClient) DeletePreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, userID string) (*http.Request, error) {
287	pathParameters := map[string]interface{}{
288		"groupId":           autorest.Encode("path", groupID),
289		"resourceGroupName": autorest.Encode("path", resourceGroupName),
290		"serviceName":       autorest.Encode("path", serviceName),
291		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
292		"userId":            autorest.Encode("path", userID),
293	}
294
295	const APIVersion = "2019-01-01"
296	queryParameters := map[string]interface{}{
297		"api-version": APIVersion,
298	}
299
300	preparer := autorest.CreatePreparer(
301		autorest.AsDelete(),
302		autorest.WithBaseURL(client.BaseURI),
303		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users/{userId}", pathParameters),
304		autorest.WithQueryParameters(queryParameters))
305	return preparer.Prepare((&http.Request{}).WithContext(ctx))
306}
307
308// DeleteSender sends the Delete request. The method will close the
309// http.Response Body if it receives an error.
310func (client GroupUserClient) DeleteSender(req *http.Request) (*http.Response, error) {
311	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
312	return autorest.SendWithSender(client, req, sd...)
313}
314
315// DeleteResponder handles the response to the Delete request. The method always
316// closes the http.Response Body.
317func (client GroupUserClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
318	err = autorest.Respond(
319		resp,
320		client.ByInspecting(),
321		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
322		autorest.ByClosing())
323	result.Response = resp
324	return
325}
326
327// List lists a collection of user entities associated with the group.
328// Parameters:
329// resourceGroupName - the name of the resource group.
330// serviceName - the name of the API Management service.
331// groupID - group identifier. Must be unique in the current API Management service instance.
332// filter - |   Field     |     Usage     |     Supported operators     |     Supported functions
333// |</br>|-------------|-------------|-------------|-------------|</br>| name | filter | ge, le, eq, ne, gt, lt
334// | substringof, contains, startswith, endswith | </br>| firstName | filter | ge, le, eq, ne, gt, lt |
335// substringof, contains, startswith, endswith | </br>| lastName | filter | ge, le, eq, ne, gt, lt |
336// substringof, contains, startswith, endswith | </br>| email | filter | ge, le, eq, ne, gt, lt | substringof,
337// contains, startswith, endswith | </br>| registrationDate | filter | ge, le, eq, ne, gt, lt |     | </br>|
338// note | filter | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith | </br>
339// top - number of records to return.
340// skip - number of records to skip.
341func (client GroupUserClient) List(ctx context.Context, resourceGroupName string, serviceName string, groupID string, filter string, top *int32, skip *int32) (result UserCollectionPage, err error) {
342	if tracing.IsEnabled() {
343		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUserClient.List")
344		defer func() {
345			sc := -1
346			if result.uc.Response.Response != nil {
347				sc = result.uc.Response.Response.StatusCode
348			}
349			tracing.EndSpan(ctx, sc, err)
350		}()
351	}
352	if err := validation.Validate([]validation.Validation{
353		{TargetValue: serviceName,
354			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
355				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
356				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
357		{TargetValue: groupID,
358			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
359				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
360		{TargetValue: top,
361			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
362				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
363		{TargetValue: skip,
364			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
365				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
366		return result, validation.NewError("apimanagement.GroupUserClient", "List", err.Error())
367	}
368
369	result.fn = client.listNextResults
370	req, err := client.ListPreparer(ctx, resourceGroupName, serviceName, groupID, filter, top, skip)
371	if err != nil {
372		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "List", nil, "Failure preparing request")
373		return
374	}
375
376	resp, err := client.ListSender(req)
377	if err != nil {
378		result.uc.Response = autorest.Response{Response: resp}
379		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "List", resp, "Failure sending request")
380		return
381	}
382
383	result.uc, err = client.ListResponder(resp)
384	if err != nil {
385		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "List", resp, "Failure responding to request")
386	}
387
388	return
389}
390
391// ListPreparer prepares the List request.
392func (client GroupUserClient) ListPreparer(ctx context.Context, resourceGroupName string, serviceName string, groupID string, filter string, top *int32, skip *int32) (*http.Request, error) {
393	pathParameters := map[string]interface{}{
394		"groupId":           autorest.Encode("path", groupID),
395		"resourceGroupName": autorest.Encode("path", resourceGroupName),
396		"serviceName":       autorest.Encode("path", serviceName),
397		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
398	}
399
400	const APIVersion = "2019-01-01"
401	queryParameters := map[string]interface{}{
402		"api-version": APIVersion,
403	}
404	if len(filter) > 0 {
405		queryParameters["$filter"] = autorest.Encode("query", filter)
406	}
407	if top != nil {
408		queryParameters["$top"] = autorest.Encode("query", *top)
409	}
410	if skip != nil {
411		queryParameters["$skip"] = autorest.Encode("query", *skip)
412	}
413
414	preparer := autorest.CreatePreparer(
415		autorest.AsGet(),
416		autorest.WithBaseURL(client.BaseURI),
417		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/groups/{groupId}/users", pathParameters),
418		autorest.WithQueryParameters(queryParameters))
419	return preparer.Prepare((&http.Request{}).WithContext(ctx))
420}
421
422// ListSender sends the List request. The method will close the
423// http.Response Body if it receives an error.
424func (client GroupUserClient) ListSender(req *http.Request) (*http.Response, error) {
425	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
426	return autorest.SendWithSender(client, req, sd...)
427}
428
429// ListResponder handles the response to the List request. The method always
430// closes the http.Response Body.
431func (client GroupUserClient) ListResponder(resp *http.Response) (result UserCollection, err error) {
432	err = autorest.Respond(
433		resp,
434		client.ByInspecting(),
435		azure.WithErrorUnlessStatusCode(http.StatusOK),
436		autorest.ByUnmarshallingJSON(&result),
437		autorest.ByClosing())
438	result.Response = autorest.Response{Response: resp}
439	return
440}
441
442// listNextResults retrieves the next set of results, if any.
443func (client GroupUserClient) listNextResults(ctx context.Context, lastResults UserCollection) (result UserCollection, err error) {
444	req, err := lastResults.userCollectionPreparer(ctx)
445	if err != nil {
446		return result, autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "listNextResults", nil, "Failure preparing next results request")
447	}
448	if req == nil {
449		return
450	}
451	resp, err := client.ListSender(req)
452	if err != nil {
453		result.Response = autorest.Response{Response: resp}
454		return result, autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "listNextResults", resp, "Failure sending next results request")
455	}
456	result, err = client.ListResponder(resp)
457	if err != nil {
458		err = autorest.NewErrorWithError(err, "apimanagement.GroupUserClient", "listNextResults", resp, "Failure responding to next results request")
459	}
460	return
461}
462
463// ListComplete enumerates all values, automatically crossing page boundaries as required.
464func (client GroupUserClient) ListComplete(ctx context.Context, resourceGroupName string, serviceName string, groupID string, filter string, top *int32, skip *int32) (result UserCollectionIterator, err error) {
465	if tracing.IsEnabled() {
466		ctx = tracing.StartSpan(ctx, fqdn+"/GroupUserClient.List")
467		defer func() {
468			sc := -1
469			if result.Response().Response.Response != nil {
470				sc = result.page.Response().Response.Response.StatusCode
471			}
472			tracing.EndSpan(ctx, sc, err)
473		}()
474	}
475	result.page, err = client.List(ctx, resourceGroupName, serviceName, groupID, filter, top, skip)
476	return
477}
478