|
8 | 8 | _ "embed" |
9 | 9 | "fmt" |
10 | 10 |
|
| 11 | + "github.com/ovh/ovhcloud-cli/internal/assets" |
11 | 12 | "github.com/ovh/ovhcloud-cli/internal/display" |
12 | 13 | filtersLib "github.com/ovh/ovhcloud-cli/internal/filters" |
13 | 14 | "github.com/ovh/ovhcloud-cli/internal/flags" |
|
21 | 22 |
|
22 | 23 | //go:embed templates/cloud_ssh_key.tmpl |
23 | 24 | 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 | + } |
24 | 35 | ) |
25 | 36 |
|
26 | 37 | func ListCloudSSHKeys(_ *cobra.Command, _ []string) { |
@@ -55,3 +66,28 @@ func GetCloudSSHKey(_ *cobra.Command, args []string) { |
55 | 66 |
|
56 | 67 | common.ManageObjectRequest(fmt.Sprintf("/v1/cloud/project/%s/sshkey", projectID), args[0], cloudSSHKeyTemplate) |
57 | 68 | } |
| 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 | +} |
0 commit comments