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