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	"net/http"
26)
27
28// ProductSubscriptionsClient is the apiManagement Client
29type ProductSubscriptionsClient struct {
30	BaseClient
31}
32
33// NewProductSubscriptionsClient creates an instance of the ProductSubscriptionsClient client.
34func NewProductSubscriptionsClient(subscriptionID string) ProductSubscriptionsClient {
35	return NewProductSubscriptionsClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewProductSubscriptionsClientWithBaseURI creates an instance of the ProductSubscriptionsClient client.
39func NewProductSubscriptionsClientWithBaseURI(baseURI string, subscriptionID string) ProductSubscriptionsClient {
40	return ProductSubscriptionsClient{NewWithBaseURI(baseURI, subscriptionID)}
41}
42
43// ListByProducts lists the collection of subscriptions to the specified product.
44// Parameters:
45// resourceGroupName - the name of the resource group.
46// serviceName - the name of the API Management service.
47// productID - product identifier. Must be unique in the current API Management service instance.
48// filter - | Field        | Supported operators    | Supported functions                         |
49// |--------------|------------------------|---------------------------------------------|
50// | id           | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
51// | name         | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
52// | stateComment | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
53// | userId       | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
54// | productId    | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
55// | state        | eq                     |                                             |
56// top - number of records to return.
57// skip - number of records to skip.
58func (client ProductSubscriptionsClient) ListByProducts(ctx context.Context, resourceGroupName string, serviceName string, productID string, filter string, top *int32, skip *int32) (result SubscriptionCollectionPage, err error) {
59	if err := validation.Validate([]validation.Validation{
60		{TargetValue: serviceName,
61			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
62				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
63				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
64		{TargetValue: productID,
65			Constraints: []validation.Constraint{{Target: "productID", Name: validation.MaxLength, Rule: 256, Chain: nil},
66				{Target: "productID", Name: validation.MinLength, Rule: 1, Chain: nil},
67				{Target: "productID", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}},
68		{TargetValue: top,
69			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
70				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
71		{TargetValue: skip,
72			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
73				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
74		return result, validation.NewError("apimanagement.ProductSubscriptionsClient", "ListByProducts", err.Error())
75	}
76
77	result.fn = client.listByProductsNextResults
78	req, err := client.ListByProductsPreparer(ctx, resourceGroupName, serviceName, productID, filter, top, skip)
79	if err != nil {
80		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "ListByProducts", nil, "Failure preparing request")
81		return
82	}
83
84	resp, err := client.ListByProductsSender(req)
85	if err != nil {
86		result.sc.Response = autorest.Response{Response: resp}
87		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "ListByProducts", resp, "Failure sending request")
88		return
89	}
90
91	result.sc, err = client.ListByProductsResponder(resp)
92	if err != nil {
93		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "ListByProducts", resp, "Failure responding to request")
94	}
95
96	return
97}
98
99// ListByProductsPreparer prepares the ListByProducts request.
100func (client ProductSubscriptionsClient) ListByProductsPreparer(ctx context.Context, resourceGroupName string, serviceName string, productID string, filter string, top *int32, skip *int32) (*http.Request, error) {
101	pathParameters := map[string]interface{}{
102		"productId":         autorest.Encode("path", productID),
103		"resourceGroupName": autorest.Encode("path", resourceGroupName),
104		"serviceName":       autorest.Encode("path", serviceName),
105		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
106	}
107
108	const APIVersion = "2016-10-10"
109	queryParameters := map[string]interface{}{
110		"api-version": APIVersion,
111	}
112	if len(filter) > 0 {
113		queryParameters["$filter"] = autorest.Encode("query", filter)
114	}
115	if top != nil {
116		queryParameters["$top"] = autorest.Encode("query", *top)
117	}
118	if skip != nil {
119		queryParameters["$skip"] = autorest.Encode("query", *skip)
120	}
121
122	preparer := autorest.CreatePreparer(
123		autorest.AsGet(),
124		autorest.WithBaseURL(client.BaseURI),
125		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/products/{productId}/subscriptions", pathParameters),
126		autorest.WithQueryParameters(queryParameters))
127	return preparer.Prepare((&http.Request{}).WithContext(ctx))
128}
129
130// ListByProductsSender sends the ListByProducts request. The method will close the
131// http.Response Body if it receives an error.
132func (client ProductSubscriptionsClient) ListByProductsSender(req *http.Request) (*http.Response, error) {
133	return autorest.SendWithSender(client, req,
134		azure.DoRetryWithRegistration(client.Client))
135}
136
137// ListByProductsResponder handles the response to the ListByProducts request. The method always
138// closes the http.Response Body.
139func (client ProductSubscriptionsClient) ListByProductsResponder(resp *http.Response) (result SubscriptionCollection, err error) {
140	err = autorest.Respond(
141		resp,
142		client.ByInspecting(),
143		azure.WithErrorUnlessStatusCode(http.StatusOK),
144		autorest.ByUnmarshallingJSON(&result),
145		autorest.ByClosing())
146	result.Response = autorest.Response{Response: resp}
147	return
148}
149
150// listByProductsNextResults retrieves the next set of results, if any.
151func (client ProductSubscriptionsClient) listByProductsNextResults(lastResults SubscriptionCollection) (result SubscriptionCollection, err error) {
152	req, err := lastResults.subscriptionCollectionPreparer()
153	if err != nil {
154		return result, autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listByProductsNextResults", nil, "Failure preparing next results request")
155	}
156	if req == nil {
157		return
158	}
159	resp, err := client.ListByProductsSender(req)
160	if err != nil {
161		result.Response = autorest.Response{Response: resp}
162		return result, autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listByProductsNextResults", resp, "Failure sending next results request")
163	}
164	result, err = client.ListByProductsResponder(resp)
165	if err != nil {
166		err = autorest.NewErrorWithError(err, "apimanagement.ProductSubscriptionsClient", "listByProductsNextResults", resp, "Failure responding to next results request")
167	}
168	return
169}
170
171// ListByProductsComplete enumerates all values, automatically crossing page boundaries as required.
172func (client ProductSubscriptionsClient) ListByProductsComplete(ctx context.Context, resourceGroupName string, serviceName string, productID string, filter string, top *int32, skip *int32) (result SubscriptionCollectionIterator, err error) {
173	result.page, err = client.ListByProducts(ctx, resourceGroupName, serviceName, productID, filter, top, skip)
174	return
175}
176