-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcloud_ssh_key.go
More file actions
93 lines (77 loc) · 2.61 KB
/
cloud_ssh_key.go
File metadata and controls
93 lines (77 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-FileCopyrightText: 2025 OVH SAS <opensource@ovh.net>
//
// SPDX-License-Identifier: Apache-2.0
package cloud
import (
_ "embed"
"fmt"
"github.com/ovh/ovhcloud-cli/internal/assets"
"github.com/ovh/ovhcloud-cli/internal/display"
filtersLib "github.com/ovh/ovhcloud-cli/internal/filters"
"github.com/ovh/ovhcloud-cli/internal/flags"
httpLib "github.com/ovh/ovhcloud-cli/internal/http"
"github.com/ovh/ovhcloud-cli/internal/services/common"
"github.com/spf13/cobra"
)
var (
cloudprojectSSHKeyColumnsToDisplay = []string{"id", "name", "regions"}
//go:embed templates/cloud_ssh_key.tmpl
cloudSSHKeyTemplate string
//go:embed parameter-samples/ssh-key-create.json
SSHKeyCreationExample string
// sshKeyCreationParameters holds the parameters for creating a new SSH key.
SSHKeyCreationParameters struct {
Name string `json:"name,omitempty"`
PublicKey string `json:"publicKey,omitempty"`
Region string `json:"region,omitempty"`
}
)
func ListCloudSSHKeys(_ *cobra.Command, _ []string) {
projectID, err := getConfiguredCloudProject()
if err != nil {
display.OutputError(&flags.OutputFormatConfig, "%s", err)
return
}
path := fmt.Sprintf("/v1/cloud/project/%s/sshkey", projectID)
var body []map[string]any
if err := httpLib.Client.Get(path, &body); err != nil {
display.OutputError(&flags.OutputFormatConfig, "failed to fetch SSH keys: %s", err)
return
}
body, err = filtersLib.FilterLines(body, flags.GenericFilters)
if err != nil {
display.OutputError(&flags.OutputFormatConfig, "failed to filter results: %s", err)
return
}
display.RenderTable(body, cloudprojectSSHKeyColumnsToDisplay, &flags.OutputFormatConfig)
}
func GetCloudSSHKey(_ *cobra.Command, args []string) {
projectID, err := getConfiguredCloudProject()
if err != nil {
display.OutputError(&flags.OutputFormatConfig, "%s", err)
return
}
common.ManageObjectRequest(fmt.Sprintf("/v1/cloud/project/%s/sshkey", projectID), args[0], cloudSSHKeyTemplate)
}
func CreateCloudSSHKey(cmd *cobra.Command, _ []string) {
projectID, err := getConfiguredCloudProject()
if err != nil {
display.OutputError(&flags.OutputFormatConfig, "%s", err)
return
}
endpoint := fmt.Sprintf("/v1/cloud/project/%s/sshkey", projectID)
_, err = common.CreateResource(
cmd,
"/v1/cloud/project/{serviceName}/sshkey",
endpoint,
SSHKeyCreationExample,
SSHKeyCreationParameters,
assets.CloudOpenapiSchema,
[]string{"name", "publicKey"},
)
if err != nil {
display.OutputError(&flags.OutputFormatConfig, "failed to create SSH key: %s", err)
return
}
display.OutputInfo(&flags.OutputFormatConfig, nil, "✅ SSH key successfully created")
}