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// DatabaseThreatDetectionPoliciesClient 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 DatabaseThreatDetectionPoliciesClient struct {
32	BaseClient
33}
34
35// NewDatabaseThreatDetectionPoliciesClient creates an instance of the DatabaseThreatDetectionPoliciesClient client.
36func NewDatabaseThreatDetectionPoliciesClient(subscriptionID string) DatabaseThreatDetectionPoliciesClient {
37	return NewDatabaseThreatDetectionPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
38}
39
40// NewDatabaseThreatDetectionPoliciesClientWithBaseURI creates an instance of the DatabaseThreatDetectionPoliciesClient
41// client using a custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI
42// (sovereign clouds, Azure stack).
43func NewDatabaseThreatDetectionPoliciesClientWithBaseURI(baseURI string, subscriptionID string) DatabaseThreatDetectionPoliciesClient {
44	return DatabaseThreatDetectionPoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
45}
46
47// CreateOrUpdate creates or updates a database's 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// databaseName - the name of the database for which database Threat Detection policy is defined.
53// parameters - the database Threat Detection policy.
54func (client DatabaseThreatDetectionPoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, databaseName string, parameters DatabaseSecurityAlertPolicy) (result DatabaseSecurityAlertPolicy, err error) {
55	if tracing.IsEnabled() {
56		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseThreatDetectionPoliciesClient.CreateOrUpdate")
57		defer func() {
58			sc := -1
59			if result.Response.Response != nil {
60				sc = result.Response.Response.StatusCode
61			}
62			tracing.EndSpan(ctx, sc, err)
63		}()
64	}
65	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, databaseName, parameters)
66	if err != nil {
67		err = autorest.NewErrorWithError(err, "sql.DatabaseThreatDetectionPoliciesClient", "CreateOrUpdate", nil, "Failure preparing request")
68		return
69	}
70
71	resp, err := client.CreateOrUpdateSender(req)
72	if err != nil {
73		result.Response = autorest.Response{Response: resp}
74		err = autorest.NewErrorWithError(err, "sql.DatabaseThreatDetectionPoliciesClient", "CreateOrUpdate", resp, "Failure sending request")
75		return
76	}
77
78	result, err = client.CreateOrUpdateResponder(resp)
79	if err != nil {
80		err = autorest.NewErrorWithError(err, "sql.DatabaseThreatDetectionPoliciesClient", "CreateOrUpdate", resp, "Failure responding to request")
81		return
82	}
83
84	return
85}
86
87// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
88func (client DatabaseThreatDetectionPoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, parameters DatabaseSecurityAlertPolicy) (*http.Request, error) {
89	pathParameters := map[string]interface{}{
90		"databaseName":            autorest.Encode("path", databaseName),
91		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
92		"securityAlertPolicyName": autorest.Encode("path", "default"),
93		"serverName":              autorest.Encode("path", serverName),
94		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
95	}
96
97	const APIVersion = "2014-04-01"
98	queryParameters := map[string]interface{}{
99		"api-version": APIVersion,
100	}
101
102	parameters.Kind = nil
103	preparer := autorest.CreatePreparer(
104		autorest.AsContentType("application/json; charset=utf-8"),
105		autorest.AsPut(),
106		autorest.WithBaseURL(client.BaseURI),
107		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/securityAlertPolicies/{securityAlertPolicyName}", pathParameters),
108		autorest.WithJSON(parameters),
109		autorest.WithQueryParameters(queryParameters))
110	return preparer.Prepare((&http.Request{}).WithContext(ctx))
111}
112
113// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
114// http.Response Body if it receives an error.
115func (client DatabaseThreatDetectionPoliciesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
116	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
117}
118
119// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
120// closes the http.Response Body.
121func (client DatabaseThreatDetectionPoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result DatabaseSecurityAlertPolicy, err error) {
122	err = autorest.Respond(
123		resp,
124		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
125		autorest.ByUnmarshallingJSON(&result),
126		autorest.ByClosing())
127	result.Response = autorest.Response{Response: resp}
128	return
129}
130
131// Get gets a database's threat detection policy.
132// Parameters:
133// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
134// from the Azure Resource Manager API or the portal.
135// serverName - the name of the server.
136// databaseName - the name of the database for which database Threat Detection policy is defined.
137func (client DatabaseThreatDetectionPoliciesClient) Get(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result DatabaseSecurityAlertPolicy, err error) {
138	if tracing.IsEnabled() {
139		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseThreatDetectionPoliciesClient.Get")
140		defer func() {
141			sc := -1
142			if result.Response.Response != nil {
143				sc = result.Response.Response.StatusCode
144			}
145			tracing.EndSpan(ctx, sc, err)
146		}()
147	}
148	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, databaseName)
149	if err != nil {
150		err = autorest.NewErrorWithError(err, "sql.DatabaseThreatDetectionPoliciesClient", "Get", nil, "Failure preparing request")
151		return
152	}
153
154	resp, err := client.GetSender(req)
155	if err != nil {
156		result.Response = autorest.Response{Response: resp}
157		err = autorest.NewErrorWithError(err, "sql.DatabaseThreatDetectionPoliciesClient", "Get", resp, "Failure sending request")
158		return
159	}
160
161	result, err = client.GetResponder(resp)
162	if err != nil {
163		err = autorest.NewErrorWithError(err, "sql.DatabaseThreatDetectionPoliciesClient", "Get", resp, "Failure responding to request")
164		return
165	}
166
167	return
168}
169
170// GetPreparer prepares the Get request.
171func (client DatabaseThreatDetectionPoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (*http.Request, error) {
172	pathParameters := map[string]interface{}{
173		"databaseName":            autorest.Encode("path", databaseName),
174		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
175		"securityAlertPolicyName": autorest.Encode("path", "default"),
176		"serverName":              autorest.Encode("path", serverName),
177		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
178	}
179
180	const APIVersion = "2014-04-01"
181	queryParameters := map[string]interface{}{
182		"api-version": APIVersion,
183	}
184
185	preparer := autorest.CreatePreparer(
186		autorest.AsGet(),
187		autorest.WithBaseURL(client.BaseURI),
188		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/securityAlertPolicies/{securityAlertPolicyName}", pathParameters),
189		autorest.WithQueryParameters(queryParameters))
190	return preparer.Prepare((&http.Request{}).WithContext(ctx))
191}
192
193// GetSender sends the Get request. The method will close the
194// http.Response Body if it receives an error.
195func (client DatabaseThreatDetectionPoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
196	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
197}
198
199// GetResponder handles the response to the Get request. The method always
200// closes the http.Response Body.
201func (client DatabaseThreatDetectionPoliciesClient) GetResponder(resp *http.Response) (result DatabaseSecurityAlertPolicy, err error) {
202	err = autorest.Respond(
203		resp,
204		azure.WithErrorUnlessStatusCode(http.StatusOK),
205		autorest.ByUnmarshallingJSON(&result),
206		autorest.ByClosing())
207	result.Response = autorest.Response{Response: resp}
208	return
209}
210