Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
c6a6ae26
Commit
c6a6ae26
authored
Oct 24, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
datasource as cfg: refactor to use bus
parent
dc002abe
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
60 deletions
+35
-60
pkg/services/provisioning/datasources/datasources.go
+9
-44
pkg/services/provisioning/datasources/datasources_test.go
+26
-16
No files found.
pkg/services/provisioning/datasources/datasources.go
View file @
c6a6ae26
...
@@ -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
}
pkg/services/provisioning/datasources/datasources_test.go
View file @
c6a6ae26
...
@@ -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
)
d
elete
(
cmd
*
models
.
DeleteDataSourceByIdCommand
)
error
{
func
mockD
elete
(
cmd
*
models
.
DeleteDataSourceByIdCommand
)
error
{
f
c
.
deleted
=
append
(
fc
.
deleted
,
cmd
)
f
akeRepo
.
deleted
=
append
(
fakeRepo
.
deleted
,
cmd
)
return
nil
return
nil
}
}
func
(
fc
*
fakeRepository
)
u
pdate
(
cmd
*
models
.
UpdateDataSourceCommand
)
error
{
func
mockU
pdate
(
cmd
*
models
.
UpdateDataSourceCommand
)
error
{
f
c
.
updated
=
append
(
fc
.
updated
,
cmd
)
f
akeRepo
.
updated
=
append
(
fakeRepo
.
updated
,
cmd
)
return
nil
return
nil
}
}
func
(
fc
*
fakeRepository
)
i
nsert
(
cmd
*
models
.
AddDataSourceCommand
)
error
{
func
mockI
nsert
(
cmd
*
models
.
AddDataSourceCommand
)
error
{
f
c
.
inserted
=
append
(
fc
.
inserted
,
cmd
)
f
akeRepo
.
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
)
g
et
(
cmd
*
models
.
GetDataSourceByNameQuery
)
error
{
func
mockG
et
(
cmd
*
models
.
GetDataSourceByNameQuery
)
error
{
for
_
,
v
:=
range
f
c
.
loadAll
{
for
_
,
v
:=
range
f
akeRepo
.
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment