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