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// ServerKeysClient is the the Azure SQL Database management API provides a RESTful set of web services that interact
18// with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update, and
19// delete databases.
20type ServerKeysClient struct {
21	BaseClient
22}
23
24// NewServerKeysClient creates an instance of the ServerKeysClient client.
25func NewServerKeysClient(subscriptionID string) ServerKeysClient {
26	return NewServerKeysClientWithBaseURI(DefaultBaseURI, subscriptionID)
27}
28
29// NewServerKeysClientWithBaseURI creates an instance of the ServerKeysClient client using a custom endpoint.  Use this
30// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
31func NewServerKeysClientWithBaseURI(baseURI string, subscriptionID string) ServerKeysClient {
32	return ServerKeysClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// CreateOrUpdate creates or updates a server key.
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// keyName - the name of the server key to be operated on (updated or created). The key name is required to be
41// in the format of 'vault_key_version'. For example, if the keyId is
42// https://YourVaultName.vault.azure.net/keys/YourKeyName/01234567890123456789012345678901, then the server key
43// name should be formatted as: YourVaultName_YourKeyName_01234567890123456789012345678901
44// parameters - the requested server key resource state.
45func (client ServerKeysClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, keyName string, parameters ServerKey) (result ServerKeysCreateOrUpdateFuture, err error) {
46	if tracing.IsEnabled() {
47		ctx = tracing.StartSpan(ctx, fqdn+"/ServerKeysClient.CreateOrUpdate")
48		defer func() {
49			sc := -1
50			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
51				sc = result.FutureAPI.Response().StatusCode
52			}
53			tracing.EndSpan(ctx, sc, err)
54		}()
55	}
56	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, keyName, parameters)
57	if err != nil {
58		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "CreateOrUpdate", nil, "Failure preparing request")
59		return
60	}
61
62	result, err = client.CreateOrUpdateSender(req)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "CreateOrUpdate", nil, "Failure sending request")
65		return
66	}
67
68	return
69}
70
71// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
72func (client ServerKeysClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, keyName string, parameters ServerKey) (*http.Request, error) {
73	pathParameters := map[string]interface{}{
74		"keyName":           autorest.Encode("path", keyName),
75		"resourceGroupName": autorest.Encode("path", resourceGroupName),
76		"serverName":        autorest.Encode("path", serverName),
77		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
78	}
79
80	const APIVersion = "2015-05-01-preview"
81	queryParameters := map[string]interface{}{
82		"api-version": APIVersion,
83	}
84
85	parameters.Location = nil
86	preparer := autorest.CreatePreparer(
87		autorest.AsContentType("application/json; charset=utf-8"),
88		autorest.AsPut(),
89		autorest.WithBaseURL(client.BaseURI),
90		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/keys/{keyName}", pathParameters),
91		autorest.WithJSON(parameters),
92		autorest.WithQueryParameters(queryParameters))
93	return preparer.Prepare((&http.Request{}).WithContext(ctx))
94}
95
96// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
97// http.Response Body if it receives an error.
98func (client ServerKeysClient) CreateOrUpdateSender(req *http.Request) (future ServerKeysCreateOrUpdateFuture, err error) {
99	var resp *http.Response
100	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
101	if err != nil {
102		return
103	}
104	var azf azure.Future
105	azf, err = azure.NewFutureFromResponse(resp)
106	future.FutureAPI = &azf
107	future.Result = future.result
108	return
109}
110
111// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
112// closes the http.Response Body.
113func (client ServerKeysClient) CreateOrUpdateResponder(resp *http.Response) (result ServerKey, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// Delete deletes the server key with the given name.
124// Parameters:
125// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
126// from the Azure Resource Manager API or the portal.
127// serverName - the name of the server.
128// keyName - the name of the server key to be deleted.
129func (client ServerKeysClient) Delete(ctx context.Context, resourceGroupName string, serverName string, keyName string) (result ServerKeysDeleteFuture, err error) {
130	if tracing.IsEnabled() {
131		ctx = tracing.StartSpan(ctx, fqdn+"/ServerKeysClient.Delete")
132		defer func() {
133			sc := -1
134			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
135				sc = result.FutureAPI.Response().StatusCode
136			}
137			tracing.EndSpan(ctx, sc, err)
138		}()
139	}
140	req, err := client.DeletePreparer(ctx, resourceGroupName, serverName, keyName)
141	if err != nil {
142		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "Delete", nil, "Failure preparing request")
143		return
144	}
145
146	result, err = client.DeleteSender(req)
147	if err != nil {
148		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "Delete", nil, "Failure sending request")
149		return
150	}
151
152	return
153}
154
155// DeletePreparer prepares the Delete request.
156func (client ServerKeysClient) DeletePreparer(ctx context.Context, resourceGroupName string, serverName string, keyName string) (*http.Request, error) {
157	pathParameters := map[string]interface{}{
158		"keyName":           autorest.Encode("path", keyName),
159		"resourceGroupName": autorest.Encode("path", resourceGroupName),
160		"serverName":        autorest.Encode("path", serverName),
161		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
162	}
163
164	const APIVersion = "2015-05-01-preview"
165	queryParameters := map[string]interface{}{
166		"api-version": APIVersion,
167	}
168
169	preparer := autorest.CreatePreparer(
170		autorest.AsDelete(),
171		autorest.WithBaseURL(client.BaseURI),
172		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/keys/{keyName}", pathParameters),
173		autorest.WithQueryParameters(queryParameters))
174	return preparer.Prepare((&http.Request{}).WithContext(ctx))
175}
176
177// DeleteSender sends the Delete request. The method will close the
178// http.Response Body if it receives an error.
179func (client ServerKeysClient) DeleteSender(req *http.Request) (future ServerKeysDeleteFuture, err error) {
180	var resp *http.Response
181	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
182	if err != nil {
183		return
184	}
185	var azf azure.Future
186	azf, err = azure.NewFutureFromResponse(resp)
187	future.FutureAPI = &azf
188	future.Result = future.result
189	return
190}
191
192// DeleteResponder handles the response to the Delete request. The method always
193// closes the http.Response Body.
194func (client ServerKeysClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
195	err = autorest.Respond(
196		resp,
197		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
198		autorest.ByClosing())
199	result.Response = resp
200	return
201}
202
203// Get gets a server key.
204// Parameters:
205// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
206// from the Azure Resource Manager API or the portal.
207// serverName - the name of the server.
208// keyName - the name of the server key to be retrieved.
209func (client ServerKeysClient) Get(ctx context.Context, resourceGroupName string, serverName string, keyName string) (result ServerKey, err error) {
210	if tracing.IsEnabled() {
211		ctx = tracing.StartSpan(ctx, fqdn+"/ServerKeysClient.Get")
212		defer func() {
213			sc := -1
214			if result.Response.Response != nil {
215				sc = result.Response.Response.StatusCode
216			}
217			tracing.EndSpan(ctx, sc, err)
218		}()
219	}
220	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, keyName)
221	if err != nil {
222		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "Get", nil, "Failure preparing request")
223		return
224	}
225
226	resp, err := client.GetSender(req)
227	if err != nil {
228		result.Response = autorest.Response{Response: resp}
229		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "Get", resp, "Failure sending request")
230		return
231	}
232
233	result, err = client.GetResponder(resp)
234	if err != nil {
235		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "Get", resp, "Failure responding to request")
236		return
237	}
238
239	return
240}
241
242// GetPreparer prepares the Get request.
243func (client ServerKeysClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, keyName string) (*http.Request, error) {
244	pathParameters := map[string]interface{}{
245		"keyName":           autorest.Encode("path", keyName),
246		"resourceGroupName": autorest.Encode("path", resourceGroupName),
247		"serverName":        autorest.Encode("path", serverName),
248		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
249	}
250
251	const APIVersion = "2015-05-01-preview"
252	queryParameters := map[string]interface{}{
253		"api-version": APIVersion,
254	}
255
256	preparer := autorest.CreatePreparer(
257		autorest.AsGet(),
258		autorest.WithBaseURL(client.BaseURI),
259		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/keys/{keyName}", pathParameters),
260		autorest.WithQueryParameters(queryParameters))
261	return preparer.Prepare((&http.Request{}).WithContext(ctx))
262}
263
264// GetSender sends the Get request. The method will close the
265// http.Response Body if it receives an error.
266func (client ServerKeysClient) GetSender(req *http.Request) (*http.Response, error) {
267	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
268}
269
270// GetResponder handles the response to the Get request. The method always
271// closes the http.Response Body.
272func (client ServerKeysClient) GetResponder(resp *http.Response) (result ServerKey, err error) {
273	err = autorest.Respond(
274		resp,
275		azure.WithErrorUnlessStatusCode(http.StatusOK),
276		autorest.ByUnmarshallingJSON(&result),
277		autorest.ByClosing())
278	result.Response = autorest.Response{Response: resp}
279	return
280}
281
282// ListByServer gets a list of server keys.
283// Parameters:
284// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
285// from the Azure Resource Manager API or the portal.
286// serverName - the name of the server.
287func (client ServerKeysClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string) (result ServerKeyListResultPage, err error) {
288	if tracing.IsEnabled() {
289		ctx = tracing.StartSpan(ctx, fqdn+"/ServerKeysClient.ListByServer")
290		defer func() {
291			sc := -1
292			if result.sklr.Response.Response != nil {
293				sc = result.sklr.Response.Response.StatusCode
294			}
295			tracing.EndSpan(ctx, sc, err)
296		}()
297	}
298	result.fn = client.listByServerNextResults
299	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName)
300	if err != nil {
301		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "ListByServer", nil, "Failure preparing request")
302		return
303	}
304
305	resp, err := client.ListByServerSender(req)
306	if err != nil {
307		result.sklr.Response = autorest.Response{Response: resp}
308		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "ListByServer", resp, "Failure sending request")
309		return
310	}
311
312	result.sklr, err = client.ListByServerResponder(resp)
313	if err != nil {
314		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "ListByServer", resp, "Failure responding to request")
315		return
316	}
317	if result.sklr.hasNextLink() && result.sklr.IsEmpty() {
318		err = result.NextWithContext(ctx)
319		return
320	}
321
322	return
323}
324
325// ListByServerPreparer prepares the ListByServer request.
326func (client ServerKeysClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
327	pathParameters := map[string]interface{}{
328		"resourceGroupName": autorest.Encode("path", resourceGroupName),
329		"serverName":        autorest.Encode("path", serverName),
330		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
331	}
332
333	const APIVersion = "2015-05-01-preview"
334	queryParameters := map[string]interface{}{
335		"api-version": APIVersion,
336	}
337
338	preparer := autorest.CreatePreparer(
339		autorest.AsGet(),
340		autorest.WithBaseURL(client.BaseURI),
341		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/keys", pathParameters),
342		autorest.WithQueryParameters(queryParameters))
343	return preparer.Prepare((&http.Request{}).WithContext(ctx))
344}
345
346// ListByServerSender sends the ListByServer request. The method will close the
347// http.Response Body if it receives an error.
348func (client ServerKeysClient) ListByServerSender(req *http.Request) (*http.Response, error) {
349	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
350}
351
352// ListByServerResponder handles the response to the ListByServer request. The method always
353// closes the http.Response Body.
354func (client ServerKeysClient) ListByServerResponder(resp *http.Response) (result ServerKeyListResult, err error) {
355	err = autorest.Respond(
356		resp,
357		azure.WithErrorUnlessStatusCode(http.StatusOK),
358		autorest.ByUnmarshallingJSON(&result),
359		autorest.ByClosing())
360	result.Response = autorest.Response{Response: resp}
361	return
362}
363
364// listByServerNextResults retrieves the next set of results, if any.
365func (client ServerKeysClient) listByServerNextResults(ctx context.Context, lastResults ServerKeyListResult) (result ServerKeyListResult, err error) {
366	req, err := lastResults.serverKeyListResultPreparer(ctx)
367	if err != nil {
368		return result, autorest.NewErrorWithError(err, "sql.ServerKeysClient", "listByServerNextResults", nil, "Failure preparing next results request")
369	}
370	if req == nil {
371		return
372	}
373	resp, err := client.ListByServerSender(req)
374	if err != nil {
375		result.Response = autorest.Response{Response: resp}
376		return result, autorest.NewErrorWithError(err, "sql.ServerKeysClient", "listByServerNextResults", resp, "Failure sending next results request")
377	}
378	result, err = client.ListByServerResponder(resp)
379	if err != nil {
380		err = autorest.NewErrorWithError(err, "sql.ServerKeysClient", "listByServerNextResults", resp, "Failure responding to next results request")
381	}
382	return
383}
384
385// ListByServerComplete enumerates all values, automatically crossing page boundaries as required.
386func (client ServerKeysClient) ListByServerComplete(ctx context.Context, resourceGroupName string, serverName string) (result ServerKeyListResultIterator, err error) {
387	if tracing.IsEnabled() {
388		ctx = tracing.StartSpan(ctx, fqdn+"/ServerKeysClient.ListByServer")
389		defer func() {
390			sc := -1
391			if result.Response().Response.Response != nil {
392				sc = result.page.Response().Response.Response.StatusCode
393			}
394			tracing.EndSpan(ctx, sc, err)
395		}()
396	}
397	result.page, err = client.ListByServer(ctx, resourceGroupName, serverName)
398	return
399}
400