Skip to content

Commit

Permalink
Updating updateUserRole to use slugName as well
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Mar 6, 2024
1 parent 5106742 commit 0558ed1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 80 deletions.
19 changes: 7 additions & 12 deletions cli/tools/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func newListUsers() simplecobra.Commander {
}

func newUpdateUserRoleCmd() simplecobra.Commander {
description := "updateUserRole <orgId> <userId> <role>"
description := "updateUserRole <orgSlugName> <userId> <role>"
return &support.SimpleCommand{
NameP: "updateUserRole",
Short: description,
Expand All @@ -163,17 +163,15 @@ func newUpdateUserRoleCmd() simplecobra.Commander {
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")
}
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(args[2], userId, orgId)
err = rootCmd.GrafanaSvc().UpdateUserInOrg(roleName, orgSlug, userId)
if err != nil {
slog.Error("Unable to update Org user")
} else {
Expand Down Expand Up @@ -220,18 +218,15 @@ func newDeleteUserRoleCmd() simplecobra.Commander {
Long: description,
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: [<orgId> <userId>]")
}
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>]")
}
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(userId, orgId)
err = rootCmd.GrafanaSvc().DeleteUserFromOrg(orgSlug, userId)
if err != nil {
slog.Error("Unable to remove user from Org")
} else {
Expand Down
44 changes: 22 additions & 22 deletions internal/service/mocks/GrafanaService.go

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

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

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

54 changes: 36 additions & 18 deletions internal/service/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type OrganizationsApi interface {
//Org Users
ListOrgUsers(orgId int64) []*models.OrgUserDTO
AddUserToOrg(role, orgSlug string, userId int64) error
DeleteUserFromOrg(userId, orgId int64) error
UpdateUserInOrg(role string, userId, orgId int64) error
DeleteUserFromOrg(orgId string, userId int64) error
UpdateUserInOrg(role, orgSlug string, userId int64) error
}

func NewOrganizationFilter(args ...string) filters.Filter {
Expand Down Expand Up @@ -348,41 +348,59 @@ func (s *DashNGoImpl) AddUserToOrg(role, orgSlug string, userId int64) error {
LoginOrEmail: userInfo.Login,
Role: role,
}
//Get Org
orgs, err := s.ListUserOrganizations()

orgId, err := s.getOrgIdFromSlug(orgSlug)
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
}

func (s *DashNGoImpl) DeleteUserFromOrg(userId, orgId int64) error {
func (s *DashNGoImpl) DeleteUserFromOrg(orgSlugName string, userId int64) error {
p := orgs.NewRemoveOrgUserParams()
orgId, err := s.getOrgIdFromSlug(orgSlugName)
if err != nil {
return err
}
p.OrgID = orgId
p.UserID = userId
_, err := s.GetAdminClient().Orgs.RemoveOrgUser(userId, orgId)
_, err = s.GetAdminClient().Orgs.RemoveOrgUser(userId, orgId)
return err
}

func (s *DashNGoImpl) UpdateUserInOrg(role string, userId, orgId int64) error {
func (s *DashNGoImpl) getOrgIdFromSlug(slugName string) (int64, error) {
//Get Org
organizations, err := s.ListUserOrganizations()
if err != nil {
return 0, fmt.Errorf("unable to retrieve user organizations, %w", err)
}
var orgId int64
for _, org := range organizations {
if slug.Make(org.Name) == slugName {
orgId = org.OrgID
break
}
}
if orgId == 0 {
return 0, fmt.Errorf("unable to find org with matching slug name of %s", slugName)
}
return orgId, nil
}

func (s *DashNGoImpl) UpdateUserInOrg(role, orgSlug string, userId int64) error {
p := orgs.NewUpdateOrgUserParams()
orgId, err := s.getOrgIdFromSlug(orgSlug)
if err != nil {
return err
}
p.OrgID = orgId
p.UserID = userId
p.Body = &models.UpdateOrgUserCommand{
Role: role,
}
_, err := s.GetAdminClient().Orgs.UpdateOrgUser(p)

_, err = s.GetAdminClient().Orgs.UpdateOrgUser(p)
return err
}
10 changes: 6 additions & 4 deletions test/organizations_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"github.com/esnet/gdg/internal/service"
"github.com/gosimple/slug"
"github.com/grafana/grafana-openapi-client-go/models"
"golang.org/x/exp/slices"
"os"
Expand Down Expand Up @@ -64,25 +65,26 @@ func TestOrganizationUserMembership(t *testing.T) {
break
}
}
assert.NotNil(t, orgUser)
//Reset if any state exists.
err := apiClient.DeleteUserFromOrg(orgUser.ID, newOrg.ID)
err := apiClient.DeleteUserFromOrg(slug.Make(newOrg.Name), orgUser.ID)
assert.Nil(t, err)
//Start CRUD test
orgUsers := apiClient.ListOrgUsers(newOrg.ID)
assert.Equal(t, len(orgUsers), 1)
assert.Equal(t, orgUsers[0].Login, "admin")
assert.Equal(t, orgUsers[0].Role, "Admin")

err = apiClient.AddUserToOrg("Admin", orgUser.ID, newOrg.ID)
err = apiClient.AddUserToOrg("Admin", slug.Make(newOrg.Name), orgUser.ID)
assert.Nil(t, err)
orgUsers = apiClient.ListOrgUsers(newOrg.ID)
assert.Equal(t, len(orgUsers), 2)
assert.Equal(t, orgUsers[1].Role, "Admin")
err = apiClient.UpdateUserInOrg("Viewer", orgUser.ID, newOrg.ID)
err = apiClient.UpdateUserInOrg("Viewer", slug.Make(newOrg.Name), orgUser.ID)
orgUsers = apiClient.ListOrgUsers(newOrg.ID)
assert.Nil(t, err)
assert.Equal(t, orgUsers[1].Role, "Viewer")
err = apiClient.DeleteUserFromOrg(orgUser.ID, newOrg.ID)
err = apiClient.DeleteUserFromOrg(slug.Make(newOrg.Name), orgUser.ID)
orgUsers = apiClient.ListOrgUsers(newOrg.ID)
assert.Equal(t, len(orgUsers), 1)
assert.Nil(t, err)
Expand Down

0 comments on commit 0558ed1

Please sign in to comment.