1package backup
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/tracing"
25	"net/http"
26)
27
28// ProtectionPoliciesClient is the open API 2.0 Specs for Azure RecoveryServices Backup service
29type ProtectionPoliciesClient struct {
30	BaseClient
31}
32
33// NewProtectionPoliciesClient creates an instance of the ProtectionPoliciesClient client.
34func NewProtectionPoliciesClient(subscriptionID string) ProtectionPoliciesClient {
35	return NewProtectionPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewProtectionPoliciesClientWithBaseURI creates an instance of the ProtectionPoliciesClient client using a custom
39// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
40// stack).
41func NewProtectionPoliciesClientWithBaseURI(baseURI string, subscriptionID string) ProtectionPoliciesClient {
42	return ProtectionPoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// CreateOrUpdate creates or modifies a backup policy. This is an asynchronous operation. Status of the operation can
46// be fetched
47// using GetPolicyOperationResult API.
48// Parameters:
49// vaultName - the name of the recovery services vault.
50// resourceGroupName - the name of the resource group where the recovery services vault is present.
51// policyName - backup policy to be created.
52// parameters - resource backup policy
53func (client ProtectionPoliciesClient) CreateOrUpdate(ctx context.Context, vaultName string, resourceGroupName string, policyName string, parameters ProtectionPolicyResource) (result ProtectionPolicyResource, err error) {
54	if tracing.IsEnabled() {
55		ctx = tracing.StartSpan(ctx, fqdn+"/ProtectionPoliciesClient.CreateOrUpdate")
56		defer func() {
57			sc := -1
58			if result.Response.Response != nil {
59				sc = result.Response.Response.StatusCode
60			}
61			tracing.EndSpan(ctx, sc, err)
62		}()
63	}
64	req, err := client.CreateOrUpdatePreparer(ctx, vaultName, resourceGroupName, policyName, parameters)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "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, "backup.ProtectionPoliciesClient", "CreateOrUpdate", resp, "Failure sending request")
74		return
75	}
76
77	result, err = client.CreateOrUpdateResponder(resp)
78	if err != nil {
79		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "CreateOrUpdate", resp, "Failure responding to request")
80		return
81	}
82
83	return
84}
85
86// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
87func (client ProtectionPoliciesClient) CreateOrUpdatePreparer(ctx context.Context, vaultName string, resourceGroupName string, policyName string, parameters ProtectionPolicyResource) (*http.Request, error) {
88	pathParameters := map[string]interface{}{
89		"policyName":        autorest.Encode("path", policyName),
90		"resourceGroupName": autorest.Encode("path", resourceGroupName),
91		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
92		"vaultName":         autorest.Encode("path", vaultName),
93	}
94
95	const APIVersion = "2019-06-15"
96	queryParameters := map[string]interface{}{
97		"api-version": APIVersion,
98	}
99
100	preparer := autorest.CreatePreparer(
101		autorest.AsContentType("application/json; charset=utf-8"),
102		autorest.AsPut(),
103		autorest.WithBaseURL(client.BaseURI),
104		autorest.WithPathParameters("/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupPolicies/{policyName}", pathParameters),
105		autorest.WithJSON(parameters),
106		autorest.WithQueryParameters(queryParameters))
107	return preparer.Prepare((&http.Request{}).WithContext(ctx))
108}
109
110// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
111// http.Response Body if it receives an error.
112func (client ProtectionPoliciesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
113	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
114}
115
116// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
117// closes the http.Response Body.
118func (client ProtectionPoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result ProtectionPolicyResource, err error) {
119	err = autorest.Respond(
120		resp,
121		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
122		autorest.ByUnmarshallingJSON(&result),
123		autorest.ByClosing())
124	result.Response = autorest.Response{Response: resp}
125	return
126}
127
128// Delete deletes specified backup policy from your Recovery Services Vault. This is an asynchronous operation. Status
129// of the
130// operation can be fetched using GetPolicyOperationResult API.
131// Parameters:
132// vaultName - the name of the recovery services vault.
133// resourceGroupName - the name of the resource group where the recovery services vault is present.
134// policyName - backup policy to be deleted.
135func (client ProtectionPoliciesClient) Delete(ctx context.Context, vaultName string, resourceGroupName string, policyName string) (result autorest.Response, err error) {
136	if tracing.IsEnabled() {
137		ctx = tracing.StartSpan(ctx, fqdn+"/ProtectionPoliciesClient.Delete")
138		defer func() {
139			sc := -1
140			if result.Response != nil {
141				sc = result.Response.StatusCode
142			}
143			tracing.EndSpan(ctx, sc, err)
144		}()
145	}
146	req, err := client.DeletePreparer(ctx, vaultName, resourceGroupName, policyName)
147	if err != nil {
148		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "Delete", nil, "Failure preparing request")
149		return
150	}
151
152	resp, err := client.DeleteSender(req)
153	if err != nil {
154		result.Response = resp
155		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "Delete", resp, "Failure sending request")
156		return
157	}
158
159	result, err = client.DeleteResponder(resp)
160	if err != nil {
161		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "Delete", resp, "Failure responding to request")
162		return
163	}
164
165	return
166}
167
168// DeletePreparer prepares the Delete request.
169func (client ProtectionPoliciesClient) DeletePreparer(ctx context.Context, vaultName string, resourceGroupName string, policyName string) (*http.Request, error) {
170	pathParameters := map[string]interface{}{
171		"policyName":        autorest.Encode("path", policyName),
172		"resourceGroupName": autorest.Encode("path", resourceGroupName),
173		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
174		"vaultName":         autorest.Encode("path", vaultName),
175	}
176
177	const APIVersion = "2016-12-01"
178	queryParameters := map[string]interface{}{
179		"api-version": APIVersion,
180	}
181
182	preparer := autorest.CreatePreparer(
183		autorest.AsDelete(),
184		autorest.WithBaseURL(client.BaseURI),
185		autorest.WithPathParameters("/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupPolicies/{policyName}", pathParameters),
186		autorest.WithQueryParameters(queryParameters))
187	return preparer.Prepare((&http.Request{}).WithContext(ctx))
188}
189
190// DeleteSender sends the Delete request. The method will close the
191// http.Response Body if it receives an error.
192func (client ProtectionPoliciesClient) DeleteSender(req *http.Request) (*http.Response, error) {
193	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
194}
195
196// DeleteResponder handles the response to the Delete request. The method always
197// closes the http.Response Body.
198func (client ProtectionPoliciesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
199	err = autorest.Respond(
200		resp,
201		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
202		autorest.ByClosing())
203	result.Response = resp
204	return
205}
206
207// Get provides the details of the backup policies associated to Recovery Services Vault. This is an asynchronous
208// operation. Status of the operation can be fetched using GetPolicyOperationResult API.
209// Parameters:
210// vaultName - the name of the recovery services vault.
211// resourceGroupName - the name of the resource group where the recovery services vault is present.
212// policyName - backup policy information to be fetched.
213func (client ProtectionPoliciesClient) Get(ctx context.Context, vaultName string, resourceGroupName string, policyName string) (result ProtectionPolicyResource, err error) {
214	if tracing.IsEnabled() {
215		ctx = tracing.StartSpan(ctx, fqdn+"/ProtectionPoliciesClient.Get")
216		defer func() {
217			sc := -1
218			if result.Response.Response != nil {
219				sc = result.Response.Response.StatusCode
220			}
221			tracing.EndSpan(ctx, sc, err)
222		}()
223	}
224	req, err := client.GetPreparer(ctx, vaultName, resourceGroupName, policyName)
225	if err != nil {
226		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "Get", nil, "Failure preparing request")
227		return
228	}
229
230	resp, err := client.GetSender(req)
231	if err != nil {
232		result.Response = autorest.Response{Response: resp}
233		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "Get", resp, "Failure sending request")
234		return
235	}
236
237	result, err = client.GetResponder(resp)
238	if err != nil {
239		err = autorest.NewErrorWithError(err, "backup.ProtectionPoliciesClient", "Get", resp, "Failure responding to request")
240		return
241	}
242
243	return
244}
245
246// GetPreparer prepares the Get request.
247func (client ProtectionPoliciesClient) GetPreparer(ctx context.Context, vaultName string, resourceGroupName string, policyName string) (*http.Request, error) {
248	pathParameters := map[string]interface{}{
249		"policyName":        autorest.Encode("path", policyName),
250		"resourceGroupName": autorest.Encode("path", resourceGroupName),
251		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
252		"vaultName":         autorest.Encode("path", vaultName),
253	}
254
255	const APIVersion = "2019-06-15"
256	queryParameters := map[string]interface{}{
257		"api-version": APIVersion,
258	}
259
260	preparer := autorest.CreatePreparer(
261		autorest.AsGet(),
262		autorest.WithBaseURL(client.BaseURI),
263		autorest.WithPathParameters("/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupPolicies/{policyName}", pathParameters),
264		autorest.WithQueryParameters(queryParameters))
265	return preparer.Prepare((&http.Request{}).WithContext(ctx))
266}
267
268// GetSender sends the Get request. The method will close the
269// http.Response Body if it receives an error.
270func (client ProtectionPoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
271	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
272}
273
274// GetResponder handles the response to the Get request. The method always
275// closes the http.Response Body.
276func (client ProtectionPoliciesClient) GetResponder(resp *http.Response) (result ProtectionPolicyResource, err error) {
277	err = autorest.Respond(
278		resp,
279		azure.WithErrorUnlessStatusCode(http.StatusOK),
280		autorest.ByUnmarshallingJSON(&result),
281		autorest.ByClosing())
282	result.Response = autorest.Response{Response: resp}
283	return
284}
285