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// DataMaskingPoliciesClient is the the Azure SQL Database management API provides a RESTful set of web services that
29// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
30// and delete databases.
31type DataMaskingPoliciesClient struct {
32	BaseClient
33}
34
35// NewDataMaskingPoliciesClient creates an instance of the DataMaskingPoliciesClient client.
36func NewDataMaskingPoliciesClient(subscriptionID string) DataMaskingPoliciesClient {
37	return NewDataMaskingPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
38}
39
40// NewDataMaskingPoliciesClientWithBaseURI creates an instance of the DataMaskingPoliciesClient client using a custom
41// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
42// stack).
43func NewDataMaskingPoliciesClientWithBaseURI(baseURI string, subscriptionID string) DataMaskingPoliciesClient {
44	return DataMaskingPoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
45}
46
47// CreateOrUpdate creates or updates a database data masking 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.
53// parameters - parameters for creating or updating a data masking policy.
54func (client DataMaskingPoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, databaseName string, parameters DataMaskingPolicy) (result DataMaskingPolicy, err error) {
55	if tracing.IsEnabled() {
56		ctx = tracing.StartSpan(ctx, fqdn+"/DataMaskingPoliciesClient.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.DataMaskingPoliciesClient", "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.DataMaskingPoliciesClient", "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.DataMaskingPoliciesClient", "CreateOrUpdate", resp, "Failure responding to request")
81		return
82	}
83
84	return
85}
86
87// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
88func (client DataMaskingPoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, parameters DataMaskingPolicy) (*http.Request, error) {
89	pathParameters := map[string]interface{}{
90		"databaseName":          autorest.Encode("path", databaseName),
91		"dataMaskingPolicyName": autorest.Encode("path", "Default"),
92		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
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.Location = nil
103	parameters.Kind = nil
104	preparer := autorest.CreatePreparer(
105		autorest.AsContentType("application/json; charset=utf-8"),
106		autorest.AsPut(),
107		autorest.WithBaseURL(client.BaseURI),
108		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/dataMaskingPolicies/{dataMaskingPolicyName}", pathParameters),
109		autorest.WithJSON(parameters),
110		autorest.WithQueryParameters(queryParameters))
111	return preparer.Prepare((&http.Request{}).WithContext(ctx))
112}
113
114// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
115// http.Response Body if it receives an error.
116func (client DataMaskingPoliciesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
117	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
118}
119
120// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
121// closes the http.Response Body.
122func (client DataMaskingPoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result DataMaskingPolicy, err error) {
123	err = autorest.Respond(
124		resp,
125		azure.WithErrorUnlessStatusCode(http.StatusOK),
126		autorest.ByUnmarshallingJSON(&result),
127		autorest.ByClosing())
128	result.Response = autorest.Response{Response: resp}
129	return
130}
131
132// Get gets a database data masking policy.
133// Parameters:
134// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
135// from the Azure Resource Manager API or the portal.
136// serverName - the name of the server.
137// databaseName - the name of the database.
138func (client DataMaskingPoliciesClient) Get(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result DataMaskingPolicy, err error) {
139	if tracing.IsEnabled() {
140		ctx = tracing.StartSpan(ctx, fqdn+"/DataMaskingPoliciesClient.Get")
141		defer func() {
142			sc := -1
143			if result.Response.Response != nil {
144				sc = result.Response.Response.StatusCode
145			}
146			tracing.EndSpan(ctx, sc, err)
147		}()
148	}
149	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, databaseName)
150	if err != nil {
151		err = autorest.NewErrorWithError(err, "sql.DataMaskingPoliciesClient", "Get", nil, "Failure preparing request")
152		return
153	}
154
155	resp, err := client.GetSender(req)
156	if err != nil {
157		result.Response = autorest.Response{Response: resp}
158		err = autorest.NewErrorWithError(err, "sql.DataMaskingPoliciesClient", "Get", resp, "Failure sending request")
159		return
160	}
161
162	result, err = client.GetResponder(resp)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "sql.DataMaskingPoliciesClient", "Get", resp, "Failure responding to request")
165		return
166	}
167
168	return
169}
170
171// GetPreparer prepares the Get request.
172func (client DataMaskingPoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (*http.Request, error) {
173	pathParameters := map[string]interface{}{
174		"databaseName":          autorest.Encode("path", databaseName),
175		"dataMaskingPolicyName": autorest.Encode("path", "Default"),
176		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
177		"serverName":            autorest.Encode("path", serverName),
178		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
179	}
180
181	const APIVersion = "2014-04-01"
182	queryParameters := map[string]interface{}{
183		"api-version": APIVersion,
184	}
185
186	preparer := autorest.CreatePreparer(
187		autorest.AsGet(),
188		autorest.WithBaseURL(client.BaseURI),
189		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/dataMaskingPolicies/{dataMaskingPolicyName}", pathParameters),
190		autorest.WithQueryParameters(queryParameters))
191	return preparer.Prepare((&http.Request{}).WithContext(ctx))
192}
193
194// GetSender sends the Get request. The method will close the
195// http.Response Body if it receives an error.
196func (client DataMaskingPoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
197	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
198}
199
200// GetResponder handles the response to the Get request. The method always
201// closes the http.Response Body.
202func (client DataMaskingPoliciesClient) GetResponder(resp *http.Response) (result DataMaskingPolicy, err error) {
203	err = autorest.Respond(
204		resp,
205		azure.WithErrorUnlessStatusCode(http.StatusOK),
206		autorest.ByUnmarshallingJSON(&result),
207		autorest.ByClosing())
208	result.Response = autorest.Response{Response: resp}
209	return
210}
211