1// +build go1.13
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// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armcompute
9
10import (
11	"context"
12	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
13	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
14	"net/http"
15	"net/url"
16	"strings"
17	"time"
18)
19
20// GalleryApplicationsClient contains the methods for the GalleryApplications group.
21// Don't use this type directly, use NewGalleryApplicationsClient() instead.
22type GalleryApplicationsClient struct {
23	con            *armcore.Connection
24	subscriptionID string
25}
26
27// NewGalleryApplicationsClient creates a new instance of GalleryApplicationsClient with the specified values.
28func NewGalleryApplicationsClient(con *armcore.Connection, subscriptionID string) *GalleryApplicationsClient {
29	return &GalleryApplicationsClient{con: con, subscriptionID: subscriptionID}
30}
31
32// BeginCreateOrUpdate - Create or update a gallery Application Definition.
33func (client *GalleryApplicationsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, galleryApplication GalleryApplication, options *GalleryApplicationsBeginCreateOrUpdateOptions) (GalleryApplicationPollerResponse, error) {
34	resp, err := client.createOrUpdate(ctx, resourceGroupName, galleryName, galleryApplicationName, galleryApplication, options)
35	if err != nil {
36		return GalleryApplicationPollerResponse{}, err
37	}
38	result := GalleryApplicationPollerResponse{
39		RawResponse: resp.Response,
40	}
41	pt, err := armcore.NewPoller("GalleryApplicationsClient.CreateOrUpdate", "", resp, client.createOrUpdateHandleError)
42	if err != nil {
43		return GalleryApplicationPollerResponse{}, err
44	}
45	poller := &galleryApplicationPoller{
46		pt:       pt,
47		pipeline: client.con.Pipeline(),
48	}
49	result.Poller = poller
50	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (GalleryApplicationResponse, error) {
51		return poller.pollUntilDone(ctx, frequency)
52	}
53	return result, nil
54}
55
56// ResumeCreateOrUpdate creates a new GalleryApplicationPoller from the specified resume token.
57// token - The value must come from a previous call to GalleryApplicationPoller.ResumeToken().
58func (client *GalleryApplicationsClient) ResumeCreateOrUpdate(token string) (GalleryApplicationPoller, error) {
59	pt, err := armcore.NewPollerFromResumeToken("GalleryApplicationsClient.CreateOrUpdate", token, client.createOrUpdateHandleError)
60	if err != nil {
61		return nil, err
62	}
63	return &galleryApplicationPoller{
64		pipeline: client.con.Pipeline(),
65		pt:       pt,
66	}, nil
67}
68
69// CreateOrUpdate - Create or update a gallery Application Definition.
70func (client *GalleryApplicationsClient) createOrUpdate(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, galleryApplication GalleryApplication, options *GalleryApplicationsBeginCreateOrUpdateOptions) (*azcore.Response, error) {
71	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, galleryName, galleryApplicationName, galleryApplication, options)
72	if err != nil {
73		return nil, err
74	}
75	resp, err := client.con.Pipeline().Do(req)
76	if err != nil {
77		return nil, err
78	}
79	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted) {
80		return nil, client.createOrUpdateHandleError(resp)
81	}
82	return resp, nil
83}
84
85// createOrUpdateCreateRequest creates the CreateOrUpdate request.
86func (client *GalleryApplicationsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, galleryApplication GalleryApplication, options *GalleryApplicationsBeginCreateOrUpdateOptions) (*azcore.Request, error) {
87	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}"
88	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
89	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
90	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
91	urlPath = strings.ReplaceAll(urlPath, "{galleryApplicationName}", url.PathEscape(galleryApplicationName))
92	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
93	if err != nil {
94		return nil, err
95	}
96	req.Telemetry(telemetryInfo)
97	query := req.URL.Query()
98	query.Set("api-version", "2020-09-30")
99	req.URL.RawQuery = query.Encode()
100	req.Header.Set("Accept", "application/json")
101	return req, req.MarshalAsJSON(galleryApplication)
102}
103
104// createOrUpdateHandleResponse handles the CreateOrUpdate response.
105func (client *GalleryApplicationsClient) createOrUpdateHandleResponse(resp *azcore.Response) (GalleryApplicationResponse, error) {
106	var val *GalleryApplication
107	if err := resp.UnmarshalAsJSON(&val); err != nil {
108		return GalleryApplicationResponse{}, err
109	}
110	return GalleryApplicationResponse{RawResponse: resp.Response, GalleryApplication: val}, nil
111}
112
113// createOrUpdateHandleError handles the CreateOrUpdate error response.
114func (client *GalleryApplicationsClient) createOrUpdateHandleError(resp *azcore.Response) error {
115	var err CloudError
116	if err := resp.UnmarshalAsJSON(&err); err != nil {
117		return err
118	}
119	return azcore.NewResponseError(&err, resp.Response)
120}
121
122// BeginDelete - Delete a gallery Application.
123func (client *GalleryApplicationsClient) BeginDelete(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, options *GalleryApplicationsBeginDeleteOptions) (HTTPPollerResponse, error) {
124	resp, err := client.delete(ctx, resourceGroupName, galleryName, galleryApplicationName, options)
125	if err != nil {
126		return HTTPPollerResponse{}, err
127	}
128	result := HTTPPollerResponse{
129		RawResponse: resp.Response,
130	}
131	pt, err := armcore.NewPoller("GalleryApplicationsClient.Delete", "", resp, client.deleteHandleError)
132	if err != nil {
133		return HTTPPollerResponse{}, err
134	}
135	poller := &httpPoller{
136		pt:       pt,
137		pipeline: client.con.Pipeline(),
138	}
139	result.Poller = poller
140	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
141		return poller.pollUntilDone(ctx, frequency)
142	}
143	return result, nil
144}
145
146// ResumeDelete creates a new HTTPPoller from the specified resume token.
147// token - The value must come from a previous call to HTTPPoller.ResumeToken().
148func (client *GalleryApplicationsClient) ResumeDelete(token string) (HTTPPoller, error) {
149	pt, err := armcore.NewPollerFromResumeToken("GalleryApplicationsClient.Delete", token, client.deleteHandleError)
150	if err != nil {
151		return nil, err
152	}
153	return &httpPoller{
154		pipeline: client.con.Pipeline(),
155		pt:       pt,
156	}, nil
157}
158
159// Delete - Delete a gallery Application.
160func (client *GalleryApplicationsClient) delete(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, options *GalleryApplicationsBeginDeleteOptions) (*azcore.Response, error) {
161	req, err := client.deleteCreateRequest(ctx, resourceGroupName, galleryName, galleryApplicationName, options)
162	if err != nil {
163		return nil, err
164	}
165	resp, err := client.con.Pipeline().Do(req)
166	if err != nil {
167		return nil, err
168	}
169	if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
170		return nil, client.deleteHandleError(resp)
171	}
172	return resp, nil
173}
174
175// deleteCreateRequest creates the Delete request.
176func (client *GalleryApplicationsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, options *GalleryApplicationsBeginDeleteOptions) (*azcore.Request, error) {
177	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}"
178	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
179	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
180	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
181	urlPath = strings.ReplaceAll(urlPath, "{galleryApplicationName}", url.PathEscape(galleryApplicationName))
182	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
183	if err != nil {
184		return nil, err
185	}
186	req.Telemetry(telemetryInfo)
187	query := req.URL.Query()
188	query.Set("api-version", "2020-09-30")
189	req.URL.RawQuery = query.Encode()
190	req.Header.Set("Accept", "application/json")
191	return req, nil
192}
193
194// deleteHandleError handles the Delete error response.
195func (client *GalleryApplicationsClient) deleteHandleError(resp *azcore.Response) error {
196	var err CloudError
197	if err := resp.UnmarshalAsJSON(&err); err != nil {
198		return err
199	}
200	return azcore.NewResponseError(&err, resp.Response)
201}
202
203// Get - Retrieves information about a gallery Application Definition.
204func (client *GalleryApplicationsClient) Get(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, options *GalleryApplicationsGetOptions) (GalleryApplicationResponse, error) {
205	req, err := client.getCreateRequest(ctx, resourceGroupName, galleryName, galleryApplicationName, options)
206	if err != nil {
207		return GalleryApplicationResponse{}, err
208	}
209	resp, err := client.con.Pipeline().Do(req)
210	if err != nil {
211		return GalleryApplicationResponse{}, err
212	}
213	if !resp.HasStatusCode(http.StatusOK) {
214		return GalleryApplicationResponse{}, client.getHandleError(resp)
215	}
216	return client.getHandleResponse(resp)
217}
218
219// getCreateRequest creates the Get request.
220func (client *GalleryApplicationsClient) getCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, options *GalleryApplicationsGetOptions) (*azcore.Request, error) {
221	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}"
222	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
223	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
224	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
225	urlPath = strings.ReplaceAll(urlPath, "{galleryApplicationName}", url.PathEscape(galleryApplicationName))
226	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
227	if err != nil {
228		return nil, err
229	}
230	req.Telemetry(telemetryInfo)
231	query := req.URL.Query()
232	query.Set("api-version", "2020-09-30")
233	req.URL.RawQuery = query.Encode()
234	req.Header.Set("Accept", "application/json")
235	return req, nil
236}
237
238// getHandleResponse handles the Get response.
239func (client *GalleryApplicationsClient) getHandleResponse(resp *azcore.Response) (GalleryApplicationResponse, error) {
240	var val *GalleryApplication
241	if err := resp.UnmarshalAsJSON(&val); err != nil {
242		return GalleryApplicationResponse{}, err
243	}
244	return GalleryApplicationResponse{RawResponse: resp.Response, GalleryApplication: val}, nil
245}
246
247// getHandleError handles the Get error response.
248func (client *GalleryApplicationsClient) getHandleError(resp *azcore.Response) error {
249	var err CloudError
250	if err := resp.UnmarshalAsJSON(&err); err != nil {
251		return err
252	}
253	return azcore.NewResponseError(&err, resp.Response)
254}
255
256// ListByGallery - List gallery Application Definitions in a gallery.
257func (client *GalleryApplicationsClient) ListByGallery(resourceGroupName string, galleryName string, options *GalleryApplicationsListByGalleryOptions) GalleryApplicationListPager {
258	return &galleryApplicationListPager{
259		pipeline: client.con.Pipeline(),
260		requester: func(ctx context.Context) (*azcore.Request, error) {
261			return client.listByGalleryCreateRequest(ctx, resourceGroupName, galleryName, options)
262		},
263		responder: client.listByGalleryHandleResponse,
264		errorer:   client.listByGalleryHandleError,
265		advancer: func(ctx context.Context, resp GalleryApplicationListResponse) (*azcore.Request, error) {
266			return azcore.NewRequest(ctx, http.MethodGet, *resp.GalleryApplicationList.NextLink)
267		},
268		statusCodes: []int{http.StatusOK},
269	}
270}
271
272// listByGalleryCreateRequest creates the ListByGallery request.
273func (client *GalleryApplicationsClient) listByGalleryCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, options *GalleryApplicationsListByGalleryOptions) (*azcore.Request, error) {
274	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications"
275	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
276	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
277	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
278	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
279	if err != nil {
280		return nil, err
281	}
282	req.Telemetry(telemetryInfo)
283	query := req.URL.Query()
284	query.Set("api-version", "2020-09-30")
285	req.URL.RawQuery = query.Encode()
286	req.Header.Set("Accept", "application/json")
287	return req, nil
288}
289
290// listByGalleryHandleResponse handles the ListByGallery response.
291func (client *GalleryApplicationsClient) listByGalleryHandleResponse(resp *azcore.Response) (GalleryApplicationListResponse, error) {
292	var val *GalleryApplicationList
293	if err := resp.UnmarshalAsJSON(&val); err != nil {
294		return GalleryApplicationListResponse{}, err
295	}
296	return GalleryApplicationListResponse{RawResponse: resp.Response, GalleryApplicationList: val}, nil
297}
298
299// listByGalleryHandleError handles the ListByGallery error response.
300func (client *GalleryApplicationsClient) listByGalleryHandleError(resp *azcore.Response) error {
301	var err CloudError
302	if err := resp.UnmarshalAsJSON(&err); err != nil {
303		return err
304	}
305	return azcore.NewResponseError(&err, resp.Response)
306}
307
308// BeginUpdate - Update a gallery Application Definition.
309func (client *GalleryApplicationsClient) BeginUpdate(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, galleryApplication GalleryApplicationUpdate, options *GalleryApplicationsBeginUpdateOptions) (GalleryApplicationPollerResponse, error) {
310	resp, err := client.update(ctx, resourceGroupName, galleryName, galleryApplicationName, galleryApplication, options)
311	if err != nil {
312		return GalleryApplicationPollerResponse{}, err
313	}
314	result := GalleryApplicationPollerResponse{
315		RawResponse: resp.Response,
316	}
317	pt, err := armcore.NewPoller("GalleryApplicationsClient.Update", "", resp, client.updateHandleError)
318	if err != nil {
319		return GalleryApplicationPollerResponse{}, err
320	}
321	poller := &galleryApplicationPoller{
322		pt:       pt,
323		pipeline: client.con.Pipeline(),
324	}
325	result.Poller = poller
326	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (GalleryApplicationResponse, error) {
327		return poller.pollUntilDone(ctx, frequency)
328	}
329	return result, nil
330}
331
332// ResumeUpdate creates a new GalleryApplicationPoller from the specified resume token.
333// token - The value must come from a previous call to GalleryApplicationPoller.ResumeToken().
334func (client *GalleryApplicationsClient) ResumeUpdate(token string) (GalleryApplicationPoller, error) {
335	pt, err := armcore.NewPollerFromResumeToken("GalleryApplicationsClient.Update", token, client.updateHandleError)
336	if err != nil {
337		return nil, err
338	}
339	return &galleryApplicationPoller{
340		pipeline: client.con.Pipeline(),
341		pt:       pt,
342	}, nil
343}
344
345// Update - Update a gallery Application Definition.
346func (client *GalleryApplicationsClient) update(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, galleryApplication GalleryApplicationUpdate, options *GalleryApplicationsBeginUpdateOptions) (*azcore.Response, error) {
347	req, err := client.updateCreateRequest(ctx, resourceGroupName, galleryName, galleryApplicationName, galleryApplication, options)
348	if err != nil {
349		return nil, err
350	}
351	resp, err := client.con.Pipeline().Do(req)
352	if err != nil {
353		return nil, err
354	}
355	if !resp.HasStatusCode(http.StatusOK) {
356		return nil, client.updateHandleError(resp)
357	}
358	return resp, nil
359}
360
361// updateCreateRequest creates the Update request.
362func (client *GalleryApplicationsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, galleryApplicationName string, galleryApplication GalleryApplicationUpdate, options *GalleryApplicationsBeginUpdateOptions) (*azcore.Request, error) {
363	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}"
364	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
365	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
366	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
367	urlPath = strings.ReplaceAll(urlPath, "{galleryApplicationName}", url.PathEscape(galleryApplicationName))
368	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
369	if err != nil {
370		return nil, err
371	}
372	req.Telemetry(telemetryInfo)
373	query := req.URL.Query()
374	query.Set("api-version", "2020-09-30")
375	req.URL.RawQuery = query.Encode()
376	req.Header.Set("Accept", "application/json")
377	return req, req.MarshalAsJSON(galleryApplication)
378}
379
380// updateHandleResponse handles the Update response.
381func (client *GalleryApplicationsClient) updateHandleResponse(resp *azcore.Response) (GalleryApplicationResponse, error) {
382	var val *GalleryApplication
383	if err := resp.UnmarshalAsJSON(&val); err != nil {
384		return GalleryApplicationResponse{}, err
385	}
386	return GalleryApplicationResponse{RawResponse: resp.Response, GalleryApplication: val}, nil
387}
388
389// updateHandleError handles the Update error response.
390func (client *GalleryApplicationsClient) updateHandleError(resp *azcore.Response) error {
391	var err CloudError
392	if err := resp.UnmarshalAsJSON(&err); err != nil {
393		return err
394	}
395	return azcore.NewResponseError(&err, resp.Response)
396}
397