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	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"io/ioutil"
17	"net/http"
18	"net/url"
19	"strings"
20)
21
22// SSHPublicKeysClient contains the methods for the SSHPublicKeys group.
23// Don't use this type directly, use NewSSHPublicKeysClient() instead.
24type SSHPublicKeysClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewSSHPublicKeysClient creates a new instance of SSHPublicKeysClient with the specified values.
30func NewSSHPublicKeysClient(con *armcore.Connection, subscriptionID string) *SSHPublicKeysClient {
31	return &SSHPublicKeysClient{con: con, subscriptionID: subscriptionID}
32}
33
34// Create - Creates a new SSH public key resource.
35func (client *SSHPublicKeysClient) Create(ctx context.Context, resourceGroupName string, sshPublicKeyName string, parameters SSHPublicKeyResource, options *SSHPublicKeysCreateOptions) (SSHPublicKeyResourceResponse, error) {
36	req, err := client.createCreateRequest(ctx, resourceGroupName, sshPublicKeyName, parameters, options)
37	if err != nil {
38		return SSHPublicKeyResourceResponse{}, err
39	}
40	resp, err := client.con.Pipeline().Do(req)
41	if err != nil {
42		return SSHPublicKeyResourceResponse{}, err
43	}
44	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
45		return SSHPublicKeyResourceResponse{}, client.createHandleError(resp)
46	}
47	return client.createHandleResponse(resp)
48}
49
50// createCreateRequest creates the Create request.
51func (client *SSHPublicKeysClient) createCreateRequest(ctx context.Context, resourceGroupName string, sshPublicKeyName string, parameters SSHPublicKeyResource, options *SSHPublicKeysCreateOptions) (*azcore.Request, error) {
52	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/sshPublicKeys/{sshPublicKeyName}"
53	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
54	urlPath = strings.ReplaceAll(urlPath, "{sshPublicKeyName}", url.PathEscape(sshPublicKeyName))
55	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
56	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
57	if err != nil {
58		return nil, err
59	}
60	req.Telemetry(telemetryInfo)
61	query := req.URL.Query()
62	query.Set("api-version", "2020-06-01")
63	req.URL.RawQuery = query.Encode()
64	req.Header.Set("Accept", "application/json")
65	return req, req.MarshalAsJSON(parameters)
66}
67
68// createHandleResponse handles the Create response.
69func (client *SSHPublicKeysClient) createHandleResponse(resp *azcore.Response) (SSHPublicKeyResourceResponse, error) {
70	var val *SSHPublicKeyResource
71	if err := resp.UnmarshalAsJSON(&val); err != nil {
72		return SSHPublicKeyResourceResponse{}, err
73	}
74	return SSHPublicKeyResourceResponse{RawResponse: resp.Response, SSHPublicKeyResource: val}, nil
75}
76
77// createHandleError handles the Create error response.
78func (client *SSHPublicKeysClient) createHandleError(resp *azcore.Response) error {
79	body, err := ioutil.ReadAll(resp.Body)
80	if err != nil {
81		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
82	}
83	if len(body) == 0 {
84		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
85	}
86	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
87}
88
89// Delete - Delete an SSH public key.
90func (client *SSHPublicKeysClient) Delete(ctx context.Context, resourceGroupName string, sshPublicKeyName string, options *SSHPublicKeysDeleteOptions) (*http.Response, error) {
91	req, err := client.deleteCreateRequest(ctx, resourceGroupName, sshPublicKeyName, options)
92	if err != nil {
93		return nil, err
94	}
95	resp, err := client.con.Pipeline().Do(req)
96	if err != nil {
97		return nil, err
98	}
99	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
100		return nil, client.deleteHandleError(resp)
101	}
102	return resp.Response, nil
103}
104
105// deleteCreateRequest creates the Delete request.
106func (client *SSHPublicKeysClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, sshPublicKeyName string, options *SSHPublicKeysDeleteOptions) (*azcore.Request, error) {
107	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/sshPublicKeys/{sshPublicKeyName}"
108	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
109	urlPath = strings.ReplaceAll(urlPath, "{sshPublicKeyName}", url.PathEscape(sshPublicKeyName))
110	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
111	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
112	if err != nil {
113		return nil, err
114	}
115	req.Telemetry(telemetryInfo)
116	query := req.URL.Query()
117	query.Set("api-version", "2020-06-01")
118	req.URL.RawQuery = query.Encode()
119	return req, nil
120}
121
122// deleteHandleError handles the Delete error response.
123func (client *SSHPublicKeysClient) deleteHandleError(resp *azcore.Response) error {
124	body, err := ioutil.ReadAll(resp.Body)
125	if err != nil {
126		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
127	}
128	if len(body) == 0 {
129		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
130	}
131	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
132}
133
134// GenerateKeyPair - Generates and returns a public/private key pair and populates the SSH public key resource with the public key. The length of the key
135// will be 3072 bits. This operation can only be performed once per
136// SSH public key resource.
137func (client *SSHPublicKeysClient) GenerateKeyPair(ctx context.Context, resourceGroupName string, sshPublicKeyName string, options *SSHPublicKeysGenerateKeyPairOptions) (SSHPublicKeyGenerateKeyPairResultResponse, error) {
138	req, err := client.generateKeyPairCreateRequest(ctx, resourceGroupName, sshPublicKeyName, options)
139	if err != nil {
140		return SSHPublicKeyGenerateKeyPairResultResponse{}, err
141	}
142	resp, err := client.con.Pipeline().Do(req)
143	if err != nil {
144		return SSHPublicKeyGenerateKeyPairResultResponse{}, err
145	}
146	if !resp.HasStatusCode(http.StatusOK) {
147		return SSHPublicKeyGenerateKeyPairResultResponse{}, client.generateKeyPairHandleError(resp)
148	}
149	return client.generateKeyPairHandleResponse(resp)
150}
151
152// generateKeyPairCreateRequest creates the GenerateKeyPair request.
153func (client *SSHPublicKeysClient) generateKeyPairCreateRequest(ctx context.Context, resourceGroupName string, sshPublicKeyName string, options *SSHPublicKeysGenerateKeyPairOptions) (*azcore.Request, error) {
154	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/sshPublicKeys/{sshPublicKeyName}/generateKeyPair"
155	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
156	urlPath = strings.ReplaceAll(urlPath, "{sshPublicKeyName}", url.PathEscape(sshPublicKeyName))
157	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
158	req, err := azcore.NewRequest(ctx, http.MethodPost, azcore.JoinPaths(client.con.Endpoint(), urlPath))
159	if err != nil {
160		return nil, err
161	}
162	req.Telemetry(telemetryInfo)
163	query := req.URL.Query()
164	query.Set("api-version", "2020-06-01")
165	req.URL.RawQuery = query.Encode()
166	req.Header.Set("Accept", "application/json")
167	return req, nil
168}
169
170// generateKeyPairHandleResponse handles the GenerateKeyPair response.
171func (client *SSHPublicKeysClient) generateKeyPairHandleResponse(resp *azcore.Response) (SSHPublicKeyGenerateKeyPairResultResponse, error) {
172	var val *SSHPublicKeyGenerateKeyPairResult
173	if err := resp.UnmarshalAsJSON(&val); err != nil {
174		return SSHPublicKeyGenerateKeyPairResultResponse{}, err
175	}
176	return SSHPublicKeyGenerateKeyPairResultResponse{RawResponse: resp.Response, SSHPublicKeyGenerateKeyPairResult: val}, nil
177}
178
179// generateKeyPairHandleError handles the GenerateKeyPair error response.
180func (client *SSHPublicKeysClient) generateKeyPairHandleError(resp *azcore.Response) error {
181	body, err := ioutil.ReadAll(resp.Body)
182	if err != nil {
183		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
184	}
185	if len(body) == 0 {
186		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
187	}
188	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
189}
190
191// Get - Retrieves information about an SSH public key.
192func (client *SSHPublicKeysClient) Get(ctx context.Context, resourceGroupName string, sshPublicKeyName string, options *SSHPublicKeysGetOptions) (SSHPublicKeyResourceResponse, error) {
193	req, err := client.getCreateRequest(ctx, resourceGroupName, sshPublicKeyName, options)
194	if err != nil {
195		return SSHPublicKeyResourceResponse{}, err
196	}
197	resp, err := client.con.Pipeline().Do(req)
198	if err != nil {
199		return SSHPublicKeyResourceResponse{}, err
200	}
201	if !resp.HasStatusCode(http.StatusOK) {
202		return SSHPublicKeyResourceResponse{}, client.getHandleError(resp)
203	}
204	return client.getHandleResponse(resp)
205}
206
207// getCreateRequest creates the Get request.
208func (client *SSHPublicKeysClient) getCreateRequest(ctx context.Context, resourceGroupName string, sshPublicKeyName string, options *SSHPublicKeysGetOptions) (*azcore.Request, error) {
209	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/sshPublicKeys/{sshPublicKeyName}"
210	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
211	urlPath = strings.ReplaceAll(urlPath, "{sshPublicKeyName}", url.PathEscape(sshPublicKeyName))
212	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
213	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
214	if err != nil {
215		return nil, err
216	}
217	req.Telemetry(telemetryInfo)
218	query := req.URL.Query()
219	query.Set("api-version", "2020-06-01")
220	req.URL.RawQuery = query.Encode()
221	req.Header.Set("Accept", "application/json")
222	return req, nil
223}
224
225// getHandleResponse handles the Get response.
226func (client *SSHPublicKeysClient) getHandleResponse(resp *azcore.Response) (SSHPublicKeyResourceResponse, error) {
227	var val *SSHPublicKeyResource
228	if err := resp.UnmarshalAsJSON(&val); err != nil {
229		return SSHPublicKeyResourceResponse{}, err
230	}
231	return SSHPublicKeyResourceResponse{RawResponse: resp.Response, SSHPublicKeyResource: val}, nil
232}
233
234// getHandleError handles the Get error response.
235func (client *SSHPublicKeysClient) getHandleError(resp *azcore.Response) error {
236	body, err := ioutil.ReadAll(resp.Body)
237	if err != nil {
238		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
239	}
240	if len(body) == 0 {
241		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
242	}
243	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
244}
245
246// ListByResourceGroup - Lists all of the SSH public keys in the specified resource group. Use the nextLink property in the response to get the next page
247// of SSH public keys.
248func (client *SSHPublicKeysClient) ListByResourceGroup(resourceGroupName string, options *SSHPublicKeysListByResourceGroupOptions) SSHPublicKeysGroupListResultPager {
249	return &sshPublicKeysGroupListResultPager{
250		pipeline: client.con.Pipeline(),
251		requester: func(ctx context.Context) (*azcore.Request, error) {
252			return client.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
253		},
254		responder: client.listByResourceGroupHandleResponse,
255		errorer:   client.listByResourceGroupHandleError,
256		advancer: func(ctx context.Context, resp SSHPublicKeysGroupListResultResponse) (*azcore.Request, error) {
257			return azcore.NewRequest(ctx, http.MethodGet, *resp.SSHPublicKeysGroupListResult.NextLink)
258		},
259		statusCodes: []int{http.StatusOK},
260	}
261}
262
263// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
264func (client *SSHPublicKeysClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, options *SSHPublicKeysListByResourceGroupOptions) (*azcore.Request, error) {
265	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/sshPublicKeys"
266	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
267	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
268	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
269	if err != nil {
270		return nil, err
271	}
272	req.Telemetry(telemetryInfo)
273	query := req.URL.Query()
274	query.Set("api-version", "2020-06-01")
275	req.URL.RawQuery = query.Encode()
276	req.Header.Set("Accept", "application/json")
277	return req, nil
278}
279
280// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
281func (client *SSHPublicKeysClient) listByResourceGroupHandleResponse(resp *azcore.Response) (SSHPublicKeysGroupListResultResponse, error) {
282	var val *SSHPublicKeysGroupListResult
283	if err := resp.UnmarshalAsJSON(&val); err != nil {
284		return SSHPublicKeysGroupListResultResponse{}, err
285	}
286	return SSHPublicKeysGroupListResultResponse{RawResponse: resp.Response, SSHPublicKeysGroupListResult: val}, nil
287}
288
289// listByResourceGroupHandleError handles the ListByResourceGroup error response.
290func (client *SSHPublicKeysClient) listByResourceGroupHandleError(resp *azcore.Response) error {
291	body, err := ioutil.ReadAll(resp.Body)
292	if err != nil {
293		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
294	}
295	if len(body) == 0 {
296		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
297	}
298	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
299}
300
301// ListBySubscription - Lists all of the SSH public keys in the subscription. Use the nextLink property in the response to get the next page of SSH public
302// keys.
303func (client *SSHPublicKeysClient) ListBySubscription(options *SSHPublicKeysListBySubscriptionOptions) SSHPublicKeysGroupListResultPager {
304	return &sshPublicKeysGroupListResultPager{
305		pipeline: client.con.Pipeline(),
306		requester: func(ctx context.Context) (*azcore.Request, error) {
307			return client.listBySubscriptionCreateRequest(ctx, options)
308		},
309		responder: client.listBySubscriptionHandleResponse,
310		errorer:   client.listBySubscriptionHandleError,
311		advancer: func(ctx context.Context, resp SSHPublicKeysGroupListResultResponse) (*azcore.Request, error) {
312			return azcore.NewRequest(ctx, http.MethodGet, *resp.SSHPublicKeysGroupListResult.NextLink)
313		},
314		statusCodes: []int{http.StatusOK},
315	}
316}
317
318// listBySubscriptionCreateRequest creates the ListBySubscription request.
319func (client *SSHPublicKeysClient) listBySubscriptionCreateRequest(ctx context.Context, options *SSHPublicKeysListBySubscriptionOptions) (*azcore.Request, error) {
320	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Compute/sshPublicKeys"
321	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
322	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
323	if err != nil {
324		return nil, err
325	}
326	req.Telemetry(telemetryInfo)
327	query := req.URL.Query()
328	query.Set("api-version", "2020-06-01")
329	req.URL.RawQuery = query.Encode()
330	req.Header.Set("Accept", "application/json")
331	return req, nil
332}
333
334// listBySubscriptionHandleResponse handles the ListBySubscription response.
335func (client *SSHPublicKeysClient) listBySubscriptionHandleResponse(resp *azcore.Response) (SSHPublicKeysGroupListResultResponse, error) {
336	var val *SSHPublicKeysGroupListResult
337	if err := resp.UnmarshalAsJSON(&val); err != nil {
338		return SSHPublicKeysGroupListResultResponse{}, err
339	}
340	return SSHPublicKeysGroupListResultResponse{RawResponse: resp.Response, SSHPublicKeysGroupListResult: val}, nil
341}
342
343// listBySubscriptionHandleError handles the ListBySubscription error response.
344func (client *SSHPublicKeysClient) listBySubscriptionHandleError(resp *azcore.Response) error {
345	body, err := ioutil.ReadAll(resp.Body)
346	if err != nil {
347		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
348	}
349	if len(body) == 0 {
350		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
351	}
352	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
353}
354
355// Update - Updates a new SSH public key resource.
356func (client *SSHPublicKeysClient) Update(ctx context.Context, resourceGroupName string, sshPublicKeyName string, parameters SSHPublicKeyUpdateResource, options *SSHPublicKeysUpdateOptions) (SSHPublicKeyResourceResponse, error) {
357	req, err := client.updateCreateRequest(ctx, resourceGroupName, sshPublicKeyName, parameters, options)
358	if err != nil {
359		return SSHPublicKeyResourceResponse{}, err
360	}
361	resp, err := client.con.Pipeline().Do(req)
362	if err != nil {
363		return SSHPublicKeyResourceResponse{}, err
364	}
365	if !resp.HasStatusCode(http.StatusOK) {
366		return SSHPublicKeyResourceResponse{}, client.updateHandleError(resp)
367	}
368	return client.updateHandleResponse(resp)
369}
370
371// updateCreateRequest creates the Update request.
372func (client *SSHPublicKeysClient) updateCreateRequest(ctx context.Context, resourceGroupName string, sshPublicKeyName string, parameters SSHPublicKeyUpdateResource, options *SSHPublicKeysUpdateOptions) (*azcore.Request, error) {
373	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/sshPublicKeys/{sshPublicKeyName}"
374	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
375	urlPath = strings.ReplaceAll(urlPath, "{sshPublicKeyName}", url.PathEscape(sshPublicKeyName))
376	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
377	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
378	if err != nil {
379		return nil, err
380	}
381	req.Telemetry(telemetryInfo)
382	query := req.URL.Query()
383	query.Set("api-version", "2020-06-01")
384	req.URL.RawQuery = query.Encode()
385	req.Header.Set("Accept", "application/json")
386	return req, req.MarshalAsJSON(parameters)
387}
388
389// updateHandleResponse handles the Update response.
390func (client *SSHPublicKeysClient) updateHandleResponse(resp *azcore.Response) (SSHPublicKeyResourceResponse, error) {
391	var val *SSHPublicKeyResource
392	if err := resp.UnmarshalAsJSON(&val); err != nil {
393		return SSHPublicKeyResourceResponse{}, err
394	}
395	return SSHPublicKeyResourceResponse{RawResponse: resp.Response, SSHPublicKeyResource: val}, nil
396}
397
398// updateHandleError handles the Update error response.
399func (client *SSHPublicKeysClient) updateHandleError(resp *azcore.Response) error {
400	body, err := ioutil.ReadAll(resp.Body)
401	if err != nil {
402		return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err)
403	}
404	if len(body) == 0 {
405		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
406	}
407	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
408}
409