1package advisor
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/tracing"
14	"net/http"
15)
16
17// ConfigurationsClient is the REST APIs for Azure Advisor
18type ConfigurationsClient struct {
19	BaseClient
20}
21
22// NewConfigurationsClient creates an instance of the ConfigurationsClient client.
23func NewConfigurationsClient(subscriptionID string) ConfigurationsClient {
24	return NewConfigurationsClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewConfigurationsClientWithBaseURI creates an instance of the ConfigurationsClient client using a custom endpoint.
28// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewConfigurationsClientWithBaseURI(baseURI string, subscriptionID string) ConfigurationsClient {
30	return ConfigurationsClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// CreateInResourceGroup sends the create in resource group request.
34// Parameters:
35// configContract - the Azure Advisor configuration data structure.
36// resourceGroup - the name of the Azure resource group.
37func (client ConfigurationsClient) CreateInResourceGroup(ctx context.Context, configContract ConfigData, resourceGroup string) (result ARMErrorResponseBody, err error) {
38	if tracing.IsEnabled() {
39		ctx = tracing.StartSpan(ctx, fqdn+"/ConfigurationsClient.CreateInResourceGroup")
40		defer func() {
41			sc := -1
42			if result.Response.Response != nil {
43				sc = result.Response.Response.StatusCode
44			}
45			tracing.EndSpan(ctx, sc, err)
46		}()
47	}
48	req, err := client.CreateInResourceGroupPreparer(ctx, configContract, resourceGroup)
49	if err != nil {
50		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "CreateInResourceGroup", nil, "Failure preparing request")
51		return
52	}
53
54	resp, err := client.CreateInResourceGroupSender(req)
55	if err != nil {
56		result.Response = autorest.Response{Response: resp}
57		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "CreateInResourceGroup", resp, "Failure sending request")
58		return
59	}
60
61	result, err = client.CreateInResourceGroupResponder(resp)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "CreateInResourceGroup", resp, "Failure responding to request")
64		return
65	}
66
67	return
68}
69
70// CreateInResourceGroupPreparer prepares the CreateInResourceGroup request.
71func (client ConfigurationsClient) CreateInResourceGroupPreparer(ctx context.Context, configContract ConfigData, resourceGroup string) (*http.Request, error) {
72	pathParameters := map[string]interface{}{
73		"resourceGroup":  autorest.Encode("path", resourceGroup),
74		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
75	}
76
77	const APIVersion = "2017-04-19"
78	queryParameters := map[string]interface{}{
79		"api-version": APIVersion,
80	}
81
82	preparer := autorest.CreatePreparer(
83		autorest.AsContentType("application/json; charset=utf-8"),
84		autorest.AsPut(),
85		autorest.WithBaseURL(client.BaseURI),
86		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Advisor/configurations", pathParameters),
87		autorest.WithJSON(configContract),
88		autorest.WithQueryParameters(queryParameters))
89	return preparer.Prepare((&http.Request{}).WithContext(ctx))
90}
91
92// CreateInResourceGroupSender sends the CreateInResourceGroup request. The method will close the
93// http.Response Body if it receives an error.
94func (client ConfigurationsClient) CreateInResourceGroupSender(req *http.Request) (*http.Response, error) {
95	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
96}
97
98// CreateInResourceGroupResponder handles the response to the CreateInResourceGroup request. The method always
99// closes the http.Response Body.
100func (client ConfigurationsClient) CreateInResourceGroupResponder(resp *http.Response) (result ARMErrorResponseBody, err error) {
101	err = autorest.Respond(
102		resp,
103		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent, http.StatusBadRequest),
104		autorest.ByUnmarshallingJSON(&result),
105		autorest.ByClosing())
106	result.Response = autorest.Response{Response: resp}
107	return
108}
109
110// CreateInSubscription create/Overwrite Azure Advisor configuration and also delete all configurations of contained
111// resource groups.
112// Parameters:
113// configContract - the Azure Advisor configuration data structure.
114func (client ConfigurationsClient) CreateInSubscription(ctx context.Context, configContract ConfigData) (result ARMErrorResponseBody, err error) {
115	if tracing.IsEnabled() {
116		ctx = tracing.StartSpan(ctx, fqdn+"/ConfigurationsClient.CreateInSubscription")
117		defer func() {
118			sc := -1
119			if result.Response.Response != nil {
120				sc = result.Response.Response.StatusCode
121			}
122			tracing.EndSpan(ctx, sc, err)
123		}()
124	}
125	req, err := client.CreateInSubscriptionPreparer(ctx, configContract)
126	if err != nil {
127		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "CreateInSubscription", nil, "Failure preparing request")
128		return
129	}
130
131	resp, err := client.CreateInSubscriptionSender(req)
132	if err != nil {
133		result.Response = autorest.Response{Response: resp}
134		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "CreateInSubscription", resp, "Failure sending request")
135		return
136	}
137
138	result, err = client.CreateInSubscriptionResponder(resp)
139	if err != nil {
140		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "CreateInSubscription", resp, "Failure responding to request")
141		return
142	}
143
144	return
145}
146
147// CreateInSubscriptionPreparer prepares the CreateInSubscription request.
148func (client ConfigurationsClient) CreateInSubscriptionPreparer(ctx context.Context, configContract ConfigData) (*http.Request, error) {
149	pathParameters := map[string]interface{}{
150		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
151	}
152
153	const APIVersion = "2017-04-19"
154	queryParameters := map[string]interface{}{
155		"api-version": APIVersion,
156	}
157
158	preparer := autorest.CreatePreparer(
159		autorest.AsContentType("application/json; charset=utf-8"),
160		autorest.AsPut(),
161		autorest.WithBaseURL(client.BaseURI),
162		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/configurations", pathParameters),
163		autorest.WithJSON(configContract),
164		autorest.WithQueryParameters(queryParameters))
165	return preparer.Prepare((&http.Request{}).WithContext(ctx))
166}
167
168// CreateInSubscriptionSender sends the CreateInSubscription request. The method will close the
169// http.Response Body if it receives an error.
170func (client ConfigurationsClient) CreateInSubscriptionSender(req *http.Request) (*http.Response, error) {
171	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
172}
173
174// CreateInSubscriptionResponder handles the response to the CreateInSubscription request. The method always
175// closes the http.Response Body.
176func (client ConfigurationsClient) CreateInSubscriptionResponder(resp *http.Response) (result ARMErrorResponseBody, err error) {
177	err = autorest.Respond(
178		resp,
179		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent, http.StatusBadRequest),
180		autorest.ByUnmarshallingJSON(&result),
181		autorest.ByClosing())
182	result.Response = autorest.Response{Response: resp}
183	return
184}
185
186// ListByResourceGroup sends the list by resource group request.
187// Parameters:
188// resourceGroup - the name of the Azure resource group.
189func (client ConfigurationsClient) ListByResourceGroup(ctx context.Context, resourceGroup string) (result ConfigurationListResult, err error) {
190	if tracing.IsEnabled() {
191		ctx = tracing.StartSpan(ctx, fqdn+"/ConfigurationsClient.ListByResourceGroup")
192		defer func() {
193			sc := -1
194			if result.Response.Response != nil {
195				sc = result.Response.Response.StatusCode
196			}
197			tracing.EndSpan(ctx, sc, err)
198		}()
199	}
200	req, err := client.ListByResourceGroupPreparer(ctx, resourceGroup)
201	if err != nil {
202		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "ListByResourceGroup", nil, "Failure preparing request")
203		return
204	}
205
206	resp, err := client.ListByResourceGroupSender(req)
207	if err != nil {
208		result.Response = autorest.Response{Response: resp}
209		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "ListByResourceGroup", resp, "Failure sending request")
210		return
211	}
212
213	result, err = client.ListByResourceGroupResponder(resp)
214	if err != nil {
215		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "ListByResourceGroup", resp, "Failure responding to request")
216		return
217	}
218
219	return
220}
221
222// ListByResourceGroupPreparer prepares the ListByResourceGroup request.
223func (client ConfigurationsClient) ListByResourceGroupPreparer(ctx context.Context, resourceGroup string) (*http.Request, error) {
224	pathParameters := map[string]interface{}{
225		"resourceGroup":  autorest.Encode("path", resourceGroup),
226		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
227	}
228
229	const APIVersion = "2017-04-19"
230	queryParameters := map[string]interface{}{
231		"api-version": APIVersion,
232	}
233
234	preparer := autorest.CreatePreparer(
235		autorest.AsGet(),
236		autorest.WithBaseURL(client.BaseURI),
237		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Advisor/configurations", pathParameters),
238		autorest.WithQueryParameters(queryParameters))
239	return preparer.Prepare((&http.Request{}).WithContext(ctx))
240}
241
242// ListByResourceGroupSender sends the ListByResourceGroup request. The method will close the
243// http.Response Body if it receives an error.
244func (client ConfigurationsClient) ListByResourceGroupSender(req *http.Request) (*http.Response, error) {
245	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
246}
247
248// ListByResourceGroupResponder handles the response to the ListByResourceGroup request. The method always
249// closes the http.Response Body.
250func (client ConfigurationsClient) ListByResourceGroupResponder(resp *http.Response) (result ConfigurationListResult, err error) {
251	err = autorest.Respond(
252		resp,
253		azure.WithErrorUnlessStatusCode(http.StatusOK),
254		autorest.ByUnmarshallingJSON(&result),
255		autorest.ByClosing())
256	result.Response = autorest.Response{Response: resp}
257	return
258}
259
260// ListBySubscription retrieve Azure Advisor configurations and also retrieve configurations of contained resource
261// groups.
262func (client ConfigurationsClient) ListBySubscription(ctx context.Context) (result ConfigurationListResultPage, err error) {
263	if tracing.IsEnabled() {
264		ctx = tracing.StartSpan(ctx, fqdn+"/ConfigurationsClient.ListBySubscription")
265		defer func() {
266			sc := -1
267			if result.clr.Response.Response != nil {
268				sc = result.clr.Response.Response.StatusCode
269			}
270			tracing.EndSpan(ctx, sc, err)
271		}()
272	}
273	result.fn = client.listBySubscriptionNextResults
274	req, err := client.ListBySubscriptionPreparer(ctx)
275	if err != nil {
276		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "ListBySubscription", nil, "Failure preparing request")
277		return
278	}
279
280	resp, err := client.ListBySubscriptionSender(req)
281	if err != nil {
282		result.clr.Response = autorest.Response{Response: resp}
283		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "ListBySubscription", resp, "Failure sending request")
284		return
285	}
286
287	result.clr, err = client.ListBySubscriptionResponder(resp)
288	if err != nil {
289		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "ListBySubscription", resp, "Failure responding to request")
290		return
291	}
292	if result.clr.hasNextLink() && result.clr.IsEmpty() {
293		err = result.NextWithContext(ctx)
294		return
295	}
296
297	return
298}
299
300// ListBySubscriptionPreparer prepares the ListBySubscription request.
301func (client ConfigurationsClient) ListBySubscriptionPreparer(ctx context.Context) (*http.Request, error) {
302	pathParameters := map[string]interface{}{
303		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
304	}
305
306	const APIVersion = "2017-04-19"
307	queryParameters := map[string]interface{}{
308		"api-version": APIVersion,
309	}
310
311	preparer := autorest.CreatePreparer(
312		autorest.AsGet(),
313		autorest.WithBaseURL(client.BaseURI),
314		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/configurations", pathParameters),
315		autorest.WithQueryParameters(queryParameters))
316	return preparer.Prepare((&http.Request{}).WithContext(ctx))
317}
318
319// ListBySubscriptionSender sends the ListBySubscription request. The method will close the
320// http.Response Body if it receives an error.
321func (client ConfigurationsClient) ListBySubscriptionSender(req *http.Request) (*http.Response, error) {
322	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
323}
324
325// ListBySubscriptionResponder handles the response to the ListBySubscription request. The method always
326// closes the http.Response Body.
327func (client ConfigurationsClient) ListBySubscriptionResponder(resp *http.Response) (result ConfigurationListResult, err error) {
328	err = autorest.Respond(
329		resp,
330		azure.WithErrorUnlessStatusCode(http.StatusOK),
331		autorest.ByUnmarshallingJSON(&result),
332		autorest.ByClosing())
333	result.Response = autorest.Response{Response: resp}
334	return
335}
336
337// listBySubscriptionNextResults retrieves the next set of results, if any.
338func (client ConfigurationsClient) listBySubscriptionNextResults(ctx context.Context, lastResults ConfigurationListResult) (result ConfigurationListResult, err error) {
339	req, err := lastResults.configurationListResultPreparer(ctx)
340	if err != nil {
341		return result, autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "listBySubscriptionNextResults", nil, "Failure preparing next results request")
342	}
343	if req == nil {
344		return
345	}
346	resp, err := client.ListBySubscriptionSender(req)
347	if err != nil {
348		result.Response = autorest.Response{Response: resp}
349		return result, autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "listBySubscriptionNextResults", resp, "Failure sending next results request")
350	}
351	result, err = client.ListBySubscriptionResponder(resp)
352	if err != nil {
353		err = autorest.NewErrorWithError(err, "advisor.ConfigurationsClient", "listBySubscriptionNextResults", resp, "Failure responding to next results request")
354	}
355	return
356}
357
358// ListBySubscriptionComplete enumerates all values, automatically crossing page boundaries as required.
359func (client ConfigurationsClient) ListBySubscriptionComplete(ctx context.Context) (result ConfigurationListResultIterator, err error) {
360	if tracing.IsEnabled() {
361		ctx = tracing.StartSpan(ctx, fqdn+"/ConfigurationsClient.ListBySubscription")
362		defer func() {
363			sc := -1
364			if result.Response().Response.Response != nil {
365				sc = result.page.Response().Response.Response.StatusCode
366			}
367			tracing.EndSpan(ctx, sc, err)
368		}()
369	}
370	result.page, err = client.ListBySubscription(ctx)
371	return
372}
373