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