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	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
119}
120
121// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
122// closes the http.Response Body.
123func (client GroupClient) CreateOrUpdateResponder(resp *http.Response) (result GroupContract, err error) {
124	err = autorest.Respond(
125		resp,
126		client.ByInspecting(),
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	}
177
178	return
179}
180
181// DeletePreparer prepares the Delete request.
182func (client GroupClient) DeletePreparer(ctx context.Context, apimBaseURL string, groupID string, ifMatch string) (*http.Request, error) {
183	urlParameters := map[string]interface{}{
184		"apimBaseUrl": apimBaseURL,
185	}
186
187	pathParameters := map[string]interface{}{
188		"groupId": autorest.Encode("path", groupID),
189	}
190
191	const APIVersion = "2017-03-01"
192	queryParameters := map[string]interface{}{
193		"api-version": APIVersion,
194	}
195
196	preparer := autorest.CreatePreparer(
197		autorest.AsDelete(),
198		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
199		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
200		autorest.WithQueryParameters(queryParameters),
201		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
202	return preparer.Prepare((&http.Request{}).WithContext(ctx))
203}
204
205// DeleteSender sends the Delete request. The method will close the
206// http.Response Body if it receives an error.
207func (client GroupClient) DeleteSender(req *http.Request) (*http.Response, error) {
208	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
209}
210
211// DeleteResponder handles the response to the Delete request. The method always
212// closes the http.Response Body.
213func (client GroupClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
214	err = autorest.Respond(
215		resp,
216		client.ByInspecting(),
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	}
264
265	return
266}
267
268// GetPreparer prepares the Get request.
269func (client GroupClient) GetPreparer(ctx context.Context, apimBaseURL string, groupID string) (*http.Request, error) {
270	urlParameters := map[string]interface{}{
271		"apimBaseUrl": apimBaseURL,
272	}
273
274	pathParameters := map[string]interface{}{
275		"groupId": autorest.Encode("path", groupID),
276	}
277
278	const APIVersion = "2017-03-01"
279	queryParameters := map[string]interface{}{
280		"api-version": APIVersion,
281	}
282
283	preparer := autorest.CreatePreparer(
284		autorest.AsGet(),
285		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
286		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
287		autorest.WithQueryParameters(queryParameters))
288	return preparer.Prepare((&http.Request{}).WithContext(ctx))
289}
290
291// GetSender sends the Get request. The method will close the
292// http.Response Body if it receives an error.
293func (client GroupClient) GetSender(req *http.Request) (*http.Response, error) {
294	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
295}
296
297// GetResponder handles the response to the Get request. The method always
298// closes the http.Response Body.
299func (client GroupClient) GetResponder(resp *http.Response) (result GroupContract, err error) {
300	err = autorest.Respond(
301		resp,
302		client.ByInspecting(),
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	}
361
362	return
363}
364
365// ListPreparer prepares the List request.
366func (client GroupClient) ListPreparer(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (*http.Request, error) {
367	urlParameters := map[string]interface{}{
368		"apimBaseUrl": apimBaseURL,
369	}
370
371	const APIVersion = "2017-03-01"
372	queryParameters := map[string]interface{}{
373		"api-version": APIVersion,
374	}
375	if len(filter) > 0 {
376		queryParameters["$filter"] = autorest.Encode("query", filter)
377	}
378	if top != nil {
379		queryParameters["$top"] = autorest.Encode("query", *top)
380	}
381	if skip != nil {
382		queryParameters["$skip"] = autorest.Encode("query", *skip)
383	}
384
385	preparer := autorest.CreatePreparer(
386		autorest.AsGet(),
387		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
388		autorest.WithPath("/groups"),
389		autorest.WithQueryParameters(queryParameters))
390	return preparer.Prepare((&http.Request{}).WithContext(ctx))
391}
392
393// ListSender sends the List request. The method will close the
394// http.Response Body if it receives an error.
395func (client GroupClient) ListSender(req *http.Request) (*http.Response, error) {
396	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
397}
398
399// ListResponder handles the response to the List request. The method always
400// closes the http.Response Body.
401func (client GroupClient) ListResponder(resp *http.Response) (result GroupCollection, err error) {
402	err = autorest.Respond(
403		resp,
404		client.ByInspecting(),
405		azure.WithErrorUnlessStatusCode(http.StatusOK),
406		autorest.ByUnmarshallingJSON(&result),
407		autorest.ByClosing())
408	result.Response = autorest.Response{Response: resp}
409	return
410}
411
412// listNextResults retrieves the next set of results, if any.
413func (client GroupClient) listNextResults(ctx context.Context, lastResults GroupCollection) (result GroupCollection, err error) {
414	req, err := lastResults.groupCollectionPreparer(ctx)
415	if err != nil {
416		return result, autorest.NewErrorWithError(err, "apimanagement.GroupClient", "listNextResults", nil, "Failure preparing next results request")
417	}
418	if req == nil {
419		return
420	}
421	resp, err := client.ListSender(req)
422	if err != nil {
423		result.Response = autorest.Response{Response: resp}
424		return result, autorest.NewErrorWithError(err, "apimanagement.GroupClient", "listNextResults", resp, "Failure sending next results request")
425	}
426	result, err = client.ListResponder(resp)
427	if err != nil {
428		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "listNextResults", resp, "Failure responding to next results request")
429	}
430	return
431}
432
433// ListComplete enumerates all values, automatically crossing page boundaries as required.
434func (client GroupClient) ListComplete(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result GroupCollectionIterator, err error) {
435	if tracing.IsEnabled() {
436		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.List")
437		defer func() {
438			sc := -1
439			if result.Response().Response.Response != nil {
440				sc = result.page.Response().Response.Response.StatusCode
441			}
442			tracing.EndSpan(ctx, sc, err)
443		}()
444	}
445	result.page, err = client.List(ctx, apimBaseURL, filter, top, skip)
446	return
447}
448
449// Update updates the details of the group specified by its identifier.
450// Parameters:
451// apimBaseURL - the management endpoint of the API Management service, for example
452// https://myapimservice.management.azure-api.net.
453// groupID - group identifier. Must be unique in the current API Management service instance.
454// parameters - update parameters.
455// ifMatch - eTag of the Group Entity. ETag should match the current entity state from the header response of
456// the GET request or it should be * for unconditional update.
457func (client GroupClient) Update(ctx context.Context, apimBaseURL string, groupID string, parameters GroupUpdateParameters, ifMatch string) (result autorest.Response, err error) {
458	if tracing.IsEnabled() {
459		ctx = tracing.StartSpan(ctx, fqdn+"/GroupClient.Update")
460		defer func() {
461			sc := -1
462			if result.Response != nil {
463				sc = result.Response.StatusCode
464			}
465			tracing.EndSpan(ctx, sc, err)
466		}()
467	}
468	if err := validation.Validate([]validation.Validation{
469		{TargetValue: groupID,
470			Constraints: []validation.Constraint{{Target: "groupID", Name: validation.MaxLength, Rule: 256, Chain: nil},
471				{Target: "groupID", Name: validation.MinLength, Rule: 1, Chain: nil},
472				{Target: "groupID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
473		return result, validation.NewError("apimanagement.GroupClient", "Update", err.Error())
474	}
475
476	req, err := client.UpdatePreparer(ctx, apimBaseURL, groupID, parameters, ifMatch)
477	if err != nil {
478		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Update", nil, "Failure preparing request")
479		return
480	}
481
482	resp, err := client.UpdateSender(req)
483	if err != nil {
484		result.Response = resp
485		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Update", resp, "Failure sending request")
486		return
487	}
488
489	result, err = client.UpdateResponder(resp)
490	if err != nil {
491		err = autorest.NewErrorWithError(err, "apimanagement.GroupClient", "Update", resp, "Failure responding to request")
492	}
493
494	return
495}
496
497// UpdatePreparer prepares the Update request.
498func (client GroupClient) UpdatePreparer(ctx context.Context, apimBaseURL string, groupID string, parameters GroupUpdateParameters, ifMatch string) (*http.Request, error) {
499	urlParameters := map[string]interface{}{
500		"apimBaseUrl": apimBaseURL,
501	}
502
503	pathParameters := map[string]interface{}{
504		"groupId": autorest.Encode("path", groupID),
505	}
506
507	const APIVersion = "2017-03-01"
508	queryParameters := map[string]interface{}{
509		"api-version": APIVersion,
510	}
511
512	preparer := autorest.CreatePreparer(
513		autorest.AsContentType("application/json; charset=utf-8"),
514		autorest.AsPatch(),
515		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
516		autorest.WithPathParameters("/groups/{groupId}", pathParameters),
517		autorest.WithJSON(parameters),
518		autorest.WithQueryParameters(queryParameters),
519		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
520	return preparer.Prepare((&http.Request{}).WithContext(ctx))
521}
522
523// UpdateSender sends the Update request. The method will close the
524// http.Response Body if it receives an error.
525func (client GroupClient) UpdateSender(req *http.Request) (*http.Response, error) {
526	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
527}
528
529// UpdateResponder handles the response to the Update request. The method always
530// closes the http.Response Body.
531func (client GroupClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
532	err = autorest.Respond(
533		resp,
534		client.ByInspecting(),
535		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
536		autorest.ByClosing())
537	result.Response = resp
538	return
539}
540