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