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