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// GroupClient is the client for the Group methods of the Apimanagement service.
19type GroupClient struct {
20	BaseClient
21}
22
23// NewGroupClient creates an instance of the GroupClient client.
24func NewGroupClient() GroupClient {
25	return GroupClient{New()}
26}
27
28// CreateOrUpdate creates or Updates a group.
29// Parameters:
30// apimBaseURL - the management endpoint of the API Management service, for example
31// https://myapimservice.management.azure-api.net.
32// groupID - group identifier. Must be unique in the current API Management service instance.
33// parameters - create parameters.
34func (client GroupClient) CreateOrUpdate(ctx context.Context, apimBaseURL string, groupID string, parameters GroupCreateParameters) (result GroupContract, err error) {
35	if tracing.IsEnabled() {
36		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.CreateOrUpdate")
37		defer func() {
38			sc := -1
39			if result.Response.Response != nil {
40				sc = result.Response.Response.StatusCode
41			}
42			tracing.EndSpan(ctx, sc, err)
43		}()
44	}
45	if err := validation.Validate([]validation.Validation{
46		{TargetValue: groupID,
47			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
48				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
49				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}},
50		{TargetValue: parameters,
51			Constraints: []validation.Constraint{{Target: "parameters.Name", Name: validation.Null, Rule: true,
52				Chain: []validation.Constraint{{Target: "parameters.Name", Name: validation.MaxLength, Rule: 300, Chain: nil},
53					{Target: "parameters.Name", Name: validation.MinLength, Rule: 1, Chain: nil},
54				}}}}}); err != nil {
55		return result, validation.NewError("apimanagement.GroupClient", "CreateOrUpdate", err.Error())
56	}
57
58	req, err := client.CreateOrUpdatePreparer(ctx, apimBaseURL, groupID, parameters)
59	if err != nil {
60		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "CreateOrUpdate", nil, "Failure preparing request")
61		return
62	}
63
64	resp, err := client.CreateOrUpdateSender(req)
65	if err != nil {
66		result.Response = autorest.Response{Response: resp}
67		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "CreateOrUpdate", resp, "Failure sending request")
68		return
69	}
70
71	result, err = client.CreateOrUpdateResponder(resp)
72	if err != nil {
73		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "CreateOrUpdate", resp, "Failure responding to request")
74		return
75	}
76
77	return
78}
79
80// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
81func (client GroupClient) CreateOrUpdatePreparer(ctx context.Context, apimBaseURL string, groupID string, parameters GroupCreateParameters) (*http.Request, error) {
82	urlParameters := map[string]interface{}{
83		"apimBaseUrl": apimBaseURL,
84	}
85
86	pathParameters := map[string]interface{}{
87		"groupId": autorest.Encode("path", groupID),
88	}
89
90	const APIVersion = "2017-03-01"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsContentType("application/json; charset=utf-8"),
97		autorest.AsPut(),
98		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
99		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
100		autorest.WithJSON(parameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
106// http.Response Body if it receives an error.
107func (client GroupClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
109}
110
111// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
112// closes the http.Response Body.
113func (client GroupClient) CreateOrUpdateResponder(resp *http.Response) (result GroupContract, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// Delete deletes specific group of the API Management service instance.
124// Parameters:
125// apimBaseURL - the management endpoint of the API Management service, for example
126// https://myapimservice.management.azure-api.net.
127// groupID - group identifier. Must be unique in the current API Management service instance.
128// ifMatch - eTag of the Group Entity. ETag should match the current entity state from the header response of
129// the GET request or it should be * for unconditional update.
130func (client GroupClient) Delete(ctx context.Context, apimBaseURL string, groupID string, ifMatch string) (result autorest.Response, err error) {
131	if tracing.IsEnabled() {
132		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.Delete")
133		defer func() {
134			sc := -1
135			if result.Response != nil {
136				sc = result.Response.StatusCode
137			}
138			tracing.EndSpan(ctx, sc, err)
139		}()
140	}
141	if err := validation.Validate([]validation.Validation{
142		{TargetValue: groupID,
143			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
144				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
145				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
146		return result, validation.NewError("apimanagement.GroupClient", "Delete", err.Error())
147	}
148
149	req, err := client.DeletePreparer(ctx, apimBaseURL, groupID, ifMatch)
150	if err != nil {
151		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Delete", nil, "Failure preparing request")
152		return
153	}
154
155	resp, err := client.DeleteSender(req)
156	if err != nil {
157		result.Response = resp
158		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Delete", resp, "Failure sending request")
159		return
160	}
161
162	result, err = client.DeleteResponder(resp)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Delete", resp, "Failure responding to request")
165		return
166	}
167
168	return
169}
170
171// DeletePreparer prepares the Delete request.
172func (client GroupClient) DeletePreparer(ctx context.Context, apimBaseURL string, groupID string, ifMatch string) (*http.Request, error) {
173	urlParameters := map[string]interface{}{
174		"apimBaseUrl": apimBaseURL,
175	}
176
177	pathParameters := map[string]interface{}{
178		"groupId": autorest.Encode("path", groupID),
179	}
180
181	const APIVersion = "2017-03-01"
182	queryParameters := map[string]interface{}{
183		"api-version": APIVersion,
184	}
185
186	preparer := autorest.CreatePreparer(
187		autorest.AsDelete(),
188		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
189		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
190		autorest.WithQueryParameters(queryParameters),
191		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
192	return preparer.Prepare((&http.Request{}).WithContext(ctx))
193}
194
195// DeleteSender sends the Delete request. The method will close the
196// http.Response Body if it receives an error.
197func (client GroupClient) DeleteSender(req *http.Request) (*http.Response, error) {
198	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
199}
200
201// DeleteResponder handles the response to the Delete request. The method always
202// closes the http.Response Body.
203func (client GroupClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
204	err = autorest.Respond(
205		resp,
206		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
207		autorest.ByClosing())
208	result.Response = resp
209	return
210}
211
212// Get gets the details of the group specified by its identifier.
213// Parameters:
214// apimBaseURL - the management endpoint of the API Management service, for example
215// https://myapimservice.management.azure-api.net.
216// groupID - group identifier. Must be unique in the current API Management service instance.
217func (client GroupClient) Get(ctx context.Context, apimBaseURL string, groupID string) (result GroupContract, err error) {
218	if tracing.IsEnabled() {
219		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.Get")
220		defer func() {
221			sc := -1
222			if result.Response.Response != nil {
223				sc = result.Response.Response.StatusCode
224			}
225			tracing.EndSpan(ctx, sc, err)
226		}()
227	}
228	if err := validation.Validate([]validation.Validation{
229		{TargetValue: groupID,
230			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
231				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
232				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
233		return result, validation.NewError("apimanagement.GroupClient", "Get", err.Error())
234	}
235
236	req, err := client.GetPreparer(ctx, apimBaseURL, groupID)
237	if err != nil {
238		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Get", nil, "Failure preparing request")
239		return
240	}
241
242	resp, err := client.GetSender(req)
243	if err != nil {
244		result.Response = autorest.Response{Response: resp}
245		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Get", resp, "Failure sending request")
246		return
247	}
248
249	result, err = client.GetResponder(resp)
250	if err != nil {
251		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Get", resp, "Failure responding to request")
252		return
253	}
254
255	return
256}
257
258// GetPreparer prepares the Get request.
259func (client GroupClient) GetPreparer(ctx context.Context, apimBaseURL string, groupID string) (*http.Request, error) {
260	urlParameters := map[string]interface{}{
261		"apimBaseUrl": apimBaseURL,
262	}
263
264	pathParameters := map[string]interface{}{
265		"groupId": autorest.Encode("path", groupID),
266	}
267
268	const APIVersion = "2017-03-01"
269	queryParameters := map[string]interface{}{
270		"api-version": APIVersion,
271	}
272
273	preparer := autorest.CreatePreparer(
274		autorest.AsGet(),
275		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
276		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
277		autorest.WithQueryParameters(queryParameters))
278	return preparer.Prepare((&http.Request{}).WithContext(ctx))
279}
280
281// GetSender sends the Get request. The method will close the
282// http.Response Body if it receives an error.
283func (client GroupClient) GetSender(req *http.Request) (*http.Response, error) {
284	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
285}
286
287// GetResponder handles the response to the Get request. The method always
288// closes the http.Response Body.
289func (client GroupClient) GetResponder(resp *http.Response) (result GroupContract, err error) {
290	err = autorest.Respond(
291		resp,
292		azure.WithErrorUnlessStatusCode(http.StatusOK),
293		autorest.ByUnmarshallingJSON(&result),
294		autorest.ByClosing())
295	result.Response = autorest.Response{Response: resp}
296	return
297}
298
299// List lists a collection of groups defined within a service instance.
300// Parameters:
301// apimBaseURL - the management endpoint of the API Management service, for example
302// https://myapimservice.management.azure-api.net.
303// filter - | Field       | Supported operators    | Supported functions                         |
304// |-------------|------------------------|---------------------------------------------|
305// | id          | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
306// | name        | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
307// | description | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
308// | type        | eq, ne                 | N/A                                         |
309// top - number of records to return.
310// skip - number of records to skip.
311func (client GroupClient) List(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result GroupCollectionPage, err error) {
312	if tracing.IsEnabled() {
313		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.List")
314		defer func() {
315			sc := -1
316			if result.gc.Response.Response != nil {
317				sc = result.gc.Response.Response.StatusCode
318			}
319			tracing.EndSpan(ctx, sc, err)
320		}()
321	}
322	if err := validation.Validate([]validation.Validation{
323		{TargetValue: top,
324			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
325				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
326		{TargetValue: skip,
327			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
328				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
329		return result, validation.NewError("apimanagement.GroupClient", "List", err.Error())
330	}
331
332	result.fn = client.listNextResults
333	req, err := client.ListPreparer(ctx, apimBaseURL, filter, top, skip)
334	if err != nil {
335		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "List", nil, "Failure preparing request")
336		return
337	}
338
339	resp, err := client.ListSender(req)
340	if err != nil {
341		result.gc.Response = autorest.Response{Response: resp}
342		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "List", resp, "Failure sending request")
343		return
344	}
345
346	result.gc, err = client.ListResponder(resp)
347	if err != nil {
348		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "List", resp, "Failure responding to request")
349		return
350	}
351	if result.gc.hasNextLink() && result.gc.IsEmpty() {
352		err = result.NextWithContext(ctx)
353		return
354	}
355
356	return
357}
358
359// ListPreparer prepares the List request.
360func (client GroupClient) ListPreparer(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (*http.Request, error) {
361	urlParameters := map[string]interface{}{
362		"apimBaseUrl": apimBaseURL,
363	}
364
365	const APIVersion = "2017-03-01"
366	queryParameters := map[string]interface{}{
367		"api-version": APIVersion,
368	}
369	if len(filter) > 0 {
370		queryParameters["$filter"] = autorest.Encode("query", filter)
371	}
372	if top != nil {
373		queryParameters["$top"] = autorest.Encode("query", *top)
374	}
375	if skip != nil {
376		queryParameters["$skip"] = autorest.Encode("query", *skip)
377	}
378
379	preparer := autorest.CreatePreparer(
380		autorest.AsGet(),
381		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
382		autorest.WithPath("/groups"),
383		autorest.WithQueryParameters(queryParameters))
384	return preparer.Prepare((&http.Request{}).WithContext(ctx))
385}
386
387// ListSender sends the List request. The method will close the
388// http.Response Body if it receives an error.
389func (client GroupClient) ListSender(req *http.Request) (*http.Response, error) {
390	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
391}
392
393// ListResponder handles the response to the List request. The method always
394// closes the http.Response Body.
395func (client GroupClient) ListResponder(resp *http.Response) (result GroupCollection, err error) {
396	err = autorest.Respond(
397		resp,
398		azure.WithErrorUnlessStatusCode(http.StatusOK),
399		autorest.ByUnmarshallingJSON(&result),
400		autorest.ByClosing())
401	result.Response = autorest.Response{Response: resp}
402	return
403}
404
405// listNextResults retrieves the next set of results, if any.
406func (client GroupClient) listNextResults(ctx context.Context, lastResults GroupCollection) (result GroupCollection, err error) {
407	req, err := lastResults.groupCollectionPreparer(ctx)
408	if err != nil {
409		return result, autorest.NewErrorWithError(err, "apimanagement.GroupClient", "listNextResults", nil, "Failure preparing next results request")
410	}
411	if req == nil {
412		return
413	}
414	resp, err := client.ListSender(req)
415	if err != nil {
416		result.Response = autorest.Response{Response: resp}
417		return result, autorest.NewErrorWithError(err, "apimanagement.GroupClient", "listNextResults", resp, "Failure sending next results request")
418	}
419	result, err = client.ListResponder(resp)
420	if err != nil {
421		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "listNextResults", resp, "Failure responding to next results request")
422	}
423	return
424}
425
426// ListComplete enumerates all values, automatically crossing page boundaries as required.
427func (client GroupClient) ListComplete(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result GroupCollectionIterator, err error) {
428	if tracing.IsEnabled() {
429		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.List")
430		defer func() {
431			sc := -1
432			if result.Response().Response.Response != nil {
433				sc = result.page.Response().Response.Response.StatusCode
434			}
435			tracing.EndSpan(ctx, sc, err)
436		}()
437	}
438	result.page, err = client.List(ctx, apimBaseURL, filter, top, skip)
439	return
440}
441
442// Update updates the details of the group specified by its identifier.
443// Parameters:
444// apimBaseURL - the management endpoint of the API Management service, for example
445// https://myapimservice.management.azure-api.net.
446// groupID - group identifier. Must be unique in the current API Management service instance.
447// parameters - update parameters.
448// ifMatch - eTag of the Group Entity. ETag should match the current entity state from the header response of
449// the GET request or it should be * for unconditional update.
450func (client GroupClient) Update(ctx context.Context, apimBaseURL string, groupID string, parameters GroupUpdateParameters, ifMatch string) (result autorest.Response, err error) {
451	if tracing.IsEnabled() {
452		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.Update")
453		defer func() {
454			sc := -1
455			if result.Response != nil {
456				sc = result.Response.StatusCode
457			}
458			tracing.EndSpan(ctx, sc, err)
459		}()
460	}
461	if err := validation.Validate([]validation.Validation{
462		{TargetValue: groupID,
463			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
464				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
465				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
466		return result, validation.NewError("apimanagement.GroupClient", "Update", err.Error())
467	}
468
469	req, err := client.UpdatePreparer(ctx, apimBaseURL, groupID, parameters, ifMatch)
470	if err != nil {
471		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Update", nil, "Failure preparing request")
472		return
473	}
474
475	resp, err := client.UpdateSender(req)
476	if err != nil {
477		result.Response = resp
478		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Update", resp, "Failure sending request")
479		return
480	}
481
482	result, err = client.UpdateResponder(resp)
483	if err != nil {
484		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Update", resp, "Failure responding to request")
485		return
486	}
487
488	return
489}
490
491// UpdatePreparer prepares the Update request.
492func (client GroupClient) UpdatePreparer(ctx context.Context, apimBaseURL string, groupID string, parameters GroupUpdateParameters, ifMatch string) (*http.Request, error) {
493	urlParameters := map[string]interface{}{
494		"apimBaseUrl": apimBaseURL,
495	}
496
497	pathParameters := map[string]interface{}{
498		"groupId": autorest.Encode("path", groupID),
499	}
500
501	const APIVersion = "2017-03-01"
502	queryParameters := map[string]interface{}{
503		"api-version": APIVersion,
504	}
505
506	preparer := autorest.CreatePreparer(
507		autorest.AsContentType("application/json; charset=utf-8"),
508		autorest.AsPatch(),
509		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
510		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
511		autorest.WithJSON(parameters),
512		autorest.WithQueryParameters(queryParameters),
513		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
514	return preparer.Prepare((&http.Request{}).WithContext(ctx))
515}
516
517// UpdateSender sends the Update request. The method will close the
518// http.Response Body if it receives an error.
519func (client GroupClient) UpdateSender(req *http.Request) (*http.Response, error) {
520	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
521}
522
523// UpdateResponder handles the response to the Update request. The method always
524// closes the http.Response Body.
525func (client GroupClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
526	err = autorest.Respond(
527		resp,
528		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
529		autorest.ByClosing())
530	result.Response = resp
531	return
532}
533