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 (
"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
}
pkg/services/provisioning/datasources/datasources_test.go
View file @
c6a6ae26
...
...
@@ -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
)
d
elete
(
cmd
*
models
.
DeleteDataSourceByIdCommand
)
error
{
f
c
.
deleted
=
append
(
fc
.
deleted
,
cmd
)
func
mockD
elete
(
cmd
*
models
.
DeleteDataSourceByIdCommand
)
error
{
f
akeRepo
.
deleted
=
append
(
fakeRepo
.
deleted
,
cmd
)
return
nil
}
func
(
fc
*
fakeRepository
)
u
pdate
(
cmd
*
models
.
UpdateDataSourceCommand
)
error
{
f
c
.
updated
=
append
(
fc
.
updated
,
cmd
)
func
mockU
pdate
(
cmd
*
models
.
UpdateDataSourceCommand
)
error
{
f
akeRepo
.
updated
=
append
(
fakeRepo
.
updated
,
cmd
)
return
nil
}
func
(
fc
*
fakeRepository
)
i
nsert
(
cmd
*
models
.
AddDataSourceCommand
)
error
{
f
c
.
inserted
=
append
(
fc
.
inserted
,
cmd
)
func
mockI
nsert
(
cmd
*
models
.
AddDataSourceCommand
)
error
{
f
akeRepo
.
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
)
g
et
(
cmd
*
models
.
GetDataSourceByNameQuery
)
error
{
for
_
,
v
:=
range
f
c
.
loadAll
{
func
mockG
et
(
cmd
*
models
.
GetDataSourceByNameQuery
)
error
{
for
_
,
v
:=
range
f
akeRepo
.
loadAll
{
if
cmd
.
Name
==
v
.
Name
&&
cmd
.
OrgId
==
v
.
OrgId
{
cmd
.
Result
=
v
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