Skip to content

Commit

Permalink
refactor(iam-api-key): Update IAM API Key manipulation to egoscale v3 (
Browse files Browse the repository at this point in the history
  • Loading branch information
mlec1 authored Nov 5, 2024
1 parent d5849d9 commit 21f11ca
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 88 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Improvements

- Private Network: related commands are migrated to egoscale v3
- refactor(iam-api-key): Update IAM API Key manipulation to egoscale v3 #643

### Bug fixes

Expand Down
50 changes: 17 additions & 33 deletions cmd/iam_api_key_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import (
"fmt"
"strings"

"github.com/google/uuid"
"github.com/spf13/cobra"

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

type iamAPIKeyShowOutput struct {
Name string `json:"name"`
Key string `json:"key"`
Secret string `json:"secret"`
Role string `json:"role-id"`
Role v3.UUID `json:"role-id"`
}

func (o *iamAPIKeyShowOutput) ToJSON() { output.JSON(o) }
Expand Down Expand Up @@ -54,46 +50,34 @@ func (c *iamAPIKeyCreateCmd) cmdPreRun(cmd *cobra.Command, args []string) error
}

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

if _, err := uuid.Parse(c.Role); err != nil {
roles, err := globalstate.EgoscaleClient.ListIAMRoles(ctx, zone)
if err != nil {
return err
}

for _, role := range roles {
if role.Name != nil && *role.Name == c.Role {
c.Role = *role.ID
break
}
}
ctx := gContext
client := globalstate.EgoscaleV3Client

listIAMRolesResp, err := client.ListIAMRoles(ctx)
if err != nil {
return err
}

role, err := globalstate.EgoscaleClient.GetIAMRole(ctx, zone, c.Role)
iamRole, err := listIAMRolesResp.FindIAMRole(c.Role)
if err != nil {
return err
}

apikey := &exoscale.APIKey{
Name: &c.Name,
RoleID: role.ID,
createAPIKeyReq := v3.CreateAPIKeyRequest{
Name: c.Name,
RoleID: iamRole.ID,
}

k, secret, err := globalstate.EgoscaleClient.CreateAPIKey(ctx, zone, apikey)
iamAPIKey, err := client.CreateAPIKey(ctx, createAPIKeyReq)
if err != nil {
return err
}

out := iamAPIKeyShowOutput{
Name: utils.DefaultString(k.Name, ""),
Key: utils.DefaultString(k.Key, ""),
Secret: secret,
Role: utils.DefaultString(k.RoleID, ""),
Name: iamAPIKey.Name,
Key: iamAPIKey.Key,
Secret: iamAPIKey.Secret,
Role: iamAPIKey.RoleID,
}

return c.outputFunc(&out, nil)
Expand Down
58 changes: 22 additions & 36 deletions cmd/iam_api_key_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ 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"
v3 "github.com/exoscale/egoscale/v3"
)

type iamAPIKeyDeleteCmd struct {
Expand Down Expand Up @@ -37,32 +34,17 @@ func (c *iamAPIKeyDeleteCmd) cmdPreRun(cmd *cobra.Command, args []string) error
}

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

if len(c.APIKey) == 27 && strings.HasPrefix(c.APIKey, "EX") {
_, err := globalstate.EgoscaleClient.GetAPIKey(ctx, zone, c.APIKey)
if err != nil {
return err
}
} else {
apikeys, err := globalstate.EgoscaleClient.ListAPIKeys(ctx, zone)
if err != nil {
return err
}

found := false
for _, apikey := range apikeys {
if apikey.Name != nil && *apikey.Name == c.APIKey {
c.APIKey = *apikey.Key
found = true
break
}
}
listAPIKeysResp, err := client.ListAPIKeys(ctx)
if err != nil {
return err
}

if !found {
return fmt.Errorf("key with name %q not found", c.APIKey)
}
apiKey, err := listAPIKeysResp.FindIAMAPIKey(c.APIKey)
if err != nil {
return err
}

if !c.Force {
Expand All @@ -71,15 +53,19 @@ func (c *iamAPIKeyDeleteCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
}

var err error
decorateAsyncOperation(fmt.Sprintf("Deleting API Key %s...", c.APIKey), func() {
err = globalstate.EgoscaleClient.DeleteAPIKey(ctx, zone, &egoscale.APIKey{Key: &c.APIKey})
})
if err != nil {
return err
}
return decorateAsyncOperations(fmt.Sprintf("Deleting API Key %s...", c.APIKey), func() error {
op, err := client.DeleteAPIKey(ctx, apiKey.Key)
if err != nil {
return fmt.Errorf("exoscale: error while deleting IAM API Key: %w", err)
}

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

return nil
})
}

func init() {
Expand Down
35 changes: 16 additions & 19 deletions cmd/iam_api_key_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import (

"github.com/spf13/cobra"

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

type iamAPIKeyListItemOutput struct {
Expand All @@ -35,17 +32,18 @@ func (o *iamAPIKeyListOutput) ToTable() {
})
defer t.Render()

zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
ctx := gContext
client := globalstate.EgoscaleV3Client

// For better UX we will print both role name and ID
rolesMap := map[string]string{}
iamRoles, err := globalstate.EgoscaleClient.ListIAMRoles(ctx, zone)
// If API returns error, can continue (print name only) as this is non-essential feature

// For better UX we will print both role name and ID
listIAMRolesResp, err := client.ListIAMRoles(ctx)
// If API returns error, can continue (print UUID only) as this is non-essential feature
if err == nil {
for _, role := range iamRoles {
if role.ID != nil && role.Name != nil {
rolesMap[*role.ID] = *role.Name
for _, role := range listIAMRolesResp.IAMRoles {
if role.ID.String() != "" && role.Name != "" {
rolesMap[role.ID.String()] = role.Name
}
}
}
Expand Down Expand Up @@ -86,22 +84,21 @@ func (c *iamAPIKeyListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
}

func (c *iamAPIKeyListCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := account.CurrentAccount.DefaultZone

ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
ctx := gContext
client := globalstate.EgoscaleV3Client

apikeys, err := globalstate.EgoscaleClient.ListAPIKeys(ctx, zone)
listAPIKeysResp, err := client.ListAPIKeys(ctx)
if err != nil {
return err
}

out := make(iamAPIKeyListOutput, 0)

for _, apikey := range apikeys {
for _, apikey := range listAPIKeysResp.APIKeys {
out = append(out, iamAPIKeyListItemOutput{
Name: utils.DefaultString(apikey.Name, ""),
Key: utils.DefaultString(apikey.Key, ""),
Role: utils.DefaultString(apikey.RoleID, ""),
Name: apikey.Name,
Key: apikey.Key,
Role: apikey.RoleID.String(),
})
}

Expand Down

0 comments on commit 21f11ca

Please sign in to comment.