Skip to content

Commit

Permalink
Implement API Key delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
kobajagi committed Nov 21, 2023
1 parent 4f6d2dc commit 784c6cf
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions cmd/iam_api_key_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"fmt"
"strings"

"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"
)

type iamAPIKeyDeleteCmd struct {
cliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"delete"`

APIKey string `cli-arg:"#" cli-usage:"NAME|KEY"`

Force bool `cli-short:"f" cli-usage:"don't prompt for confirmation"`
}

func (c *iamAPIKeyDeleteCmd) cmdAliases() []string { return gDeleteAlias }

func (c *iamAPIKeyDeleteCmd) cmdShort() string {
return "Delete API Key"
}

func (c *iamAPIKeyDeleteCmd) cmdLong() string {
return `This command deletes existing API Key.`
}

func (c *iamAPIKeyDeleteCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}

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

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

var err error
decorateAsyncOperation(fmt.Sprintf("Deleting API Key %s...", c.APIKey), func() {
if len(c.APIKey) != 27 || !strings.HasPrefix(c.APIKey, "EX") {
apikeys, err := globalstate.EgoscaleClient.ListAPIKeys(ctx, zone)
if err != nil {
return
}

for _, apikey := range apikeys {
if apikey.Name != nil && *apikey.Name == c.APIKey {
c.APIKey = *apikey.Key
break
}
}
}

err = globalstate.EgoscaleClient.DeleteAPIKey(ctx, zone, &egoscale.APIKey{Key: &c.APIKey})
})
if err != nil {
return err
}

return nil
}

func init() {
cobra.CheckErr(registerCLICommand(iamAPIKeyCmd, &iamAPIKeyDeleteCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}

0 comments on commit 784c6cf

Please sign in to comment.