1package subscription
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/satori/go.uuid"
25	"net/http"
26)
27
28// DefinitionsClient is the subscription definitions client provides an interface to create, modify and retrieve azure
29// subscriptions programmatically.
30type DefinitionsClient struct {
31	BaseClient
32}
33
34// NewDefinitionsClient creates an instance of the DefinitionsClient client.
35func NewDefinitionsClient() DefinitionsClient {
36	return NewDefinitionsClientWithBaseURI(DefaultBaseURI)
37}
38
39// NewDefinitionsClientWithBaseURI creates an instance of the DefinitionsClient client.
40func NewDefinitionsClientWithBaseURI(baseURI string) DefinitionsClient {
41	return DefinitionsClient{NewWithBaseURI(baseURI)}
42}
43
44// Create create an Azure subscription definition.
45// Parameters:
46// subscriptionDefinitionName - the name of the Azure subscription definition.
47// body - the subscription definition creation.
48func (client DefinitionsClient) Create(ctx context.Context, subscriptionDefinitionName string, body Definition) (result DefinitionsCreateFuture, err error) {
49	req, err := client.CreatePreparer(ctx, subscriptionDefinitionName, body)
50	if err != nil {
51		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "Create", nil, "Failure preparing request")
52		return
53	}
54
55	result, err = client.CreateSender(req)
56	if err != nil {
57		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "Create", result.Response(), "Failure sending request")
58		return
59	}
60
61	return
62}
63
64// CreatePreparer prepares the Create request.
65func (client DefinitionsClient) CreatePreparer(ctx context.Context, subscriptionDefinitionName string, body Definition) (*http.Request, error) {
66	pathParameters := map[string]interface{}{
67		"subscriptionDefinitionName": autorest.Encode("path", subscriptionDefinitionName),
68	}
69
70	const APIVersion = "2017-11-01-preview"
71	queryParameters := map[string]interface{}{
72		"api-version": APIVersion,
73	}
74
75	preparer := autorest.CreatePreparer(
76		autorest.AsContentType("application/json; charset=utf-8"),
77		autorest.AsPut(),
78		autorest.WithBaseURL(client.BaseURI),
79		autorest.WithPathParameters("/providers/Microsoft.Subscription/subscriptionDefinitions/{subscriptionDefinitionName}", pathParameters),
80		autorest.WithJSON(body),
81		autorest.WithQueryParameters(queryParameters))
82	return preparer.Prepare((&http.Request{}).WithContext(ctx))
83}
84
85// CreateSender sends the Create request. The method will close the
86// http.Response Body if it receives an error.
87func (client DefinitionsClient) CreateSender(req *http.Request) (future DefinitionsCreateFuture, err error) {
88	sender := autorest.DecorateSender(client, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
89	future.Future = azure.NewFuture(req)
90	future.req = req
91	_, err = future.Done(sender)
92	if err != nil {
93		return
94	}
95	err = autorest.Respond(future.Response(),
96		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted))
97	return
98}
99
100// CreateResponder handles the response to the Create request. The method always
101// closes the http.Response Body.
102func (client DefinitionsClient) CreateResponder(resp *http.Response) (result Definition, err error) {
103	err = autorest.Respond(
104		resp,
105		client.ByInspecting(),
106		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
107		autorest.ByUnmarshallingJSON(&result),
108		autorest.ByClosing())
109	result.Response = autorest.Response{Response: resp}
110	return
111}
112
113// Get get an Azure subscription definition.
114// Parameters:
115// subscriptionDefinitionName - the name of the Azure subscription definition.
116func (client DefinitionsClient) Get(ctx context.Context, subscriptionDefinitionName string) (result Definition, err error) {
117	req, err := client.GetPreparer(ctx, subscriptionDefinitionName)
118	if err != nil {
119		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "Get", nil, "Failure preparing request")
120		return
121	}
122
123	resp, err := client.GetSender(req)
124	if err != nil {
125		result.Response = autorest.Response{Response: resp}
126		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "Get", resp, "Failure sending request")
127		return
128	}
129
130	result, err = client.GetResponder(resp)
131	if err != nil {
132		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "Get", resp, "Failure responding to request")
133	}
134
135	return
136}
137
138// GetPreparer prepares the Get request.
139func (client DefinitionsClient) GetPreparer(ctx context.Context, subscriptionDefinitionName string) (*http.Request, error) {
140	pathParameters := map[string]interface{}{
141		"subscriptionDefinitionName": autorest.Encode("path", subscriptionDefinitionName),
142	}
143
144	const APIVersion = "2017-11-01-preview"
145	queryParameters := map[string]interface{}{
146		"api-version": APIVersion,
147	}
148
149	preparer := autorest.CreatePreparer(
150		autorest.AsGet(),
151		autorest.WithBaseURL(client.BaseURI),
152		autorest.WithPathParameters("/providers/Microsoft.Subscription/subscriptionDefinitions/{subscriptionDefinitionName}", pathParameters),
153		autorest.WithQueryParameters(queryParameters))
154	return preparer.Prepare((&http.Request{}).WithContext(ctx))
155}
156
157// GetSender sends the Get request. The method will close the
158// http.Response Body if it receives an error.
159func (client DefinitionsClient) GetSender(req *http.Request) (*http.Response, error) {
160	return autorest.SendWithSender(client, req,
161		autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
162}
163
164// GetResponder handles the response to the Get request. The method always
165// closes the http.Response Body.
166func (client DefinitionsClient) GetResponder(resp *http.Response) (result Definition, err error) {
167	err = autorest.Respond(
168		resp,
169		client.ByInspecting(),
170		azure.WithErrorUnlessStatusCode(http.StatusOK),
171		autorest.ByUnmarshallingJSON(&result),
172		autorest.ByClosing())
173	result.Response = autorest.Response{Response: resp}
174	return
175}
176
177// GetOperationStatus retrieves the status of the subscription definition PUT operation. The URI of this API is
178// returned in the Location field of the response header.
179// Parameters:
180// operationID - the operation ID, which can be found from the Location field in the generate recommendation
181// response header.
182func (client DefinitionsClient) GetOperationStatus(ctx context.Context, operationID uuid.UUID) (result Definition, err error) {
183	req, err := client.GetOperationStatusPreparer(ctx, operationID)
184	if err != nil {
185		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "GetOperationStatus", nil, "Failure preparing request")
186		return
187	}
188
189	resp, err := client.GetOperationStatusSender(req)
190	if err != nil {
191		result.Response = autorest.Response{Response: resp}
192		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "GetOperationStatus", resp, "Failure sending request")
193		return
194	}
195
196	result, err = client.GetOperationStatusResponder(resp)
197	if err != nil {
198		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "GetOperationStatus", resp, "Failure responding to request")
199	}
200
201	return
202}
203
204// GetOperationStatusPreparer prepares the GetOperationStatus request.
205func (client DefinitionsClient) GetOperationStatusPreparer(ctx context.Context, operationID uuid.UUID) (*http.Request, error) {
206	pathParameters := map[string]interface{}{
207		"operationId": autorest.Encode("path", operationID),
208	}
209
210	const APIVersion = "2017-11-01-preview"
211	queryParameters := map[string]interface{}{
212		"api-version": APIVersion,
213	}
214
215	preparer := autorest.CreatePreparer(
216		autorest.AsGet(),
217		autorest.WithBaseURL(client.BaseURI),
218		autorest.WithPathParameters("/providers/Microsoft.Subscription/subscriptionOperations/{operationId}", pathParameters),
219		autorest.WithQueryParameters(queryParameters))
220	return preparer.Prepare((&http.Request{}).WithContext(ctx))
221}
222
223// GetOperationStatusSender sends the GetOperationStatus request. The method will close the
224// http.Response Body if it receives an error.
225func (client DefinitionsClient) GetOperationStatusSender(req *http.Request) (*http.Response, error) {
226	return autorest.SendWithSender(client, req,
227		autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
228}
229
230// GetOperationStatusResponder handles the response to the GetOperationStatus request. The method always
231// closes the http.Response Body.
232func (client DefinitionsClient) GetOperationStatusResponder(resp *http.Response) (result Definition, err error) {
233	err = autorest.Respond(
234		resp,
235		client.ByInspecting(),
236		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
237		autorest.ByUnmarshallingJSON(&result),
238		autorest.ByClosing())
239	result.Response = autorest.Response{Response: resp}
240	return
241}
242
243// List list an Azure subscription definition by subscriptionId.
244func (client DefinitionsClient) List(ctx context.Context) (result DefinitionListPage, err error) {
245	result.fn = client.listNextResults
246	req, err := client.ListPreparer(ctx)
247	if err != nil {
248		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "List", nil, "Failure preparing request")
249		return
250	}
251
252	resp, err := client.ListSender(req)
253	if err != nil {
254		result.dl.Response = autorest.Response{Response: resp}
255		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "List", resp, "Failure sending request")
256		return
257	}
258
259	result.dl, err = client.ListResponder(resp)
260	if err != nil {
261		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "List", resp, "Failure responding to request")
262	}
263
264	return
265}
266
267// ListPreparer prepares the List request.
268func (client DefinitionsClient) ListPreparer(ctx context.Context) (*http.Request, error) {
269	const APIVersion = "2017-11-01-preview"
270	queryParameters := map[string]interface{}{
271		"api-version": APIVersion,
272	}
273
274	preparer := autorest.CreatePreparer(
275		autorest.AsGet(),
276		autorest.WithBaseURL(client.BaseURI),
277		autorest.WithPath("/providers/Microsoft.Subscription/subscriptionDefinitions"),
278		autorest.WithQueryParameters(queryParameters))
279	return preparer.Prepare((&http.Request{}).WithContext(ctx))
280}
281
282// ListSender sends the List request. The method will close the
283// http.Response Body if it receives an error.
284func (client DefinitionsClient) ListSender(req *http.Request) (*http.Response, error) {
285	return autorest.SendWithSender(client, req,
286		autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
287}
288
289// ListResponder handles the response to the List request. The method always
290// closes the http.Response Body.
291func (client DefinitionsClient) ListResponder(resp *http.Response) (result DefinitionList, err error) {
292	err = autorest.Respond(
293		resp,
294		client.ByInspecting(),
295		azure.WithErrorUnlessStatusCode(http.StatusOK),
296		autorest.ByUnmarshallingJSON(&result),
297		autorest.ByClosing())
298	result.Response = autorest.Response{Response: resp}
299	return
300}
301
302// listNextResults retrieves the next set of results, if any.
303func (client DefinitionsClient) listNextResults(lastResults DefinitionList) (result DefinitionList, err error) {
304	req, err := lastResults.definitionListPreparer()
305	if err != nil {
306		return result, autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "listNextResults", nil, "Failure preparing next results request")
307	}
308	if req == nil {
309		return
310	}
311	resp, err := client.ListSender(req)
312	if err != nil {
313		result.Response = autorest.Response{Response: resp}
314		return result, autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "listNextResults", resp, "Failure sending next results request")
315	}
316	result, err = client.ListResponder(resp)
317	if err != nil {
318		err = autorest.NewErrorWithError(err, "subscription.DefinitionsClient", "listNextResults", resp, "Failure responding to next results request")
319	}
320	return
321}
322
323// ListComplete enumerates all values, automatically crossing page boundaries as required.
324func (client DefinitionsClient) ListComplete(ctx context.Context) (result DefinitionListIterator, err error) {
325	result.page, err = client.List(ctx)
326	return
327}
328