Skip to content

Commit

Permalink
feat: add detele/list stack user access roles
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ragot committed Dec 18, 2023
1 parent a3e1043 commit 70a83b2
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 106 deletions.
2 changes: 0 additions & 2 deletions components/fctl/cmd/cloud/organizations/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package organizations

import (
"github.com/formancehq/fctl/cmd/cloud/organizations/invitations"
"github.com/formancehq/fctl/cmd/cloud/organizations/stacks"
"github.com/formancehq/fctl/cmd/cloud/organizations/users"
fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
Expand All @@ -18,7 +17,6 @@ func NewCommand() *cobra.Command {
NewDeleteCommand(),
users.NewCommand(),
invitations.NewCommand(),
stacks.NewCommand(),
),
)
}

This file was deleted.

10 changes: 6 additions & 4 deletions components/fctl/cmd/cloud/organizations/users/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package users

import (
"strings"

"github.com/formancehq/fctl/membershipclient"
fctl "github.com/formancehq/fctl/pkg"
"github.com/pterm/pterm"
Expand Down Expand Up @@ -83,17 +85,17 @@ func (c *ListController) Render(cmd *cobra.Command, args []string) error {
i.ID,
i.Email,
func() string {
roles := ""
roles := []string{}

for _, role := range i.Roles {
if role == "ADMIN" {
roles += pterm.LightRed(role) + " | "
roles = append(roles, pterm.LightRed(role))
} else {
roles += pterm.LightGreen(role) + " | "
roles = append(roles, pterm.LightGreen(role))
}
}

return roles
return strings.Join(roles, " | ")
}(),
}
})
Expand Down
80 changes: 80 additions & 0 deletions components/fctl/cmd/stack/roles/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package roles

import (
"github.com/formancehq/fctl/membershipclient"
fctl "github.com/formancehq/fctl/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type DeletedUserAccessStore struct {
Stack *membershipclient.Stack `json:"stack"`
Status string `json:"status"`
}
type StackDeleteController struct {
store *DeletedUserAccessStore
}

var _ fctl.Controller[*DeletedUserAccessStore] = (*StackDeleteController)(nil)

func NewDefaultDeletedUserAccessStore() *DeletedUserAccessStore {
return &DeletedUserAccessStore{
Stack: &membershipclient.Stack{},
Status: "",
}
}

func NewStackDeleteController() *StackDeleteController {
return &StackDeleteController{
store: NewDefaultDeletedUserAccessStore(),
}
}

func NewDeleteCommand() *cobra.Command {
const (
stackNameFlag = "name"
)
return fctl.NewMembershipCommand("delete <stack-id> <user-id>",
fctl.WithConfirmFlag(),
fctl.WithShortDescription("Delete user access from a stack within an organization"),
fctl.WithAliases("del", "d"),
fctl.WithArgs(cobra.ExactArgs(2)),
fctl.WithController[*DeletedUserAccessStore](NewStackDeleteController()),
)
}
func (c *StackDeleteController) GetStore() *DeletedUserAccessStore {
return c.store
}

func (c *StackDeleteController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

apiClient, err := fctl.NewMembershipClient(cmd, cfg)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

res, err := apiClient.DefaultApi.DeleteStackUserAccess(cmd.Context(), organizationID, args[0], args[1]).Execute()
if err != nil {
return nil, err
}

if res.StatusCode > 300 {
return nil, err
}

return c, nil
}

func (c *StackDeleteController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Stack user access deleted.")
return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stacks
package roles

import (
"strings"
Expand All @@ -9,41 +9,41 @@ import (
"github.com/spf13/cobra"
)

type ListStackAccessRolesStore struct {
type ListStore struct {
list []membershipclient.StackUserAccess
}
type ListStackAccessRolesController struct {
store *ListStackAccessRolesStore
type ListController struct {
store *ListStore
}

var _ fctl.Controller[*ListStackAccessRolesStore] = (*ListStackAccessRolesController)(nil)
var _ fctl.Controller[*ListStore] = (*ListController)(nil)

func NewDefaultListStackAccessRolesStore() *ListStackAccessRolesStore {
return &ListStackAccessRolesStore{
func NewDefaultListStore() *ListStore {
return &ListStore{
list: []membershipclient.StackUserAccess{},
}
}

func NewListStackAccessRolesController() *ListStackAccessRolesController {
return &ListStackAccessRolesController{
store: NewDefaultListStackAccessRolesStore(),
func NewListController() *ListController {
return &ListController{
store: NewDefaultListStore(),
}
}

func NewListStackAccessRolesCommand() *cobra.Command {
return fctl.NewCommand("list-access-roles <stack-id>",
func NewListCommand() *cobra.Command {
return fctl.NewCommand("list <stack-id>",
fctl.WithAliases("usar"),
fctl.WithShortDescription("List Stack Access Roles within an organization by stacks"),
fctl.WithArgs(cobra.MinimumNArgs(1)),
fctl.WithController[*ListStackAccessRolesStore](NewListStackAccessRolesController()),
fctl.WithController[*ListStore](NewListController()),
)
}

func (c *ListStackAccessRolesController) GetStore() *ListStackAccessRolesStore {
func (c *ListController) GetStore() *ListStore {
return c.store
}

func (c *ListStackAccessRolesController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

cfg, err := fctl.GetConfig(cmd)
if err != nil {
Expand All @@ -69,21 +69,33 @@ func (c *ListStackAccessRolesController) Run(cmd *cobra.Command, args []string)
return nil, err
}

c.store.list = append(c.store.list, ListStackUsersAccesses...)
c.store.list = append(c.store.list, ListStackUsersAccesses.Data...)

return c, nil
}

func (c *ListStackAccessRolesController) Render(cmd *cobra.Command, args []string) error {
func (c *ListController) Render(cmd *cobra.Command, args []string) error {
stackUserAccessMap := fctl.Map(c.store.list, func(o membershipclient.StackUserAccess) []string {
return []string{
o.StackId,
o.UserId,
strings.Join(o.Roles, "|"),
func() string {
roles := []string{}

for _, role := range o.Roles {
if role == "ADMIN" {
roles = append(roles, pterm.LightRed(role))
} else {
roles = append(roles, pterm.LightGreen(role))
}
}

return strings.Join(roles, " | ")
}(),
}
})

tableData := fctl.Prepend(stackUserAccessMap, []string{"StackId", "UserId", "Roles"})
tableData := fctl.Prepend(stackUserAccessMap, []string{"Stack Id", "User Id", "Roles"})

return pterm.DefaultTable.WithHasHeader().WithWriter(cmd.OutOrStdout()).WithData(tableData).Render()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package stacks
package roles

import (
fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
return fctl.NewMembershipCommand("stacks",
return fctl.NewMembershipCommand("roles",
fctl.WithAliases("s"),
fctl.WithShortDescription("Stack users management within an organization"),
fctl.WithChildCommands(
NewUpsertStackAccessRolesCommand(),
NewListStackAccessRolesCommand(),
NewUpsertCommand(),
NewListCommand(),
NewDeleteCommand(),
),
)
}
Loading

0 comments on commit 70a83b2

Please sign in to comment.