mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
datasource as cfg: setting for purging datasources not in cfg
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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,19 +44,40 @@ 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
|
||||
|
||||
for _, ds := range datasources.Datasources {
|
||||
if ds.OrgId == 0 {
|
||||
ds.OrgId = 1
|
||||
all, err := dc.repository.loadAllDatasources()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil && err != models.ErrDataSourceNotFound {
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user