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