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	"net/http"
25)
26
27// PoliciesClient is the open API 2.0 Specs for Azure RecoveryServices Backup service
28type PoliciesClient struct {
29	BaseClient
30}
31
32// NewPoliciesClient creates an instance of the PoliciesClient client.
33func NewPoliciesClient(subscriptionID string) PoliciesClient {
34	return NewPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
35}
36
37// NewPoliciesClientWithBaseURI creates an instance of the PoliciesClient client.
38func NewPoliciesClientWithBaseURI(baseURI string, subscriptionID string) PoliciesClient {
39	return PoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
40}
41
42// List lists of backup policies associated with Recovery Services Vault. API provides pagination parameters to fetch
43// scoped results.
44// Parameters:
45// vaultName - the name of the recovery services vault.
46// resourceGroupName - the name of the resource group where the recovery services vault is present.
47// filter - oData filter options.
48func (client PoliciesClient) List(ctx context.Context, vaultName string, resourceGroupName string, filter string) (result ProtectionPolicyResourceListPage, err error) {
49	result.fn = client.listNextResults
50	req, err := client.ListPreparer(ctx, vaultName, resourceGroupName, filter)
51	if err != nil {
52		err = autorest.NewErrorWithError(err, "backup.PoliciesClient", "List", nil, "Failure preparing request")
53		return
54	}
55
56	resp, err := client.ListSender(req)
57	if err != nil {
58		result.pprl.Response = autorest.Response{Response: resp}
59		err = autorest.NewErrorWithError(err, "backup.PoliciesClient", "List", resp, "Failure sending request")
60		return
61	}
62
63	result.pprl, err = client.ListResponder(resp)
64	if err != nil {
65		err = autorest.NewErrorWithError(err, "backup.PoliciesClient", "List", resp, "Failure responding to request")
66	}
67
68	return
69}
70
71// ListPreparer prepares the List request.
72func (client PoliciesClient) ListPreparer(ctx context.Context, vaultName string, resourceGroupName string, filter string) (*http.Request, error) {
73	pathParameters := map[string]interface{}{
74		"resourceGroupName": autorest.Encode("path", resourceGroupName),
75		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
76		"vaultName":         autorest.Encode("path", vaultName),
77	}
78
79	const APIVersion = "2017-07-01"
80	queryParameters := map[string]interface{}{
81		"api-version": APIVersion,
82	}
83	if len(filter) > 0 {
84		queryParameters["$filter"] = autorest.Encode("query", filter)
85	}
86
87	preparer := autorest.CreatePreparer(
88		autorest.AsGet(),
89		autorest.WithBaseURL(client.BaseURI),
90		autorest.WithPathParameters("/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupPolicies", pathParameters),
91		autorest.WithQueryParameters(queryParameters))
92	return preparer.Prepare((&http.Request{}).WithContext(ctx))
93}
94
95// ListSender sends the List request. The method will close the
96// http.Response Body if it receives an error.
97func (client PoliciesClient) ListSender(req *http.Request) (*http.Response, error) {
98	return autorest.SendWithSender(client, req,
99		azure.DoRetryWithRegistration(client.Client))
100}
101
102// ListResponder handles the response to the List request. The method always
103// closes the http.Response Body.
104func (client PoliciesClient) ListResponder(resp *http.Response) (result ProtectionPolicyResourceList, err error) {
105	err = autorest.Respond(
106		resp,
107		client.ByInspecting(),
108		azure.WithErrorUnlessStatusCode(http.StatusOK),
109		autorest.ByUnmarshallingJSON(&result),
110		autorest.ByClosing())
111	result.Response = autorest.Response{Response: resp}
112	return
113}
114
115// listNextResults retrieves the next set of results, if any.
116func (client PoliciesClient) listNextResults(lastResults ProtectionPolicyResourceList) (result ProtectionPolicyResourceList, err error) {
117	req, err := lastResults.protectionPolicyResourceListPreparer()
118	if err != nil {
119		return result, autorest.NewErrorWithError(err, "backup.PoliciesClient", "listNextResults", nil, "Failure preparing next results request")
120	}
121	if req == nil {
122		return
123	}
124	resp, err := client.ListSender(req)
125	if err != nil {
126		result.Response = autorest.Response{Response: resp}
127		return result, autorest.NewErrorWithError(err, "backup.PoliciesClient", "listNextResults", resp, "Failure sending next results request")
128	}
129	result, err = client.ListResponder(resp)
130	if err != nil {
131		err = autorest.NewErrorWithError(err, "backup.PoliciesClient", "listNextResults", resp, "Failure responding to next results request")
132	}
133	return
134}
135
136// ListComplete enumerates all values, automatically crossing page boundaries as required.
137func (client PoliciesClient) ListComplete(ctx context.Context, vaultName string, resourceGroupName string, filter string) (result ProtectionPolicyResourceListIterator, err error) {
138	result.page, err = client.List(ctx, vaultName, resourceGroupName, filter)
139	return
140}
141