Skip to content

Commit

Permalink
fix(iam): set organization ID filter on listing commands (#2469)
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute authored Aug 23, 2022
1 parent 825cb7d commit 3c23c88
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
9 changes: 3 additions & 6 deletions internal/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ func ExtractCommands(ctx context.Context) *Commands {
return extractMeta(ctx).Commands
}

func GetOrganizationIDFromContext(ctx context.Context) (organizationID string) {
func GetOrganizationIDFromContext(ctx context.Context) string {
client := ExtractClient(ctx)
organizationID, exists := client.GetDefaultOrganizationID()
if !exists {
panic("no default organization ID found")
}
organizationID, _ := client.GetDefaultOrganizationID()
return organizationID
}

func GetProjectIDFromContext(ctx context.Context) (projectID string) {
func GetProjectIDFromContext(ctx context.Context) string {
client := ExtractClient(ctx)
projectID, exists := client.GetDefaultProjectID()
if !exists {
Expand Down
26 changes: 25 additions & 1 deletion internal/namespaces/iam/v1alpha1/custom.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
package iam

import "github.com/scaleway/scaleway-cli/v2/internal/core"
import (
"context"

"github.com/scaleway/scaleway-cli/v2/internal/core"
)

func GetCommands() *core.Commands {
cmds := GetGeneratedCommands()

// These commands have an "optional" organization-id that is required for now.
for _, commandPath := range [][]string{
{"iam", "group", "list"},
{"iam", "api-key", "list"},
{"iam", "ssh-key", "list"},
{"iam", "user", "list"},
{"iam", "policy", "list"},
{"iam", "application", "list"},
} {
cmds.MustFind(commandPath...).Override(setOrganizationDefaultValue)
}

return cmds
}

func setOrganizationDefaultValue(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("organization-id").Default = func(ctx context.Context) (value string, doc string) {
organizationID := core.GetOrganizationIDFromContext(ctx)
return organizationID, "<retrieved from config>"
}
return c
}

0 comments on commit 3c23c88

Please sign in to comment.