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	"net/http"
26)
27
28// DataMaskingRulesClient 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 DataMaskingRulesClient struct {
32	BaseClient
33}
34
35// NewDataMaskingRulesClient creates an instance of the DataMaskingRulesClient client.
36func NewDataMaskingRulesClient(subscriptionID string) DataMaskingRulesClient {
37	return NewDataMaskingRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
38}
39
40// NewDataMaskingRulesClientWithBaseURI creates an instance of the DataMaskingRulesClient client.
41func NewDataMaskingRulesClientWithBaseURI(baseURI string, subscriptionID string) DataMaskingRulesClient {
42	return DataMaskingRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// CreateOrUpdate creates or updates a database data masking rule.
46// Parameters:
47// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
48// from the Azure Resource Manager API or the portal.
49// serverName - the name of the server.
50// databaseName - the name of the database.
51// dataMaskingRuleName - the name of the data masking rule.
52// parameters - the required parameters for creating or updating a data masking rule.
53func (client DataMaskingRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, databaseName string, dataMaskingRuleName string, parameters DataMaskingRule) (result DataMaskingRule, err error) {
54	if err := validation.Validate([]validation.Validation{
55		{TargetValue: parameters,
56			Constraints: []validation.Constraint{{Target: "parameters.DataMaskingRuleProperties", Name: validation.Null, Rule: false,
57				Chain: []validation.Constraint{{Target: "parameters.DataMaskingRuleProperties.SchemaName", Name: validation.Null, Rule: true, Chain: nil},
58					{Target: "parameters.DataMaskingRuleProperties.TableName", Name: validation.Null, Rule: true, Chain: nil},
59					{Target: "parameters.DataMaskingRuleProperties.ColumnName", Name: validation.Null, Rule: true, Chain: nil},
60				}}}}}); err != nil {
61		return result, validation.NewError("sql.DataMaskingRulesClient", "CreateOrUpdate", err.Error())
62	}
63
64	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, databaseName, dataMaskingRuleName, parameters)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
67		return
68	}
69
70	resp, err := client.CreateOrUpdateSender(req)
71	if err != nil {
72		result.Response = autorest.Response{Response: resp}
73		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "CreateOrUpdate", resp, "Failure sending request")
74		return
75	}
76
77	result, err = client.CreateOrUpdateResponder(resp)
78	if err != nil {
79		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "CreateOrUpdate", resp, "Failure responding to request")
80	}
81
82	return
83}
84
85// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
86func (client DataMaskingRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, dataMaskingRuleName string, parameters DataMaskingRule) (*http.Request, error) {
87	pathParameters := map[string]interface{}{
88		"databaseName":          autorest.Encode("path", databaseName),
89		"dataMaskingPolicyName": autorest.Encode("path", "Default"),
90		"dataMaskingRuleName":   autorest.Encode("path", dataMaskingRuleName),
91		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
92		"serverName":            autorest.Encode("path", serverName),
93		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
94	}
95
96	const APIVersion = "2014-04-01"
97	queryParameters := map[string]interface{}{
98		"api-version": APIVersion,
99	}
100
101	preparer := autorest.CreatePreparer(
102		autorest.AsContentType("application/json; charset=utf-8"),
103		autorest.AsPut(),
104		autorest.WithBaseURL(client.BaseURI),
105		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/dataMaskingPolicies/{dataMaskingPolicyName}/rules/{dataMaskingRuleName}", pathParameters),
106		autorest.WithJSON(parameters),
107		autorest.WithQueryParameters(queryParameters))
108	return preparer.Prepare((&http.Request{}).WithContext(ctx))
109}
110
111// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
112// http.Response Body if it receives an error.
113func (client DataMaskingRulesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
114	return autorest.SendWithSender(client, req,
115		azure.DoRetryWithRegistration(client.Client))
116}
117
118// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
119// closes the http.Response Body.
120func (client DataMaskingRulesClient) CreateOrUpdateResponder(resp *http.Response) (result DataMaskingRule, err error) {
121	err = autorest.Respond(
122		resp,
123		client.ByInspecting(),
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// ListByDatabase gets a list of database data masking rules.
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.
137func (client DataMaskingRulesClient) ListByDatabase(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result DataMaskingRuleListResult, err error) {
138	req, err := client.ListByDatabasePreparer(ctx, resourceGroupName, serverName, databaseName)
139	if err != nil {
140		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "ListByDatabase", nil, "Failure preparing request")
141		return
142	}
143
144	resp, err := client.ListByDatabaseSender(req)
145	if err != nil {
146		result.Response = autorest.Response{Response: resp}
147		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "ListByDatabase", resp, "Failure sending request")
148		return
149	}
150
151	result, err = client.ListByDatabaseResponder(resp)
152	if err != nil {
153		err = autorest.NewErrorWithError(err, "sql.DataMaskingRulesClient", "ListByDatabase", resp, "Failure responding to request")
154	}
155
156	return
157}
158
159// ListByDatabasePreparer prepares the ListByDatabase request.
160func (client DataMaskingRulesClient) ListByDatabasePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (*http.Request, error) {
161	pathParameters := map[string]interface{}{
162		"databaseName":          autorest.Encode("path", databaseName),
163		"dataMaskingPolicyName": autorest.Encode("path", "Default"),
164		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
165		"serverName":            autorest.Encode("path", serverName),
166		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
167	}
168
169	const APIVersion = "2014-04-01"
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/servers/{serverName}/databases/{databaseName}/dataMaskingPolicies/{dataMaskingPolicyName}/rules", pathParameters),
178		autorest.WithQueryParameters(queryParameters))
179	return preparer.Prepare((&http.Request{}).WithContext(ctx))
180}
181
182// ListByDatabaseSender sends the ListByDatabase request. The method will close the
183// http.Response Body if it receives an error.
184func (client DataMaskingRulesClient) ListByDatabaseSender(req *http.Request) (*http.Response, error) {
185	return autorest.SendWithSender(client, req,
186		azure.DoRetryWithRegistration(client.Client))
187}
188
189// ListByDatabaseResponder handles the response to the ListByDatabase request. The method always
190// closes the http.Response Body.
191func (client DataMaskingRulesClient) ListByDatabaseResponder(resp *http.Response) (result DataMaskingRuleListResult, err error) {
192	err = autorest.Respond(
193		resp,
194		client.ByInspecting(),
195		azure.WithErrorUnlessStatusCode(http.StatusOK),
196		autorest.ByUnmarshallingJSON(&result),
197		autorest.ByClosing())
198	result.Response = autorest.Response{Response: resp}
199	return
200}
201