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