Skip to content

Commit

Permalink
[FEATURE] Nested Folder support
Browse files Browse the repository at this point in the history
ChangeLog:
 - Updating documentation capturing changelog
 - nested-folders affects dashboards, folders, and folder permissions
 - Add support for wildcards on dashboard folder whitelist

TechDebt:
 - Updating to allow custom grafana ENV overrides to be passed from test
 - Removed go-funk library in favor of maps, slices and samber/lo
 - Adding gofumpt over gofmt
 - Adding Folder Permissions test
  • Loading branch information
safaci2000 committed Sep 11, 2024
1 parent 9b56e13 commit c21a152
Show file tree
Hide file tree
Showing 113 changed files with 35,281 additions and 802 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.23.0"

- name: Verify go version
run: go version
- name: Install GoTest
run: go install gotest.tools/gotestsum@latest
- name: Test
env:
ENTERPRISE_LICENSE: ${{ secrets.ENTERPRISE_LICENSE }}
run: go test -v ./...
run: gotestsum --junitfile report.xml --format testname
# - go test -v ./...

6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ tasks:
- go install github.com/vektra/mockery/v2@{{ .MOCKERY_VERSION}}
- go install github.com/goreleaser/goreleaser@{{ .RELEASER_VERSION }}
- go install github.com/evilmartians/lefthook@{{ .LEFTHOOK_VERSION }}
- go install mvdan.cc/[email protected]

- lefthook install

security:
desc: "Run security scan"
cmds:
- gosec --exclude=G402,G304 ./...
format:
desc: reformat the code to match stricter conventions
cmds:
- gofumpt -l -w .
lint:
desc: "Lint project, skipping test files."
cmds:
Expand Down
2 changes: 1 addition & 1 deletion cli/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backup

import (
"context"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
Expand Down Expand Up @@ -36,7 +37,6 @@ limited to clear/delete, list, download and upload. Any other functionality wil
newUsersCommand(),
},
}

}

// GetOrganizationName wrapper for verbose version below.
Expand Down
5 changes: 4 additions & 1 deletion cli/backup/connection_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package backup
import (
"context"
"fmt"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/tools"
"github.com/jedib0t/go-pretty/v6/table"
"log/slog"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -81,6 +82,7 @@ func newConnectionsPermissionListCmd() simplecobra.Commander {
},
}
}

func newConnectionsPermissionClearCmd() simplecobra.Commander {
description := "Clear Connection Permissions"
return &support.SimpleCommand{
Expand Down Expand Up @@ -144,6 +146,7 @@ func newConnectionsPermissionDownloadCmd() simplecobra.Commander {
},
}
}

func newConnectionsPermissionUploadCmd() simplecobra.Commander {
description := "Upload Connection Permissions"
return &support.SimpleCommand{
Expand Down
4 changes: 3 additions & 1 deletion cli/backup/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package backup
import (
"context"
"fmt"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
"log/slog"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -110,6 +111,7 @@ func newDownloadConnectionsCmd() simplecobra.Commander {
},
}
}

func newListConnectionsCmd() simplecobra.Commander {
description := "List all connections for the given Organization"
return &support.SimpleCommand{
Expand Down
35 changes: 26 additions & 9 deletions cli/backup/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,24 @@ func newListDashboardsCmd() simplecobra.Commander {
cmd.Aliases = []string{"l"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
rootCmd.TableObj.AppendHeader(table.Row{"id", "Title", "Slug", "Folder", "UID", "Tags", "URL"})
cfg := config.Config().GetDefaultGrafanaConfig()
if cfg.GetDashboardSettings().NestedFolders {
rootCmd.TableObj.AppendHeader(table.Row{"id", "Title", "Slug", "Folder", "NestedPath", "UID", "Tags", "URL"})
} else {
rootCmd.TableObj.AppendHeader(table.Row{"id", "Title", "Slug", "Folder", "UID", "Tags", "URL"})
}

filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
boards := rootCmd.GrafanaSvc().ListDashboards(filters)

slog.Info("Listing dashboards for context",
slog.String("context", GetContext()),
slog.String("orgName", GetOrganizationName()),
slog.Any("count", len(boards)))
printCount := func(count int) {
slog.Info("Listing dashboards for context",
slog.String("context", GetContext()),
slog.String("orgName", GetOrganizationName()),
slog.Any("count", count))
}
count := 0

for _, link := range boards {
base, err := url.Parse(config.Config().GetDefaultGrafanaConfig().URL)
var baseHost string
Expand All @@ -181,6 +190,11 @@ func newListDashboardsCmd() simplecobra.Commander {
base.Path = ""
baseHost = base.String()
}
if string(link.Type) == service.SearchTypeFolder {
slog.Debug("skipping entry for", slog.Any("slug", link.Slug), slog.Any("url", fmt.Sprintf("%s/%s", baseHost, link.UID)), slog.Any("type", link.Type))
continue
}
count++
urlValue := fmt.Sprintf("%s%s", baseHost, link.URL)
var tagVal string
if len(link.Tags) > 0 {
Expand All @@ -190,12 +204,15 @@ func newListDashboardsCmd() simplecobra.Commander {
}
}

rootCmd.TableObj.AppendRow(table.Row{
link.ID, link.Title, link.Slug, link.FolderTitle,
link.UID, tagVal, urlValue,
})
baseRow := table.Row{link.ID, link.Title, link.Slug, link.FolderTitle}
if cfg.GetDashboardSettings().NestedFolders {
baseRow = append(baseRow, service.GetNestedFolder(link.FolderTitle, link.FolderUID, rootCmd.GrafanaSvc()))
}
baseRow = append(baseRow, table.Row{link.UID, tagVal, urlValue}...)
rootCmd.TableObj.AppendRow(baseRow)

}
printCount(count)
if len(boards) > 0 {
rootCmd.Render(cd.CobraCommand, boards)
} else {
Expand Down
5 changes: 4 additions & 1 deletion cli/backup/folder_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package backup

import (
"context"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"log/slog"
)

func newFolderPermissionCommand() simplecobra.Commander {
Expand Down Expand Up @@ -61,6 +62,7 @@ func newFolderPermissionListCmd() simplecobra.Commander {
},
}
}

func newFolderPermissionDownloadCmd() simplecobra.Commander {
description := "download Folders Permissions"
return &support.SimpleCommand{
Expand Down Expand Up @@ -88,6 +90,7 @@ func newFolderPermissionDownloadCmd() simplecobra.Commander {
},
}
}

func newFolderPermissionUploadCmd() simplecobra.Commander {
description := "upload Folders Permissions"
return &support.SimpleCommand{
Expand Down
25 changes: 18 additions & 7 deletions cli/backup/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package backup

import (
"context"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/filters"
"github.com/jedib0t/go-pretty/v6/table"
"log/slog"

"github.com/spf13/cobra"
)
Expand All @@ -20,7 +21,6 @@ func getFolderFilter() filters.Filter {
return nil
}
return service.NewFolderFilter()

}

func newFolderCommand() simplecobra.Commander {
Expand All @@ -30,7 +30,7 @@ func newFolderCommand() simplecobra.Commander {
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"fld", "folder"}
cmd.Aliases = []string{"fld", "folder", "f"}
cmd.PersistentFlags().BoolVar(&useFolderFilters, "use-filters", false, "Default to false, but if passed then will only operate on the list of folders listed in the configuration file")
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
Expand All @@ -44,7 +44,6 @@ func newFolderCommand() simplecobra.Commander {
newFolderUploadCmd(),
},
}

}

func newFolderClearCmd() simplecobra.Commander {
Expand Down Expand Up @@ -85,21 +84,32 @@ func newFolderListCmd() simplecobra.Commander {
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Listing Folders for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "uid", "title"})
folders := rootCmd.GrafanaSvc().ListFolder(getFolderFilter())
cfg := config.Config().GetDefaultGrafanaConfig()
if cfg.GetDashboardSettings().NestedFolders {
rootCmd.TableObj.AppendHeader(table.Row{"uid", "title", "nestedPath"})
} else {
rootCmd.TableObj.AppendHeader(table.Row{"uid", "title"})
}
folders := rootCmd.GrafanaSvc().ListFolders(getFolderFilter())

if len(folders) == 0 {
slog.Info("No folders found")
} else {
for _, folder := range folders {
rootCmd.TableObj.AppendRow(table.Row{folder.ID, folder.UID, folder.Title})
row := table.Row{folder.UID, folder.Title}
if cfg.GetDashboardSettings().NestedFolders {
row = append(row, folder.NestedPath)
}

rootCmd.TableObj.AppendRow(row)
}
rootCmd.Render(cd.CobraCommand, folders)
}
return nil
},
}
}

func newFolderDownloadCmd() simplecobra.Commander {
description := "Download Folders from grafana"
return &support.SimpleCommand{
Expand All @@ -125,6 +135,7 @@ func newFolderDownloadCmd() simplecobra.Commander {
},
}
}

func newFolderUploadCmd() simplecobra.Commander {
description := "upload Folders to grafana"
return &support.SimpleCommand{
Expand Down
21 changes: 11 additions & 10 deletions cli/backup/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package backup

import (
"context"
"log"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service/filters"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"log"
"log/slog"
)

func newLibraryElementsCommand() simplecobra.Commander {
Expand Down Expand Up @@ -44,23 +45,22 @@ func newLibraryElementsClearCmd() simplecobra.Commander {
cmd.Aliases = []string{"c"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
//filter := getLibraryGlobalFlags(cli)
deletedLibrarys := rootCmd.GrafanaSvc().DeleteAllLibraryElements(nil)
deletedLibraries := rootCmd.GrafanaSvc().DeleteAllLibraryElements(nil)
rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})
for _, file := range deletedLibrarys {
for _, file := range deletedLibraries {
rootCmd.TableObj.AppendRow(table.Row{"library", file})
}
if len(deletedLibrarys) == 0 {
if len(deletedLibraries) == 0 {
slog.Info("No library were found. 0 libraries removed")

} else {
slog.Info("libraries were deleted", "count", len(deletedLibrarys))
rootCmd.Render(cd.CobraCommand, deletedLibrarys)
slog.Info("libraries were deleted", "count", len(deletedLibraries))
rootCmd.Render(cd.CobraCommand, deletedLibraries)
}
return nil
},
}
}

func newLibraryElementsListCmd() simplecobra.Commander {
description := "List all library Elements"
return &support.SimpleCommand{
Expand All @@ -78,7 +78,6 @@ func newLibraryElementsListCmd() simplecobra.Commander {
slog.Info("Listing library for context", "context", config.Config().GetGDGConfig().GetContext())
for _, link := range elements {
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Meta.FolderName, link.Name, link.Type})

}
if len(elements) > 0 {
rootCmd.Render(cd.CobraCommand, elements)
Expand All @@ -90,6 +89,7 @@ func newLibraryElementsListCmd() simplecobra.Commander {
},
}
}

func newLibraryElementsDownloadCmd() simplecobra.Commander {
description := "Download all library from grafana to local file system"
return &support.SimpleCommand{
Expand All @@ -111,6 +111,7 @@ func newLibraryElementsDownloadCmd() simplecobra.Commander {
},
}
}

func newLibraryElementsUploadCmd() simplecobra.Commander {
description := "upload all library to grafana"
return &support.SimpleCommand{
Expand Down
Loading

0 comments on commit c21a152

Please sign in to comment.