Skip to content

Commit

Permalink
Merge pull request #260 from esnet/feature/orgProps
Browse files Browse the repository at this point in the history
Adding support for Org Properties
  • Loading branch information
safaci2000 committed Mar 11, 2024
2 parents 77955d2 + 0bba334 commit c6ad7db
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 67 deletions.
12 changes: 9 additions & 3 deletions cli/backup/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,22 @@ func newOrganizationsListCmd() simplecobra.Commander {
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", "organization Name", "org slug ID"})
rootCmd.TableObj.AppendHeader(table.Row{"id", "organization Name", "org slug ID", "HomeDashboardUID", "Theme", "WeekStart"})
listOrganizations := rootCmd.GrafanaSvc().ListOrganizations(filter)
sort.Slice(listOrganizations, func(a, b int) bool {
return listOrganizations[a].ID < listOrganizations[b].ID
return listOrganizations[a].Organization.ID < listOrganizations[b].Organization.ID
})
if len(listOrganizations) == 0 {
slog.Info("No organizations found")
} else {
for _, org := range listOrganizations {
rootCmd.TableObj.AppendRow(table.Row{org.ID, org.Name, slug.Make(org.Name)})
rootCmd.TableObj.AppendRow(table.Row{org.Organization.ID,
org.Organization.Name,
slug.Make(org.Organization.Name),
org.Preferences.HomeDashboardUID,
org.Preferences.Theme,
org.Preferences.WeekStart,
})
}
rootCmd.TableObj.Render()
}
Expand Down
116 changes: 116 additions & 0 deletions cli/tools/org_preferences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package tools

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

func newOrgPreferenceCommand() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "preferences",
Short: "Update organization preferences",
Long: "Update organization preferences",
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
return cd.CobraCommand.Help()

},
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"preference", "pref", "p", "prefs"}
},
CommandsList: []simplecobra.Commander{
//Preferences
newGetOrgPreferenceCmd(),
newUpdateOrgPreferenceCmd(),
},
}

}

func newUpdateOrgPreferenceCmd() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "set",
Short: "Set --orgName [--homeDashUid, --theme, --weekstart] to set Org preferences",
Long: "Set --orgName [--homeDashUid, --theme, --weekstart] to set Org preferences",
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.PersistentFlags().StringP("orgName", "", "", "Organization Name")
cmd.PersistentFlags().StringP("homeDashUid", "", "", "UID for the home dashboard")
cmd.PersistentFlags().StringP("theme", "", "", "light, dark")
cmd.PersistentFlags().StringP("weekstart", "", "", "day of the week (sunday, monday, etc)")

},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("update the org preferences")
org, _ := cd.CobraCommand.Flags().GetString("orgName")
home, _ := cd.CobraCommand.Flags().GetString("homeDashUid")
theme, _ := cd.CobraCommand.Flags().GetString("theme")
weekstart, _ := cd.CobraCommand.Flags().GetString("weekstart")
if org == "" {
log.Fatal("--orgName is a required parameter")
}
if home != "" && theme != "" && weekstart == "" {
log.Fatal("At least one of [--homeDashUid, --theme, --weekstart] needs to be set")
}

rootCmd.GrafanaSvc().InitOrganizations()
prefere, err := rootCmd.GrafanaSvc().GetOrgPreferences(org)
if err != nil {
log.Fatal(err.Error())
}
if home != "" {
prefere.HomeDashboardUID = home
}
if theme != "" {
prefere.Theme = theme
}
if weekstart != "" {
prefere.WeekStart = weekstart
}

err = rootCmd.GrafanaSvc().UploadOrgPreferences(org, prefere)
if err != nil {
log.Fatalf("Failed to update org preferences, %v", err)
}
slog.Info("Preferences update for organization", slog.Any("organization", org))

return nil

},
}

}

func newGetOrgPreferenceCmd() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "get",
Short: "get <orgName> returns org preferences",
Long: "get <orgId> returns org preferences",
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.PersistentFlags().StringP("orgName", "", "", "Organization Name")
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
orgName, _ := cd.CobraCommand.Flags().GetString("orgName")

rootCmd.GrafanaSvc().InitOrganizations()
pref, err := rootCmd.GrafanaSvc().GetOrgPreferences(orgName)
if err != nil {
log.Fatal(err.Error())
}

rootCmd.TableObj.AppendHeader(table.Row{"field", "value"})
rootCmd.TableObj.AppendRow(table.Row{"HomeDashboardUID", pref.HomeDashboardUID})
rootCmd.TableObj.AppendRow(table.Row{"Theme", pref.Theme})
rootCmd.TableObj.AppendRow(table.Row{"WeekStart", pref.WeekStart})

rootCmd.TableObj.Render()

return nil

},
}

}
5 changes: 5 additions & 0 deletions cli/tools/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func newOrgCommand() simplecobra.Commander {
newUpdateUserRoleCmd(),
newAddUserRoleCmd(),
newDeleteUserRoleCmd(),
//Preferences
newOrgPreferenceCommand(),
},
}

Expand All @@ -53,6 +55,9 @@ func newSetOrgCmd() simplecobra.Commander {
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
orgName, _ := cd.CobraCommand.Flags().GetString("orgName")
slugName, _ := cd.CobraCommand.Flags().GetString("orgSlugName")
if orgName == "" && slugName == "" {
return errors.New("must set either --orgName or --orgSlugName flag")
}
if orgName != "" || slugName != "" {
var useSlug = false
if slugName != "" {
Expand Down
10 changes: 8 additions & 2 deletions internal/config/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package config

import (
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"github.com/sethvargo/go-password/password"
"github.com/spf13/viper"
"log/slog"
"math/rand/v2"
"math/big"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -74,7 +75,12 @@ func (u *UserSettings) GetPassword(username string) string {
return u.defaultUserPassword(username)
}

passLength := rand.IntN(u.MaxLength-u.MinLength) + u.MinLength
nBig, err := rand.Int(rand.Reader, big.NewInt(int64(u.MaxLength)))
if err != nil {
slog.Warn("Failed to get random value")
return u.defaultUserPassword(username)
}
passLength := int(nBig.Int64() + int64(u.MinLength))
res, err := password.Generate(passLength, 1, 1, false, false)
if err != nil {
slog.Warn("unable to generate a proper random password, falling back on default password pattern",
Expand Down
117 changes: 111 additions & 6 deletions internal/service/mocks/GrafanaService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c6ad7db

Please sign in to comment.