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