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/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// DataMaskingRulesClient is the the Azure SQL Database management API provides a RESTful set of web services that
30// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
31// and delete databases.
32type DataMaskingRulesClient struct {
33	BaseClient
34}
35
36// NewDataMaskingRulesClient creates an instance of the DataMaskingRulesClient client.
37func NewDataMaskingRulesClient(subscriptionID string) DataMaskingRulesClient {
38	return NewDataMaskingRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
39}
40
41// NewDataMaskingRulesClientWithBaseURI creates an instance of the DataMaskingRulesClient client using a custom
42// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
43// stack).
44func NewDataMaskingRulesClientWithBaseURI(baseURI string, subscriptionID string) DataMaskingRulesClient {
45	return DataMaskingRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
46}
47
48// CreateOrUpdate creates or updates a database data masking rule.
49// Parameters:
50// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
51// from the Azure Resource Manager API or the portal.
52// serverName - the name of the server.
53// databaseName - the name of the database.
54// dataMaskingRuleName - the name of the data masking rule.
55// parameters - the required parameters for creating or updating a data masking rule.
56func (client DataMaskingRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, databaseName string, dataMaskingRuleName string, parameters DataMaskingRule) (result DataMaskingRule, err error) {
57	if tracing.IsEnabled() {
58		ctx = tracing.StartSpan(ctx, fqdn+"/DataMaskingRulesClient.CreateOrUpdate")
59		defer func() {
60			sc := -1
61			if result.Response.Response != nil {
62				sc = result.Response.Response.StatusCode
63			}
64			tracing.EndSpan(ctx, sc, err)
65		}()
66	}
67	if err := validation.Validate([]validation.Validation{
68		{TargetValue: parameters,
69			Constraints: []validation.Constraint{{Target: "parameters.DataMaskingRuleProperties", Name: validation.Null, Rule: false,
70				Chain: []validation.Constraint{{Target: "parameters.DataMaskingRuleProperties.SchemaName", Name: validation.Null, Rule: true, Chain: nil},
71					{Target: "parameters.DataMaskingRuleProperties.TableName", Name: validation.Null, Rule: true, Chain: nil},
72					{Target: "parameters.DataMaskingRuleProperties.ColumnName", Name: validation.Null, Rule: true, Chain: nil},
73				}}}}}); err != nil {
74		return result, validation.NewError("sql.DataMaskingRulesClient", "CreateOrUpdate", err.Error())
75	}
76
77	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, databaseName, dataMaskingRuleName, parameters)
78	if err != nil {
79		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
80		return
81	}
82
83	resp, err := client.CreateOrUpdateSender(req)
84	if err != nil {
85		result.Response = autorest.Response{Response: resp}
86		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "CreateOrUpdate", resp, "Failure sending request")
87		return
88	}
89
90	result, err = client.CreateOrUpdateResponder(resp)
91	if err != nil {
92		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "CreateOrUpdate", resp, "Failure responding to request")
93	}
94
95	return
96}
97
98// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
99func (client DataMaskingRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, dataMaskingRuleName string, parameters DataMaskingRule) (*http.Request, error) {
100	pathParameters := map[string]interface{}{
101		"databaseName":          autorest.Encode("path", databaseName),
102		"dataMaskingPolicyName": autorest.Encode("path", "Default"),
103		"dataMaskingRuleName":   autorest.Encode("path", dataMaskingRuleName),
104		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
105		"serverName":            autorest.Encode("path", serverName),
106		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
107	}
108
109	const APIVersion = "2014-04-01"
110	queryParameters := map[string]interface{}{
111		"api-version": APIVersion,
112	}
113
114	parameters.Location = nil
115	parameters.Kind = nil
116	preparer := autorest.CreatePreparer(
117		autorest.AsContentType("application/json; charset=utf-8"),
118		autorest.AsPut(),
119		autorest.WithBaseURL(client.BaseURI),
120		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/dataMaskingPolicies/{dataMaskingPolicyName}/rules/{dataMaskingRuleName}", pathParameters),
121		autorest.WithJSON(parameters),
122		autorest.WithQueryParameters(queryParameters))
123	return preparer.Prepare((&http.Request{}).WithContext(ctx))
124}
125
126// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
127// http.Response Body if it receives an error.
128func (client DataMaskingRulesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
129	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
130}
131
132// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
133// closes the http.Response Body.
134func (client DataMaskingRulesClient) CreateOrUpdateResponder(resp *http.Response) (result DataMaskingRule, err error) {
135	err = autorest.Respond(
136		resp,
137		client.ByInspecting(),
138		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
139		autorest.ByUnmarshallingJSON(&result),
140		autorest.ByClosing())
141	result.Response = autorest.Response{Response: resp}
142	return
143}
144
145// ListByDatabase gets a list of database data masking rules.
146// Parameters:
147// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
148// from the Azure Resource Manager API or the portal.
149// serverName - the name of the server.
150// databaseName - the name of the database.
151func (client DataMaskingRulesClient) ListByDatabase(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result DataMaskingRuleListResult, err error) {
152	if tracing.IsEnabled() {
153		ctx = tracing.StartSpan(ctx, fqdn+"/DataMaskingRulesClient.ListByDatabase")
154		defer func() {
155			sc := -1
156			if result.Response.Response != nil {
157				sc = result.Response.Response.StatusCode
158			}
159			tracing.EndSpan(ctx, sc, err)
160		}()
161	}
162	req, err := client.ListByDatabasePreparer(ctx, resourceGroupName, serverName, databaseName)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "ListByDatabase", nil, "Failure preparing request")
165		return
166	}
167
168	resp, err := client.ListByDatabaseSender(req)
169	if err != nil {
170		result.Response = autorest.Response{Response: resp}
171		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "ListByDatabase", resp, "Failure sending request")
172		return
173	}
174
175	result, err = client.ListByDatabaseResponder(resp)
176	if err != nil {
177		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "ListByDatabase", resp, "Failure responding to request")
178	}
179
180	return
181}
182
183// ListByDatabasePreparer prepares the ListByDatabase request.
184func (client DataMaskingRulesClient) ListByDatabasePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (*http.Request, error) {
185	pathParameters := map[string]interface{}{
186		"databaseName":          autorest.Encode("path", databaseName),
187		"dataMaskingPolicyName": autorest.Encode("path", "Default"),
188		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
189		"serverName":            autorest.Encode("path", serverName),
190		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
191	}
192
193	const APIVersion = "2014-04-01"
194	queryParameters := map[string]interface{}{
195		"api-version": APIVersion,
196	}
197
198	preparer := autorest.CreatePreparer(
199		autorest.AsGet(),
200		autorest.WithBaseURL(client.BaseURI),
201		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/dataMaskingPolicies/{dataMaskingPolicyName}/rules", pathParameters),
202		autorest.WithQueryParameters(queryParameters))
203	return preparer.Prepare((&http.Request{}).WithContext(ctx))
204}
205
206// ListByDatabaseSender sends the ListByDatabase request. The method will close the
207// http.Response Body if it receives an error.
208func (client DataMaskingRulesClient) ListByDatabaseSender(req *http.Request) (*http.Response, error) {
209	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
210}
211
212// ListByDatabaseResponder handles the response to the ListByDatabase request. The method always
213// closes the http.Response Body.
214func (client DataMaskingRulesClient) ListByDatabaseResponder(resp *http.Response) (result DataMaskingRuleListResult, err error) {
215	err = autorest.Respond(
216		resp,
217		client.ByInspecting(),
218		azure.WithErrorUnlessStatusCode(http.StatusOK),
219		autorest.ByUnmarshallingJSON(&result),
220		autorest.ByClosing())
221	result.Response = autorest.Response{Response: resp}
222	return
223}
224