Skip to content

Commit

Permalink
Fixing Team Member Permissions when uploading data.
Browse files Browse the repository at this point in the history
ChangeLog:
  - Add support for admin team members.

Fixes #149
  • Loading branch information
safaci2000 committed Aug 11, 2023
1 parent d48e05d commit 51d5a8c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var downloadTeamCmd = &cobra.Command{

log.Infof("Importing Teams for context: '%s'", config.Config().AppConfig.GetContext())
filter := api.NewTeamFilter(parseTeamGlobalFlags(cmd)...)
savedFiles := grafanaSvc.ImportTeams(filter)
savedFiles := grafanaSvc.DownloadTeams(filter)
if len(savedFiles) == 0 {
log.Info("No teams found")
} else {
Expand All @@ -57,7 +57,7 @@ var uploadTeamCmd = &cobra.Command{
log.Infof("Exporting Teams for context: '%s'", config.Config().AppConfig.GetContext())
log.Warn("Currently support for import Admin members is not support, there will be 1 admin, which is the default admin user")
filter := api.NewTeamFilter(parseTeamGlobalFlags(cmd)...)
savedFiles := grafanaSvc.ExportTeams(filter)
savedFiles := grafanaSvc.UploadTeams(filter)
if len(savedFiles) == 0 {
log.Info("No teams found")
} else {
Expand Down
20 changes: 16 additions & 4 deletions internal/service/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

type TeamsApi interface {
//Team
ImportTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO
ExportTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO
DownloadTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO
UploadTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO
ListTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO
DeleteTeam(filter filters.Filter) ([]*models.TeamDTO, error)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func NewTeamFilter(entries ...string) filters.Filter {
}

// Import Teams
func (s *DashNGoImpl) ImportTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO {
func (s *DashNGoImpl) DownloadTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO {
teamListing := maps.Keys(s.ListTeams(filter))
importedTeams := make(map[*models.TeamDTO][]*models.TeamMemberDTO)
teamPath := buildResourceFolder("", config.TeamResource)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *DashNGoImpl) ImportTeams(filter filters.Filter) map[*models.TeamDTO][]*
}

// Export Teams
func (s *DashNGoImpl) ExportTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO {
func (s *DashNGoImpl) UploadTeams(filter filters.Filter) map[*models.TeamDTO][]*models.TeamMemberDTO {
filesInDir, err := s.storage.FindAllFiles(config.Config().GetDefaultGrafanaConfig().GetPath(config.TeamResource), true)
if err != nil {
log.WithError(err).Errorf("failed to list files in directory for teams")
Expand Down Expand Up @@ -276,5 +276,17 @@ func (s *DashNGoImpl) addTeamMember(team *models.TeamDTO, userDTO *models.TeamMe
log.Error(errorMsg)
return "", errors.New(errorMsg)
}
if userDTO.Permission == AdminUserPermission {
adminPatch := teams.NewUpdateTeamMemberParams()
adminPatch.TeamID = fmt.Sprintf("%d", team.ID)
adminPatch.UserID = userDTO.UserID
adminPatch.Body = &models.UpdateTeamMemberCommand{Permission: AdminUserPermission}
response, err := s.client.Teams.UpdateTeamMember(adminPatch, s.getAuth())
if err != nil {
return "", err
}
log.WithField("message", response.GetPayload().Message).Debugf("Updated permissions for user %s on team %s", userDTO.Name, team.Name)
}

return msg.GetPayload().Message, nil
}
1 change: 1 addition & 0 deletions test/data/teams/engineers/members.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"name": "supertux",
"orgId": 1,
"teamId": 5,
"permission": 4,
"userId": 2
}
]
4 changes: 2 additions & 2 deletions test/team_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestTeamCRUD(t *testing.T) {
users := apiClient.ListUsers(service.NewUserFilter(""))
assert.Equal(t, len(users), 2)
log.Info("Exporting all teams")
apiClient.ExportTeams(filter)
apiClient.UploadTeams(filter)
log.Info("Listing all Teams")
teamsMap := apiClient.ListTeams(filter)
teams := maps.Keys(teamsMap)
Expand All @@ -43,7 +43,7 @@ func TestTeamCRUD(t *testing.T) {
assert.Equal(t, musicianTeam.Name, "musicians")
//Import Teams
log.Info("Importing teams")
list := apiClient.ImportTeams(filter)
list := apiClient.DownloadTeams(filter)
assert.Equal(t, len(list), len(teams))
//CleanUp
_, err := apiClient.DeleteTeam(filter)
Expand Down

0 comments on commit 51d5a8c

Please sign in to comment.