Commit c3a768b3 by bergquist Committed by Carl Bergquist

provsioning: dont stop grafana due to missing

parent 579d2b63
...@@ -5,20 +5,25 @@ import ( ...@@ -5,20 +5,25 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/grafana/grafana/pkg/log"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
type configReader struct { type configReader struct {
path string path string
log log.Logger
} }
func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) { func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
var dashboards []*DashboardsAsConfig
files, err := ioutil.ReadDir(cr.path) files, err := ioutil.ReadDir(cr.path)
if err != nil { if err != nil {
return nil, err cr.log.Error("cant read dashboard provisioning files from directory", "path", cr.path)
return dashboards, nil
} }
var dashboards []*DashboardsAsConfig
for _, file := range files { for _, file := range files {
if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") { if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
continue continue
...@@ -30,13 +35,13 @@ func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) { ...@@ -30,13 +35,13 @@ func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
return nil, err return nil, err
} }
var datasource []*DashboardsAsConfig var dashCfg []*DashboardsAsConfig
err = yaml.Unmarshal(yamlFile, &datasource) err = yaml.Unmarshal(yamlFile, &dashCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dashboards = append(dashboards, datasource...) dashboards = append(dashboards, dashCfg...)
} }
for i := range dashboards { for i := range dashboards {
......
...@@ -3,6 +3,7 @@ package dashboards ...@@ -3,6 +3,7 @@ package dashboards
import ( import (
"testing" "testing"
"github.com/grafana/grafana/pkg/log"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
...@@ -16,7 +17,7 @@ func TestDashboardsAsConfig(t *testing.T) { ...@@ -16,7 +17,7 @@ func TestDashboardsAsConfig(t *testing.T) {
Convey("Can read config file", func() { Convey("Can read config file", func() {
cfgProvifer := configReader{path: simpleDashboardConfig} cfgProvifer := configReader{path: simpleDashboardConfig, log: log.New("test-logger")}
cfg, err := cfgProvifer.readConfig() cfg, err := cfgProvifer.readConfig()
if err != nil { if err != nil {
t.Fatalf("readConfig return an error %v", err) t.Fatalf("readConfig return an error %v", err)
...@@ -47,16 +48,26 @@ func TestDashboardsAsConfig(t *testing.T) { ...@@ -47,16 +48,26 @@ func TestDashboardsAsConfig(t *testing.T) {
So(ds2.Options["path"], ShouldEqual, "/var/lib/grafana/dashboards") So(ds2.Options["path"], ShouldEqual, "/var/lib/grafana/dashboards")
}) })
Convey("Should skip broken config files", func() { Convey("Should skip invalid path", func() {
cfgProvifer := configReader{path: brokenConfigs} cfgProvifer := configReader{path: "/invalid-directory", log: log.New("test-logger")}
cfg, err := cfgProvifer.readConfig() cfg, err := cfgProvifer.readConfig()
if err != nil { if err != nil {
t.Fatalf("readConfig return an error %v", err) t.Fatalf("readConfig return an error %v", err)
} }
So(len(cfg), ShouldEqual, 0) So(len(cfg), ShouldEqual, 0)
})
Convey("Should skip broken config files", func() {
cfgProvifer := configReader{path: brokenConfigs, log: log.New("test-logger")}
cfg, err := cfgProvifer.readConfig()
if err != nil {
t.Fatalf("readConfig return an error %v", err)
}
So(len(cfg), ShouldEqual, 0)
}) })
}) })
} }
...@@ -14,9 +14,10 @@ type DashboardProvisioner struct { ...@@ -14,9 +14,10 @@ type DashboardProvisioner struct {
} }
func Provision(ctx context.Context, configDirectory string) (*DashboardProvisioner, error) { func Provision(ctx context.Context, configDirectory string) (*DashboardProvisioner, error) {
log := log.New("provisioning.dashboard")
d := &DashboardProvisioner{ d := &DashboardProvisioner{
cfgReader: &configReader{path: configDirectory}, cfgReader: &configReader{path: configDirectory, log: log},
log: log.New("provisioning.dashboard"), log: log,
ctx: ctx, ctx: ctx,
} }
......
...@@ -25,13 +25,13 @@ func Provision(configDirectory string) error { ...@@ -25,13 +25,13 @@ func Provision(configDirectory string) error {
type DatasourceProvisioner struct { type DatasourceProvisioner struct {
log log.Logger log log.Logger
cfgProvider configReader cfgProvider *configReader
} }
func newDatasourceProvisioner(log log.Logger) DatasourceProvisioner { func newDatasourceProvisioner(log log.Logger) DatasourceProvisioner {
return DatasourceProvisioner{ return DatasourceProvisioner{
log: log, log: log,
cfgProvider: configReader{}, cfgProvider: &configReader{log: log},
} }
} }
...@@ -95,15 +95,19 @@ func (dc *DatasourceProvisioner) deleteDatasources(dsToDelete []*DeleteDatasourc ...@@ -95,15 +95,19 @@ func (dc *DatasourceProvisioner) deleteDatasources(dsToDelete []*DeleteDatasourc
return nil return nil
} }
type configReader struct{} type configReader struct {
log log.Logger
}
func (cr *configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
var datasources []*DatasourcesAsConfig
func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
files, err := ioutil.ReadDir(path) files, err := ioutil.ReadDir(path)
if err != nil { if err != nil {
return nil, err cr.log.Error("cant read datasource provisioning files from directory", "path", path)
return datasources, nil
} }
var datasources []*DatasourcesAsConfig
for _, file := range files { for _, file := range files {
if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") { if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
filename, _ := filepath.Abs(filepath.Join(path, file.Name())) filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
) )
var ( var (
logger log.Logger = log.New("fake.logger") logger log.Logger = log.New("fake.log")
oneDatasourcesConfig string = "" oneDatasourcesConfig string = ""
twoDatasourcesConfig string = "./test-configs/two-datasources" twoDatasourcesConfig string = "./test-configs/two-datasources"
twoDatasourcesConfigPurgeOthers string = "./test-configs/insert-two-delete-two" twoDatasourcesConfigPurgeOthers string = "./test-configs/insert-two-delete-two"
...@@ -115,12 +115,23 @@ func TestDatasourceAsConfig(t *testing.T) { ...@@ -115,12 +115,23 @@ func TestDatasourceAsConfig(t *testing.T) {
}) })
Convey("broken yaml should return error", func() { Convey("broken yaml should return error", func() {
_, err := configReader{}.readConfig(brokenYaml) reader := &configReader{}
_, err := reader.readConfig(brokenYaml)
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
}) })
Convey("skip invalid directory", func() {
cfgProvifer := &configReader{log: log.New("test logger")}
cfg, err := cfgProvifer.readConfig("./invalid-directory")
if err != nil {
t.Fatalf("readConfig return an error %v", err)
}
So(len(cfg), ShouldEqual, 0)
})
Convey("can read all properties", func() { Convey("can read all properties", func() {
cfgProvifer := configReader{} cfgProvifer := &configReader{log: log.New("test logger")}
cfg, err := cfgProvifer.readConfig(allProperties) cfg, err := cfgProvifer.readConfig(allProperties)
if err != nil { if err != nil {
t.Fatalf("readConfig return an error %v", err) t.Fatalf("readConfig return an error %v", err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment