Commit c6a6ae26 by bergquist

datasource as cfg: refactor to use bus

parent dc002abe
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
...@@ -24,17 +25,15 @@ func Apply(configPath string) error { ...@@ -24,17 +25,15 @@ func Apply(configPath string) error {
type DatasourceConfigurator struct { type DatasourceConfigurator struct {
log log.Logger log log.Logger
cfgProvider configProvider cfgProvider configProvider
repository datasourceRepository
} }
func NewDatasourceConfiguration() DatasourceConfigurator { func NewDatasourceConfiguration() DatasourceConfigurator {
return newDatasourceConfiguration(log.New("setting.datasource"), sqlDatasourceRepository{}) return newDatasourceConfiguration(log.New("setting.datasource"))
} }
func newDatasourceConfiguration(log log.Logger, repo datasourceRepository) DatasourceConfigurator { func newDatasourceConfiguration(log log.Logger) DatasourceConfigurator {
return DatasourceConfigurator{ return DatasourceConfigurator{
log: log, log: log,
repository: repo,
cfgProvider: configProvider{}, cfgProvider: configProvider{},
} }
} }
...@@ -59,10 +58,11 @@ func (dc *DatasourceConfigurator) applyChanges(configPath string) error { ...@@ -59,10 +58,11 @@ func (dc *DatasourceConfigurator) applyChanges(configPath string) error {
} }
} }
allDatasources, err := dc.repository.loadAllDatasources() cmd := &models.GetAllDataSourcesQuery{}
if err != nil { if err = bus.Dispatch(cmd); err != nil {
return err return err
} }
allDatasources := cmd.Result
if err := dc.deleteDatasourcesNotInConfiguration(cfg, allDatasources); err != nil { if err := dc.deleteDatasourcesNotInConfiguration(cfg, allDatasources); err != nil {
return err return err
...@@ -80,14 +80,13 @@ func (dc *DatasourceConfigurator) applyChanges(configPath string) error { ...@@ -80,14 +80,13 @@ func (dc *DatasourceConfigurator) applyChanges(configPath string) error {
if dbDatasource == nil { if dbDatasource == nil {
dc.log.Info("inserting datasource from configuration ", "name", ds.Name) dc.log.Info("inserting datasource from configuration ", "name", ds.Name)
insertCmd := createInsertCommand(ds) insertCmd := createInsertCommand(ds)
err := dc.repository.insert(insertCmd) if err := bus.Dispatch(insertCmd); err != nil {
if err != nil {
return err return err
} }
} else { } else {
dc.log.Debug("updating datasource from configuration", "name", ds.Name) dc.log.Debug("updating datasource from configuration", "name", ds.Name)
updateCmd := createUpdateCommand(ds, dbDatasource.Id) updateCmd := createUpdateCommand(ds, dbDatasource.Id)
if err := dc.repository.update(updateCmd); err != nil { if err := bus.Dispatch(updateCmd); err != nil {
return err return err
} }
} }
...@@ -109,7 +108,7 @@ func (dc *DatasourceConfigurator) deleteDatasourcesNotInConfiguration(cfg *Datas ...@@ -109,7 +108,7 @@ func (dc *DatasourceConfigurator) deleteDatasourcesNotInConfiguration(cfg *Datas
if delete { if delete {
dc.log.Info("deleting datasource from configuration", "name", dbDS.Name) dc.log.Info("deleting datasource from configuration", "name", dbDS.Name)
cmd := &models.DeleteDataSourceByIdCommand{Id: dbDS.Id, OrgId: dbDS.OrgId} cmd := &models.DeleteDataSourceByIdCommand{Id: dbDS.Id, OrgId: dbDS.OrgId}
if err := dc.repository.delete(cmd); err != nil { if err := bus.Dispatch(cmd); err != nil {
return err return err
} }
} }
...@@ -119,15 +118,6 @@ func (dc *DatasourceConfigurator) deleteDatasourcesNotInConfiguration(cfg *Datas ...@@ -119,15 +118,6 @@ func (dc *DatasourceConfigurator) deleteDatasourcesNotInConfiguration(cfg *Datas
return nil return nil
} }
type datasourceRepository interface {
insert(*models.AddDataSourceCommand) error
update(*models.UpdateDataSourceCommand) error
delete(*models.DeleteDataSourceByIdCommand) error
get(*models.GetDataSourceByNameQuery) error
loadAllDatasources() ([]*models.DataSource, error)
}
type sqlDatasourceRepository struct{}
type configProvider struct{} type configProvider struct{}
func (configProvider) readConfig(path string) (*DatasourcesAsConfig, error) { func (configProvider) readConfig(path string) (*DatasourcesAsConfig, error) {
...@@ -147,28 +137,3 @@ func (configProvider) readConfig(path string) (*DatasourcesAsConfig, error) { ...@@ -147,28 +137,3 @@ func (configProvider) readConfig(path string) (*DatasourcesAsConfig, error) {
return datasources, nil return datasources, nil
} }
func (sqlDatasourceRepository) delete(cmd *models.DeleteDataSourceByIdCommand) error {
return bus.Dispatch(cmd)
}
func (sqlDatasourceRepository) update(cmd *models.UpdateDataSourceCommand) error {
return bus.Dispatch(cmd)
}
func (sqlDatasourceRepository) insert(cmd *models.AddDataSourceCommand) error {
return bus.Dispatch(cmd)
}
func (sqlDatasourceRepository) get(cmd *models.GetDataSourceByNameQuery) error {
return bus.Dispatch(cmd)
}
func (sqlDatasourceRepository) loadAllDatasources() ([]*models.DataSource, error) {
dss := &models.GetAllDataSourcesQuery{}
if err := bus.Dispatch(dss); err != nil {
return nil, err
}
return dss.Result, nil
}
...@@ -3,6 +3,7 @@ package datasources ...@@ -3,6 +3,7 @@ package datasources
import ( import (
"testing" "testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
...@@ -16,15 +17,23 @@ var ( ...@@ -16,15 +17,23 @@ var (
twoDatasourcesConfigPurgeOthers string = "./test-configs/two-datasources-purge-others.yaml" twoDatasourcesConfigPurgeOthers string = "./test-configs/two-datasources-purge-others.yaml"
doubleDatasourcesConfig string = "./test-configs/double-default-datasources.yaml" doubleDatasourcesConfig string = "./test-configs/double-default-datasources.yaml"
allProperties string = "./test-configs/all-properties.yaml" allProperties string = "./test-configs/all-properties.yaml"
fakeRepo *fakeRepository
) )
func TestDatasourceAsConfig(t *testing.T) { func TestDatasourceAsConfig(t *testing.T) {
Convey("Testing datasource as configuration", t, func() { Convey("Testing datasource as configuration", t, func() {
fakeRepo := &fakeRepository{} fakeRepo = &fakeRepository{}
bus.ClearBusHandlers()
bus.AddHandler("test", mockDelete)
bus.AddHandler("test", mockInsert)
bus.AddHandler("test", mockUpdate)
bus.AddHandler("test", mockGet)
bus.AddHandler("test", mockGetAll)
Convey("One configured datasource", func() { Convey("One configured datasource", func() {
Convey("no datasource in database", func() { Convey("no datasource in database", func() {
dc := newDatasourceConfiguration(logger, fakeRepo) dc := newDatasourceConfiguration(logger)
err := dc.applyChanges(twoDatasourcesConfig) err := dc.applyChanges(twoDatasourcesConfig)
if err != nil { if err != nil {
t.Fatalf("applyChanges return an error %v", err) t.Fatalf("applyChanges return an error %v", err)
...@@ -41,7 +50,7 @@ func TestDatasourceAsConfig(t *testing.T) { ...@@ -41,7 +50,7 @@ func TestDatasourceAsConfig(t *testing.T) {
} }
Convey("should update one datasource", func() { Convey("should update one datasource", func() {
dc := newDatasourceConfiguration(logger, fakeRepo) dc := newDatasourceConfiguration(logger)
err := dc.applyChanges(twoDatasourcesConfig) err := dc.applyChanges(twoDatasourcesConfig)
if err != nil { if err != nil {
t.Fatalf("applyChanges return an error %v", err) t.Fatalf("applyChanges return an error %v", err)
...@@ -54,7 +63,7 @@ func TestDatasourceAsConfig(t *testing.T) { ...@@ -54,7 +63,7 @@ func TestDatasourceAsConfig(t *testing.T) {
}) })
Convey("Two datasources with is_default", func() { Convey("Two datasources with is_default", func() {
dc := newDatasourceConfiguration(logger, fakeRepo) dc := newDatasourceConfiguration(logger)
err := dc.applyChanges(doubleDatasourcesConfig) err := dc.applyChanges(doubleDatasourcesConfig)
Convey("should raise error", func() { Convey("should raise error", func() {
So(err, ShouldEqual, ErrInvalidConfigToManyDefault) So(err, ShouldEqual, ErrInvalidConfigToManyDefault)
...@@ -70,7 +79,7 @@ func TestDatasourceAsConfig(t *testing.T) { ...@@ -70,7 +79,7 @@ func TestDatasourceAsConfig(t *testing.T) {
} }
Convey("should have two new datasources", func() { Convey("should have two new datasources", func() {
dc := newDatasourceConfiguration(logger, fakeRepo) dc := newDatasourceConfiguration(logger)
err := dc.applyChanges(twoDatasourcesConfigPurgeOthers) err := dc.applyChanges(twoDatasourcesConfigPurgeOthers)
if err != nil { if err != nil {
t.Fatalf("applyChanges return an error %v", err) t.Fatalf("applyChanges return an error %v", err)
...@@ -91,7 +100,7 @@ func TestDatasourceAsConfig(t *testing.T) { ...@@ -91,7 +100,7 @@ func TestDatasourceAsConfig(t *testing.T) {
} }
Convey("should have two new datasources", func() { Convey("should have two new datasources", func() {
dc := newDatasourceConfiguration(logger, fakeRepo) dc := newDatasourceConfiguration(logger)
err := dc.applyChanges(twoDatasourcesConfig) err := dc.applyChanges(twoDatasourcesConfig)
if err != nil { if err != nil {
t.Fatalf("applyChanges return an error %v", err) t.Fatalf("applyChanges return an error %v", err)
...@@ -140,27 +149,28 @@ type fakeRepository struct { ...@@ -140,27 +149,28 @@ type fakeRepository struct {
loadAll []*models.DataSource loadAll []*models.DataSource
} }
func (fc *fakeRepository) delete(cmd *models.DeleteDataSourceByIdCommand) error { func mockDelete(cmd *models.DeleteDataSourceByIdCommand) error {
fc.deleted = append(fc.deleted, cmd) fakeRepo.deleted = append(fakeRepo.deleted, cmd)
return nil return nil
} }
func (fc *fakeRepository) update(cmd *models.UpdateDataSourceCommand) error { func mockUpdate(cmd *models.UpdateDataSourceCommand) error {
fc.updated = append(fc.updated, cmd) fakeRepo.updated = append(fakeRepo.updated, cmd)
return nil return nil
} }
func (fc *fakeRepository) insert(cmd *models.AddDataSourceCommand) error { func mockInsert(cmd *models.AddDataSourceCommand) error {
fc.inserted = append(fc.inserted, cmd) fakeRepo.inserted = append(fakeRepo.inserted, cmd)
return nil return nil
} }
func (fc *fakeRepository) loadAllDatasources() ([]*models.DataSource, error) { func mockGetAll(cmd *models.GetAllDataSourcesQuery) error {
return fc.loadAll, nil cmd.Result = fakeRepo.loadAll
return nil
} }
func (fc *fakeRepository) get(cmd *models.GetDataSourceByNameQuery) error { func mockGet(cmd *models.GetDataSourceByNameQuery) error {
for _, v := range fc.loadAll { for _, v := range fakeRepo.loadAll {
if cmd.Name == v.Name && cmd.OrgId == v.OrgId { if cmd.Name == v.Name && cmd.OrgId == v.OrgId {
cmd.Result = v cmd.Result = v
return nil return nil
......
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