Commit c6a6ae26 by bergquist

datasource as cfg: refactor to use bus

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