1package advisor
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/tracing"
25	"github.com/satori/go.uuid"
26	"net/http"
27)
28
29// RecommendationsClient is the REST APIs for Azure Advisor
30type RecommendationsClient struct {
31	BaseClient
32}
33
34// NewRecommendationsClient creates an instance of the RecommendationsClient client.
35func NewRecommendationsClient(subscriptionID string) RecommendationsClient {
36	return NewRecommendationsClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewRecommendationsClientWithBaseURI creates an instance of the RecommendationsClient client using a custom endpoint.
40// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewRecommendationsClientWithBaseURI(baseURI string, subscriptionID string) RecommendationsClient {
42	return RecommendationsClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// Generate initiates the recommendation generation or computation process for a subscription. This operation is
46// asynchronous. The generated recommendations are stored in a cache in the Advisor service.
47func (client RecommendationsClient) Generate(ctx context.Context) (result autorest.Response, err error) {
48	if tracing.IsEnabled() {
49		ctx = tracing.StartSpan(ctx, fqdn+"/RecommendationsClient.Generate")
50		defer func() {
51			sc := -1
52			if result.Response != nil {
53				sc = result.Response.StatusCode
54			}
55			tracing.EndSpan(ctx, sc, err)
56		}()
57	}
58	req, err := client.GeneratePreparer(ctx)
59	if err != nil {
60		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "Generate", nil, "Failure preparing request")
61		return
62	}
63
64	resp, err := client.GenerateSender(req)
65	if err != nil {
66		result.Response = resp
67		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "Generate", resp, "Failure sending request")
68		return
69	}
70
71	result, err = client.GenerateResponder(resp)
72	if err != nil {
73		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "Generate", resp, "Failure responding to request")
74		return
75	}
76
77	return
78}
79
80// GeneratePreparer prepares the Generate request.
81func (client RecommendationsClient) GeneratePreparer(ctx context.Context) (*http.Request, error) {
82	pathParameters := map[string]interface{}{
83		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
84	}
85
86	const APIVersion = "2017-04-19"
87	queryParameters := map[string]interface{}{
88		"api-version": APIVersion,
89	}
90
91	preparer := autorest.CreatePreparer(
92		autorest.AsPost(),
93		autorest.WithBaseURL(client.BaseURI),
94		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/generateRecommendations", pathParameters),
95		autorest.WithQueryParameters(queryParameters))
96	return preparer.Prepare((&http.Request{}).WithContext(ctx))
97}
98
99// GenerateSender sends the Generate request. The method will close the
100// http.Response Body if it receives an error.
101func (client RecommendationsClient) GenerateSender(req *http.Request) (*http.Response, error) {
102	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
103}
104
105// GenerateResponder handles the response to the Generate request. The method always
106// closes the http.Response Body.
107func (client RecommendationsClient) GenerateResponder(resp *http.Response) (result autorest.Response, err error) {
108	err = autorest.Respond(
109		resp,
110		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
111		autorest.ByClosing())
112	result.Response = resp
113	return
114}
115
116// Get obtains details of a cached recommendation.
117// Parameters:
118// resourceURI - the fully qualified Azure Resource Manager identifier of the resource to which the
119// recommendation applies.
120// recommendationID - the recommendation ID.
121func (client RecommendationsClient) Get(ctx context.Context, resourceURI string, recommendationID string) (result ResourceRecommendationBase, err error) {
122	if tracing.IsEnabled() {
123		ctx = tracing.StartSpan(ctx, fqdn+"/RecommendationsClient.Get")
124		defer func() {
125			sc := -1
126			if result.Response.Response != nil {
127				sc = result.Response.Response.StatusCode
128			}
129			tracing.EndSpan(ctx, sc, err)
130		}()
131	}
132	req, err := client.GetPreparer(ctx, resourceURI, recommendationID)
133	if err != nil {
134		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "Get", nil, "Failure preparing request")
135		return
136	}
137
138	resp, err := client.GetSender(req)
139	if err != nil {
140		result.Response = autorest.Response{Response: resp}
141		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "Get", resp, "Failure sending request")
142		return
143	}
144
145	result, err = client.GetResponder(resp)
146	if err != nil {
147		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "Get", resp, "Failure responding to request")
148		return
149	}
150
151	return
152}
153
154// GetPreparer prepares the Get request.
155func (client RecommendationsClient) GetPreparer(ctx context.Context, resourceURI string, recommendationID string) (*http.Request, error) {
156	pathParameters := map[string]interface{}{
157		"recommendationId": autorest.Encode("path", recommendationID),
158		"resourceUri":      autorest.Encode("path", resourceURI),
159	}
160
161	const APIVersion = "2017-04-19"
162	queryParameters := map[string]interface{}{
163		"api-version": APIVersion,
164	}
165
166	preparer := autorest.CreatePreparer(
167		autorest.AsGet(),
168		autorest.WithBaseURL(client.BaseURI),
169		autorest.WithPathParameters("/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}", pathParameters),
170		autorest.WithQueryParameters(queryParameters))
171	return preparer.Prepare((&http.Request{}).WithContext(ctx))
172}
173
174// GetSender sends the Get request. The method will close the
175// http.Response Body if it receives an error.
176func (client RecommendationsClient) GetSender(req *http.Request) (*http.Response, error) {
177	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
178}
179
180// GetResponder handles the response to the Get request. The method always
181// closes the http.Response Body.
182func (client RecommendationsClient) GetResponder(resp *http.Response) (result ResourceRecommendationBase, err error) {
183	err = autorest.Respond(
184		resp,
185		azure.WithErrorUnlessStatusCode(http.StatusOK),
186		autorest.ByUnmarshallingJSON(&result),
187		autorest.ByClosing())
188	result.Response = autorest.Response{Response: resp}
189	return
190}
191
192// GetGenerateStatus retrieves the status of the recommendation computation or generation process. Invoke this API
193// after calling the generation recommendation. The URI of this API is returned in the Location field of the response
194// header.
195// Parameters:
196// operationID - the operation ID, which can be found from the Location field in the generate recommendation
197// response header.
198func (client RecommendationsClient) GetGenerateStatus(ctx context.Context, operationID uuid.UUID) (result autorest.Response, err error) {
199	if tracing.IsEnabled() {
200		ctx = tracing.StartSpan(ctx, fqdn+"/RecommendationsClient.GetGenerateStatus")
201		defer func() {
202			sc := -1
203			if result.Response != nil {
204				sc = result.Response.StatusCode
205			}
206			tracing.EndSpan(ctx, sc, err)
207		}()
208	}
209	req, err := client.GetGenerateStatusPreparer(ctx, operationID)
210	if err != nil {
211		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "GetGenerateStatus", nil, "Failure preparing request")
212		return
213	}
214
215	resp, err := client.GetGenerateStatusSender(req)
216	if err != nil {
217		result.Response = resp
218		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "GetGenerateStatus", resp, "Failure sending request")
219		return
220	}
221
222	result, err = client.GetGenerateStatusResponder(resp)
223	if err != nil {
224		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "GetGenerateStatus", resp, "Failure responding to request")
225		return
226	}
227
228	return
229}
230
231// GetGenerateStatusPreparer prepares the GetGenerateStatus request.
232func (client RecommendationsClient) GetGenerateStatusPreparer(ctx context.Context, operationID uuid.UUID) (*http.Request, error) {
233	pathParameters := map[string]interface{}{
234		"operationId":    autorest.Encode("path", operationID),
235		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
236	}
237
238	const APIVersion = "2017-04-19"
239	queryParameters := map[string]interface{}{
240		"api-version": APIVersion,
241	}
242
243	preparer := autorest.CreatePreparer(
244		autorest.AsGet(),
245		autorest.WithBaseURL(client.BaseURI),
246		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/generateRecommendations/{operationId}", pathParameters),
247		autorest.WithQueryParameters(queryParameters))
248	return preparer.Prepare((&http.Request{}).WithContext(ctx))
249}
250
251// GetGenerateStatusSender sends the GetGenerateStatus request. The method will close the
252// http.Response Body if it receives an error.
253func (client RecommendationsClient) GetGenerateStatusSender(req *http.Request) (*http.Response, error) {
254	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
255}
256
257// GetGenerateStatusResponder handles the response to the GetGenerateStatus request. The method always
258// closes the http.Response Body.
259func (client RecommendationsClient) GetGenerateStatusResponder(resp *http.Response) (result autorest.Response, err error) {
260	err = autorest.Respond(
261		resp,
262		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
263		autorest.ByClosing())
264	result.Response = resp
265	return
266}
267
268// List obtains cached recommendations for a subscription. The recommendations are generated or computed by invoking
269// generateRecommendations.
270// Parameters:
271// filter - the filter to apply to the recommendations.<br>Filter can be applied to properties ['ResourceId',
272// 'ResourceGroup', 'RecommendationTypeGuid', '[Category](#category)'] with operators ['eq', 'and',
273// 'or'].<br>Example:<br>- $filter=Category eq 'Cost' and ResourceGroup eq 'MyResourceGroup'
274// top - the number of recommendations per page if a paged version of this API is being used.
275// skipToken - the page-continuation token to use with a paged version of this API.
276func (client RecommendationsClient) List(ctx context.Context, filter string, top *int32, skipToken string) (result ResourceRecommendationBaseListResultPage, err error) {
277	if tracing.IsEnabled() {
278		ctx = tracing.StartSpan(ctx, fqdn+"/RecommendationsClient.List")
279		defer func() {
280			sc := -1
281			if result.rrblr.Response.Response != nil {
282				sc = result.rrblr.Response.Response.StatusCode
283			}
284			tracing.EndSpan(ctx, sc, err)
285		}()
286	}
287	result.fn = client.listNextResults
288	req, err := client.ListPreparer(ctx, filter, top, skipToken)
289	if err != nil {
290		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "List", nil, "Failure preparing request")
291		return
292	}
293
294	resp, err := client.ListSender(req)
295	if err != nil {
296		result.rrblr.Response = autorest.Response{Response: resp}
297		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "List", resp, "Failure sending request")
298		return
299	}
300
301	result.rrblr, err = client.ListResponder(resp)
302	if err != nil {
303		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "List", resp, "Failure responding to request")
304		return
305	}
306	if result.rrblr.hasNextLink() && result.rrblr.IsEmpty() {
307		err = result.NextWithContext(ctx)
308		return
309	}
310
311	return
312}
313
314// ListPreparer prepares the List request.
315func (client RecommendationsClient) ListPreparer(ctx context.Context, filter string, top *int32, skipToken string) (*http.Request, error) {
316	pathParameters := map[string]interface{}{
317		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
318	}
319
320	const APIVersion = "2017-04-19"
321	queryParameters := map[string]interface{}{
322		"api-version": APIVersion,
323	}
324	if len(filter) > 0 {
325		queryParameters["$filter"] = autorest.Encode("query", filter)
326	}
327	if top != nil {
328		queryParameters["$top"] = autorest.Encode("query", *top)
329	}
330	if len(skipToken) > 0 {
331		queryParameters["$skipToken"] = autorest.Encode("query", skipToken)
332	}
333
334	preparer := autorest.CreatePreparer(
335		autorest.AsGet(),
336		autorest.WithBaseURL(client.BaseURI),
337		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/recommendations", pathParameters),
338		autorest.WithQueryParameters(queryParameters))
339	return preparer.Prepare((&http.Request{}).WithContext(ctx))
340}
341
342// ListSender sends the List request. The method will close the
343// http.Response Body if it receives an error.
344func (client RecommendationsClient) ListSender(req *http.Request) (*http.Response, error) {
345	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
346}
347
348// ListResponder handles the response to the List request. The method always
349// closes the http.Response Body.
350func (client RecommendationsClient) ListResponder(resp *http.Response) (result ResourceRecommendationBaseListResult, err error) {
351	err = autorest.Respond(
352		resp,
353		azure.WithErrorUnlessStatusCode(http.StatusOK),
354		autorest.ByUnmarshallingJSON(&result),
355		autorest.ByClosing())
356	result.Response = autorest.Response{Response: resp}
357	return
358}
359
360// listNextResults retrieves the next set of results, if any.
361func (client RecommendationsClient) listNextResults(ctx context.Context, lastResults ResourceRecommendationBaseListResult) (result ResourceRecommendationBaseListResult, err error) {
362	req, err := lastResults.resourceRecommendationBaseListResultPreparer(ctx)
363	if err != nil {
364		return result, autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "listNextResults", nil, "Failure preparing next results request")
365	}
366	if req == nil {
367		return
368	}
369	resp, err := client.ListSender(req)
370	if err != nil {
371		result.Response = autorest.Response{Response: resp}
372		return result, autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "listNextResults", resp, "Failure sending next results request")
373	}
374	result, err = client.ListResponder(resp)
375	if err != nil {
376		err = autorest.NewErrorWithError(err, "advisor.RecommendationsClient", "listNextResults", resp, "Failure responding to next results request")
377	}
378	return
379}
380
381// ListComplete enumerates all values, automatically crossing page boundaries as required.
382func (client RecommendationsClient) ListComplete(ctx context.Context, filter string, top *int32, skipToken string) (result ResourceRecommendationBaseListResultIterator, err error) {
383	if tracing.IsEnabled() {
384		ctx = tracing.StartSpan(ctx, fqdn+"/RecommendationsClient.List")
385		defer func() {
386			sc := -1
387			if result.Response().Response.Response != nil {
388				sc = result.page.Response().Response.Response.StatusCode
389			}
390			tracing.EndSpan(ctx, sc, err)
391		}()
392	}
393	result.page, err = client.List(ctx, filter, top, skipToken)
394	return
395}
396