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		return
86	}
87
88	return
89}
90
91// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
92func (client GroupClient) CreateOrUpdatePreparer(ctx context.Context, apimBaseURL string, groupID string, parameters GroupCreateParameters) (*http.Request, error) {
93	urlParameters := map[string]interface{}{
94		"apimBaseUrl": apimBaseURL,
95	}
96
97	pathParameters := map[string]interface{}{
98		"groupId": autorest.Encode("path", groupID),
99	}
100
101	const APIVersion = "2017-03-01"
102	queryParameters := map[string]interface{}{
103		"api-version": APIVersion,
104	}
105
106	preparer := autorest.CreatePreparer(
107		autorest.AsContentType("application/json; charset=utf-8"),
108		autorest.AsPut(),
109		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
110		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
111		autorest.WithJSON(parameters),
112		autorest.WithQueryParameters(queryParameters))
113	return preparer.Prepare((&http.Request{}).WithContext(ctx))
114}
115
116// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
117// http.Response Body if it receives an error.
118func (client GroupClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
119	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
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		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
128		autorest.ByUnmarshallingJSON(&result),
129		autorest.ByClosing())
130	result.Response = autorest.Response{Response: resp}
131	return
132}
133
134// Delete deletes specific group of the API Management service instance.
135// Parameters:
136// apimBaseURL - the management endpoint of the API Management service, for example
137// https://myapimservice.management.azure-api.net.
138// groupID - group identifier. Must be unique in the current API Management service instance.
139// ifMatch - eTag of the Group Entity. ETag should match the current entity state from the header response of
140// the GET request or it should be * for unconditional update.
141func (client GroupClient) Delete(ctx context.Context, apimBaseURL string, groupID string, ifMatch string) (result autorest.Response, err error) {
142	if tracing.IsEnabled() {
143		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.Delete")
144		defer func() {
145			sc := -1
146			if result.Response != nil {
147				sc = result.Response.StatusCode
148			}
149			tracing.EndSpan(ctx, sc, err)
150		}()
151	}
152	if err := validation.Validate([]validation.Validation{
153		{TargetValue: groupID,
154			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
155				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
156				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
157		return result, validation.NewError("apimanagement.GroupClient", "Delete", err.Error())
158	}
159
160	req, err := client.DeletePreparer(ctx, apimBaseURL, groupID, ifMatch)
161	if err != nil {
162		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Delete", nil, "Failure preparing request")
163		return
164	}
165
166	resp, err := client.DeleteSender(req)
167	if err != nil {
168		result.Response = resp
169		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Delete", resp, "Failure sending request")
170		return
171	}
172
173	result, err = client.DeleteResponder(resp)
174	if err != nil {
175		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Delete", resp, "Failure responding to request")
176		return
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	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
210}
211
212// DeleteResponder handles the response to the Delete request. The method always
213// closes the http.Response Body.
214func (client GroupClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
215	err = autorest.Respond(
216		resp,
217		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
218		autorest.ByClosing())
219	result.Response = resp
220	return
221}
222
223// Get gets the details of the group specified by its identifier.
224// Parameters:
225// apimBaseURL - the management endpoint of the API Management service, for example
226// https://myapimservice.management.azure-api.net.
227// groupID - group identifier. Must be unique in the current API Management service instance.
228func (client GroupClient) Get(ctx context.Context, apimBaseURL string, groupID string) (result GroupContract, err error) {
229	if tracing.IsEnabled() {
230		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.Get")
231		defer func() {
232			sc := -1
233			if result.Response.Response != nil {
234				sc = result.Response.Response.StatusCode
235			}
236			tracing.EndSpan(ctx, sc, err)
237		}()
238	}
239	if err := validation.Validate([]validation.Validation{
240		{TargetValue: groupID,
241			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
242				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
243				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
244		return result, validation.NewError("apimanagement.GroupClient", "Get", err.Error())
245	}
246
247	req, err := client.GetPreparer(ctx, apimBaseURL, groupID)
248	if err != nil {
249		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Get", nil, "Failure preparing request")
250		return
251	}
252
253	resp, err := client.GetSender(req)
254	if err != nil {
255		result.Response = autorest.Response{Response: resp}
256		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Get", resp, "Failure sending request")
257		return
258	}
259
260	result, err = client.GetResponder(resp)
261	if err != nil {
262		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Get", resp, "Failure responding to request")
263		return
264	}
265
266	return
267}
268
269// GetPreparer prepares the Get request.
270func (client GroupClient) GetPreparer(ctx context.Context, apimBaseURL string, groupID string) (*http.Request, error) {
271	urlParameters := map[string]interface{}{
272		"apimBaseUrl": apimBaseURL,
273	}
274
275	pathParameters := map[string]interface{}{
276		"groupId": autorest.Encode("path", groupID),
277	}
278
279	const APIVersion = "2017-03-01"
280	queryParameters := map[string]interface{}{
281		"api-version": APIVersion,
282	}
283
284	preparer := autorest.CreatePreparer(
285		autorest.AsGet(),
286		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
287		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
288		autorest.WithQueryParameters(queryParameters))
289	return preparer.Prepare((&http.Request{}).WithContext(ctx))
290}
291
292// GetSender sends the Get request. The method will close the
293// http.Response Body if it receives an error.
294func (client GroupClient) GetSender(req *http.Request) (*http.Response, error) {
295	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
296}
297
298// GetResponder handles the response to the Get request. The method always
299// closes the http.Response Body.
300func (client GroupClient) GetResponder(resp *http.Response) (result GroupContract, err error) {
301	err = autorest.Respond(
302		resp,
303		azure.WithErrorUnlessStatusCode(http.StatusOK),
304		autorest.ByUnmarshallingJSON(&result),
305		autorest.ByClosing())
306	result.Response = autorest.Response{Response: resp}
307	return
308}
309
310// List lists a collection of groups defined within a service instance.
311// Parameters:
312// apimBaseURL - the management endpoint of the API Management service, for example
313// https://myapimservice.management.azure-api.net.
314// filter - | Field       | Supported operators    | Supported functions                         |
315// |-------------|------------------------|---------------------------------------------|
316// | id          | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
317// | name        | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
318// | description | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
319// | type        | eq, ne                 | N/A                                         |
320// top - number of records to return.
321// skip - number of records to skip.
322func (client GroupClient) List(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result GroupCollectionPage, err error) {
323	if tracing.IsEnabled() {
324		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.List")
325		defer func() {
326			sc := -1
327			if result.gc.Response.Response != nil {
328				sc = result.gc.Response.Response.StatusCode
329			}
330			tracing.EndSpan(ctx, sc, err)
331		}()
332	}
333	if err := validation.Validate([]validation.Validation{
334		{TargetValue: top,
335			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
336				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
337		{TargetValue: skip,
338			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
339				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
340		return result, validation.NewError("apimanagement.GroupClient", "List", err.Error())
341	}
342
343	result.fn = client.listNextResults
344	req, err := client.ListPreparer(ctx, apimBaseURL, filter, top, skip)
345	if err != nil {
346		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "List", nil, "Failure preparing request")
347		return
348	}
349
350	resp, err := client.ListSender(req)
351	if err != nil {
352		result.gc.Response = autorest.Response{Response: resp}
353		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "List", resp, "Failure sending request")
354		return
355	}
356
357	result.gc, err = client.ListResponder(resp)
358	if err != nil {
359		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "List", resp, "Failure responding to request")
360		return
361	}
362	if result.gc.hasNextLink() && result.gc.IsEmpty() {
363		err = result.NextWithContext(ctx)
364		return
365	}
366
367	return
368}
369
370// ListPreparer prepares the List request.
371func (client GroupClient) ListPreparer(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (*http.Request, error) {
372	urlParameters := map[string]interface{}{
373		"apimBaseUrl": apimBaseURL,
374	}
375
376	const APIVersion = "2017-03-01"
377	queryParameters := map[string]interface{}{
378		"api-version": APIVersion,
379	}
380	if len(filter) > 0 {
381		queryParameters["$filter"] = autorest.Encode("query", filter)
382	}
383	if top != nil {
384		queryParameters["$top"] = autorest.Encode("query", *top)
385	}
386	if skip != nil {
387		queryParameters["$skip"] = autorest.Encode("query", *skip)
388	}
389
390	preparer := autorest.CreatePreparer(
391		autorest.AsGet(),
392		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
393		autorest.WithPath("/groups"),
394		autorest.WithQueryParameters(queryParameters))
395	return preparer.Prepare((&http.Request{}).WithContext(ctx))
396}
397
398// ListSender sends the List request. The method will close the
399// http.Response Body if it receives an error.
400func (client GroupClient) ListSender(req *http.Request) (*http.Response, error) {
401	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
402}
403
404// ListResponder handles the response to the List request. The method always
405// closes the http.Response Body.
406func (client GroupClient) ListResponder(resp *http.Response) (result GroupCollection, err error) {
407	err = autorest.Respond(
408		resp,
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		return
497	}
498
499	return
500}
501
502// UpdatePreparer prepares the Update request.
503func (client GroupClient) UpdatePreparer(ctx context.Context, apimBaseURL string, groupID string, parameters GroupUpdateParameters, ifMatch string) (*http.Request, error) {
504	urlParameters := map[string]interface{}{
505		"apimBaseUrl": apimBaseURL,
506	}
507
508	pathParameters := map[string]interface{}{
509		"groupId": autorest.Encode("path", groupID),
510	}
511
512	const APIVersion = "2017-03-01"
513	queryParameters := map[string]interface{}{
514		"api-version": APIVersion,
515	}
516
517	preparer := autorest.CreatePreparer(
518		autorest.AsContentType("application/json; charset=utf-8"),
519		autorest.AsPatch(),
520		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
521		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
522		autorest.WithJSON(parameters),
523		autorest.WithQueryParameters(queryParameters),
524		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
525	return preparer.Prepare((&http.Request{}).WithContext(ctx))
526}
527
528// UpdateSender sends the Update request. The method will close the
529// http.Response Body if it receives an error.
530func (client GroupClient) UpdateSender(req *http.Request) (*http.Response, error) {
531	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
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		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
540		autorest.ByClosing())
541	result.Response = resp
542	return
543}
544