Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving User commands of tools->orgs to tools->orgs->users #293

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cli/tools/auth_service_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package tools
import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"slices"
"sort"
"strconv"
"strings"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
Expand Down Expand Up @@ -148,7 +149,7 @@ func newServiceAccount() simplecobra.Commander {
CommandsList: []simplecobra.Commander{},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
if len(args) < 2 {
return errors.New("requires a key name and a role('admin','viewer','editor') [ttl optional] ")
return fmt.Errorf("requires a key name and a role(%s) [ttl optional]", strings.Join(getBasicRoles(), ", "))
}
name := args[0]
role := args[1]
Expand All @@ -166,8 +167,8 @@ func newServiceAccount() simplecobra.Commander {
expiration = 0
}

if !slices.Contains([]string{"admin", "editor", "viewer"}, role) {
log.Fatal("Invalid role specified")
if !validBasicRole(role) {
log.Fatalf("Invalid role specified, '%s'. Valid roles are:[%s]", role, strings.Join(getBasicRoles(), ", "))
}
serviceAcct, err := rootCmd.GrafanaSvc().CreateServiceAccount(name, role, expiration)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cli/tools/auth_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package tools

import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"slices"
"sort"
"strconv"
"strings"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
Expand Down Expand Up @@ -101,7 +101,7 @@ func newNewTokenCmd() simplecobra.Commander {
CommandsList: []simplecobra.Commander{},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
if len(args) < 2 {
return errors.New("requires a key name and a role('admin','viewer','editor') [ttl optional] ")
return fmt.Errorf("requires a key name and a role(%s) [ttl optional] ", strings.Join(getBasicRoles(), ", "))
}
name := args[0]
role := args[1]
Expand All @@ -119,8 +119,8 @@ func newNewTokenCmd() simplecobra.Commander {
expiration = 0
}

if !slices.Contains([]string{"admin", "editor", "viewer"}, role) {
log.Fatal("Invalid role specified")
if !validBasicRole(role) {
log.Fatalf("Invalid role specified, '%s'. Valid roles are:[%s]", role, strings.Join(getBasicRoles(), ", "))
}
key, err := rootCmd.GrafanaSvc().CreateAPIKey(name, role, expiration)
if err != nil {
Expand Down
192 changes: 192 additions & 0 deletions cli/tools/org_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package tools

import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"strconv"
"strings"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)

func newOrgUsersCommand() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "users",
Short: "Manage organization users",
Long: "Manager organization users",
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
return cd.CobraCommand.Help()
},
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"user"}
},
CommandsList: []simplecobra.Commander{
newListUsers(),
newAddUserRoleCmd(),
newDeleteUserRoleCmd(),
newGetUserOrgCmd(),
newUpdateUserRoleCmd(),
},
}
}

func newGetUserOrgCmd() simplecobra.Commander {
description := "display org associated with user"
return &support.SimpleCommand{
NameP: "currentOrg",
Short: description,
Long: description,
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Listing organizations for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "name"})
org := rootCmd.GrafanaSvc().GetUserOrganization()
if org == nil {
slog.Info("No organizations found")
} else {
rootCmd.TableObj.AppendRow(table.Row{org.ID, org.Name})
rootCmd.Render(cd.CobraCommand, map[string]interface{}{"id": org.ID, "name": org.Name})
}
return nil
},
}
}

func newListUsers() simplecobra.Commander {
description := "list <orgId> list an Organization users"
return &support.SimpleCommand{
NameP: "list",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"listUsers"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
if len(args) < 1 {
return errors.New("requires an orgId to be specified")
}
orgId, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Fatal("unable to parse orgId to numeric value")
}
slog.Info("Listing org users for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "login", "orgId", "name", "email", "role"})
users := rootCmd.GrafanaSvc().ListOrgUsers(orgId)
if len(users) == 0 {
slog.Info("No users found")
} else {
for _, user := range users {
rootCmd.TableObj.AppendRow(table.Row{user.UserID, user.Login, user.OrgID, user.Name, user.Email, user.Role})
}
rootCmd.Render(cd.CobraCommand, users)
}
return nil
},
}
}

func newUpdateUserRoleCmd() simplecobra.Commander {
description := "updateRole <orgSlugName> <userId> <role>"
return &support.SimpleCommand{
NameP: "updateRole",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"updateUserRole"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
if len(args) < 3 {
return fmt.Errorf("requires the following parameters to be specified: [<orgId> <userId> <role>]\nValid roles are: [%s]", strings.Join(getBasicRoles(), ", "))
}
orgSlug := args[0]
roleName := args[2]
userId, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
log.Fatal("unable to parse userId to numeric value")
}
slog.Info("Listing org users for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"login", "orgId", "name", "email", "role"})
err = rootCmd.GrafanaSvc().UpdateUserInOrg(roleName, orgSlug, userId)
if err != nil {
slog.Error("Unable to update Org user")
} else {
slog.Info("User has been updated")
}
return nil
},
}
}

func newAddUserRoleCmd() simplecobra.Commander {
description := "add <orgSlugName> <userId> <role>"
return &support.SimpleCommand{
NameP: "add",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"addUser", "addUsers"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
if len(args) < 3 {
return fmt.Errorf("requires the following parameters to be specified: [<orgSlugName> <userId> <role>]\nValid roles are: [%s]", strings.Join(getBasicRoles(), ", "))
}
orgSlug := args[0]
userId, err := strconv.ParseInt(args[1], 10, 64)
role := args[2]
if err != nil {
log.Fatal("unable to parse userId to numeric value")
}
slog.Info("Add user to org for context",
slog.Any("context", config.Config().GetGDGConfig().GetContext()),
slog.Any("organization", config.Config().GetDefaultGrafanaConfig().OrganizationName),
)
if !validBasicRole(role) {
log.Fatalf("Invalid role specified, '%s'. Valid roles are:[%s]", role, strings.Join(getBasicRoles(), ", "))
}
rootCmd.TableObj.AppendHeader(table.Row{"login", "orgId", "name", "email", "role"})
err = rootCmd.GrafanaSvc().AddUserToOrg(role, orgSlug, userId)
if err != nil {
slog.Error("Unable to add user to Org", slog.Any("err", err.Error()))
} else {
slog.Info("User has been add to Org", slog.Any("userId", userId), slog.String("organization", orgSlug))
}
return nil
},
}
}

func newDeleteUserRoleCmd() simplecobra.Commander {
description := "deleteUser <orgSlug> <userId> removes a user from the given Organization (This will NOT delete the actual user from Grafana)"
return &support.SimpleCommand{
NameP: "delete",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"deleteUser", "remove"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
if len(args) < 2 {
return fmt.Errorf("requires the following parameters to be specified: [<orgSlugName> <userId>]")
}
orgSlug := args[0]
userId, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
log.Fatal("unable to parse userId to numeric value")
}
slog.Info("Update org for context", "context", config.Config().GetGDGConfig().GetContext())
err = rootCmd.GrafanaSvc().DeleteUserFromOrg(orgSlug, userId)
if err != nil {
slog.Error("Unable to remove user from Org", slog.Any("err", err.Error()))
} else {
slog.Info("User has been removed from Org", "userId", orgSlug)
}
return nil
},
}
}
Loading
Loading