1package sql
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// ServerAdvisorsClient is the the Azure SQL Database management API provides a RESTful set of web services that
18// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
19// and delete databases.
20type ServerAdvisorsClient struct {
21	BaseClient
22}
23
24// NewServerAdvisorsClient creates an instance of the ServerAdvisorsClient client.
25func NewServerAdvisorsClient(subscriptionID string) ServerAdvisorsClient {
26	return NewServerAdvisorsClientWithBaseURI(DefaultBaseURI, subscriptionID)
27}
28
29// NewServerAdvisorsClientWithBaseURI creates an instance of the ServerAdvisorsClient client using a custom endpoint.
30// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
31func NewServerAdvisorsClientWithBaseURI(baseURI string, subscriptionID string) ServerAdvisorsClient {
32	return ServerAdvisorsClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// CreateOrUpdate creates or updates a server advisor.
36// Parameters:
37// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
38// from the Azure Resource Manager API or the portal.
39// serverName - the name of the server.
40// advisorName - the name of the Server Advisor.
41// parameters - the requested advisor resource state.
42func (client ServerAdvisorsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, advisorName string, parameters Advisor) (result Advisor, err error) {
43	if tracing.IsEnabled() {
44		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAdvisorsClient.CreateOrUpdate")
45		defer func() {
46			sc := -1
47			if result.Response.Response != nil {
48				sc = result.Response.Response.StatusCode
49			}
50			tracing.EndSpan(ctx, sc, err)
51		}()
52	}
53	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, advisorName, parameters)
54	if err != nil {
55		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "CreateOrUpdate", nil, "Failure preparing request")
56		return
57	}
58
59	resp, err := client.CreateOrUpdateSender(req)
60	if err != nil {
61		result.Response = autorest.Response{Response: resp}
62		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "CreateOrUpdate", resp, "Failure sending request")
63		return
64	}
65
66	result, err = client.CreateOrUpdateResponder(resp)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "CreateOrUpdate", resp, "Failure responding to request")
69		return
70	}
71
72	return
73}
74
75// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
76func (client ServerAdvisorsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, advisorName string, parameters Advisor) (*http.Request, error) {
77	pathParameters := map[string]interface{}{
78		"advisorName":       autorest.Encode("path", advisorName),
79		"resourceGroupName": autorest.Encode("path", resourceGroupName),
80		"serverName":        autorest.Encode("path", serverName),
81		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
82	}
83
84	const APIVersion = "2014-04-01"
85	queryParameters := map[string]interface{}{
86		"api-version": APIVersion,
87	}
88
89	parameters.Kind = nil
90	parameters.Location = nil
91	preparer := autorest.CreatePreparer(
92		autorest.AsContentType("application/json; charset=utf-8"),
93		autorest.AsPut(),
94		autorest.WithBaseURL(client.BaseURI),
95		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/advisors/{advisorName}", pathParameters),
96		autorest.WithJSON(parameters),
97		autorest.WithQueryParameters(queryParameters))
98	return preparer.Prepare((&http.Request{}).WithContext(ctx))
99}
100
101// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
102// http.Response Body if it receives an error.
103func (client ServerAdvisorsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
104	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
105}
106
107// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
108// closes the http.Response Body.
109func (client ServerAdvisorsClient) CreateOrUpdateResponder(resp *http.Response) (result Advisor, err error) {
110	err = autorest.Respond(
111		resp,
112		azure.WithErrorUnlessStatusCode(http.StatusOK),
113		autorest.ByUnmarshallingJSON(&result),
114		autorest.ByClosing())
115	result.Response = autorest.Response{Response: resp}
116	return
117}
118
119// Get gets a server advisor.
120// Parameters:
121// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
122// from the Azure Resource Manager API or the portal.
123// serverName - the name of the server.
124// advisorName - the name of the Server Advisor.
125func (client ServerAdvisorsClient) Get(ctx context.Context, resourceGroupName string, serverName string, advisorName string) (result Advisor, err error) {
126	if tracing.IsEnabled() {
127		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAdvisorsClient.Get")
128		defer func() {
129			sc := -1
130			if result.Response.Response != nil {
131				sc = result.Response.Response.StatusCode
132			}
133			tracing.EndSpan(ctx, sc, err)
134		}()
135	}
136	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, advisorName)
137	if err != nil {
138		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "Get", nil, "Failure preparing request")
139		return
140	}
141
142	resp, err := client.GetSender(req)
143	if err != nil {
144		result.Response = autorest.Response{Response: resp}
145		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "Get", resp, "Failure sending request")
146		return
147	}
148
149	result, err = client.GetResponder(resp)
150	if err != nil {
151		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "Get", resp, "Failure responding to request")
152		return
153	}
154
155	return
156}
157
158// GetPreparer prepares the Get request.
159func (client ServerAdvisorsClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, advisorName string) (*http.Request, error) {
160	pathParameters := map[string]interface{}{
161		"advisorName":       autorest.Encode("path", advisorName),
162		"resourceGroupName": autorest.Encode("path", resourceGroupName),
163		"serverName":        autorest.Encode("path", serverName),
164		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
165	}
166
167	const APIVersion = "2014-04-01"
168	queryParameters := map[string]interface{}{
169		"api-version": APIVersion,
170	}
171
172	preparer := autorest.CreatePreparer(
173		autorest.AsGet(),
174		autorest.WithBaseURL(client.BaseURI),
175		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/advisors/{advisorName}", pathParameters),
176		autorest.WithQueryParameters(queryParameters))
177	return preparer.Prepare((&http.Request{}).WithContext(ctx))
178}
179
180// GetSender sends the Get request. The method will close the
181// http.Response Body if it receives an error.
182func (client ServerAdvisorsClient) GetSender(req *http.Request) (*http.Response, error) {
183	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
184}
185
186// GetResponder handles the response to the Get request. The method always
187// closes the http.Response Body.
188func (client ServerAdvisorsClient) GetResponder(resp *http.Response) (result Advisor, err error) {
189	err = autorest.Respond(
190		resp,
191		azure.WithErrorUnlessStatusCode(http.StatusOK),
192		autorest.ByUnmarshallingJSON(&result),
193		autorest.ByClosing())
194	result.Response = autorest.Response{Response: resp}
195	return
196}
197
198// ListByServer gets a list of server advisors.
199// Parameters:
200// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
201// from the Azure Resource Manager API or the portal.
202// serverName - the name of the server.
203func (client ServerAdvisorsClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string) (result AdvisorListResult, err error) {
204	if tracing.IsEnabled() {
205		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAdvisorsClient.ListByServer")
206		defer func() {
207			sc := -1
208			if result.Response.Response != nil {
209				sc = result.Response.Response.StatusCode
210			}
211			tracing.EndSpan(ctx, sc, err)
212		}()
213	}
214	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName)
215	if err != nil {
216		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "ListByServer", nil, "Failure preparing request")
217		return
218	}
219
220	resp, err := client.ListByServerSender(req)
221	if err != nil {
222		result.Response = autorest.Response{Response: resp}
223		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "ListByServer", resp, "Failure sending request")
224		return
225	}
226
227	result, err = client.ListByServerResponder(resp)
228	if err != nil {
229		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "ListByServer", resp, "Failure responding to request")
230		return
231	}
232
233	return
234}
235
236// ListByServerPreparer prepares the ListByServer request.
237func (client ServerAdvisorsClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
238	pathParameters := map[string]interface{}{
239		"resourceGroupName": autorest.Encode("path", resourceGroupName),
240		"serverName":        autorest.Encode("path", serverName),
241		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
242	}
243
244	const APIVersion = "2014-04-01"
245	queryParameters := map[string]interface{}{
246		"api-version": APIVersion,
247	}
248
249	preparer := autorest.CreatePreparer(
250		autorest.AsGet(),
251		autorest.WithBaseURL(client.BaseURI),
252		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/advisors", pathParameters),
253		autorest.WithQueryParameters(queryParameters))
254	return preparer.Prepare((&http.Request{}).WithContext(ctx))
255}
256
257// ListByServerSender sends the ListByServer request. The method will close the
258// http.Response Body if it receives an error.
259func (client ServerAdvisorsClient) ListByServerSender(req *http.Request) (*http.Response, error) {
260	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
261}
262
263// ListByServerResponder handles the response to the ListByServer request. The method always
264// closes the http.Response Body.
265func (client ServerAdvisorsClient) ListByServerResponder(resp *http.Response) (result AdvisorListResult, err error) {
266	err = autorest.Respond(
267		resp,
268		azure.WithErrorUnlessStatusCode(http.StatusOK),
269		autorest.ByUnmarshallingJSON(&result),
270		autorest.ByClosing())
271	result.Response = autorest.Response{Response: resp}
272	return
273}
274
275// Update updates a server advisor.
276// Parameters:
277// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
278// from the Azure Resource Manager API or the portal.
279// serverName - the name of the server.
280// advisorName - the name of the Server Advisor.
281// parameters - the requested advisor resource state.
282func (client ServerAdvisorsClient) Update(ctx context.Context, resourceGroupName string, serverName string, advisorName string, parameters Advisor) (result Advisor, err error) {
283	if tracing.IsEnabled() {
284		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAdvisorsClient.Update")
285		defer func() {
286			sc := -1
287			if result.Response.Response != nil {
288				sc = result.Response.Response.StatusCode
289			}
290			tracing.EndSpan(ctx, sc, err)
291		}()
292	}
293	req, err := client.UpdatePreparer(ctx, resourceGroupName, serverName, advisorName, parameters)
294	if err != nil {
295		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "Update", nil, "Failure preparing request")
296		return
297	}
298
299	resp, err := client.UpdateSender(req)
300	if err != nil {
301		result.Response = autorest.Response{Response: resp}
302		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "Update", resp, "Failure sending request")
303		return
304	}
305
306	result, err = client.UpdateResponder(resp)
307	if err != nil {
308		err = autorest.NewErrorWithError(err, "sql.ServerAdvisorsClient", "Update", resp, "Failure responding to request")
309		return
310	}
311
312	return
313}
314
315// UpdatePreparer prepares the Update request.
316func (client ServerAdvisorsClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, advisorName string, parameters Advisor) (*http.Request, error) {
317	pathParameters := map[string]interface{}{
318		"advisorName":       autorest.Encode("path", advisorName),
319		"resourceGroupName": autorest.Encode("path", resourceGroupName),
320		"serverName":        autorest.Encode("path", serverName),
321		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
322	}
323
324	const APIVersion = "2014-04-01"
325	queryParameters := map[string]interface{}{
326		"api-version": APIVersion,
327	}
328
329	parameters.Kind = nil
330	parameters.Location = nil
331	preparer := autorest.CreatePreparer(
332		autorest.AsContentType("application/json; charset=utf-8"),
333		autorest.AsPatch(),
334		autorest.WithBaseURL(client.BaseURI),
335		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/advisors/{advisorName}", pathParameters),
336		autorest.WithJSON(parameters),
337		autorest.WithQueryParameters(queryParameters))
338	return preparer.Prepare((&http.Request{}).WithContext(ctx))
339}
340
341// UpdateSender sends the Update request. The method will close the
342// http.Response Body if it receives an error.
343func (client ServerAdvisorsClient) UpdateSender(req *http.Request) (*http.Response, error) {
344	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
345}
346
347// UpdateResponder handles the response to the Update request. The method always
348// closes the http.Response Body.
349func (client ServerAdvisorsClient) UpdateResponder(resp *http.Response) (result Advisor, err error) {
350	err = autorest.Respond(
351		resp,
352		azure.WithErrorUnlessStatusCode(http.StatusOK),
353		autorest.ByUnmarshallingJSON(&result),
354		autorest.ByClosing())
355	result.Response = autorest.Response{Response: resp}
356	return
357}
358