1package vmwarecloudsimple
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// VirtualMachineTemplatesClient is the description of the new service
29type VirtualMachineTemplatesClient struct {
30	BaseClient
31}
32
33// NewVirtualMachineTemplatesClient creates an instance of the VirtualMachineTemplatesClient client.
34func NewVirtualMachineTemplatesClient(subscriptionID string, referer string) VirtualMachineTemplatesClient {
35	return NewVirtualMachineTemplatesClientWithBaseURI(DefaultBaseURI, subscriptionID, referer)
36}
37
38// NewVirtualMachineTemplatesClientWithBaseURI creates an instance of the VirtualMachineTemplatesClient client using a
39// custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds,
40// Azure stack).
41func NewVirtualMachineTemplatesClientWithBaseURI(baseURI string, subscriptionID string, referer string) VirtualMachineTemplatesClient {
42	return VirtualMachineTemplatesClient{NewWithBaseURI(baseURI, subscriptionID, referer)}
43}
44
45// Get returns virtual machine templates by its name
46// Parameters:
47// regionID - the region Id (westus, eastus)
48// pcName - the private cloud name
49// virtualMachineTemplateName - virtual machine template id (vsphereId)
50func (client VirtualMachineTemplatesClient) Get(ctx context.Context, regionID string, pcName string, virtualMachineTemplateName string) (result VirtualMachineTemplate, err error) {
51	if tracing.IsEnabled() {
52		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualMachineTemplatesClient.Get")
53		defer func() {
54			sc := -1
55			if result.Response.Response != nil {
56				sc = result.Response.Response.StatusCode
57			}
58			tracing.EndSpan(ctx, sc, err)
59		}()
60	}
61	req, err := client.GetPreparer(ctx, regionID, pcName, virtualMachineTemplateName)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "Get", nil, "Failure preparing request")
64		return
65	}
66
67	resp, err := client.GetSender(req)
68	if err != nil {
69		result.Response = autorest.Response{Response: resp}
70		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "Get", resp, "Failure sending request")
71		return
72	}
73
74	result, err = client.GetResponder(resp)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "Get", resp, "Failure responding to request")
77		return
78	}
79
80	return
81}
82
83// GetPreparer prepares the Get request.
84func (client VirtualMachineTemplatesClient) GetPreparer(ctx context.Context, regionID string, pcName string, virtualMachineTemplateName string) (*http.Request, error) {
85	pathParameters := map[string]interface{}{
86		"pcName":                     autorest.Encode("path", pcName),
87		"regionId":                   autorest.Encode("path", regionID),
88		"subscriptionId":             autorest.Encode("path", client.SubscriptionID),
89		"virtualMachineTemplateName": autorest.Encode("path", virtualMachineTemplateName),
90	}
91
92	const APIVersion = "2019-04-01"
93	queryParameters := map[string]interface{}{
94		"api-version": APIVersion,
95	}
96
97	preparer := autorest.CreatePreparer(
98		autorest.AsGet(),
99		autorest.WithBaseURL(client.BaseURI),
100		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.VMwareCloudSimple/locations/{regionId}/privateClouds/{pcName}/virtualMachineTemplates/{virtualMachineTemplateName}", pathParameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// GetSender sends the Get request. The method will close the
106// http.Response Body if it receives an error.
107func (client VirtualMachineTemplatesClient) GetSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
109}
110
111// GetResponder handles the response to the Get request. The method always
112// closes the http.Response Body.
113func (client VirtualMachineTemplatesClient) GetResponder(resp *http.Response) (result VirtualMachineTemplate, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// List returns list of virtual machine templates in region for private cloud
124// Parameters:
125// pcName - the private cloud name
126// regionID - the region Id (westus, eastus)
127// resourcePoolName - resource pool used to derive vSphere cluster which contains VM templates
128func (client VirtualMachineTemplatesClient) List(ctx context.Context, pcName string, regionID string, resourcePoolName string) (result VirtualMachineTemplateListResponsePage, err error) {
129	if tracing.IsEnabled() {
130		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualMachineTemplatesClient.List")
131		defer func() {
132			sc := -1
133			if result.vmtlr.Response.Response != nil {
134				sc = result.vmtlr.Response.Response.StatusCode
135			}
136			tracing.EndSpan(ctx, sc, err)
137		}()
138	}
139	result.fn = client.listNextResults
140	req, err := client.ListPreparer(ctx, pcName, regionID, resourcePoolName)
141	if err != nil {
142		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "List", nil, "Failure preparing request")
143		return
144	}
145
146	resp, err := client.ListSender(req)
147	if err != nil {
148		result.vmtlr.Response = autorest.Response{Response: resp}
149		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "List", resp, "Failure sending request")
150		return
151	}
152
153	result.vmtlr, err = client.ListResponder(resp)
154	if err != nil {
155		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "List", resp, "Failure responding to request")
156		return
157	}
158	if result.vmtlr.hasNextLink() && result.vmtlr.IsEmpty() {
159		err = result.NextWithContext(ctx)
160		return
161	}
162
163	return
164}
165
166// ListPreparer prepares the List request.
167func (client VirtualMachineTemplatesClient) ListPreparer(ctx context.Context, pcName string, regionID string, resourcePoolName string) (*http.Request, error) {
168	pathParameters := map[string]interface{}{
169		"pcName":         autorest.Encode("path", pcName),
170		"regionId":       autorest.Encode("path", regionID),
171		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
172	}
173
174	const APIVersion = "2019-04-01"
175	queryParameters := map[string]interface{}{
176		"api-version":      APIVersion,
177		"resourcePoolName": autorest.Encode("query", resourcePoolName),
178	}
179
180	preparer := autorest.CreatePreparer(
181		autorest.AsGet(),
182		autorest.WithBaseURL(client.BaseURI),
183		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.VMwareCloudSimple/locations/{regionId}/privateClouds/{pcName}/virtualMachineTemplates", pathParameters),
184		autorest.WithQueryParameters(queryParameters))
185	return preparer.Prepare((&http.Request{}).WithContext(ctx))
186}
187
188// ListSender sends the List request. The method will close the
189// http.Response Body if it receives an error.
190func (client VirtualMachineTemplatesClient) ListSender(req *http.Request) (*http.Response, error) {
191	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
192}
193
194// ListResponder handles the response to the List request. The method always
195// closes the http.Response Body.
196func (client VirtualMachineTemplatesClient) ListResponder(resp *http.Response) (result VirtualMachineTemplateListResponse, err error) {
197	err = autorest.Respond(
198		resp,
199		azure.WithErrorUnlessStatusCode(http.StatusOK),
200		autorest.ByUnmarshallingJSON(&result),
201		autorest.ByClosing())
202	result.Response = autorest.Response{Response: resp}
203	return
204}
205
206// listNextResults retrieves the next set of results, if any.
207func (client VirtualMachineTemplatesClient) listNextResults(ctx context.Context, lastResults VirtualMachineTemplateListResponse) (result VirtualMachineTemplateListResponse, err error) {
208	req, err := lastResults.virtualMachineTemplateListResponsePreparer(ctx)
209	if err != nil {
210		return result, autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "listNextResults", nil, "Failure preparing next results request")
211	}
212	if req == nil {
213		return
214	}
215	resp, err := client.ListSender(req)
216	if err != nil {
217		result.Response = autorest.Response{Response: resp}
218		return result, autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "listNextResults", resp, "Failure sending next results request")
219	}
220	result, err = client.ListResponder(resp)
221	if err != nil {
222		err = autorest.NewErrorWithError(err, "vmwarecloudsimple.VirtualMachineTemplatesClient", "listNextResults", resp, "Failure responding to next results request")
223	}
224	return
225}
226
227// ListComplete enumerates all values, automatically crossing page boundaries as required.
228func (client VirtualMachineTemplatesClient) ListComplete(ctx context.Context, pcName string, regionID string, resourcePoolName string) (result VirtualMachineTemplateListResponseIterator, err error) {
229	if tracing.IsEnabled() {
230		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualMachineTemplatesClient.List")
231		defer func() {
232			sc := -1
233			if result.Response().Response.Response != nil {
234				sc = result.page.Response().Response.Response.StatusCode
235			}
236			tracing.EndSpan(ctx, sc, err)
237		}()
238	}
239	result.page, err = client.List(ctx, pcName, regionID, resourcePoolName)
240	return
241}
242