Skip to content

Commit

Permalink
Cleaning up Permission based listing
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Sep 16, 2024
1 parent 5e5f2a2 commit aad4705
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
23 changes: 20 additions & 3 deletions cli/backup/connection_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"os"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
Expand Down Expand Up @@ -36,6 +37,15 @@ func newConnectionsPermissionCmd() simplecobra.Commander {
}
}

// getConnectionTbWriter returns a table object for use with newConnectionsPermissionListCmd
func getConnPermTblWriter() table.Writer {
writer := table.NewWriter()
writer.SetOutputMirror(os.Stdout)
writer.SetStyle(table.StyleLight)
writer.AppendHeader(table.Row{"id", "uid", "name", "slug", "type", "default", "url"}, table.RowConfig{AutoMerge: true})
return writer
}

func newConnectionsPermissionListCmd() simplecobra.Commander {
description := "List Connection Permissions"
return &support.SimpleCommand{
Expand All @@ -56,8 +66,14 @@ func newConnectionsPermissionListCmd() simplecobra.Commander {
slog.Info("No connections found")
} else {
for _, item := range connections {
rootCmd.TableObj.AppendRow(table.Row{item.Connection.ID, item.Connection.UID, item.Connection.Name, service.GetSlug(item.Connection.Name), item.Connection.Type, item.Connection.IsDefault, getConnectionURL(item.Connection.UID)})
writer := getConnPermTblWriter()
writer.AppendRow(table.Row{item.Connection.ID, item.Connection.UID, item.Connection.Name, service.GetSlug(item.Connection.Name), item.Connection.Type, item.Connection.IsDefault, getConnectionURL(item.Connection.UID)})
writer.Render()
if item.Permissions != nil {
twConfigs := table.NewWriter()
twConfigs.SetOutputMirror(os.Stdout)
twConfigs.SetStyle(table.StyleDouble)
twConfigs.AppendHeader(table.Row{"Connection UID", "Permission Granted", "Permission Type", "Permission Grantee"})
for _, perm := range item.Permissions {
permissionType := "BuiltinRole"
value := ""
Expand All @@ -72,11 +88,12 @@ func newConnectionsPermissionListCmd() simplecobra.Commander {
} else {
permissionType = "unsupported"
}
rootCmd.TableObj.AppendRow(table.Row{item.Connection.ID, item.Connection.UID, " PERMISSION-->", perm.Permission, permissionType, value})
twConfigs.AppendRow(table.Row{item.Connection.UID, perm.Permission, permissionType, value})
}
twConfigs.Render()
}
}
rootCmd.Render(cd.CobraCommand, connections)
// rootCmd.Render(cd.CobraCommand, connections)
}
return nil
},
Expand Down
28 changes: 23 additions & 5 deletions cli/backup/folder_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backup
import (
"context"
"log/slog"
"os"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
Expand Down Expand Up @@ -31,6 +32,15 @@ func newFolderPermissionCommand() simplecobra.Commander {
}
}

// getConnectionTbWriter returns a table object for use with newConnectionsPermissionListCmd
func getFolderPermTblWriter() table.Writer {
writer := table.NewWriter()
writer.SetOutputMirror(os.Stdout)
writer.SetStyle(table.StyleLight)
writer.AppendHeader(table.Row{"folder ID", "folderUid", "folder Name"}, table.RowConfig{AutoMerge: true})
return writer
}

func newFolderPermissionListCmd() simplecobra.Commander {
description := "list Folder Permissions"
return &support.SimpleCommand{
Expand All @@ -44,20 +54,28 @@ func newFolderPermissionListCmd() simplecobra.Commander {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}

slog.Info("Listing Folders for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"folder ID", "folderUid", "folder Name", "UserID", "Team Name", "Role", "Permission Name"}, rowConfigAutoMerge)
rootCmd.TableObj.AppendHeader(table.Row{"folderUid", "folder ID", "folder Name", "UserID", "Team Name", "Role", "Permission Name"}, rowConfigAutoMerge)
folders := rootCmd.GrafanaSvc().ListFolderPermissions(getFolderFilter())

if len(folders) == 0 {
slog.Info("No folders found")
return nil
}
for key, value := range folders {
rootCmd.TableObj.AppendRow(table.Row{key.ID, key.UID, key.Title})
for _, entry := range value {
rootCmd.TableObj.AppendRow(table.Row{"", "", " PERMISSION--->", entry.UserLogin, entry.Team, entry.Role, entry.PermissionName}, rowConfigAutoMerge)
writer := getFolderPermTblWriter()
writer.AppendRow(table.Row{key.UID, key.ID, key.Title})
writer.Render()
if len(value) > 0 {
twConfigs := table.NewWriter()
twConfigs.SetOutputMirror(os.Stdout)
twConfigs.SetStyle(table.StyleDouble)
twConfigs.AppendHeader(table.Row{"Folder UID", "UserID", "Team Name", "Role", "Permission Name"})
for _, entry := range value {
twConfigs.AppendRow(table.Row{key.UID, entry.UserLogin, entry.Team, entry.Role, entry.PermissionName})
}
twConfigs.Render()
}
}
rootCmd.Render(cd.CobraCommand, folders)
return nil
},
}
Expand Down
51 changes: 29 additions & 22 deletions website/content/docs/usage_guide/enterprise_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,35 @@ You can additionally filter by connection slug in order to only operate on a sin

{{< details "Permission Listing" >}}
```
┌────┬───────────┬───────────────────┬───────────────┬─────────────────────────────────┬────────────────────────────────┬──────────────────────────────────────────────────────────────┐
│ ID │ UID │ NAME │ SLUG │ TYPE │ DEFAULT │ URL │
├────┼───────────┼───────────────────┼───────────────┼─────────────────────────────────┼────────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ 1 │ uL86Byf4k │ Google Sheets │ google-sheets │ grafana-googlesheets-datasource │ false │ http://localhost:3000/connections/datasources/edit/uL86Byf4k │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Admin │ User │ user:admin │ │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Edit │ User │ user:tux │ │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Query │ User │ user:sa-1-test-service-account │ │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Query │ Team │ team:engineers │ │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Admin │ BuiltinRole │ builtInRole:Admin │ │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Query │ BuiltinRole │ builtInRole:Viewer │ │
│ 1 │ uL86Byf4k │ PERMISSION--> │ Query │ BuiltinRole │ builtInRole:Editor │ │
│ 3 │ rg9qPqP7z │ netsage │ netsage │ elasticsearch │ true │ http://localhost:3000/connections/datasources/edit/rg9qPqP7z │
│ 3 │ rg9qPqP7z │ PERMISSION--> │ Admin │ User │ user:admin │ │
│ 3 │ rg9qPqP7z │ PERMISSION--> │ Query │ BuiltinRole │ builtInRole:Editor │ │
│ 3 │ rg9qPqP7z │ PERMISSION--> │ Admin │ BuiltinRole │ builtInRole:Admin │ │
│ 3 │ rg9qPqP7z │ PERMISSION--> │ Query │ BuiltinRole │ builtInRole:Viewer │ │
│ 2 │ i6uqEqE7k │ Netsage TSDS │ netsage-tsds │ globalnoc-tsds-datasource │ false │ http://localhost:3000/connections/datasources/edit/i6uqEqE7k │
│ 2 │ i6uqEqE7k │ PERMISSION--> │ Admin │ User │ user:admin │ │
│ 2 │ i6uqEqE7k │ PERMISSION--> │ Query │ BuiltinRole │ builtInRole:Viewer │ │
│ 2 │ i6uqEqE7k │ PERMISSION--> │ Query │ BuiltinRole │ builtInRole:Editor │ │
│ 2 │ i6uqEqE7k │ PERMISSION--> │ Admin │ BuiltinRole │ builtInRole:Admin │ │
└────┴───────────┴───────────────────┴───────────────┴─────────────────────────────────┴────────────────────────────────┴──────────────────────────────────────────────────────────────┘
┌────┬───────────┬───────────────┬───────────────┬─────────────────────────────────┬─────────┬──────────────────────────────────────────────────────────────┐
│ ID │ UID │ NAME │ SLUG │ TYPE │ DEFAULT │ URL │
├────┼───────────┼───────────────┼───────────────┼─────────────────────────────────┼─────────┼──────────────────────────────────────────────────────────────┤
│ 1 │ uL86Byf4k │ Google Sheets │ google-sheets │ grafana-googlesheets-datasource │ false │ http://localhost:3000/connections/datasources/edit/uL86Byf4k │
└────┴───────────┴───────────────┴───────────────┴─────────────────────────────────┴─────────┴──────────────────────────────────────────────────────────────┘
╔════════════════╦════════════════════╦═════════════════╦════════════════════╗
║ CONNECTION UID ║ PERMISSION GRANTED ║ PERMISSION TYPE ║ PERMISSION GRANTEE ║
╠════════════════╬════════════════════╬═════════════════╬════════════════════╣
║ uL86Byf4k ║ Admin ║ User ║ user:admin ║
║ uL86Byf4k ║ Admin ║ User ║ user:tux ║
║ uL86Byf4k ║ Edit ║ User ║ user:bob ║
║ uL86Byf4k ║ Query ║ Team ║ team:musicians ║
║ uL86Byf4k ║ Query ║ BuiltinRole ║ builtInRole:Viewer ║
║ uL86Byf4k ║ Query ║ BuiltinRole ║ builtInRole:Editor ║
║ uL86Byf4k ║ Admin ║ BuiltinRole ║ builtInRole:Admin ║
╚════════════════╩════════════════════╩═════════════════╩════════════════════╝
┌────┬───────────┬─────────┬─────────┬───────────────┬─────────┬──────────────────────────────────────────────────────────────┐
│ ID │ UID │ NAME │ SLUG │ TYPE │ DEFAULT │ URL │
├────┼───────────┼─────────┼─────────┼───────────────┼─────────┼──────────────────────────────────────────────────────────────┤
│ 3 │ rg9qPqP7z │ netsage │ netsage │ elasticsearch │ true │ http://localhost:3000/connections/datasources/edit/rg9qPqP7z │
└────┴───────────┴─────────┴─────────┴───────────────┴─────────┴──────────────────────────────────────────────────────────────┘
╔════════════════╦════════════════════╦═════════════════╦════════════════════╗
║ CONNECTION UID ║ PERMISSION GRANTED ║ PERMISSION TYPE ║ PERMISSION GRANTEE ║
╠════════════════╬════════════════════╬═════════════════╬════════════════════╣
║ rg9qPqP7z ║ Admin ║ User ║ user:admin ║
║ rg9qPqP7z ║ Admin ║ BuiltinRole ║ builtInRole:Admin ║
║ rg9qPqP7z ║ Query ║ BuiltinRole ║ builtInRole:Viewer ║
║ rg9qPqP7z ║ Query ║ BuiltinRole ║ builtInRole:Editor ║
╚════════════════╩════════════════════╩═════════════════╩════════════════════╝
```
{{< /details >}}

Expand Down

0 comments on commit aad4705

Please sign in to comment.