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// ServerSecurityAlertPoliciesClient is the the Azure SQL Database management API provides a RESTful set of web
18// services that interact with Azure SQL Database services to manage your databases. The API enables you to create,
19// retrieve, update, and delete databases.
20type ServerSecurityAlertPoliciesClient struct {
21	BaseClient
22}
23
24// NewServerSecurityAlertPoliciesClient creates an instance of the ServerSecurityAlertPoliciesClient client.
25func NewServerSecurityAlertPoliciesClient(subscriptionID string) ServerSecurityAlertPoliciesClient {
26	return NewServerSecurityAlertPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
27}
28
29// NewServerSecurityAlertPoliciesClientWithBaseURI creates an instance of the ServerSecurityAlertPoliciesClient client
30// using a custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign
31// clouds, Azure stack).
32func NewServerSecurityAlertPoliciesClientWithBaseURI(baseURI string, subscriptionID string) ServerSecurityAlertPoliciesClient {
33	return ServerSecurityAlertPoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
34}
35
36// CreateOrUpdate creates or updates a threat detection policy.
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 server security alert policy.
42func (client ServerSecurityAlertPoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, parameters ServerSecurityAlertPolicy) (result ServerSecurityAlertPoliciesCreateOrUpdateFuture, err error) {
43	if tracing.IsEnabled() {
44		ctx = tracing.StartSpan(ctx, fqdn+"/ServerSecurityAlertPoliciesClient.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.ServerSecurityAlertPoliciesClient", "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.ServerSecurityAlertPoliciesClient", "CreateOrUpdate", nil, "Failure sending request")
62		return
63	}
64
65	return
66}
67
68// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
69func (client ServerSecurityAlertPoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, parameters ServerSecurityAlertPolicy) (*http.Request, error) {
70	pathParameters := map[string]interface{}{
71		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
72		"securityAlertPolicyName": autorest.Encode("path", "Default"),
73		"serverName":              autorest.Encode("path", serverName),
74		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
75	}
76
77	const APIVersion = "2017-03-01-preview"
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/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/securityAlertPolicies/{securityAlertPolicyName}", pathParameters),
87		autorest.WithJSON(parameters),
88		autorest.WithQueryParameters(queryParameters))
89	return preparer.Prepare((&http.Request{}).WithContext(ctx))
90}
91
92// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
93// http.Response Body if it receives an error.
94func (client ServerSecurityAlertPoliciesClient) CreateOrUpdateSender(req *http.Request) (future ServerSecurityAlertPoliciesCreateOrUpdateFuture, err error) {
95	var resp *http.Response
96	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
97	if err != nil {
98		return
99	}
100	var azf azure.Future
101	azf, err = azure.NewFutureFromResponse(resp)
102	future.FutureAPI = &azf
103	future.Result = future.result
104	return
105}
106
107// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
108// closes the http.Response Body.
109func (client ServerSecurityAlertPoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result ServerSecurityAlertPolicy, err error) {
110	err = autorest.Respond(
111		resp,
112		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
113		autorest.ByUnmarshallingJSON(&result),
114		autorest.ByClosing())
115	result.Response = autorest.Response{Response: resp}
116	return
117}
118
119// Get get a server's security alert policy.
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.
124func (client ServerSecurityAlertPoliciesClient) Get(ctx context.Context, resourceGroupName string, serverName string) (result ServerSecurityAlertPolicy, err error) {
125	if tracing.IsEnabled() {
126		ctx = tracing.StartSpan(ctx, fqdn+"/ServerSecurityAlertPoliciesClient.Get")
127		defer func() {
128			sc := -1
129			if result.Response.Response != nil {
130				sc = result.Response.Response.StatusCode
131			}
132			tracing.EndSpan(ctx, sc, err)
133		}()
134	}
135	req, err := client.GetPreparer(ctx, resourceGroupName, serverName)
136	if err != nil {
137		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "Get", nil, "Failure preparing request")
138		return
139	}
140
141	resp, err := client.GetSender(req)
142	if err != nil {
143		result.Response = autorest.Response{Response: resp}
144		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "Get", resp, "Failure sending request")
145		return
146	}
147
148	result, err = client.GetResponder(resp)
149	if err != nil {
150		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "Get", resp, "Failure responding to request")
151		return
152	}
153
154	return
155}
156
157// GetPreparer prepares the Get request.
158func (client ServerSecurityAlertPoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
159	pathParameters := map[string]interface{}{
160		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
161		"securityAlertPolicyName": autorest.Encode("path", "Default"),
162		"serverName":              autorest.Encode("path", serverName),
163		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
164	}
165
166	const APIVersion = "2017-03-01-preview"
167	queryParameters := map[string]interface{}{
168		"api-version": APIVersion,
169	}
170
171	preparer := autorest.CreatePreparer(
172		autorest.AsGet(),
173		autorest.WithBaseURL(client.BaseURI),
174		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/securityAlertPolicies/{securityAlertPolicyName}", pathParameters),
175		autorest.WithQueryParameters(queryParameters))
176	return preparer.Prepare((&http.Request{}).WithContext(ctx))
177}
178
179// GetSender sends the Get request. The method will close the
180// http.Response Body if it receives an error.
181func (client ServerSecurityAlertPoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
182	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
183}
184
185// GetResponder handles the response to the Get request. The method always
186// closes the http.Response Body.
187func (client ServerSecurityAlertPoliciesClient) GetResponder(resp *http.Response) (result ServerSecurityAlertPolicy, err error) {
188	err = autorest.Respond(
189		resp,
190		azure.WithErrorUnlessStatusCode(http.StatusOK),
191		autorest.ByUnmarshallingJSON(&result),
192		autorest.ByClosing())
193	result.Response = autorest.Response{Response: resp}
194	return
195}
196
197// ListByServer get the server's threat detection policies.
198// Parameters:
199// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
200// from the Azure Resource Manager API or the portal.
201// serverName - the name of the server.
202func (client ServerSecurityAlertPoliciesClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string) (result LogicalServerSecurityAlertPolicyListResultPage, err error) {
203	if tracing.IsEnabled() {
204		ctx = tracing.StartSpan(ctx, fqdn+"/ServerSecurityAlertPoliciesClient.ListByServer")
205		defer func() {
206			sc := -1
207			if result.lssaplr.Response.Response != nil {
208				sc = result.lssaplr.Response.Response.StatusCode
209			}
210			tracing.EndSpan(ctx, sc, err)
211		}()
212	}
213	result.fn = client.listByServerNextResults
214	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName)
215	if err != nil {
216		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "ListByServer", nil, "Failure preparing request")
217		return
218	}
219
220	resp, err := client.ListByServerSender(req)
221	if err != nil {
222		result.lssaplr.Response = autorest.Response{Response: resp}
223		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "ListByServer", resp, "Failure sending request")
224		return
225	}
226
227	result.lssaplr, err = client.ListByServerResponder(resp)
228	if err != nil {
229		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "ListByServer", resp, "Failure responding to request")
230		return
231	}
232	if result.lssaplr.hasNextLink() && result.lssaplr.IsEmpty() {
233		err = result.NextWithContext(ctx)
234		return
235	}
236
237	return
238}
239
240// ListByServerPreparer prepares the ListByServer request.
241func (client ServerSecurityAlertPoliciesClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
242	pathParameters := map[string]interface{}{
243		"resourceGroupName": autorest.Encode("path", resourceGroupName),
244		"serverName":        autorest.Encode("path", serverName),
245		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
246	}
247
248	const APIVersion = "2017-03-01-preview"
249	queryParameters := map[string]interface{}{
250		"api-version": APIVersion,
251	}
252
253	preparer := autorest.CreatePreparer(
254		autorest.AsGet(),
255		autorest.WithBaseURL(client.BaseURI),
256		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/securityAlertPolicies", pathParameters),
257		autorest.WithQueryParameters(queryParameters))
258	return preparer.Prepare((&http.Request{}).WithContext(ctx))
259}
260
261// ListByServerSender sends the ListByServer request. The method will close the
262// http.Response Body if it receives an error.
263func (client ServerSecurityAlertPoliciesClient) ListByServerSender(req *http.Request) (*http.Response, error) {
264	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
265}
266
267// ListByServerResponder handles the response to the ListByServer request. The method always
268// closes the http.Response Body.
269func (client ServerSecurityAlertPoliciesClient) ListByServerResponder(resp *http.Response) (result LogicalServerSecurityAlertPolicyListResult, err error) {
270	err = autorest.Respond(
271		resp,
272		azure.WithErrorUnlessStatusCode(http.StatusOK),
273		autorest.ByUnmarshallingJSON(&result),
274		autorest.ByClosing())
275	result.Response = autorest.Response{Response: resp}
276	return
277}
278
279// listByServerNextResults retrieves the next set of results, if any.
280func (client ServerSecurityAlertPoliciesClient) listByServerNextResults(ctx context.Context, lastResults LogicalServerSecurityAlertPolicyListResult) (result LogicalServerSecurityAlertPolicyListResult, err error) {
281	req, err := lastResults.logicalServerSecurityAlertPolicyListResultPreparer(ctx)
282	if err != nil {
283		return result, autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "listByServerNextResults", nil, "Failure preparing next results request")
284	}
285	if req == nil {
286		return
287	}
288	resp, err := client.ListByServerSender(req)
289	if err != nil {
290		result.Response = autorest.Response{Response: resp}
291		return result, autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "listByServerNextResults", resp, "Failure sending next results request")
292	}
293	result, err = client.ListByServerResponder(resp)
294	if err != nil {
295		err = autorest.NewErrorWithError(err, "sql.ServerSecurityAlertPoliciesClient", "listByServerNextResults", resp, "Failure responding to next results request")
296	}
297	return
298}
299
300// ListByServerComplete enumerates all values, automatically crossing page boundaries as required.
301func (client ServerSecurityAlertPoliciesClient) ListByServerComplete(ctx context.Context, resourceGroupName string, serverName string) (result LogicalServerSecurityAlertPolicyListResultIterator, err error) {
302	if tracing.IsEnabled() {
303		ctx = tracing.StartSpan(ctx, fqdn+"/ServerSecurityAlertPoliciesClient.ListByServer")
304		defer func() {
305			sc := -1
306			if result.Response().Response.Response != nil {
307				sc = result.page.Response().Response.Response.StatusCode
308			}
309			tracing.EndSpan(ctx, sc, err)
310		}()
311	}
312	result.page, err = client.ListByServer(ctx, resourceGroupName, serverName)
313	return
314}
315