Skip to content

Commit

Permalink
refactor(ssh-keys): Update ssh-keys register, show and delete to egos… (
Browse files Browse the repository at this point in the history
  • Loading branch information
mlec1 authored Nov 4, 2024
1 parent 226b5a4 commit f0aea5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
24 changes: 14 additions & 10 deletions cmd/ssh_key_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
v3 "github.com/exoscale/egoscale/v3"
)

type computeSSHKeyDeleteCmd struct {
Expand All @@ -34,20 +32,26 @@ func (c *computeSSHKeyDeleteCmd) cmdPreRun(cmd *cobra.Command, args []string) er
}

func (c *computeSSHKeyDeleteCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
ctx := gContext

if !c.Force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete SSH key %s?", c.Name)) {
return nil
}
}

var err error
decorateAsyncOperation(fmt.Sprintf("Deleting SSH key %s...", c.Name), func() {
err = globalstate.EgoscaleClient.DeleteSSHKey(ctx, account.CurrentAccount.DefaultZone, &egoscale.SSHKey{Name: &c.Name})
err := decorateAsyncOperations(fmt.Sprintf("Deleting SSH key %s...", c.Name), func() error {
op, err := globalstate.EgoscaleV3Client.DeleteSSHKey(ctx, c.Name)
if err != nil {
return fmt.Errorf("exoscale: error while deleting SSH key: %w", err)
}

_, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return fmt.Errorf("exoscale: error while waiting for SSH key deletion: %w", err)
}

return nil
})
if err != nil {
return err
Expand Down
44 changes: 25 additions & 19 deletions cmd/ssh_key_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package cmd

import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
v3 "github.com/exoscale/egoscale/v3"
)

type computeSSHKeyRegisterCmd struct {
Expand Down Expand Up @@ -42,37 +41,44 @@ func (c *computeSSHKeyRegisterCmd) cmdPreRun(cmd *cobra.Command, args []string)
}

func (c *computeSSHKeyRegisterCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var (
sshKey *egoscale.SSHKey
err error
)

// Template registration can take a _long time_, raising
// the Exoscale API client timeout as a precaution.
globalstate.EgoscaleClient.SetTimeout(30 * time.Minute)
client := globalstate.EgoscaleV3Client.WithHttpClient(&http.Client{Timeout: 30 * time.Minute})

ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
ctx := gContext

publicKey, err := os.ReadFile(c.PublicKeyFile)
if err != nil {
return err
}

decorateAsyncOperation(fmt.Sprintf("Registering SSH key %q...", c.Name), func() {
sshKey, err = globalstate.EgoscaleClient.RegisterSSHKey(ctx, account.CurrentAccount.DefaultZone, c.Name, string(publicKey))
registerKeyRequest := v3.RegisterSSHKeyRequest{
Name: c.Name,
PublicKey: string(publicKey),
}

err = decorateAsyncOperations(fmt.Sprintf("Registering SSH key %q...", c.Name), func() error {
op, err := client.RegisterSSHKey(ctx, registerKeyRequest)
if err != nil {
return fmt.Errorf("exoscale: error while registering SSH key: %w", err)
}

_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return fmt.Errorf("exoscale: error while waiting for SSH key registration: %w", err)
}

return nil
})
if err != nil {
return err
}

if !globalstate.Quiet {
return c.outputFunc(&computeSSHKeyShowOutput{
Fingerprint: *sshKey.Fingerprint,
Name: *sshKey.Name,
}, nil)
return (&computeSSHKeyShowCmd{
cliCommandSettings: c.cliCommandSettings,
Key: c.Name,
}).cmdRun(nil, nil)
}

return nil
Expand Down
13 changes: 4 additions & 9 deletions cmd/ssh_key_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
exoapi "github.com/exoscale/egoscale/v2/api"
)

type computeSSHKeyShowOutput struct {
Expand Down Expand Up @@ -48,19 +46,16 @@ func (c *computeSSHKeyShowCmd) cmdPreRun(cmd *cobra.Command, args []string) erro
}

func (c *computeSSHKeyShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
ctx := gContext

sshKey, err := globalstate.EgoscaleClient.GetSSHKey(ctx, account.CurrentAccount.DefaultZone, c.Key)
sshKey, err := globalstate.EgoscaleV3Client.GetSSHKey(ctx, c.Key)
if err != nil {
return err
}

return c.outputFunc(&computeSSHKeyShowOutput{
Name: *sshKey.Name,
Fingerprint: *sshKey.Fingerprint,
Name: sshKey.Name,
Fingerprint: sshKey.Fingerprint,
}, nil)
}

Expand Down

0 comments on commit f0aea5f

Please sign in to comment.