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
930da631
Commit
930da631
authored
Oct 11, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
datasource as cfg: setting for purging datasources not in cfg
parent
0f29b8ac
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
14 deletions
+62
-14
pkg/cmd/grafana-server/server.go
+1
-1
pkg/setting/datasources/datasources.go
+30
-12
pkg/setting/datasources/datasources_test.go
+31
-1
No files found.
pkg/cmd/grafana-server/server.go
View file @
930da631
...
...
@@ -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"
)
...
...
pkg/setting/datasources/datasources.go
View file @
930da631
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,18 +44,39 @@ 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
all
,
err
:=
dc
.
repository
.
loadAllDatasources
()
if
err
!=
nil
{
return
err
}
for
_
,
ds
:=
range
datasources
.
Datasources
{
if
ds
.
OrgId
==
0
{
ds
.
OrgId
=
1
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
)
...
...
pkg/setting/datasources/datasources_test.go
View file @
930da631
...
...
@@ -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
)
})
})
})
})
}
...
...
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