Skip to content

Commit e9faabf

Browse files
author
Adrien Pensart
committed
feat cloud: add SSH key deletion
Signed-off-by: Adrien Pensart <adrien.pensart@corp.ovh.com>
1 parent 1fcaf43 commit e9faabf

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

doc/ovhcloud_cloud_ssh-key.md

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

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

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

internal/cmd/cloud_ssh_key.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,12 @@ func initCloudSSHKeyCommand(cloudCmd *cobra.Command) {
4646
sshKeyCreateCmd.MarkFlagsMutuallyExclusive("from-file", "editor")
4747
sshKeyCmd.AddCommand(sshKeyCreateCmd)
4848

49+
sshKeyCmd.AddCommand(&cobra.Command{
50+
Use: "delete <ssh_key_id>",
51+
Short: "Delete a SSH key",
52+
Run: cloud.DeleteCloudSSHKey,
53+
Args: cobra.ExactArgs(1),
54+
})
55+
4956
cloudCmd.AddCommand(sshKeyCmd)
5057
}

internal/services/cloud/cloud_ssh_key.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package cloud
77
import (
88
_ "embed"
99
"fmt"
10+
"net/url"
1011

1112
"github.com/ovh/ovhcloud-cli/internal/assets"
1213
"github.com/ovh/ovhcloud-cli/internal/display"
@@ -91,3 +92,20 @@ func CreateCloudSSHKey(cmd *cobra.Command, _ []string) {
9192

9293
display.OutputInfo(&flags.OutputFormatConfig, nil, "✅ SSH key successfully created")
9394
}
95+
96+
func DeleteCloudSSHKey(_ *cobra.Command, args []string) {
97+
projectID, err := getConfiguredCloudProject()
98+
if err != nil {
99+
display.OutputError(&flags.OutputFormatConfig, "%s", err)
100+
return
101+
}
102+
103+
endpoint := fmt.Sprintf("/v1/cloud/project/%s/sshkey/%s", projectID, url.PathEscape(args[0]))
104+
105+
if err := httpLib.Client.Delete(endpoint, nil); err != nil {
106+
display.OutputError(&flags.OutputFormatConfig, "error deleting SSH key %q: %s", args[0], err)
107+
return
108+
}
109+
110+
display.OutputInfo(&flags.OutputFormatConfig, nil, "✅ SSH key successfully deleted")
111+
}

0 commit comments

Comments
 (0)