Commit 930da631 by bergquist

datasource as cfg: setting for purging datasources not in cfg

parent 0f29b8ac
......@@ -56,7 +56,7 @@ func (g *GrafanaServerImpl) Start() {
g.writePIDFile()
initSql()
err, _ := dsSettings.Init(filepath.Join(setting.HomePath, "conf/datasources.yaml"))
err := dsSettings.Init(filepath.Join(setting.HomePath, "conf/datasources.yaml"))
if err != nil {
g.log.Error("Failed to load datasources from config", "error", err)
g.Shutdown(1, "Startup failed")
......
package datasources
import (
"io"
"io/ioutil"
"path/filepath"
......@@ -14,18 +13,16 @@ import (
// TODO: secure jsonData
// TODO: auto reload on file changes
// TODO: remove get method since all datasources is in memory
type DatasourcesAsConfig struct {
PurgeOtherDatasources bool
Datasources []models.DataSource
}
func Init(configPath string) (error, io.Closer) {
func Init(configPath string) error {
dc := NewDatasourceConfiguration()
dc.applyChanges(configPath)
return nil, ioutil.NopCloser(nil)
return dc.applyChanges(configPath)
}
type DatasourceConfigurator struct {
......@@ -47,18 +44,39 @@ func newDatasourceConfiguration(log log.Logger, cfgProvider configProvider, repo
}
func (dc *DatasourceConfigurator) applyChanges(configPath string) error {
datasources, err := dc.cfgProvider.readConfig(configPath)
cfg, err := dc.cfgProvider.readConfig(configPath)
if err != nil {
return err
}
//read all datasources
//delete datasources not in list
all, err := dc.repository.loadAllDatasources()
if err != nil {
return err
}
for _, ds := range datasources.Datasources {
if ds.OrgId == 0 {
ds.OrgId = 1
for i, _ := range cfg.Datasources {
if cfg.Datasources[i].OrgId == 0 {
cfg.Datasources[i].OrgId = 1
}
}
if cfg.PurgeOtherDatasources {
for _, dbDatasource := range all {
delete := true
for _, cfgDatasource := range cfg.Datasources {
if dbDatasource.Name == cfgDatasource.Name && dbDatasource.OrgId == cfgDatasource.OrgId {
delete = false
}
}
if delete {
dc.log.Info("deleting datasource since PurgeOtherDatasource is enabled", "name", dbDatasource.Name)
dc.repository.delete(&models.DeleteDataSourceByIdCommand{Id: dbDatasource.Id, OrgId: dbDatasource.OrgId})
}
}
}
for _, ds := range cfg.Datasources {
query := &models.GetDataSourceByNameQuery{Name: ds.Name, OrgId: ds.OrgId}
err := dc.repository.get(query)
......
......@@ -14,7 +14,6 @@ var logger log.Logger = log.New("fake.logger")
func TestDatasourceAsConfig(t *testing.T) {
Convey("Testing datasource as configuration", t, func() {
fakeCfg := &fakeConfig{}
fakeRepo := &fakeRepository{}
Convey("One configured datasource", func() {
......@@ -101,6 +100,37 @@ func TestDatasourceAsConfig(t *testing.T) {
So(len(fakeRepo.updated), ShouldEqual, 0)
})
})
})
Convey("Two configured datasource and purge others = false", func() {
fakeCfg.cfg = &DatasourcesAsConfig{
PurgeOtherDatasources: false,
Datasources: []models.DataSource{
models.DataSource{Name: "graphite", OrgId: 1},
models.DataSource{Name: "prometheus", OrgId: 1},
},
}
Convey("two other datasources in database", func() {
fakeRepo.loadAll = []*models.DataSource{
&models.DataSource{Name: "old-graphite", OrgId: 1, Id: 1},
&models.DataSource{Name: "old-graphite2", OrgId: 1, Id: 2},
}
Convey("should have two new datasources", func() {
dc := newDatasourceConfiguration(logger, fakeCfg, fakeRepo)
err := dc.applyChanges("mock/config.yaml")
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 2)
So(len(fakeRepo.updated), ShouldEqual, 0)
})
})
})
})
}
......
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