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