Skip to content

Commit

Permalink
Changing AddUserToOrg to take in a slugname instead of orgID
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Mar 6, 2024
1 parent b43568b commit 5106742
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
15 changes: 6 additions & 9 deletions cli/tools/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func newOrgCommand() simplecobra.Commander {
func newSetOrgCmd() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "set",
Short: "Set --orgId --orgName to set user Org",
Long: "Set --orgId --orgName to set user Org",
Short: "Set --orgSlugName --orgName to set user Org",
Long: "Set --orgSlugName --orgName to set user Org",
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.PersistentFlags().StringP("orgName", "o", "", "Set user Org by Name (not slug)")
cmd.PersistentFlags().StringP("orgSlugName", "", "", "Set user Org by slug name")
Expand Down Expand Up @@ -185,26 +185,23 @@ func newUpdateUserRoleCmd() simplecobra.Commander {
}

func newAddUserRoleCmd() simplecobra.Commander {
description := "addUser <orgId> <userId> <role>"
description := "addUser <orgSlugName> <userId> <role>"
return &support.SimpleCommand{
NameP: "addUser",
Short: description,
Long: description,
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: [admin, editor, viewer]")
}
orgId, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Fatal("unable to parse orgId to numeric value")
return fmt.Errorf("requires the following parameters to be specified: [<orgSlugName> <userId> <role>]\nValid roles are: [admin, editor, viewer]")
}
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("Add user to org for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"login", "orgId", "name", "email", "role"})
err = rootCmd.GrafanaSvc().AddUserToOrg(args[2], userId, orgId)
err = rootCmd.GrafanaSvc().AddUserToOrg(args[2], orgSlug, userId)
if err != nil {
slog.Error("Unable to add user to Org")
} else {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/lmittmann/tint v1.0.3
github.com/mattn/go-isatty v0.0.20
github.com/samber/lo v1.39.0
github.com/sethvargo/go-password v0.2.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand Down Expand Up @@ -136,7 +137,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sethvargo/go-password v0.2.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.11 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,6 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
22 changes: 11 additions & 11 deletions internal/service/mocks/GrafanaService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions internal/service/mocks/OrganizationsApi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions internal/service/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type OrganizationsApi interface {
InitOrganizations()
//Org Users
ListOrgUsers(orgId int64) []*models.OrgUserDTO
AddUserToOrg(role string, userId, orgId int64) error
AddUserToOrg(role, orgSlug string, userId int64) error
DeleteUserFromOrg(userId, orgId int64) error
UpdateUserInOrg(role string, userId, orgId int64) error
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (s *DashNGoImpl) ListOrgUsers(orgId int64) []*models.OrgUserDTO {
return resp.GetPayload()
}

func (s *DashNGoImpl) AddUserToOrg(role string, userId, orgId int64) error {
func (s *DashNGoImpl) AddUserToOrg(role, orgSlug string, userId int64) error {
userInfo, err := s.getUserById(userId)
if err != nil {
return fmt.Errorf("failed to retrieve user with Id: %d", userId)
Expand All @@ -348,6 +348,22 @@ func (s *DashNGoImpl) AddUserToOrg(role string, userId, orgId int64) error {
LoginOrEmail: userInfo.Login,
Role: role,
}
//Get Org
orgs, err := s.ListUserOrganizations()
if err != nil {
return fmt.Errorf("unable to retrieve user orgs, %w", err)
}
var orgId int64
for _, org := range orgs {
if slug.Make(org.Name) == orgSlug {
orgId = org.OrgID
break
}
}
if orgId == 0 {
return fmt.Errorf("unable to find a valid org with slug value of %s", orgSlug)
}

_, err = s.GetAdminClient().Orgs.AddOrgUser(orgId, request)
return err
}
Expand Down

0 comments on commit 5106742

Please sign in to comment.