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/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// GalleryImagesClient is the compute Client
30type GalleryImagesClient struct {
31	BaseClient
32}
33
34// NewGalleryImagesClient creates an instance of the GalleryImagesClient client.
35func NewGalleryImagesClient(subscriptionID string) GalleryImagesClient {
36	return NewGalleryImagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewGalleryImagesClientWithBaseURI creates an instance of the GalleryImagesClient client.
40func NewGalleryImagesClientWithBaseURI(baseURI string, subscriptionID string) GalleryImagesClient {
41	return GalleryImagesClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// CreateOrUpdate create or update a gallery Image Definition.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// galleryName - the name of the Shared Image Gallery in which the Image Definition is to be created.
48// galleryImageName - the name of the gallery Image Definition to be created or updated. The allowed characters
49// are alphabets and numbers with dots, dashes, and periods allowed in the middle. The maximum length is 80
50// characters.
51// galleryImage - parameters supplied to the create or update gallery image operation.
52func (client GalleryImagesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, galleryName string, galleryImageName string, galleryImage GalleryImage) (result GalleryImagesCreateOrUpdateFuture, err error) {
53	if tracing.IsEnabled() {
54		ctx = tracing.StartSpan(ctx, fqdn+"/GalleryImagesClient.CreateOrUpdate")
55		defer func() {
56			sc := -1
57			if result.Response() != nil {
58				sc = result.Response().StatusCode
59			}
60			tracing.EndSpan(ctx, sc, err)
61		}()
62	}
63	if err := validation.Validate([]validation.Validation{
64		{TargetValue: galleryImage,
65			Constraints: []validation.Constraint{{Target: "galleryImage.GalleryImageProperties", Name: validation.Null, Rule: false,
66				Chain: []validation.Constraint{{Target: "galleryImage.GalleryImageProperties.Identifier", Name: validation.Null, Rule: true,
67					Chain: []validation.Constraint{{Target: "galleryImage.GalleryImageProperties.Identifier.Publisher", Name: validation.Null, Rule: true, Chain: nil},
68						{Target: "galleryImage.GalleryImageProperties.Identifier.Offer", Name: validation.Null, Rule: true, Chain: nil},
69						{Target: "galleryImage.GalleryImageProperties.Identifier.Sku", Name: validation.Null, Rule: true, Chain: nil},
70					}},
71				}}}}}); err != nil {
72		return result, validation.NewError("compute.GalleryImagesClient", "CreateOrUpdate", err.Error())
73	}
74
75	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, galleryName, galleryImageName, galleryImage)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "CreateOrUpdate", nil, "Failure preparing request")
78		return
79	}
80
81	result, err = client.CreateOrUpdateSender(req)
82	if err != nil {
83		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "CreateOrUpdate", result.Response(), "Failure sending request")
84		return
85	}
86
87	return
88}
89
90// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
91func (client GalleryImagesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, galleryName string, galleryImageName string, galleryImage GalleryImage) (*http.Request, error) {
92	pathParameters := map[string]interface{}{
93		"galleryImageName":  autorest.Encode("path", galleryImageName),
94		"galleryName":       autorest.Encode("path", galleryName),
95		"resourceGroupName": autorest.Encode("path", resourceGroupName),
96		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
97	}
98
99	const APIVersion = "2018-06-01"
100	queryParameters := map[string]interface{}{
101		"api-version": APIVersion,
102	}
103
104	preparer := autorest.CreatePreparer(
105		autorest.AsContentType("application/json; charset=utf-8"),
106		autorest.AsPut(),
107		autorest.WithBaseURL(client.BaseURI),
108		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/images/{galleryImageName}", pathParameters),
109		autorest.WithJSON(galleryImage),
110		autorest.WithQueryParameters(queryParameters))
111	return preparer.Prepare((&http.Request{}).WithContext(ctx))
112}
113
114// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
115// http.Response Body if it receives an error.
116func (client GalleryImagesClient) CreateOrUpdateSender(req *http.Request) (future GalleryImagesCreateOrUpdateFuture, err error) {
117	var resp *http.Response
118	resp, err = autorest.SendWithSender(client, req,
119		azure.DoRetryWithRegistration(client.Client))
120	if err != nil {
121		return
122	}
123	future.Future, err = azure.NewFutureFromResponse(resp)
124	return
125}
126
127// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
128// closes the http.Response Body.
129func (client GalleryImagesClient) CreateOrUpdateResponder(resp *http.Response) (result GalleryImage, err error) {
130	err = autorest.Respond(
131		resp,
132		client.ByInspecting(),
133		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
134		autorest.ByUnmarshallingJSON(&result),
135		autorest.ByClosing())
136	result.Response = autorest.Response{Response: resp}
137	return
138}
139
140// Delete delete a gallery image.
141// Parameters:
142// resourceGroupName - the name of the resource group.
143// galleryName - the name of the Shared Image Gallery in which the Image Definition is to be deleted.
144// galleryImageName - the name of the gallery Image Definition to be deleted.
145func (client GalleryImagesClient) Delete(ctx context.Context, resourceGroupName string, galleryName string, galleryImageName string) (result GalleryImagesDeleteFuture, err error) {
146	if tracing.IsEnabled() {
147		ctx = tracing.StartSpan(ctx, fqdn+"/GalleryImagesClient.Delete")
148		defer func() {
149			sc := -1
150			if result.Response() != nil {
151				sc = result.Response().StatusCode
152			}
153			tracing.EndSpan(ctx, sc, err)
154		}()
155	}
156	req, err := client.DeletePreparer(ctx, resourceGroupName, galleryName, galleryImageName)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "Delete", nil, "Failure preparing request")
159		return
160	}
161
162	result, err = client.DeleteSender(req)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "Delete", result.Response(), "Failure sending request")
165		return
166	}
167
168	return
169}
170
171// DeletePreparer prepares the Delete request.
172func (client GalleryImagesClient) DeletePreparer(ctx context.Context, resourceGroupName string, galleryName string, galleryImageName string) (*http.Request, error) {
173	pathParameters := map[string]interface{}{
174		"galleryImageName":  autorest.Encode("path", galleryImageName),
175		"galleryName":       autorest.Encode("path", galleryName),
176		"resourceGroupName": autorest.Encode("path", resourceGroupName),
177		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
178	}
179
180	const APIVersion = "2018-06-01"
181	queryParameters := map[string]interface{}{
182		"api-version": APIVersion,
183	}
184
185	preparer := autorest.CreatePreparer(
186		autorest.AsDelete(),
187		autorest.WithBaseURL(client.BaseURI),
188		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/images/{galleryImageName}", pathParameters),
189		autorest.WithQueryParameters(queryParameters))
190	return preparer.Prepare((&http.Request{}).WithContext(ctx))
191}
192
193// DeleteSender sends the Delete request. The method will close the
194// http.Response Body if it receives an error.
195func (client GalleryImagesClient) DeleteSender(req *http.Request) (future GalleryImagesDeleteFuture, err error) {
196	var resp *http.Response
197	resp, err = autorest.SendWithSender(client, req,
198		azure.DoRetryWithRegistration(client.Client))
199	if err != nil {
200		return
201	}
202	future.Future, err = azure.NewFutureFromResponse(resp)
203	return
204}
205
206// DeleteResponder handles the response to the Delete request. The method always
207// closes the http.Response Body.
208func (client GalleryImagesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
209	err = autorest.Respond(
210		resp,
211		client.ByInspecting(),
212		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
213		autorest.ByClosing())
214	result.Response = resp
215	return
216}
217
218// Get retrieves information about a gallery Image Definition.
219// Parameters:
220// resourceGroupName - the name of the resource group.
221// galleryName - the name of the Shared Image Gallery from which the Image Definitions are to be retrieved.
222// galleryImageName - the name of the gallery Image Definition to be retrieved.
223func (client GalleryImagesClient) Get(ctx context.Context, resourceGroupName string, galleryName string, galleryImageName string) (result GalleryImage, err error) {
224	if tracing.IsEnabled() {
225		ctx = tracing.StartSpan(ctx, fqdn+"/GalleryImagesClient.Get")
226		defer func() {
227			sc := -1
228			if result.Response.Response != nil {
229				sc = result.Response.Response.StatusCode
230			}
231			tracing.EndSpan(ctx, sc, err)
232		}()
233	}
234	req, err := client.GetPreparer(ctx, resourceGroupName, galleryName, galleryImageName)
235	if err != nil {
236		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "Get", nil, "Failure preparing request")
237		return
238	}
239
240	resp, err := client.GetSender(req)
241	if err != nil {
242		result.Response = autorest.Response{Response: resp}
243		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "Get", resp, "Failure sending request")
244		return
245	}
246
247	result, err = client.GetResponder(resp)
248	if err != nil {
249		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "Get", resp, "Failure responding to request")
250	}
251
252	return
253}
254
255// GetPreparer prepares the Get request.
256func (client GalleryImagesClient) GetPreparer(ctx context.Context, resourceGroupName string, galleryName string, galleryImageName string) (*http.Request, error) {
257	pathParameters := map[string]interface{}{
258		"galleryImageName":  autorest.Encode("path", galleryImageName),
259		"galleryName":       autorest.Encode("path", galleryName),
260		"resourceGroupName": autorest.Encode("path", resourceGroupName),
261		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
262	}
263
264	const APIVersion = "2018-06-01"
265	queryParameters := map[string]interface{}{
266		"api-version": APIVersion,
267	}
268
269	preparer := autorest.CreatePreparer(
270		autorest.AsGet(),
271		autorest.WithBaseURL(client.BaseURI),
272		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/images/{galleryImageName}", pathParameters),
273		autorest.WithQueryParameters(queryParameters))
274	return preparer.Prepare((&http.Request{}).WithContext(ctx))
275}
276
277// GetSender sends the Get request. The method will close the
278// http.Response Body if it receives an error.
279func (client GalleryImagesClient) GetSender(req *http.Request) (*http.Response, error) {
280	return autorest.SendWithSender(client, req,
281		azure.DoRetryWithRegistration(client.Client))
282}
283
284// GetResponder handles the response to the Get request. The method always
285// closes the http.Response Body.
286func (client GalleryImagesClient) GetResponder(resp *http.Response) (result GalleryImage, err error) {
287	err = autorest.Respond(
288		resp,
289		client.ByInspecting(),
290		azure.WithErrorUnlessStatusCode(http.StatusOK),
291		autorest.ByUnmarshallingJSON(&result),
292		autorest.ByClosing())
293	result.Response = autorest.Response{Response: resp}
294	return
295}
296
297// ListByGallery list gallery Image Definitions in a gallery.
298// Parameters:
299// resourceGroupName - the name of the resource group.
300// galleryName - the name of the Shared Image Gallery from which Image Definitions are to be listed.
301func (client GalleryImagesClient) ListByGallery(ctx context.Context, resourceGroupName string, galleryName string) (result GalleryImageListPage, err error) {
302	if tracing.IsEnabled() {
303		ctx = tracing.StartSpan(ctx, fqdn+"/GalleryImagesClient.ListByGallery")
304		defer func() {
305			sc := -1
306			if result.gil.Response.Response != nil {
307				sc = result.gil.Response.Response.StatusCode
308			}
309			tracing.EndSpan(ctx, sc, err)
310		}()
311	}
312	result.fn = client.listByGalleryNextResults
313	req, err := client.ListByGalleryPreparer(ctx, resourceGroupName, galleryName)
314	if err != nil {
315		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "ListByGallery", nil, "Failure preparing request")
316		return
317	}
318
319	resp, err := client.ListByGallerySender(req)
320	if err != nil {
321		result.gil.Response = autorest.Response{Response: resp}
322		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "ListByGallery", resp, "Failure sending request")
323		return
324	}
325
326	result.gil, err = client.ListByGalleryResponder(resp)
327	if err != nil {
328		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "ListByGallery", resp, "Failure responding to request")
329	}
330
331	return
332}
333
334// ListByGalleryPreparer prepares the ListByGallery request.
335func (client GalleryImagesClient) ListByGalleryPreparer(ctx context.Context, resourceGroupName string, galleryName string) (*http.Request, error) {
336	pathParameters := map[string]interface{}{
337		"galleryName":       autorest.Encode("path", galleryName),
338		"resourceGroupName": autorest.Encode("path", resourceGroupName),
339		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
340	}
341
342	const APIVersion = "2018-06-01"
343	queryParameters := map[string]interface{}{
344		"api-version": APIVersion,
345	}
346
347	preparer := autorest.CreatePreparer(
348		autorest.AsGet(),
349		autorest.WithBaseURL(client.BaseURI),
350		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/images", pathParameters),
351		autorest.WithQueryParameters(queryParameters))
352	return preparer.Prepare((&http.Request{}).WithContext(ctx))
353}
354
355// ListByGallerySender sends the ListByGallery request. The method will close the
356// http.Response Body if it receives an error.
357func (client GalleryImagesClient) ListByGallerySender(req *http.Request) (*http.Response, error) {
358	return autorest.SendWithSender(client, req,
359		azure.DoRetryWithRegistration(client.Client))
360}
361
362// ListByGalleryResponder handles the response to the ListByGallery request. The method always
363// closes the http.Response Body.
364func (client GalleryImagesClient) ListByGalleryResponder(resp *http.Response) (result GalleryImageList, err error) {
365	err = autorest.Respond(
366		resp,
367		client.ByInspecting(),
368		azure.WithErrorUnlessStatusCode(http.StatusOK),
369		autorest.ByUnmarshallingJSON(&result),
370		autorest.ByClosing())
371	result.Response = autorest.Response{Response: resp}
372	return
373}
374
375// listByGalleryNextResults retrieves the next set of results, if any.
376func (client GalleryImagesClient) listByGalleryNextResults(ctx context.Context, lastResults GalleryImageList) (result GalleryImageList, err error) {
377	req, err := lastResults.galleryImageListPreparer(ctx)
378	if err != nil {
379		return result, autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "listByGalleryNextResults", nil, "Failure preparing next results request")
380	}
381	if req == nil {
382		return
383	}
384	resp, err := client.ListByGallerySender(req)
385	if err != nil {
386		result.Response = autorest.Response{Response: resp}
387		return result, autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "listByGalleryNextResults", resp, "Failure sending next results request")
388	}
389	result, err = client.ListByGalleryResponder(resp)
390	if err != nil {
391		err = autorest.NewErrorWithError(err, "compute.GalleryImagesClient", "listByGalleryNextResults", resp, "Failure responding to next results request")
392	}
393	return
394}
395
396// ListByGalleryComplete enumerates all values, automatically crossing page boundaries as required.
397func (client GalleryImagesClient) ListByGalleryComplete(ctx context.Context, resourceGroupName string, galleryName string) (result GalleryImageListIterator, err error) {
398	if tracing.IsEnabled() {
399		ctx = tracing.StartSpan(ctx, fqdn+"/GalleryImagesClient.ListByGallery")
400		defer func() {
401			sc := -1
402			if result.Response().Response.Response != nil {
403				sc = result.page.Response().Response.Response.StatusCode
404			}
405			tracing.EndSpan(ctx, sc, err)
406		}()
407	}
408	result.page, err = client.ListByGallery(ctx, resourceGroupName, galleryName)
409	return
410}
411