Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ssh-keys): Update ssh-keys register, show and delete to egos… #639

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 15 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,27 @@ 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 err
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here.

}

_, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return err
}

return nil
mlec1 marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
return err
Expand Down
43 changes: 30 additions & 13 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 @@ -43,35 +42,53 @@ func (c *computeSSHKeyRegisterCmd) cmdPreRun(cmd *cobra.Command, args []string)

func (c *computeSSHKeyRegisterCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var (
sshKey *egoscale.SSHKey
sshKey *v3.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)

mlec1 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

in the function, we usually add more error wrapping the same way you did in this file

}

_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return err
}

return nil
mlec1 marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
return err
}

if !globalstate.Quiet {
sshKey, err = client.GetSSHKey(ctx, c.Name)
if err != nil {
return err
}

mlec1 marked this conversation as resolved.
Show resolved Hide resolved
return c.outputFunc(&computeSSHKeyShowOutput{
Fingerprint: *sshKey.Fingerprint,
Name: *sshKey.Name,
Fingerprint: sshKey.Fingerprint,
Name: sshKey.Name,
}, nil)
mlec1 marked this conversation as resolved.
Show resolved Hide resolved
}

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