-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
})) | ||
} |