1package compute
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// ImagesClient is the compute Client
29type ImagesClient struct {
30	BaseClient
31}
32
33// NewImagesClient creates an instance of the ImagesClient client.
34func NewImagesClient(subscriptionID string) ImagesClient {
35	return NewImagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewImagesClientWithBaseURI creates an instance of the ImagesClient client.
39func NewImagesClientWithBaseURI(baseURI string, subscriptionID string) ImagesClient {
40	return ImagesClient{NewWithBaseURI(baseURI, subscriptionID)}
41}
42
43// CreateOrUpdate create or update an image.
44// Parameters:
45// resourceGroupName - the name of the resource group.
46// imageName - the name of the image.
47// parameters - parameters supplied to the Create Image operation.
48func (client ImagesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, imageName string, parameters Image) (result ImagesCreateOrUpdateFuture, err error) {
49	if tracing.IsEnabled() {
50		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.CreateOrUpdate")
51		defer func() {
52			sc := -1
53			if result.Response() != nil {
54				sc = result.Response().StatusCode
55			}
56			tracing.EndSpan(ctx, sc, err)
57		}()
58	}
59	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, imageName, parameters)
60	if err != nil {
61		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "CreateOrUpdate", nil, "Failure preparing request")
62		return
63	}
64
65	result, err = client.CreateOrUpdateSender(req)
66	if err != nil {
67		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "CreateOrUpdate", result.Response(), "Failure sending request")
68		return
69	}
70
71	return
72}
73
74// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
75func (client ImagesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, imageName string, parameters Image) (*http.Request, error) {
76	pathParameters := map[string]interface{}{
77		"imageName":         autorest.Encode("path", imageName),
78		"resourceGroupName": autorest.Encode("path", resourceGroupName),
79		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
80	}
81
82	const APIVersion = "2017-12-01"
83	queryParameters := map[string]interface{}{
84		"api-version": APIVersion,
85	}
86
87	preparer := autorest.CreatePreparer(
88		autorest.AsContentType("application/json; charset=utf-8"),
89		autorest.AsPut(),
90		autorest.WithBaseURL(client.BaseURI),
91		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/images/{imageName}", pathParameters),
92		autorest.WithJSON(parameters),
93		autorest.WithQueryParameters(queryParameters))
94	return preparer.Prepare((&http.Request{}).WithContext(ctx))
95}
96
97// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
98// http.Response Body if it receives an error.
99func (client ImagesClient) CreateOrUpdateSender(req *http.Request) (future ImagesCreateOrUpdateFuture, err error) {
100	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
101	var resp *http.Response
102	resp, err = autorest.SendWithSender(client, req, sd...)
103	if err != nil {
104		return
105	}
106	future.Future, err = azure.NewFutureFromResponse(resp)
107	return
108}
109
110// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
111// closes the http.Response Body.
112func (client ImagesClient) CreateOrUpdateResponder(resp *http.Response) (result Image, err error) {
113	err = autorest.Respond(
114		resp,
115		client.ByInspecting(),
116		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// Delete deletes an Image.
124// Parameters:
125// resourceGroupName - the name of the resource group.
126// imageName - the name of the image.
127func (client ImagesClient) Delete(ctx context.Context, resourceGroupName string, imageName string) (result ImagesDeleteFuture, err error) {
128	if tracing.IsEnabled() {
129		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.Delete")
130		defer func() {
131			sc := -1
132			if result.Response() != nil {
133				sc = result.Response().StatusCode
134			}
135			tracing.EndSpan(ctx, sc, err)
136		}()
137	}
138	req, err := client.DeletePreparer(ctx, resourceGroupName, imageName)
139	if err != nil {
140		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Delete", nil, "Failure preparing request")
141		return
142	}
143
144	result, err = client.DeleteSender(req)
145	if err != nil {
146		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Delete", result.Response(), "Failure sending request")
147		return
148	}
149
150	return
151}
152
153// DeletePreparer prepares the Delete request.
154func (client ImagesClient) DeletePreparer(ctx context.Context, resourceGroupName string, imageName string) (*http.Request, error) {
155	pathParameters := map[string]interface{}{
156		"imageName":         autorest.Encode("path", imageName),
157		"resourceGroupName": autorest.Encode("path", resourceGroupName),
158		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
159	}
160
161	const APIVersion = "2017-12-01"
162	queryParameters := map[string]interface{}{
163		"api-version": APIVersion,
164	}
165
166	preparer := autorest.CreatePreparer(
167		autorest.AsDelete(),
168		autorest.WithBaseURL(client.BaseURI),
169		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/images/{imageName}", pathParameters),
170		autorest.WithQueryParameters(queryParameters))
171	return preparer.Prepare((&http.Request{}).WithContext(ctx))
172}
173
174// DeleteSender sends the Delete request. The method will close the
175// http.Response Body if it receives an error.
176func (client ImagesClient) DeleteSender(req *http.Request) (future ImagesDeleteFuture, err error) {
177	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
178	var resp *http.Response
179	resp, err = autorest.SendWithSender(client, req, sd...)
180	if err != nil {
181		return
182	}
183	future.Future, err = azure.NewFutureFromResponse(resp)
184	return
185}
186
187// DeleteResponder handles the response to the Delete request. The method always
188// closes the http.Response Body.
189func (client ImagesClient) DeleteResponder(resp *http.Response) (result OperationStatusResponse, err error) {
190	err = autorest.Respond(
191		resp,
192		client.ByInspecting(),
193		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
194		autorest.ByUnmarshallingJSON(&result),
195		autorest.ByClosing())
196	result.Response = autorest.Response{Response: resp}
197	return
198}
199
200// Get gets an image.
201// Parameters:
202// resourceGroupName - the name of the resource group.
203// imageName - the name of the image.
204// expand - the expand expression to apply on the operation.
205func (client ImagesClient) Get(ctx context.Context, resourceGroupName string, imageName string, expand string) (result Image, err error) {
206	if tracing.IsEnabled() {
207		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.Get")
208		defer func() {
209			sc := -1
210			if result.Response.Response != nil {
211				sc = result.Response.Response.StatusCode
212			}
213			tracing.EndSpan(ctx, sc, err)
214		}()
215	}
216	req, err := client.GetPreparer(ctx, resourceGroupName, imageName, expand)
217	if err != nil {
218		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Get", nil, "Failure preparing request")
219		return
220	}
221
222	resp, err := client.GetSender(req)
223	if err != nil {
224		result.Response = autorest.Response{Response: resp}
225		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Get", resp, "Failure sending request")
226		return
227	}
228
229	result, err = client.GetResponder(resp)
230	if err != nil {
231		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Get", resp, "Failure responding to request")
232	}
233
234	return
235}
236
237// GetPreparer prepares the Get request.
238func (client ImagesClient) GetPreparer(ctx context.Context, resourceGroupName string, imageName string, expand string) (*http.Request, error) {
239	pathParameters := map[string]interface{}{
240		"imageName":         autorest.Encode("path", imageName),
241		"resourceGroupName": autorest.Encode("path", resourceGroupName),
242		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
243	}
244
245	const APIVersion = "2017-12-01"
246	queryParameters := map[string]interface{}{
247		"api-version": APIVersion,
248	}
249	if len(expand) > 0 {
250		queryParameters["$expand"] = autorest.Encode("query", expand)
251	}
252
253	preparer := autorest.CreatePreparer(
254		autorest.AsGet(),
255		autorest.WithBaseURL(client.BaseURI),
256		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/images/{imageName}", pathParameters),
257		autorest.WithQueryParameters(queryParameters))
258	return preparer.Prepare((&http.Request{}).WithContext(ctx))
259}
260
261// GetSender sends the Get request. The method will close the
262// http.Response Body if it receives an error.
263func (client ImagesClient) GetSender(req *http.Request) (*http.Response, error) {
264	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
265	return autorest.SendWithSender(client, req, sd...)
266}
267
268// GetResponder handles the response to the Get request. The method always
269// closes the http.Response Body.
270func (client ImagesClient) GetResponder(resp *http.Response) (result Image, err error) {
271	err = autorest.Respond(
272		resp,
273		client.ByInspecting(),
274		azure.WithErrorUnlessStatusCode(http.StatusOK),
275		autorest.ByUnmarshallingJSON(&result),
276		autorest.ByClosing())
277	result.Response = autorest.Response{Response: resp}
278	return
279}
280
281// List gets the list of Images in the subscription. Use nextLink property in the response to get the next page of
282// Images. Do this till nextLink is null to fetch all the Images.
283func (client ImagesClient) List(ctx context.Context) (result ImageListResultPage, err error) {
284	if tracing.IsEnabled() {
285		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.List")
286		defer func() {
287			sc := -1
288			if result.ilr.Response.Response != nil {
289				sc = result.ilr.Response.Response.StatusCode
290			}
291			tracing.EndSpan(ctx, sc, err)
292		}()
293	}
294	result.fn = client.listNextResults
295	req, err := client.ListPreparer(ctx)
296	if err != nil {
297		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "List", nil, "Failure preparing request")
298		return
299	}
300
301	resp, err := client.ListSender(req)
302	if err != nil {
303		result.ilr.Response = autorest.Response{Response: resp}
304		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "List", resp, "Failure sending request")
305		return
306	}
307
308	result.ilr, err = client.ListResponder(resp)
309	if err != nil {
310		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "List", resp, "Failure responding to request")
311	}
312
313	return
314}
315
316// ListPreparer prepares the List request.
317func (client ImagesClient) ListPreparer(ctx context.Context) (*http.Request, error) {
318	pathParameters := map[string]interface{}{
319		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
320	}
321
322	const APIVersion = "2017-12-01"
323	queryParameters := map[string]interface{}{
324		"api-version": APIVersion,
325	}
326
327	preparer := autorest.CreatePreparer(
328		autorest.AsGet(),
329		autorest.WithBaseURL(client.BaseURI),
330		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Compute/images", pathParameters),
331		autorest.WithQueryParameters(queryParameters))
332	return preparer.Prepare((&http.Request{}).WithContext(ctx))
333}
334
335// ListSender sends the List request. The method will close the
336// http.Response Body if it receives an error.
337func (client ImagesClient) ListSender(req *http.Request) (*http.Response, error) {
338	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
339	return autorest.SendWithSender(client, req, sd...)
340}
341
342// ListResponder handles the response to the List request. The method always
343// closes the http.Response Body.
344func (client ImagesClient) ListResponder(resp *http.Response) (result ImageListResult, err error) {
345	err = autorest.Respond(
346		resp,
347		client.ByInspecting(),
348		azure.WithErrorUnlessStatusCode(http.StatusOK),
349		autorest.ByUnmarshallingJSON(&result),
350		autorest.ByClosing())
351	result.Response = autorest.Response{Response: resp}
352	return
353}
354
355// listNextResults retrieves the next set of results, if any.
356func (client ImagesClient) listNextResults(ctx context.Context, lastResults ImageListResult) (result ImageListResult, err error) {
357	req, err := lastResults.imageListResultPreparer(ctx)
358	if err != nil {
359		return result, autorest.NewErrorWithError(err, "compute.ImagesClient", "listNextResults", nil, "Failure preparing next results request")
360	}
361	if req == nil {
362		return
363	}
364	resp, err := client.ListSender(req)
365	if err != nil {
366		result.Response = autorest.Response{Response: resp}
367		return result, autorest.NewErrorWithError(err, "compute.ImagesClient", "listNextResults", resp, "Failure sending next results request")
368	}
369	result, err = client.ListResponder(resp)
370	if err != nil {
371		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "listNextResults", resp, "Failure responding to next results request")
372	}
373	return
374}
375
376// ListComplete enumerates all values, automatically crossing page boundaries as required.
377func (client ImagesClient) ListComplete(ctx context.Context) (result ImageListResultIterator, err error) {
378	if tracing.IsEnabled() {
379		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.List")
380		defer func() {
381			sc := -1
382			if result.Response().Response.Response != nil {
383				sc = result.page.Response().Response.Response.StatusCode
384			}
385			tracing.EndSpan(ctx, sc, err)
386		}()
387	}
388	result.page, err = client.List(ctx)
389	return
390}
391
392// ListByResourceGroup gets the list of images under a resource group.
393// Parameters:
394// resourceGroupName - the name of the resource group.
395func (client ImagesClient) ListByResourceGroup(ctx context.Context, resourceGroupName string) (result ImageListResultPage, err error) {
396	if tracing.IsEnabled() {
397		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.ListByResourceGroup")
398		defer func() {
399			sc := -1
400			if result.ilr.Response.Response != nil {
401				sc = result.ilr.Response.Response.StatusCode
402			}
403			tracing.EndSpan(ctx, sc, err)
404		}()
405	}
406	result.fn = client.listByResourceGroupNextResults
407	req, err := client.ListByResourceGroupPreparer(ctx, resourceGroupName)
408	if err != nil {
409		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "ListByResourceGroup", nil, "Failure preparing request")
410		return
411	}
412
413	resp, err := client.ListByResourceGroupSender(req)
414	if err != nil {
415		result.ilr.Response = autorest.Response{Response: resp}
416		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "ListByResourceGroup", resp, "Failure sending request")
417		return
418	}
419
420	result.ilr, err = client.ListByResourceGroupResponder(resp)
421	if err != nil {
422		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "ListByResourceGroup", resp, "Failure responding to request")
423	}
424
425	return
426}
427
428// ListByResourceGroupPreparer prepares the ListByResourceGroup request.
429func (client ImagesClient) ListByResourceGroupPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
430	pathParameters := map[string]interface{}{
431		"resourceGroupName": autorest.Encode("path", resourceGroupName),
432		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
433	}
434
435	const APIVersion = "2017-12-01"
436	queryParameters := map[string]interface{}{
437		"api-version": APIVersion,
438	}
439
440	preparer := autorest.CreatePreparer(
441		autorest.AsGet(),
442		autorest.WithBaseURL(client.BaseURI),
443		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/images", pathParameters),
444		autorest.WithQueryParameters(queryParameters))
445	return preparer.Prepare((&http.Request{}).WithContext(ctx))
446}
447
448// ListByResourceGroupSender sends the ListByResourceGroup request. The method will close the
449// http.Response Body if it receives an error.
450func (client ImagesClient) ListByResourceGroupSender(req *http.Request) (*http.Response, error) {
451	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
452	return autorest.SendWithSender(client, req, sd...)
453}
454
455// ListByResourceGroupResponder handles the response to the ListByResourceGroup request. The method always
456// closes the http.Response Body.
457func (client ImagesClient) ListByResourceGroupResponder(resp *http.Response) (result ImageListResult, err error) {
458	err = autorest.Respond(
459		resp,
460		client.ByInspecting(),
461		azure.WithErrorUnlessStatusCode(http.StatusOK),
462		autorest.ByUnmarshallingJSON(&result),
463		autorest.ByClosing())
464	result.Response = autorest.Response{Response: resp}
465	return
466}
467
468// listByResourceGroupNextResults retrieves the next set of results, if any.
469func (client ImagesClient) listByResourceGroupNextResults(ctx context.Context, lastResults ImageListResult) (result ImageListResult, err error) {
470	req, err := lastResults.imageListResultPreparer(ctx)
471	if err != nil {
472		return result, autorest.NewErrorWithError(err, "compute.ImagesClient", "listByResourceGroupNextResults", nil, "Failure preparing next results request")
473	}
474	if req == nil {
475		return
476	}
477	resp, err := client.ListByResourceGroupSender(req)
478	if err != nil {
479		result.Response = autorest.Response{Response: resp}
480		return result, autorest.NewErrorWithError(err, "compute.ImagesClient", "listByResourceGroupNextResults", resp, "Failure sending next results request")
481	}
482	result, err = client.ListByResourceGroupResponder(resp)
483	if err != nil {
484		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "listByResourceGroupNextResults", resp, "Failure responding to next results request")
485	}
486	return
487}
488
489// ListByResourceGroupComplete enumerates all values, automatically crossing page boundaries as required.
490func (client ImagesClient) ListByResourceGroupComplete(ctx context.Context, resourceGroupName string) (result ImageListResultIterator, err error) {
491	if tracing.IsEnabled() {
492		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.ListByResourceGroup")
493		defer func() {
494			sc := -1
495			if result.Response().Response.Response != nil {
496				sc = result.page.Response().Response.Response.StatusCode
497			}
498			tracing.EndSpan(ctx, sc, err)
499		}()
500	}
501	result.page, err = client.ListByResourceGroup(ctx, resourceGroupName)
502	return
503}
504
505// Update update an image.
506// Parameters:
507// resourceGroupName - the name of the resource group.
508// imageName - the name of the image.
509// parameters - parameters supplied to the Update Image operation.
510func (client ImagesClient) Update(ctx context.Context, resourceGroupName string, imageName string, parameters ImageUpdate) (result ImagesUpdateFuture, err error) {
511	if tracing.IsEnabled() {
512		ctx = tracing.StartSpan(ctx, fqdn+"/ImagesClient.Update")
513		defer func() {
514			sc := -1
515			if result.Response() != nil {
516				sc = result.Response().StatusCode
517			}
518			tracing.EndSpan(ctx, sc, err)
519		}()
520	}
521	req, err := client.UpdatePreparer(ctx, resourceGroupName, imageName, parameters)
522	if err != nil {
523		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Update", nil, "Failure preparing request")
524		return
525	}
526
527	result, err = client.UpdateSender(req)
528	if err != nil {
529		err = autorest.NewErrorWithError(err, "compute.ImagesClient", "Update", result.Response(), "Failure sending request")
530		return
531	}
532
533	return
534}
535
536// UpdatePreparer prepares the Update request.
537func (client ImagesClient) UpdatePreparer(ctx context.Context, resourceGroupName string, imageName string, parameters ImageUpdate) (*http.Request, error) {
538	pathParameters := map[string]interface{}{
539		"imageName":         autorest.Encode("path", imageName),
540		"resourceGroupName": autorest.Encode("path", resourceGroupName),
541		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
542	}
543
544	const APIVersion = "2017-12-01"
545	queryParameters := map[string]interface{}{
546		"api-version": APIVersion,
547	}
548
549	preparer := autorest.CreatePreparer(
550		autorest.AsContentType("application/json; charset=utf-8"),
551		autorest.AsPatch(),
552		autorest.WithBaseURL(client.BaseURI),
553		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/images/{imageName}", pathParameters),
554		autorest.WithJSON(parameters),
555		autorest.WithQueryParameters(queryParameters))
556	return preparer.Prepare((&http.Request{}).WithContext(ctx))
557}
558
559// UpdateSender sends the Update request. The method will close the
560// http.Response Body if it receives an error.
561func (client ImagesClient) UpdateSender(req *http.Request) (future ImagesUpdateFuture, err error) {
562	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
563	var resp *http.Response
564	resp, err = autorest.SendWithSender(client, req, sd...)
565	if err != nil {
566		return
567	}
568	future.Future, err = azure.NewFutureFromResponse(resp)
569	return
570}
571
572// UpdateResponder handles the response to the Update request. The method always
573// closes the http.Response Body.
574func (client ImagesClient) UpdateResponder(resp *http.Response) (result Image, err error) {
575	err = autorest.Respond(
576		resp,
577		client.ByInspecting(),
578		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
579		autorest.ByUnmarshallingJSON(&result),
580		autorest.ByClosing())
581	result.Response = autorest.Response{Response: resp}
582	return
583}
584