Commit c3a768b3 by bergquist Committed by Carl Bergquist

provsioning: dont stop grafana due to missing

parent 579d2b63
......@@ -5,20 +5,25 @@ import (
"path/filepath"
"strings"
"github.com/grafana/grafana/pkg/log"
yaml "gopkg.in/yaml.v2"
)
type configReader struct {
path string
log log.Logger
}
func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
var dashboards []*DashboardsAsConfig
files, err := ioutil.ReadDir(cr.path)
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 {
if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
continue
......@@ -30,13 +35,13 @@ func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
return nil, err
}
var datasource []*DashboardsAsConfig
err = yaml.Unmarshal(yamlFile, &datasource)
var dashCfg []*DashboardsAsConfig
err = yaml.Unmarshal(yamlFile, &dashCfg)
if err != nil {
return nil, err
}
dashboards = append(dashboards, datasource...)
dashboards = append(dashboards, dashCfg...)
}
for i := range dashboards {
......
......@@ -3,6 +3,7 @@ package dashboards
import (
"testing"
"github.com/grafana/grafana/pkg/log"
. "github.com/smartystreets/goconvey/convey"
)
......@@ -16,7 +17,7 @@ func TestDashboardsAsConfig(t *testing.T) {
Convey("Can read config file", func() {
cfgProvifer := configReader{path: simpleDashboardConfig}
cfgProvifer := configReader{path: simpleDashboardConfig, log: log.New("test-logger")}
cfg, err := cfgProvifer.readConfig()
if err != nil {
t.Fatalf("readConfig return an error %v", err)
......@@ -47,16 +48,26 @@ func TestDashboardsAsConfig(t *testing.T) {
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()
if err != nil {
t.Fatalf("readConfig return an error %v", err)
}
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 {
}
func Provision(ctx context.Context, configDirectory string) (*DashboardProvisioner, error) {
log := log.New("provisioning.dashboard")
d := &DashboardProvisioner{
cfgReader: &configReader{path: configDirectory},
log: log.New("provisioning.dashboard"),
cfgReader: &configReader{path: configDirectory, log: log},
log: log,
ctx: ctx,
}
......
......@@ -25,13 +25,13 @@ func Provision(configDirectory string) error {
type DatasourceProvisioner struct {
log log.Logger
cfgProvider configReader
cfgProvider *configReader
}
func newDatasourceProvisioner(log log.Logger) DatasourceProvisioner {
return DatasourceProvisioner{
log: log,
cfgProvider: configReader{},
cfgProvider: &configReader{log: log},
}
}
......@@ -95,15 +95,19 @@ func (dc *DatasourceProvisioner) deleteDatasources(dsToDelete []*DeleteDatasourc
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)
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 {
if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
......
......@@ -11,7 +11,7 @@ import (
)
var (
logger log.Logger = log.New("fake.logger")
logger log.Logger = log.New("fake.log")
oneDatasourcesConfig string = ""
twoDatasourcesConfig string = "./test-configs/two-datasources"
twoDatasourcesConfigPurgeOthers string = "./test-configs/insert-two-delete-two"
......@@ -115,12 +115,23 @@ func TestDatasourceAsConfig(t *testing.T) {
})
Convey("broken yaml should return error", func() {
_, err := configReader{}.readConfig(brokenYaml)
reader := &configReader{}
_, err := reader.readConfig(brokenYaml)
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() {
cfgProvifer := configReader{}
cfgProvifer := &configReader{log: log.New("test logger")}
cfg, err := cfgProvifer.readConfig(allProperties)
if err != nil {
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