-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from netsage-project/feature/users
Adding ability to list and promote users.
- Loading branch information
Showing
9 changed files
with
164 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
kind: pipeline | ||
name: default | ||
|
||
steps: | ||
- name: build | ||
image: golang:1.16 | ||
commands: | ||
- make linux | ||
- cp conf/importer-example.yml conf/importer.yml | ||
- ./bin/grafana-dashboard-manager_linux version | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,11 @@ | |
"mode": "debug", | ||
"program": "${workspaceFolder}", | ||
"args": [ | ||
"ds", | ||
"export" | ||
//"datasources", | ||
//"list" | ||
//"-f", | ||
//"c3fjqulMz" | ||
"users", | ||
"list", | ||
//"promote", | ||
//"-u", | ||
//"[email protected]" | ||
] | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/netsage-project/sdk" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
//ListUsers list all grafana users | ||
func ListUsers(client *sdk.Client) []sdk.User { | ||
ctx := context.Background() | ||
users, err := client.GetAllUsers(ctx) | ||
if err != nil { | ||
logrus.Panic("Unable to get users") | ||
} | ||
return users | ||
} | ||
|
||
//PromoteUser promote the user to have Admin Access | ||
func PromoteUser(client *sdk.Client, userLogin string) (*sdk.StatusMessage, error) { | ||
|
||
ctx := context.Background() | ||
//Get all users | ||
users := ListUsers(client) | ||
var user *sdk.User | ||
for _, item := range users { | ||
if item.Login == userLogin { | ||
user = &item | ||
break | ||
} | ||
|
||
} | ||
|
||
if user == nil { | ||
return nil, fmt.Errorf("user: '%s' could not be found", userLogin) | ||
} | ||
|
||
role := sdk.UserPermissions{ | ||
IsGrafanaAdmin: true, | ||
} | ||
msg, err := client.UpdateUserPermissions(ctx, role, user.ID) | ||
if err != nil { | ||
errorMsg := fmt.Sprintf("failed to promote user: '%s'", userLogin) | ||
logrus.Error(errorMsg) | ||
return nil, errors.New(errorMsg) | ||
} | ||
|
||
return &msg, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/netsage-project/grafana-dashboard-manager/api" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var promoteUser = &cobra.Command{ | ||
Use: "promote", | ||
Short: "Promote User to Grafana Admin", | ||
Long: `Promote User to Grafana Admin`, | ||
Aliases: []string{"godmode"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
userLogin, _ := cmd.Flags().GetString("user") | ||
|
||
msg, err := api.PromoteUser(client, userLogin) | ||
if err != nil { | ||
logrus.Error(err.Error()) | ||
} else { | ||
logrus.Info(*msg.Message) | ||
logrus.Info("Please note user is a grafana admin, not necessarily an Org admin. You may need to promote yourself manually per org") | ||
} | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
userCmd.AddCommand(promoteUser) | ||
promoteUser.Flags().StringP("user", "u", "", "user email") | ||
promoteUser.MarkFlagRequired("user") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// userCmd represents the version command | ||
var userCmd = &cobra.Command{ | ||
Use: "users", | ||
Short: "Manage users", | ||
Long: `Manage users.`, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(userCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/jedib0t/go-pretty/table" | ||
"github.com/netsage-project/grafana-dashboard-manager/api" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var listUserCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "list users", | ||
Long: `list users`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
tableObj.AppendHeader(table.Row{"id", "login", "name", "email", "admin", "grafanaAdmin", "disabled", "authLabels"}) | ||
users := api.ListUsers(client) | ||
if len(users) == 0 { | ||
log.Info("No users found") | ||
} else { | ||
for _, user := range users { | ||
var labels string | ||
if len(user.AuthLabels) > 0 { | ||
labels = strings.Join(user.AuthLabels, ", ") | ||
|
||
} | ||
tableObj.AppendRow(table.Row{user.ID, user.Login, user.Name, user.Email, user.IsAdmin, user.IsAdmin, user.IsDisabled, labels}) | ||
} | ||
tableObj.Render() | ||
} | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
userCmd.AddCommand(listUserCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters