Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Org refactoring #256

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 0 additions & 154 deletions cli/backup/alertnotifications.go

This file was deleted.

12 changes: 11 additions & 1 deletion cli/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/spf13/cobra"
)

Expand All @@ -27,7 +28,6 @@ limited to clear/delete, list, download and upload. Any other functionality wil
},
CommandsList: []simplecobra.Commander{
newDashboardCommand(),
newAlertNotificationsCommand(),
newConnectionsCommand(),
newFolderCommand(),
newLibraryElementsCommand(),
Expand All @@ -38,3 +38,13 @@ limited to clear/delete, list, download and upload. Any other functionality wil
}

}

// GetOrganizationName wrapper for verbose version below.
func GetOrganizationName() string {
return config.Config().GetDefaultGrafanaConfig().GetOrganizationName()
}

// GetContext wrapper for verbose version below.
func GetContext() string {
return config.Config().GetGDGConfig().GetContext()
}
2 changes: 1 addition & 1 deletion cli/backup/connection_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func newConnectionsPermissionDownloadCmd() simplecobra.Commander {
cmd.Aliases = []string{"d"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("import Connections for context",
slog.Info("Download Connections for context",
"context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"filename"})
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
Expand Down
9 changes: 6 additions & 3 deletions cli/backup/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newClearConnectionsCmd() simplecobra.Commander {
cmd.Aliases = []string{"c"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Delete connections")
slog.Info("Delete connections", slog.String("Organization", GetOrganizationName()))
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("datasource")
filters := service.NewConnectionFilter(dashboardFilter)
savedFiles := rootCmd.GrafanaSvc().DeleteAllConnections(filters)
Expand All @@ -71,7 +71,7 @@ func newUploadConnectionsCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Uploading connections")
slog.Info("Uploading connections", slog.String("Organization", GetOrganizationName()))
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(dashboardFilter)
exportedList := rootCmd.GrafanaSvc().UploadConnections(filters)
Expand All @@ -96,6 +96,7 @@ func newDownloadConnectionsCmd() simplecobra.Commander {
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Importing connections for context",
slog.String("Organization", GetOrganizationName()),
"context", config.Config().GetGDGConfig().GetContext())
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(dashboardFilter)
Expand Down Expand Up @@ -123,7 +124,9 @@ func newListConnectionsCmd() simplecobra.Commander {
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(dashboardFilter)
dsListing := rootCmd.GrafanaSvc().ListConnections(filters)
slog.Info("Listing connections for context", "context", config.Config().GetGDGConfig().GetContext())
slog.Info("Listing connections for context",
slog.String("Organization", GetOrganizationName()),
slog.String("context", GetContext()))
if len(dsListing) == 0 {
slog.Info("No connections found")
} else {
Expand Down
21 changes: 13 additions & 8 deletions cli/backup/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ func newDashboardCommand() simplecobra.Commander {
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"dash", "dashboard"}
dashboard := cmd
dashboard.PersistentFlags().BoolVarP(&skipConfirmAction, "skip-confirmation", "", false, "when set to true, bypass confirmation prompts")
dashboard.PersistentFlags().StringP("dashboard", "d", "", "filter by dashboard slug")
dashboard.PersistentFlags().StringP("folder", "f", "", "Filter by Folder Name (Quotes in names not supported)")
dashboard.PersistentFlags().StringArrayP("tags", "t", []string{}, "Filter by list of comma delimited tags")
cmd.PersistentFlags().BoolVarP(&skipConfirmAction, "skip-confirmation", "", false, "when set to true, bypass confirmation prompts")
cmd.PersistentFlags().StringP("dashboard", "d", "", "filter by dashboard slug")
cmd.PersistentFlags().StringP("folder", "f", "", "Filter by Folder Name (Quotes in names not supported)")
cmd.PersistentFlags().StringArrayP("tags", "t", []string{}, "Filter by list of comma delimited tags")
},
CommandsList: []simplecobra.Commander{
newListDashboardsCmd(),
Expand Down Expand Up @@ -110,7 +109,9 @@ func newUploadDashboardsCmd() simplecobra.Commander {
rootCmd.TableObj.AppendHeader(table.Row{"Title", "id", "folder", "UID"})
boards := rootCmd.GrafanaSvc().ListDashboards(filter)

slog.Info(fmt.Sprintf("%d dashboards have been uploaded", len(boards)))
slog.Info("dashboards have been uploaded", slog.Any("count", len(boards)),
slog.String("context", GetContext()),
slog.String("Organization", GetOrganizationName()))
for _, link := range boards {
rootCmd.TableObj.AppendRow(table.Row{link.Title, link.ID, link.FolderTitle, link.UID})
}
Expand Down Expand Up @@ -138,7 +139,8 @@ func newDownloadDashboardsCmd() simplecobra.Commander {
filter := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
savedFiles := rootCmd.GrafanaSvc().DownloadDashboards(filter)
slog.Info("Downloading dashboards for context",
"context", config.Config().GetGDGConfig().GetContext())
slog.String("Organization", GetOrganizationName()),
"context", GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})
for _, file := range savedFiles {
rootCmd.TableObj.AppendRow(table.Row{"dashboard", file})
Expand All @@ -165,7 +167,10 @@ func newListDashboardsCmd() simplecobra.Commander {
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
boards := rootCmd.GrafanaSvc().ListDashboards(filters)

slog.Info("Listing dashboards for context", slog.String("context", config.Config().GetGDGConfig().GetContext()), slog.Any("count", len(boards)))
slog.Info("Listing dashboards for context",
slog.String("context", GetContext()),
slog.String("orgName", GetOrganizationName()),
slog.Any("count", len(boards)))
for _, link := range boards {
base, err := url.Parse(config.Config().GetDefaultGrafanaConfig().URL)
var baseHost string
Expand Down
28 changes: 22 additions & 6 deletions cli/backup/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/gosimple/slug"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"log/slog"
"sort"
)

func parseOrganizationGlobalFlags(command *cobra.Command) []string {
orgName, _ := command.Flags().GetString("org-name")
return []string{orgName}
}

func newOrganizationsCommand() simplecobra.Commander {
description := "Manage Grafana Organizations."
return &support.SimpleCommand{
Expand All @@ -19,6 +26,12 @@ func newOrganizationsCommand() simplecobra.Commander {
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"org", "orgs"}
cmd.PersistentFlags().StringP("org-name", "o", "", "when set to true, bypass confirmation prompts")
},

InitCFunc: func(cd *simplecobra.Commandeer, r *support.RootCommand) error {
r.GrafanaSvc().InitOrganizations()
return nil
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
return cd.CobraCommand.Help()
Expand All @@ -42,17 +55,18 @@ func newOrganizationsListCmd() simplecobra.Commander {
cmd.Aliases = []string{"l"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
filter := service.NewOrganizationFilter(parseOrganizationGlobalFlags(cd.CobraCommand)...)
slog.Info("Listing organizations for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "org"})
listOrganizations := rootCmd.GrafanaSvc().ListOrganizations()
rootCmd.TableObj.AppendHeader(table.Row{"id", "organization Name", "org slug ID"})
listOrganizations := rootCmd.GrafanaSvc().ListOrganizations(filter)
sort.Slice(listOrganizations, func(a, b int) bool {
return listOrganizations[a].ID < listOrganizations[b].ID
})
if len(listOrganizations) == 0 {
slog.Info("No organizations found")
} else {
for _, org := range listOrganizations {
rootCmd.TableObj.AppendRow(table.Row{org.ID, org.Name})
rootCmd.TableObj.AppendRow(table.Row{org.ID, org.Name, slug.Make(org.Name)})
}
rootCmd.TableObj.Render()
}
Expand All @@ -73,7 +87,8 @@ func newOrganizationsDownloadCmd() simplecobra.Commander {
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Downloading organizations for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"file"})
listOrganizations := rootCmd.GrafanaSvc().DownloadOrganizations()
filter := service.NewOrganizationFilter(parseOrganizationGlobalFlags(cd.CobraCommand)...)
listOrganizations := rootCmd.GrafanaSvc().DownloadOrganizations(filter)
if len(listOrganizations) == 0 {
slog.Info("No organizations found")
} else {
Expand All @@ -97,9 +112,10 @@ func newOrganizationsUploadCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Uploading Folders for context: '%s'", "context", config.Config().GetGDGConfig().GetContext())
slog.Info("Uploading Folders for context: ", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"file"})
folders := rootCmd.GrafanaSvc().UploadOrganizations()
filter := service.NewOrganizationFilter(parseOrganizationGlobalFlags(cd.CobraCommand)...)
folders := rootCmd.GrafanaSvc().UploadOrganizations(filter)
if len(folders) == 0 {
slog.Info("No Orgs were uploaded")
} else {
Expand Down
Loading
Loading