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