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