Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Added by goreleaser init:
dist/
ovhcloud
ovhcloud.wasm
1 change: 1 addition & 0 deletions doc/ovhcloud_cloud_ssh-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Manage SSH keys in the given cloud project
### SEE ALSO

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

44 changes: 44 additions & 0 deletions doc/ovhcloud_cloud_ssh-key_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## ovhcloud cloud ssh-key create

Create a new SSH key

```
ovhcloud cloud ssh-key create [flags]
```

### Options

```
--editor Use a text editor to define parameters
--from-file string File containing parameters
-h, --help help for create
--init-file string Create a file with example parameters
--name string Name for the SSH key to create
--public-key string Public key for the SSH key to create
--region string Region for the SSH key to create (optional)
--replace Replace parameters file if it already exists
```

### Options inherited from parent commands

```
--cloud-project string Cloud project ID
-d, --debug Activate debug mode (will log all HTTP requests details)
-e, --ignore-errors Ignore errors in API calls when it is not fatal to the execution
-o, --output string Output format: json, yaml, interactive, or a custom format expression (using https://github.com/PaesslerAG/gval syntax)
Examples:
--output json
--output yaml
--output interactive
--output 'id' (to extract a single field)
--output 'nested.field.subfield' (to extract a nested field)
--output '[id, "name"]' (to extract multiple fields as an array)
--output '{"newKey": oldKey, "otherKey": nested.field}' (to extract and rename fields in an object)
--output 'name+","+type' (to extract and concatenate fields in a string)
--output '(nbFieldA + nbFieldB) * 10' (to compute values from numeric fields)
```

### SEE ALSO

* [ovhcloud cloud ssh-key](ovhcloud_cloud_ssh-key.md) - Manage SSH keys in the given cloud project

15 changes: 15 additions & 0 deletions internal/cmd/cloud_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"github.com/ovh/ovhcloud-cli/internal/assets"
"github.com/ovh/ovhcloud-cli/internal/services/cloud"
"github.com/spf13/cobra"
)
Expand All @@ -31,5 +32,19 @@ func initCloudSSHKeyCommand(cloudCmd *cobra.Command) {
Args: cobra.ExactArgs(1),
})

sshKeyCreateCmd := &cobra.Command{
Use: "create",
Short: "Create a new SSH key",
Run: cloud.CreateCloudSSHKey,
}
sshKeyCreateCmd.Flags().StringVar(&cloud.SSHKeyCreationParameters.Name, "name", "", "Name for the SSH key to create")
sshKeyCreateCmd.Flags().StringVar(&cloud.SSHKeyCreationParameters.PublicKey, "public-key", "", "Public key for the SSH key to create")
sshKeyCreateCmd.Flags().StringVar(&cloud.SSHKeyCreationParameters.Region, "region", "", "Region for the SSH key to create (optional)")
addInitParameterFileFlag(sshKeyCreateCmd, assets.CloudOpenapiSchema, "/v1/cloud/project/{serviceName}/sshkey", "post", cloud.SSHKeyCreationExample, nil)
addInteractiveEditorFlag(sshKeyCreateCmd)
addFromFileFlag(sshKeyCreateCmd)
sshKeyCreateCmd.MarkFlagsMutuallyExclusive("from-file", "editor")
sshKeyCmd.AddCommand(sshKeyCreateCmd)

cloudCmd.AddCommand(sshKeyCmd)
}
36 changes: 36 additions & 0 deletions internal/services/cloud/cloud_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
Expand All @@ -21,6 +22,16 @@ var (

//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"`
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both should also have the omitempty tag so that common.CreateResource works correctly

PublicKey string `json:"publicKey"`
Region string `json:"region,omitempty"`
}
)

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

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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file will never be used if you don't set the following flags on the command: https://github.com/ovh/ovhcloud-cli/blob/main/internal/cmd/account.go#L81-L84

"name": "NewSSHKey",
"publicKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQCo9+BpMRYQ/dL3DS2CyJxRF+j6ctbT3/Qp84+KeFhnii7NT7fELilKUSnxS30WAvQCCo2yU1orfgqr41mM70MB example-key"
}