Skip to content

Commit 1fcaf43

Browse files
AdrienPensartAdrien Pensart
andauthored
feat cloud: add SSH key creation (#150)
* feat cloud: add SSH key creation Signed-off-by: Adrien Pensart <adrien.pensart@corp.ovh.com> --------- Signed-off-by: Adrien Pensart <adrien.pensart@corp.ovh.com> Co-authored-by: Adrien Pensart <adrien.pensart@corp.ovh.com>
1 parent 3daa931 commit 1fcaf43

6 files changed

Lines changed: 101 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Added by goreleaser init:
22
dist/
3+
ovhcloud
34
ovhcloud.wasm

doc/ovhcloud_cloud_ssh-key.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Manage SSH keys in the given cloud project
3030
### SEE ALSO
3131

3232
* [ovhcloud cloud](ovhcloud_cloud.md) - Manage your projects and services in the Public Cloud universe (MKS, MPR, MRS, Object Storage...)
33+
* [ovhcloud cloud ssh-key create](ovhcloud_cloud_ssh-key_create.md) - Create a new SSH key
3334
* [ovhcloud cloud ssh-key get](ovhcloud_cloud_ssh-key_get.md) - Get information about a SSH key
3435
* [ovhcloud cloud ssh-key list](ovhcloud_cloud_ssh-key_list.md) - List SSH keys
3536

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## ovhcloud cloud ssh-key create
2+
3+
Create a new SSH key
4+
5+
```
6+
ovhcloud cloud ssh-key create [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
--editor Use a text editor to define parameters
13+
--from-file string File containing parameters
14+
-h, --help help for create
15+
--init-file string Create a file with example parameters
16+
--name string Name for the SSH key to create
17+
--public-key string Public key for the SSH key to create
18+
--region string Region for the SSH key to create (optional)
19+
--replace Replace parameters file if it already exists
20+
```
21+
22+
### Options inherited from parent commands
23+
24+
```
25+
--cloud-project string Cloud project ID
26+
-d, --debug Activate debug mode (will log all HTTP requests details)
27+
-e, --ignore-errors Ignore errors in API calls when it is not fatal to the execution
28+
-o, --output string Output format: json, yaml, interactive, or a custom format expression (using https://github.com/PaesslerAG/gval syntax)
29+
Examples:
30+
--output json
31+
--output yaml
32+
--output interactive
33+
--output 'id' (to extract a single field)
34+
--output 'nested.field.subfield' (to extract a nested field)
35+
--output '[id, "name"]' (to extract multiple fields as an array)
36+
--output '{"newKey": oldKey, "otherKey": nested.field}' (to extract and rename fields in an object)
37+
--output 'name+","+type' (to extract and concatenate fields in a string)
38+
--output '(nbFieldA + nbFieldB) * 10' (to compute values from numeric fields)
39+
```
40+
41+
### SEE ALSO
42+
43+
* [ovhcloud cloud ssh-key](ovhcloud_cloud_ssh-key.md) - Manage SSH keys in the given cloud project
44+

internal/cmd/cloud_ssh_key.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package cmd
66

77
import (
8+
"github.com/ovh/ovhcloud-cli/internal/assets"
89
"github.com/ovh/ovhcloud-cli/internal/services/cloud"
910
"github.com/spf13/cobra"
1011
)
@@ -31,5 +32,19 @@ func initCloudSSHKeyCommand(cloudCmd *cobra.Command) {
3132
Args: cobra.ExactArgs(1),
3233
})
3334

35+
sshKeyCreateCmd := &cobra.Command{
36+
Use: "create",
37+
Short: "Create a new SSH key",
38+
Run: cloud.CreateCloudSSHKey,
39+
}
40+
sshKeyCreateCmd.Flags().StringVar(&cloud.SSHKeyCreationParameters.Name, "name", "", "Name for the SSH key to create")
41+
sshKeyCreateCmd.Flags().StringVar(&cloud.SSHKeyCreationParameters.PublicKey, "public-key", "", "Public key for the SSH key to create")
42+
sshKeyCreateCmd.Flags().StringVar(&cloud.SSHKeyCreationParameters.Region, "region", "", "Region for the SSH key to create (optional)")
43+
addInitParameterFileFlag(sshKeyCreateCmd, assets.CloudOpenapiSchema, "/v1/cloud/project/{serviceName}/sshkey", "post", cloud.SSHKeyCreationExample, nil)
44+
addInteractiveEditorFlag(sshKeyCreateCmd)
45+
addFromFileFlag(sshKeyCreateCmd)
46+
sshKeyCreateCmd.MarkFlagsMutuallyExclusive("from-file", "editor")
47+
sshKeyCmd.AddCommand(sshKeyCreateCmd)
48+
3449
cloudCmd.AddCommand(sshKeyCmd)
3550
}

internal/services/cloud/cloud_ssh_key.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "embed"
99
"fmt"
1010

11+
"github.com/ovh/ovhcloud-cli/internal/assets"
1112
"github.com/ovh/ovhcloud-cli/internal/display"
1213
filtersLib "github.com/ovh/ovhcloud-cli/internal/filters"
1314
"github.com/ovh/ovhcloud-cli/internal/flags"
@@ -21,6 +22,16 @@ var (
2122

2223
//go:embed templates/cloud_ssh_key.tmpl
2324
cloudSSHKeyTemplate string
25+
26+
//go:embed parameter-samples/ssh-key-create.json
27+
SSHKeyCreationExample string
28+
29+
// sshKeyCreationParameters holds the parameters for creating a new SSH key.
30+
SSHKeyCreationParameters struct {
31+
Name string `json:"name,omitempty"`
32+
PublicKey string `json:"publicKey,omitempty"`
33+
Region string `json:"region,omitempty"`
34+
}
2435
)
2536

2637
func ListCloudSSHKeys(_ *cobra.Command, _ []string) {
@@ -55,3 +66,28 @@ func GetCloudSSHKey(_ *cobra.Command, args []string) {
5566

5667
common.ManageObjectRequest(fmt.Sprintf("/v1/cloud/project/%s/sshkey", projectID), args[0], cloudSSHKeyTemplate)
5768
}
69+
70+
func CreateCloudSSHKey(cmd *cobra.Command, _ []string) {
71+
projectID, err := getConfiguredCloudProject()
72+
if err != nil {
73+
display.OutputError(&flags.OutputFormatConfig, "%s", err)
74+
return
75+
}
76+
77+
endpoint := fmt.Sprintf("/v1/cloud/project/%s/sshkey", projectID)
78+
_, err = common.CreateResource(
79+
cmd,
80+
"/v1/cloud/project/{serviceName}/sshkey",
81+
endpoint,
82+
SSHKeyCreationExample,
83+
SSHKeyCreationParameters,
84+
assets.CloudOpenapiSchema,
85+
[]string{"name", "publicKey"},
86+
)
87+
if err != nil {
88+
display.OutputError(&flags.OutputFormatConfig, "failed to create SSH key: %s", err)
89+
return
90+
}
91+
92+
display.OutputInfo(&flags.OutputFormatConfig, nil, "✅ SSH key successfully created")
93+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "NewSSHKey",
3+
"publicKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQCo9+BpMRYQ/dL3DS2CyJxRF+j6ctbT3/Qp84+KeFhnii7NT7fELilKUSnxS30WAvQCCo2yU1orfgqr41mM70MB example-key"
4+
}

0 commit comments

Comments
 (0)