Skip to content

Commit

Permalink
Adds support for Org based override of Watched Folders
Browse files Browse the repository at this point in the history
Fixes #193
  • Loading branch information
safaci2000 committed Oct 25, 2023
1 parent f6b31ca commit 4fbdd93
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
9 changes: 7 additions & 2 deletions config/importer-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ storage_engine:
kind: cloud
cloud_type: s3
bucket_name: ""
## The configuration below is mainly intended for OSS alternatives like ceph and minio. If you use a known cloud provider
## The configuration below is mainly intended for OSS alternatives like ceph and minio. If you use a known cloud provider
## like aws, gcs, azure please setup the auth using the provided tooling from the cloud provider.
# For example, having a valid AWS bucket configured in ~/.aws/credentials will be sufficient without needing to provide the auth in the config.
### valid boolean values can be represented as true, "true", or "1"
Expand All @@ -18,7 +18,7 @@ storage_engine:
access_id: "" ## this value can also be read from: AWS_ACCESS_KEY. config file is given precedence
secret_key: "" ## same as above, can be read from: AWS_SECRET_KEY with config file is given precedence.
init_bucket: "true" ## Only supported for custom workflows. Will attempt to create a bucket if one does not exist.
endpoint: "http://localhost:9000"
endpoint: "http://localhost:9000"
ssl_enabled: "false"

contexts:
Expand Down Expand Up @@ -115,6 +115,11 @@ contexts:
watched:
- Folder1
- Folder2
watched_folders_override:
- organization_id: 0
folders:
- General
- SpecialFolder

global:
debug: true
Expand Down
1 change: 1 addition & 0 deletions config/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ contexts:
- Folder1
- Folder2


global:
debug: true
ignore_ssl_errors: false ##when set to true will ignore invalid SSL errors
Expand Down
15 changes: 15 additions & 0 deletions internal/config/config_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,23 @@ func (s *GrafanaConfig) GetTeamOutput() string {
return path.Join(s.OutputPath, TeamResource)
}

// GetOrgMonitoredFolders return the OrganizationMonitoredFolders that override a given Org
func (s *GrafanaConfig) GetOrgMonitoredFolders(orgId int64) []string {
for _, item := range s.MonitoredFoldersOverride {
if item.OrganizationId == orgId && len(item.Folders) > 0 {
return item.Folders
}
}

return nil
}

// GetMonitoredFolders return a list of the monitored folders alternatively returns the "General" folder.
func (s *GrafanaConfig) GetMonitoredFolders() []string {
orgFolders := s.GetOrgMonitoredFolders(s.OrganizationId)
if len(orgFolders) > 0 {
return orgFolders
}
if len(s.MonitoredFolders) == 0 {
return []string{"General"}
}
Expand Down
41 changes: 41 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/esnet/gdg/internal/config"
"github.com/esnet/grafana-swagger-api-golang/goclient/models"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -59,6 +60,46 @@ func TestSetup(t *testing.T) {
validateGrafanaQA(t, grafanaConf)
}

func TestWatchedFoldersConfig(t *testing.T) {
os.Setenv("GDG_CONTEXT_NAME", "qa")
//clear all ENV values
for _, key := range os.Environ() {
if strings.Contains(key, "GDG_") {
os.Unsetenv(key)
}
}

os.Setenv("GDG_CONTEXT_NAME", "qa")
config.InitConfig("testing.yml", "")
conf := config.Config().ViperConfig()
log.Info(conf.ConfigFileUsed())

confobj := config.Config().GetAppConfig()
_ = confobj
log.Infof(confobj.ContextName)
assert.NotNil(t, conf)
context := conf.GetString("context_name")
assert.Equal(t, context, "qa")
grafanaConf := config.Config().GetDefaultGrafanaConfig()
assert.NotNil(t, grafanaConf)
grafanaConf.MonitoredFoldersOverride = []config.MonitoredOrgFolders{{
OrganizationId: 0,
Folders: []string{"General", "SpecialFolder"},
}}
folders := grafanaConf.GetMonitoredFolders()
assert.True(t, slices.Contains(folders, "SpecialFolder"))
grafanaConf.OrganizationId = 2
folders = grafanaConf.GetMonitoredFolders()
assert.False(t, slices.Contains(folders, "SpecialFolder"))
assert.True(t, slices.Contains(folders, "Folder2"))
grafanaConf.OrganizationId = 0
grafanaConf.MonitoredFoldersOverride = nil
folders = grafanaConf.GetMonitoredFolders()
assert.False(t, slices.Contains(folders, "SpecialFolder"))
assert.True(t, slices.Contains(folders, "Folder2"))

}

// Ensures that if the config is on a completely different path, the searchPath is updated accordingly
func TestSetupDifferentPath(t *testing.T) {
cfgFile := DuplicateConfig(t)
Expand Down
26 changes: 16 additions & 10 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ type AppConfig struct {

// GrafanaConfig model wraps auth and watched list for grafana
type GrafanaConfig struct {
Storage string `mapstructure:"storage" yaml:"storage"`
adminEnabled bool `mapstructure:"-" yaml:"-"`
EnterpriseSupport bool `mapstructure:"enterprise_support" yaml:"enterprise_support"`
URL string `mapstructure:"url" yaml:"url"`
APIToken string `mapstructure:"token" yaml:"token"`
UserName string `mapstructure:"user_name" yaml:"user_name"`
Password string `mapstructure:"password" yaml:"password"`
OrganizationId int64 `mapstructure:"organization_id" yaml:"organization_id"`
MonitoredFolders []string `mapstructure:"watched" yaml:"watched"`
DataSourceSettings *ConnectionSettings `mapstructure:"connections" yaml:"connections"`
Storage string `mapstructure:"storage" yaml:"storage"`
adminEnabled bool `mapstructure:"-" yaml:"-"`
EnterpriseSupport bool `mapstructure:"enterprise_support" yaml:"enterprise_support"`
URL string `mapstructure:"url" yaml:"url"`
APIToken string `mapstructure:"token" yaml:"token"`
UserName string `mapstructure:"user_name" yaml:"user_name"`
Password string `mapstructure:"password" yaml:"password"`
OrganizationId int64 `mapstructure:"organization_id" yaml:"organization_id"`
MonitoredFoldersOverride []MonitoredOrgFolders `mapstructure:"watched_folders_override" yaml:"watched_folders_override"`
MonitoredFolders []string `mapstructure:"watched" yaml:"watched"`
DataSourceSettings *ConnectionSettings `mapstructure:"connections" yaml:"connections"`
//Datasources are deprecated, please use Connections
LegacyConnectionSettings map[string]interface{} `mapstructure:"datasources" yaml:"datasources"`
FilterOverrides *FilterOverrides `mapstructure:"filter_override" yaml:"filter_override"`
OutputPath string `mapstructure:"output_path" yaml:"output_path"`
}

type MonitoredOrgFolders struct {
OrganizationId int64 `json:"organization_id" yaml:"organization_id"`
Folders []string `json:"folders" yaml:"folders"`
}

// GetOrganizationId returns the id of the organization (defaults to 1 if unset)
func (s *GrafanaConfig) GetOrganizationId() int64 {
if s.OrganizationId > 1 {
Expand Down

0 comments on commit 4fbdd93

Please sign in to comment.