1package batch
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/autorest/date"
25	"github.com/Azure/go-autorest/autorest/validation"
26	"github.com/Azure/go-autorest/tracing"
27	"github.com/gofrs/uuid"
28	"net/http"
29)
30
31// ApplicationClient is the a client for issuing REST requests to the Azure Batch service.
32type ApplicationClient struct {
33	BaseClient
34}
35
36// NewApplicationClient creates an instance of the ApplicationClient client.
37func NewApplicationClient() ApplicationClient {
38	return NewApplicationClientWithBaseURI(DefaultBaseURI)
39}
40
41// NewApplicationClientWithBaseURI creates an instance of the ApplicationClient client using a custom endpoint.  Use
42// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
43func NewApplicationClientWithBaseURI(baseURI string) ApplicationClient {
44	return ApplicationClient{NewWithBaseURI(baseURI)}
45}
46
47// Get sends the get request.
48// Parameters:
49// applicationID - the ID of the application.
50// timeout - the maximum time that the server can spend processing the request, in seconds. The default is 30
51// seconds.
52// clientRequestID - the caller-generated request identity, in the form of a GUID with no decoration such as
53// curly braces, e.g. 9C4D50EE-2D56-4CD3-8152-34347DC9F2B0.
54// returnClientRequestID - whether the server should return the client-request-id in the response.
55// ocpDate - the time the request was issued. Client libraries typically set this to the current system clock
56// time; set it explicitly if you are calling the REST API directly.
57func (client ApplicationClient) Get(ctx context.Context, applicationID string, timeout *int32, clientRequestID *uuid.UUID, returnClientRequestID *bool, ocpDate *date.TimeRFC1123) (result ApplicationSummary, err error) {
58	if tracing.IsEnabled() {
59		ctx = tracing.StartSpan(ctx, fqdn+"/ApplicationClient.Get")
60		defer func() {
61			sc := -1
62			if result.Response.Response != nil {
63				sc = result.Response.Response.StatusCode
64			}
65			tracing.EndSpan(ctx, sc, err)
66		}()
67	}
68	req, err := client.GetPreparer(ctx, applicationID, timeout, clientRequestID, returnClientRequestID, ocpDate)
69	if err != nil {
70		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "Get", nil, "Failure preparing request")
71		return
72	}
73
74	resp, err := client.GetSender(req)
75	if err != nil {
76		result.Response = autorest.Response{Response: resp}
77		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "Get", resp, "Failure sending request")
78		return
79	}
80
81	result, err = client.GetResponder(resp)
82	if err != nil {
83		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "Get", resp, "Failure responding to request")
84		return
85	}
86
87	return
88}
89
90// GetPreparer prepares the Get request.
91func (client ApplicationClient) GetPreparer(ctx context.Context, applicationID string, timeout *int32, clientRequestID *uuid.UUID, returnClientRequestID *bool, ocpDate *date.TimeRFC1123) (*http.Request, error) {
92	pathParameters := map[string]interface{}{
93		"applicationId": autorest.Encode("path", applicationID),
94	}
95
96	const APIVersion = "2017-05-01.5.0"
97	queryParameters := map[string]interface{}{
98		"api-version": APIVersion,
99	}
100	if timeout != nil {
101		queryParameters["timeout"] = autorest.Encode("query", *timeout)
102	} else {
103		queryParameters["timeout"] = autorest.Encode("query", 30)
104	}
105
106	preparer := autorest.CreatePreparer(
107		autorest.AsGet(),
108		autorest.WithBaseURL(client.BaseURI),
109		autorest.WithPathParameters("/applications/{applicationId}", pathParameters),
110		autorest.WithQueryParameters(queryParameters))
111	if clientRequestID != nil {
112		preparer = autorest.DecoratePreparer(preparer,
113			autorest.WithHeader("client-request-id", autorest.String(clientRequestID)))
114	}
115	if returnClientRequestID != nil {
116		preparer = autorest.DecoratePreparer(preparer,
117			autorest.WithHeader("return-client-request-id", autorest.String(returnClientRequestID)))
118	} else {
119		preparer = autorest.DecoratePreparer(preparer,
120			autorest.WithHeader("return-client-request-id", autorest.String(false)))
121	}
122	if ocpDate != nil {
123		preparer = autorest.DecoratePreparer(preparer,
124			autorest.WithHeader("ocp-date", autorest.String(ocpDate)))
125	}
126	return preparer.Prepare((&http.Request{}).WithContext(ctx))
127}
128
129// GetSender sends the Get request. The method will close the
130// http.Response Body if it receives an error.
131func (client ApplicationClient) GetSender(req *http.Request) (*http.Response, error) {
132	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
133}
134
135// GetResponder handles the response to the Get request. The method always
136// closes the http.Response Body.
137func (client ApplicationClient) GetResponder(resp *http.Response) (result ApplicationSummary, err error) {
138	err = autorest.Respond(
139		resp,
140		azure.WithErrorUnlessStatusCode(http.StatusOK),
141		autorest.ByUnmarshallingJSON(&result),
142		autorest.ByClosing())
143	result.Response = autorest.Response{Response: resp}
144	return
145}
146
147// List this operation returns only applications and versions that are available for use on compute nodes; that is,
148// that can be used in an application package reference. For administrator information about applications and versions
149// that are not yet available to compute nodes, use the Azure portal or the Azure Resource Manager API.
150// Parameters:
151// maxResults - the maximum number of items to return in the response. A maximum of 1000 applications can be
152// returned.
153// timeout - the maximum time that the server can spend processing the request, in seconds. The default is 30
154// seconds.
155// clientRequestID - the caller-generated request identity, in the form of a GUID with no decoration such as
156// curly braces, e.g. 9C4D50EE-2D56-4CD3-8152-34347DC9F2B0.
157// returnClientRequestID - whether the server should return the client-request-id in the response.
158// ocpDate - the time the request was issued. Client libraries typically set this to the current system clock
159// time; set it explicitly if you are calling the REST API directly.
160func (client ApplicationClient) List(ctx context.Context, maxResults *int32, timeout *int32, clientRequestID *uuid.UUID, returnClientRequestID *bool, ocpDate *date.TimeRFC1123) (result ApplicationListResultPage, err error) {
161	if tracing.IsEnabled() {
162		ctx = tracing.StartSpan(ctx, fqdn+"/ApplicationClient.List")
163		defer func() {
164			sc := -1
165			if result.alr.Response.Response != nil {
166				sc = result.alr.Response.Response.StatusCode
167			}
168			tracing.EndSpan(ctx, sc, err)
169		}()
170	}
171	if err := validation.Validate([]validation.Validation{
172		{TargetValue: maxResults,
173			Constraints: []validation.Constraint{{Target: "maxResults", Name: validation.Null, Rule: false,
174				Chain: []validation.Constraint{{Target: "maxResults", Name: validation.InclusiveMaximum, Rule: int64(1000), Chain: nil},
175					{Target: "maxResults", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil},
176				}}}}}); err != nil {
177		return result, validation.NewError("batch.ApplicationClient", "List", err.Error())
178	}
179
180	result.fn = client.listNextResults
181	req, err := client.ListPreparer(ctx, maxResults, timeout, clientRequestID, returnClientRequestID, ocpDate)
182	if err != nil {
183		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "List", nil, "Failure preparing request")
184		return
185	}
186
187	resp, err := client.ListSender(req)
188	if err != nil {
189		result.alr.Response = autorest.Response{Response: resp}
190		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "List", resp, "Failure sending request")
191		return
192	}
193
194	result.alr, err = client.ListResponder(resp)
195	if err != nil {
196		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "List", resp, "Failure responding to request")
197		return
198	}
199	if result.alr.hasNextLink() && result.alr.IsEmpty() {
200		err = result.NextWithContext(ctx)
201		return
202	}
203
204	return
205}
206
207// ListPreparer prepares the List request.
208func (client ApplicationClient) ListPreparer(ctx context.Context, maxResults *int32, timeout *int32, clientRequestID *uuid.UUID, returnClientRequestID *bool, ocpDate *date.TimeRFC1123) (*http.Request, error) {
209	const APIVersion = "2017-05-01.5.0"
210	queryParameters := map[string]interface{}{
211		"api-version": APIVersion,
212	}
213	if maxResults != nil {
214		queryParameters["maxresults"] = autorest.Encode("query", *maxResults)
215	} else {
216		queryParameters["maxresults"] = autorest.Encode("query", 1000)
217	}
218	if timeout != nil {
219		queryParameters["timeout"] = autorest.Encode("query", *timeout)
220	} else {
221		queryParameters["timeout"] = autorest.Encode("query", 30)
222	}
223
224	preparer := autorest.CreatePreparer(
225		autorest.AsGet(),
226		autorest.WithBaseURL(client.BaseURI),
227		autorest.WithPath("/applications"),
228		autorest.WithQueryParameters(queryParameters))
229	if clientRequestID != nil {
230		preparer = autorest.DecoratePreparer(preparer,
231			autorest.WithHeader("client-request-id", autorest.String(clientRequestID)))
232	}
233	if returnClientRequestID != nil {
234		preparer = autorest.DecoratePreparer(preparer,
235			autorest.WithHeader("return-client-request-id", autorest.String(returnClientRequestID)))
236	} else {
237		preparer = autorest.DecoratePreparer(preparer,
238			autorest.WithHeader("return-client-request-id", autorest.String(false)))
239	}
240	if ocpDate != nil {
241		preparer = autorest.DecoratePreparer(preparer,
242			autorest.WithHeader("ocp-date", autorest.String(ocpDate)))
243	}
244	return preparer.Prepare((&http.Request{}).WithContext(ctx))
245}
246
247// ListSender sends the List request. The method will close the
248// http.Response Body if it receives an error.
249func (client ApplicationClient) ListSender(req *http.Request) (*http.Response, error) {
250	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
251}
252
253// ListResponder handles the response to the List request. The method always
254// closes the http.Response Body.
255func (client ApplicationClient) ListResponder(resp *http.Response) (result ApplicationListResult, err error) {
256	err = autorest.Respond(
257		resp,
258		azure.WithErrorUnlessStatusCode(http.StatusOK),
259		autorest.ByUnmarshallingJSON(&result),
260		autorest.ByClosing())
261	result.Response = autorest.Response{Response: resp}
262	return
263}
264
265// listNextResults retrieves the next set of results, if any.
266func (client ApplicationClient) listNextResults(ctx context.Context, lastResults ApplicationListResult) (result ApplicationListResult, err error) {
267	req, err := lastResults.applicationListResultPreparer(ctx)
268	if err != nil {
269		return result, autorest.NewErrorWithError(err, "batch.ApplicationClient", "listNextResults", nil, "Failure preparing next results request")
270	}
271	if req == nil {
272		return
273	}
274	resp, err := client.ListSender(req)
275	if err != nil {
276		result.Response = autorest.Response{Response: resp}
277		return result, autorest.NewErrorWithError(err, "batch.ApplicationClient", "listNextResults", resp, "Failure sending next results request")
278	}
279	result, err = client.ListResponder(resp)
280	if err != nil {
281		err = autorest.NewErrorWithError(err, "batch.ApplicationClient", "listNextResults", resp, "Failure responding to next results request")
282	}
283	return
284}
285
286// ListComplete enumerates all values, automatically crossing page boundaries as required.
287func (client ApplicationClient) ListComplete(ctx context.Context, maxResults *int32, timeout *int32, clientRequestID *uuid.UUID, returnClientRequestID *bool, ocpDate *date.TimeRFC1123) (result ApplicationListResultIterator, err error) {
288	if tracing.IsEnabled() {
289		ctx = tracing.StartSpan(ctx, fqdn+"/ApplicationClient.List")
290		defer func() {
291			sc := -1
292			if result.Response().Response.Response != nil {
293				sc = result.page.Response().Response.Response.StatusCode
294			}
295			tracing.EndSpan(ctx, sc, err)
296		}()
297	}
298	result.page, err = client.List(ctx, maxResults, timeout, clientRequestID, returnClientRequestID, ocpDate)
299	return
300}
301