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// GalleriesClient contains the methods for the Galleries group.
21// Don't use this type directly, use NewGalleriesClient() instead.
22type GalleriesClient struct {
23	con            *armcore.Connection
24	subscriptionID string
25}
26
27// NewGalleriesClient creates a new instance of GalleriesClient with the specified values.
28func NewGalleriesClient(con *armcore.Connection, subscriptionID string) *GalleriesClient {
29	return &GalleriesClient{con: con, subscriptionID: subscriptionID}
30}
31
32// BeginCreateOrUpdate - Create or update a Shared Image Gallery.
33func (client *GalleriesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, galleryName string, gallery Gallery, options *GalleriesBeginCreateOrUpdateOptions) (GalleryPollerResponse, error) {
34	resp, err := client.createOrUpdate(ctx, resourceGroupName, galleryName, gallery, options)
35	if err != nil {
36		return GalleryPollerResponse{}, err
37	}
38	result := GalleryPollerResponse{
39		RawResponse: resp.Response,
40	}
41	pt, err := armcore.NewPoller("GalleriesClient.CreateOrUpdate", "", resp, client.createOrUpdateHandleError)
42	if err != nil {
43		return GalleryPollerResponse{}, err
44	}
45	poller := &galleryPoller{
46		pt:       pt,
47		pipeline: client.con.Pipeline(),
48	}
49	result.Poller = poller
50	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (GalleryResponse, error) {
51		return poller.pollUntilDone(ctx, frequency)
52	}
53	return result, nil
54}
55
56// ResumeCreateOrUpdate creates a new GalleryPoller from the specified resume token.
57// token - The value must come from a previous call to GalleryPoller.ResumeToken().
58func (client *GalleriesClient) ResumeCreateOrUpdate(token string) (GalleryPoller, error) {
59	pt, err := armcore.NewPollerFromResumeToken("GalleriesClient.CreateOrUpdate", token, client.createOrUpdateHandleError)
60	if err != nil {
61		return nil, err
62	}
63	return &galleryPoller{
64		pipeline: client.con.Pipeline(),
65		pt:       pt,
66	}, nil
67}
68
69// CreateOrUpdate - Create or update a Shared Image Gallery.
70func (client *GalleriesClient) createOrUpdate(ctx context.Context, resourceGroupName string, galleryName string, gallery Gallery, options *GalleriesBeginCreateOrUpdateOptions) (*azcore.Response, error) {
71	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, galleryName, gallery, 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 *GalleriesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, gallery Gallery, options *GalleriesBeginCreateOrUpdateOptions) (*azcore.Request, error) {
87	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}"
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	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
92	if err != nil {
93		return nil, err
94	}
95	req.Telemetry(telemetryInfo)
96	query := req.URL.Query()
97	query.Set("api-version", "2020-09-30")
98	req.URL.RawQuery = query.Encode()
99	req.Header.Set("Accept", "application/json")
100	return req, req.MarshalAsJSON(gallery)
101}
102
103// createOrUpdateHandleResponse handles the CreateOrUpdate response.
104func (client *GalleriesClient) createOrUpdateHandleResponse(resp *azcore.Response) (GalleryResponse, error) {
105	var val *Gallery
106	if err := resp.UnmarshalAsJSON(&val); err != nil {
107		return GalleryResponse{}, err
108	}
109	return GalleryResponse{RawResponse: resp.Response, Gallery: val}, nil
110}
111
112// createOrUpdateHandleError handles the CreateOrUpdate error response.
113func (client *GalleriesClient) createOrUpdateHandleError(resp *azcore.Response) error {
114	var err CloudError
115	if err := resp.UnmarshalAsJSON(&err); err != nil {
116		return err
117	}
118	return azcore.NewResponseError(&err, resp.Response)
119}
120
121// BeginDelete - Delete a Shared Image Gallery.
122func (client *GalleriesClient) BeginDelete(ctx context.Context, resourceGroupName string, galleryName string, options *GalleriesBeginDeleteOptions) (HTTPPollerResponse, error) {
123	resp, err := client.delete(ctx, resourceGroupName, galleryName, options)
124	if err != nil {
125		return HTTPPollerResponse{}, err
126	}
127	result := HTTPPollerResponse{
128		RawResponse: resp.Response,
129	}
130	pt, err := armcore.NewPoller("GalleriesClient.Delete", "", resp, client.deleteHandleError)
131	if err != nil {
132		return HTTPPollerResponse{}, err
133	}
134	poller := &httpPoller{
135		pt:       pt,
136		pipeline: client.con.Pipeline(),
137	}
138	result.Poller = poller
139	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
140		return poller.pollUntilDone(ctx, frequency)
141	}
142	return result, nil
143}
144
145// ResumeDelete creates a new HTTPPoller from the specified resume token.
146// token - The value must come from a previous call to HTTPPoller.ResumeToken().
147func (client *GalleriesClient) ResumeDelete(token string) (HTTPPoller, error) {
148	pt, err := armcore.NewPollerFromResumeToken("GalleriesClient.Delete", token, client.deleteHandleError)
149	if err != nil {
150		return nil, err
151	}
152	return &httpPoller{
153		pipeline: client.con.Pipeline(),
154		pt:       pt,
155	}, nil
156}
157
158// Delete - Delete a Shared Image Gallery.
159func (client *GalleriesClient) delete(ctx context.Context, resourceGroupName string, galleryName string, options *GalleriesBeginDeleteOptions) (*azcore.Response, error) {
160	req, err := client.deleteCreateRequest(ctx, resourceGroupName, galleryName, options)
161	if err != nil {
162		return nil, err
163	}
164	resp, err := client.con.Pipeline().Do(req)
165	if err != nil {
166		return nil, err
167	}
168	if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
169		return nil, client.deleteHandleError(resp)
170	}
171	return resp, nil
172}
173
174// deleteCreateRequest creates the Delete request.
175func (client *GalleriesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, options *GalleriesBeginDeleteOptions) (*azcore.Request, error) {
176	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}"
177	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
178	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
179	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
180	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
181	if err != nil {
182		return nil, err
183	}
184	req.Telemetry(telemetryInfo)
185	query := req.URL.Query()
186	query.Set("api-version", "2020-09-30")
187	req.URL.RawQuery = query.Encode()
188	req.Header.Set("Accept", "application/json")
189	return req, nil
190}
191
192// deleteHandleError handles the Delete error response.
193func (client *GalleriesClient) deleteHandleError(resp *azcore.Response) error {
194	var err CloudError
195	if err := resp.UnmarshalAsJSON(&err); err != nil {
196		return err
197	}
198	return azcore.NewResponseError(&err, resp.Response)
199}
200
201// Get - Retrieves information about a Shared Image Gallery.
202func (client *GalleriesClient) Get(ctx context.Context, resourceGroupName string, galleryName string, options *GalleriesGetOptions) (GalleryResponse, error) {
203	req, err := client.getCreateRequest(ctx, resourceGroupName, galleryName, options)
204	if err != nil {
205		return GalleryResponse{}, err
206	}
207	resp, err := client.con.Pipeline().Do(req)
208	if err != nil {
209		return GalleryResponse{}, err
210	}
211	if !resp.HasStatusCode(http.StatusOK) {
212		return GalleryResponse{}, client.getHandleError(resp)
213	}
214	return client.getHandleResponse(resp)
215}
216
217// getCreateRequest creates the Get request.
218func (client *GalleriesClient) getCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, options *GalleriesGetOptions) (*azcore.Request, error) {
219	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}"
220	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
221	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
222	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
223	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
224	if err != nil {
225		return nil, err
226	}
227	req.Telemetry(telemetryInfo)
228	query := req.URL.Query()
229	query.Set("api-version", "2020-09-30")
230	if options != nil && options.SelectParameter != nil {
231		query.Set("$select", string(*options.SelectParameter))
232	}
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 *GalleriesClient) getHandleResponse(resp *azcore.Response) (GalleryResponse, error) {
240	var val *Gallery
241	if err := resp.UnmarshalAsJSON(&val); err != nil {
242		return GalleryResponse{}, err
243	}
244	return GalleryResponse{RawResponse: resp.Response, Gallery: val}, nil
245}
246
247// getHandleError handles the Get error response.
248func (client *GalleriesClient) 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// List - List galleries under a subscription.
257func (client *GalleriesClient) List(options *GalleriesListOptions) GalleryListPager {
258	return &galleryListPager{
259		pipeline: client.con.Pipeline(),
260		requester: func(ctx context.Context) (*azcore.Request, error) {
261			return client.listCreateRequest(ctx, options)
262		},
263		responder: client.listHandleResponse,
264		errorer:   client.listHandleError,
265		advancer: func(ctx context.Context, resp GalleryListResponse) (*azcore.Request, error) {
266			return azcore.NewRequest(ctx, http.MethodGet, *resp.GalleryList.NextLink)
267		},
268		statusCodes: []int{http.StatusOK},
269	}
270}
271
272// listCreateRequest creates the List request.
273func (client *GalleriesClient) listCreateRequest(ctx context.Context, options *GalleriesListOptions) (*azcore.Request, error) {
274	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Compute/galleries"
275	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
276	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
277	if err != nil {
278		return nil, err
279	}
280	req.Telemetry(telemetryInfo)
281	query := req.URL.Query()
282	query.Set("api-version", "2020-09-30")
283	req.URL.RawQuery = query.Encode()
284	req.Header.Set("Accept", "application/json")
285	return req, nil
286}
287
288// listHandleResponse handles the List response.
289func (client *GalleriesClient) listHandleResponse(resp *azcore.Response) (GalleryListResponse, error) {
290	var val *GalleryList
291	if err := resp.UnmarshalAsJSON(&val); err != nil {
292		return GalleryListResponse{}, err
293	}
294	return GalleryListResponse{RawResponse: resp.Response, GalleryList: val}, nil
295}
296
297// listHandleError handles the List error response.
298func (client *GalleriesClient) listHandleError(resp *azcore.Response) error {
299	var err CloudError
300	if err := resp.UnmarshalAsJSON(&err); err != nil {
301		return err
302	}
303	return azcore.NewResponseError(&err, resp.Response)
304}
305
306// ListByResourceGroup - List galleries under a resource group.
307func (client *GalleriesClient) ListByResourceGroup(resourceGroupName string, options *GalleriesListByResourceGroupOptions) GalleryListPager {
308	return &galleryListPager{
309		pipeline: client.con.Pipeline(),
310		requester: func(ctx context.Context) (*azcore.Request, error) {
311			return client.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
312		},
313		responder: client.listByResourceGroupHandleResponse,
314		errorer:   client.listByResourceGroupHandleError,
315		advancer: func(ctx context.Context, resp GalleryListResponse) (*azcore.Request, error) {
316			return azcore.NewRequest(ctx, http.MethodGet, *resp.GalleryList.NextLink)
317		},
318		statusCodes: []int{http.StatusOK},
319	}
320}
321
322// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
323func (client *GalleriesClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, options *GalleriesListByResourceGroupOptions) (*azcore.Request, error) {
324	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries"
325	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
326	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
327	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
328	if err != nil {
329		return nil, err
330	}
331	req.Telemetry(telemetryInfo)
332	query := req.URL.Query()
333	query.Set("api-version", "2020-09-30")
334	req.URL.RawQuery = query.Encode()
335	req.Header.Set("Accept", "application/json")
336	return req, nil
337}
338
339// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
340func (client *GalleriesClient) listByResourceGroupHandleResponse(resp *azcore.Response) (GalleryListResponse, error) {
341	var val *GalleryList
342	if err := resp.UnmarshalAsJSON(&val); err != nil {
343		return GalleryListResponse{}, err
344	}
345	return GalleryListResponse{RawResponse: resp.Response, GalleryList: val}, nil
346}
347
348// listByResourceGroupHandleError handles the ListByResourceGroup error response.
349func (client *GalleriesClient) listByResourceGroupHandleError(resp *azcore.Response) error {
350	var err CloudError
351	if err := resp.UnmarshalAsJSON(&err); err != nil {
352		return err
353	}
354	return azcore.NewResponseError(&err, resp.Response)
355}
356
357// BeginUpdate - Update a Shared Image Gallery.
358func (client *GalleriesClient) BeginUpdate(ctx context.Context, resourceGroupName string, galleryName string, gallery GalleryUpdate, options *GalleriesBeginUpdateOptions) (GalleryPollerResponse, error) {
359	resp, err := client.update(ctx, resourceGroupName, galleryName, gallery, options)
360	if err != nil {
361		return GalleryPollerResponse{}, err
362	}
363	result := GalleryPollerResponse{
364		RawResponse: resp.Response,
365	}
366	pt, err := armcore.NewPoller("GalleriesClient.Update", "", resp, client.updateHandleError)
367	if err != nil {
368		return GalleryPollerResponse{}, err
369	}
370	poller := &galleryPoller{
371		pt:       pt,
372		pipeline: client.con.Pipeline(),
373	}
374	result.Poller = poller
375	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (GalleryResponse, error) {
376		return poller.pollUntilDone(ctx, frequency)
377	}
378	return result, nil
379}
380
381// ResumeUpdate creates a new GalleryPoller from the specified resume token.
382// token - The value must come from a previous call to GalleryPoller.ResumeToken().
383func (client *GalleriesClient) ResumeUpdate(token string) (GalleryPoller, error) {
384	pt, err := armcore.NewPollerFromResumeToken("GalleriesClient.Update", token, client.updateHandleError)
385	if err != nil {
386		return nil, err
387	}
388	return &galleryPoller{
389		pipeline: client.con.Pipeline(),
390		pt:       pt,
391	}, nil
392}
393
394// Update - Update a Shared Image Gallery.
395func (client *GalleriesClient) update(ctx context.Context, resourceGroupName string, galleryName string, gallery GalleryUpdate, options *GalleriesBeginUpdateOptions) (*azcore.Response, error) {
396	req, err := client.updateCreateRequest(ctx, resourceGroupName, galleryName, gallery, options)
397	if err != nil {
398		return nil, err
399	}
400	resp, err := client.con.Pipeline().Do(req)
401	if err != nil {
402		return nil, err
403	}
404	if !resp.HasStatusCode(http.StatusOK) {
405		return nil, client.updateHandleError(resp)
406	}
407	return resp, nil
408}
409
410// updateCreateRequest creates the Update request.
411func (client *GalleriesClient) updateCreateRequest(ctx context.Context, resourceGroupName string, galleryName string, gallery GalleryUpdate, options *GalleriesBeginUpdateOptions) (*azcore.Request, error) {
412	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}"
413	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
414	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
415	urlPath = strings.ReplaceAll(urlPath, "{galleryName}", url.PathEscape(galleryName))
416	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
417	if err != nil {
418		return nil, err
419	}
420	req.Telemetry(telemetryInfo)
421	query := req.URL.Query()
422	query.Set("api-version", "2020-09-30")
423	req.URL.RawQuery = query.Encode()
424	req.Header.Set("Accept", "application/json")
425	return req, req.MarshalAsJSON(gallery)
426}
427
428// updateHandleResponse handles the Update response.
429func (client *GalleriesClient) updateHandleResponse(resp *azcore.Response) (GalleryResponse, error) {
430	var val *Gallery
431	if err := resp.UnmarshalAsJSON(&val); err != nil {
432		return GalleryResponse{}, err
433	}
434	return GalleryResponse{RawResponse: resp.Response, Gallery: val}, nil
435}
436
437// updateHandleError handles the Update error response.
438func (client *GalleriesClient) updateHandleError(resp *azcore.Response) error {
439	var err CloudError
440	if err := resp.UnmarshalAsJSON(&err); err != nil {
441		return err
442	}
443	return azcore.NewResponseError(&err, resp.Response)
444}
445