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
80f40040
Unverified
Commit
80f40040
authored
Apr 15, 2020
by
Carl Bergquist
Committed by
GitHub
Apr 15, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Linting fixes for dashboard provsioning (#23576)
parent
010c67cc
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
106 additions
and
84 deletions
+106
-84
Makefile
+2
-1
pkg/api/dashboard.go
+3
-3
pkg/api/dashboard_test.go
+1
-1
pkg/services/provisioning/dashboards/config_reader.go
+1
-1
pkg/services/provisioning/dashboards/dashboard.go
+19
-11
pkg/services/provisioning/dashboards/dashboard_mock.go
+22
-15
pkg/services/provisioning/dashboards/file_reader.go
+31
-26
pkg/services/provisioning/dashboards/file_reader_linux_test.go
+1
-1
pkg/services/provisioning/dashboards/file_reader_test.go
+15
-14
pkg/services/provisioning/provisioning.go
+4
-4
pkg/services/provisioning/provisioning_mock.go
+6
-6
pkg/services/provisioning/provisioning_test.go
+1
-1
No files found.
Makefile
View file @
80f40040
...
...
@@ -85,7 +85,8 @@ revive-alerting: scripts/go/bin/revive
@
scripts/go/bin/revive
\
-formatter
stylish
\
./pkg/services/alerting/...
\
./pkg/services/provisioning/datasources/...
./pkg/services/provisioning/datasources/...
\
./pkg/services/provisioning/dashboards/...
scripts/go/bin/golangci-lint
:
scripts/go/go.mod
@
cd
scripts/go
;
\
...
...
pkg/api/dashboard.go
View file @
80f40040
...
...
@@ -113,8 +113,8 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) Response {
}
if
provisioningData
!=
nil
{
allowU
iUpdate
:=
hs
.
ProvisioningService
.
GetAllowUi
UpdatesFromConfig
(
provisioningData
.
Name
)
if
!
allowU
i
Update
{
allowU
IUpdate
:=
hs
.
ProvisioningService
.
GetAllowUI
UpdatesFromConfig
(
provisioningData
.
Name
)
if
!
allowU
I
Update
{
meta
.
Provisioned
=
true
}
...
...
@@ -232,7 +232,7 @@ func (hs *HTTPServer) PostDashboard(c *models.ReqContext, cmd models.SaveDashboa
allowUiUpdate
:=
true
if
provisioningData
!=
nil
{
allowUiUpdate
=
hs
.
ProvisioningService
.
GetAllowU
i
UpdatesFromConfig
(
provisioningData
.
Name
)
allowUiUpdate
=
hs
.
ProvisioningService
.
GetAllowU
I
UpdatesFromConfig
(
provisioningData
.
Name
)
}
dashItem
:=
&
dashboards
.
SaveDashboardDTO
{
...
...
pkg/api/dashboard_test.go
View file @
80f40040
...
...
@@ -964,7 +964,7 @@ func TestDashboardApiEndpoint(t *testing.T) {
mock
.
GetDashboardProvisionerResolvedPathFunc
=
func
(
name
string
)
string
{
return
"/tmp/grafana/dashboards"
}
mock
.
GetAllowU
i
UpdatesFromConfigFunc
=
func
(
name
string
)
bool
{
mock
.
GetAllowU
I
UpdatesFromConfigFunc
=
func
(
name
string
)
bool
{
return
true
}
...
...
pkg/services/provisioning/dashboards/config_reader.go
View file @
80f40040
...
...
@@ -92,7 +92,7 @@ func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
dashboard
.
UpdateIntervalSeconds
=
10
}
if
len
(
dashboard
.
FolderUid
)
>
0
{
uidUsage
[
dashboard
.
FolderUid
]
+=
1
uidUsage
[
dashboard
.
FolderUid
]
++
}
}
...
...
pkg/services/provisioning/dashboards/dashboard.go
View file @
80f40040
...
...
@@ -9,22 +9,27 @@ import (
"github.com/grafana/grafana/pkg/util/errutil"
)
// DashboardProvisioner is responsible for syncing dashboard from disc to
// Grafanas database.
type
DashboardProvisioner
interface
{
Provision
()
error
PollChanges
(
ctx
context
.
Context
)
GetProvisionerResolvedPath
(
name
string
)
string
GetAllowU
i
UpdatesFromConfig
(
name
string
)
bool
GetAllowU
I
UpdatesFromConfig
(
name
string
)
bool
}
// DashboardProvisionerFactory creates DashboardProvisioners based on input
type
DashboardProvisionerFactory
func
(
string
)
(
DashboardProvisioner
,
error
)
type
DashboardProvisionerImpl
struct
{
// Provisioner is responsible for syncing dashboard from disc to Grafanas database.
type
Provisioner
struct
{
log
log
.
Logger
fileReaders
[]
*
f
ileReader
fileReaders
[]
*
F
ileReader
configs
[]
*
DashboardsAsConfig
}
func
NewDashboardProvisionerImpl
(
configDirectory
string
)
(
*
DashboardProvisionerImpl
,
error
)
{
// New returns a new DashboardProvisioner
func
New
(
configDirectory
string
)
(
*
Provisioner
,
error
)
{
logger
:=
log
.
New
(
"provisioning.dashboard"
)
cfgReader
:=
&
configReader
{
path
:
configDirectory
,
log
:
logger
}
configs
,
err
:=
cfgReader
.
readConfig
()
...
...
@@ -39,7 +44,7 @@ func NewDashboardProvisionerImpl(configDirectory string) (*DashboardProvisionerI
return
nil
,
errutil
.
Wrap
(
"Failed to initialize file readers"
,
err
)
}
d
:=
&
DashboardProvisionerImpl
{
d
:=
&
Provisioner
{
log
:
logger
,
fileReaders
:
fileReaders
,
configs
:
configs
,
...
...
@@ -48,7 +53,9 @@ func NewDashboardProvisionerImpl(configDirectory string) (*DashboardProvisionerI
return
d
,
nil
}
func
(
provider
*
DashboardProvisionerImpl
)
Provision
()
error
{
// Provision starts scanning the disc for dashboards and updates
// the database with the latest versions of those dashboards
func
(
provider
*
Provisioner
)
Provision
()
error
{
for
_
,
reader
:=
range
provider
.
fileReaders
{
if
err
:=
reader
.
startWalkingDisk
();
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
...
...
@@ -66,7 +73,7 @@ func (provider *DashboardProvisionerImpl) Provision() error {
// PollChanges starts polling for changes in dashboard definition files. It creates goroutine for each provider
// defined in the config.
func
(
provider
*
DashboardProvisionerImpl
)
PollChanges
(
ctx
context
.
Context
)
{
func
(
provider
*
Provisioner
)
PollChanges
(
ctx
context
.
Context
)
{
for
_
,
reader
:=
range
provider
.
fileReaders
{
go
reader
.
pollChanges
(
ctx
)
}
...
...
@@ -74,7 +81,7 @@ func (provider *DashboardProvisionerImpl) PollChanges(ctx context.Context) {
// GetProvisionerResolvedPath returns resolved path for the specified provisioner name. Can be used to generate
// relative path to provisioning file from it's external_id.
func
(
provider
*
DashboardProvisionerImpl
)
GetProvisionerResolvedPath
(
name
string
)
string
{
func
(
provider
*
Provisioner
)
GetProvisionerResolvedPath
(
name
string
)
string
{
for
_
,
reader
:=
range
provider
.
fileReaders
{
if
reader
.
Cfg
.
Name
==
name
{
return
reader
.
resolvedPath
()
...
...
@@ -83,7 +90,8 @@ func (provider *DashboardProvisionerImpl) GetProvisionerResolvedPath(name string
return
""
}
func
(
provider
*
DashboardProvisionerImpl
)
GetAllowUiUpdatesFromConfig
(
name
string
)
bool
{
// GetAllowUIUpdatesFromConfig return if a dashboard provisioner allows updates from the UI
func
(
provider
*
Provisioner
)
GetAllowUIUpdatesFromConfig
(
name
string
)
bool
{
for
_
,
config
:=
range
provider
.
configs
{
if
config
.
Name
==
name
{
return
config
.
AllowUiUpdates
...
...
@@ -92,8 +100,8 @@ func (provider *DashboardProvisionerImpl) GetAllowUiUpdatesFromConfig(name strin
return
false
}
func
getFileReaders
(
configs
[]
*
DashboardsAsConfig
,
logger
log
.
Logger
)
([]
*
f
ileReader
,
error
)
{
var
readers
[]
*
f
ileReader
func
getFileReaders
(
configs
[]
*
DashboardsAsConfig
,
logger
log
.
Logger
)
([]
*
F
ileReader
,
error
)
{
var
readers
[]
*
F
ileReader
for
_
,
config
:=
range
configs
{
switch
config
.
Type
{
...
...
pkg/services/provisioning/dashboards/dashboard_mock.go
View file @
80f40040
...
...
@@ -2,28 +2,32 @@ package dashboards
import
"context"
type
Calls
struct
{
// Calls is a mock implementation of the provisioner interface
type
calls
struct
{
Provision
[]
interface
{}
PollChanges
[]
interface
{}
GetProvisionerResolvedPath
[]
interface
{}
GetAllowU
i
UpdatesFromConfig
[]
interface
{}
GetAllowU
I
UpdatesFromConfig
[]
interface
{}
}
type
DashboardProvisionerMock
struct
{
Calls
*
Calls
// ProvisionerMock is a mock implementation of `Provisioner`
type
ProvisionerMock
struct
{
Calls
*
calls
ProvisionFunc
func
()
error
PollChangesFunc
func
(
ctx
context
.
Context
)
GetProvisionerResolvedPathFunc
func
(
name
string
)
string
GetAllowU
i
UpdatesFromConfigFunc
func
(
name
string
)
bool
GetAllowU
I
UpdatesFromConfigFunc
func
(
name
string
)
bool
}
func
NewDashboardProvisionerMock
()
*
DashboardProvisionerMock
{
return
&
DashboardProvisionerMock
{
Calls
:
&
Calls
{},
// NewDashboardProvisionerMock returns a new dashboardprovisionermock
func
NewDashboardProvisionerMock
()
*
ProvisionerMock
{
return
&
ProvisionerMock
{
Calls
:
&
calls
{},
}
}
func
(
dpm
*
DashboardProvisionerMock
)
Provision
()
error
{
// Provision is a mock implementation of `Provisioner.Provision`
func
(
dpm
*
ProvisionerMock
)
Provision
()
error
{
dpm
.
Calls
.
Provision
=
append
(
dpm
.
Calls
.
Provision
,
nil
)
if
dpm
.
ProvisionFunc
!=
nil
{
return
dpm
.
ProvisionFunc
()
...
...
@@ -31,14 +35,16 @@ func (dpm *DashboardProvisionerMock) Provision() error {
return
nil
}
func
(
dpm
*
DashboardProvisionerMock
)
PollChanges
(
ctx
context
.
Context
)
{
// PollChanges is a mock implementation of `Provisioner.PollChanges`
func
(
dpm
*
ProvisionerMock
)
PollChanges
(
ctx
context
.
Context
)
{
dpm
.
Calls
.
PollChanges
=
append
(
dpm
.
Calls
.
PollChanges
,
ctx
)
if
dpm
.
PollChangesFunc
!=
nil
{
dpm
.
PollChangesFunc
(
ctx
)
}
}
func
(
dpm
*
DashboardProvisionerMock
)
GetProvisionerResolvedPath
(
name
string
)
string
{
// GetProvisionerResolvedPath is a mock implementation of `Provisioner.GetProvisionerResolvedPath`
func
(
dpm
*
ProvisionerMock
)
GetProvisionerResolvedPath
(
name
string
)
string
{
dpm
.
Calls
.
GetProvisionerResolvedPath
=
append
(
dpm
.
Calls
.
GetProvisionerResolvedPath
,
name
)
if
dpm
.
GetProvisionerResolvedPathFunc
!=
nil
{
return
dpm
.
GetProvisionerResolvedPathFunc
(
name
)
...
...
@@ -46,10 +52,11 @@ func (dpm *DashboardProvisionerMock) GetProvisionerResolvedPath(name string) str
return
""
}
func
(
dpm
*
DashboardProvisionerMock
)
GetAllowUiUpdatesFromConfig
(
name
string
)
bool
{
dpm
.
Calls
.
GetAllowUiUpdatesFromConfig
=
append
(
dpm
.
Calls
.
GetAllowUiUpdatesFromConfig
,
name
)
if
dpm
.
GetAllowUiUpdatesFromConfigFunc
!=
nil
{
return
dpm
.
GetAllowUiUpdatesFromConfigFunc
(
name
)
// GetAllowUIUpdatesFromConfig is a mock implementation of `Provisioner.GetAllowUIUpdatesFromConfig`
func
(
dpm
*
ProvisionerMock
)
GetAllowUIUpdatesFromConfig
(
name
string
)
bool
{
dpm
.
Calls
.
GetAllowUIUpdatesFromConfig
=
append
(
dpm
.
Calls
.
GetAllowUIUpdatesFromConfig
,
name
)
if
dpm
.
GetAllowUIUpdatesFromConfigFunc
!=
nil
{
return
dpm
.
GetAllowUIUpdatesFromConfigFunc
(
name
)
}
return
false
}
pkg/services/provisioning/dashboards/file_reader.go
View file @
80f40040
...
...
@@ -21,17 +21,22 @@ import (
)
var
(
// ErrFolderNameMissing is returned when folder name is missing.
ErrFolderNameMissing
=
errors
.
New
(
"Folder name missing"
)
)
type
fileReader
struct
{
// FileReader is responsible for reading dashboards from disc and
// insert/update dashboards to the Grafana database using
// `dashboards.DashboardProvisioningService`
type
FileReader
struct
{
Cfg
*
DashboardsAsConfig
Path
string
log
log
.
Logger
dashboardProvisioningService
dashboards
.
DashboardProvisioningService
}
func
NewDashboardFileReader
(
cfg
*
DashboardsAsConfig
,
log
log
.
Logger
)
(
*
fileReader
,
error
)
{
// NewDashboardFileReader returns a new filereader based on `DashboardsAsConfig`
func
NewDashboardFileReader
(
cfg
*
DashboardsAsConfig
,
log
log
.
Logger
)
(
*
FileReader
,
error
)
{
var
path
string
path
,
ok
:=
cfg
.
Options
[
"path"
]
.
(
string
)
if
!
ok
{
...
...
@@ -43,7 +48,7 @@ func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
log
.
Warn
(
"[Deprecated] The folder property is deprecated. Please use path instead."
)
}
return
&
f
ileReader
{
return
&
F
ileReader
{
Cfg
:
cfg
,
Path
:
path
,
log
:
log
,
...
...
@@ -52,7 +57,7 @@ func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
}
// pollChanges periodically runs startWalkingDisk based on interval specified in the config.
func
(
fr
*
f
ileReader
)
pollChanges
(
ctx
context
.
Context
)
{
func
(
fr
*
F
ileReader
)
pollChanges
(
ctx
context
.
Context
)
{
ticker
:=
time
.
NewTicker
(
time
.
Duration
(
int64
(
time
.
Second
)
*
fr
.
Cfg
.
UpdateIntervalSeconds
))
for
{
...
...
@@ -69,14 +74,14 @@ func (fr *fileReader) pollChanges(ctx context.Context) {
// startWalkingDisk traverses the file system for defined path, reads dashboard definition files and applies any change
// to the database.
func
(
fr
*
f
ileReader
)
startWalkingDisk
()
error
{
func
(
fr
*
F
ileReader
)
startWalkingDisk
()
error
{
fr
.
log
.
Debug
(
"Start walking disk"
,
"path"
,
fr
.
Path
)
resolvedPath
:=
fr
.
resolvedPath
()
if
_
,
err
:=
os
.
Stat
(
resolvedPath
);
err
!=
nil
{
return
err
}
folderI
d
,
err
:=
getOrCreateFolderId
(
fr
.
Cfg
,
fr
.
dashboardProvisioningService
)
folderI
D
,
err
:=
getOrCreateFolderID
(
fr
.
Cfg
,
fr
.
dashboardProvisioningService
)
if
err
!=
nil
&&
err
!=
ErrFolderNameMissing
{
return
err
}
...
...
@@ -98,7 +103,7 @@ func (fr *fileReader) startWalkingDisk() error {
// save dashboards based on json files
for
path
,
fileInfo
:=
range
filesFoundOnDisk
{
provisioningMetadata
,
err
:=
fr
.
saveDashboard
(
path
,
folderI
d
,
fileInfo
,
provisionedDashboardRefs
)
provisioningMetadata
,
err
:=
fr
.
saveDashboard
(
path
,
folderI
D
,
fileInfo
,
provisionedDashboardRefs
)
sanityChecker
.
track
(
provisioningMetadata
)
if
err
!=
nil
{
fr
.
log
.
Error
(
"failed to save dashboard"
,
"error"
,
err
)
...
...
@@ -110,7 +115,7 @@ func (fr *fileReader) startWalkingDisk() error {
}
// handleMissingDashboardFiles will unprovision or delete dashboards which are missing on disk.
func
(
fr
*
f
ileReader
)
handleMissingDashboardFiles
(
provisionedDashboardRefs
map
[
string
]
*
models
.
DashboardProvisioning
,
filesFoundOnDisk
map
[
string
]
os
.
FileInfo
)
{
func
(
fr
*
F
ileReader
)
handleMissingDashboardFiles
(
provisionedDashboardRefs
map
[
string
]
*
models
.
DashboardProvisioning
,
filesFoundOnDisk
map
[
string
]
os
.
FileInfo
)
{
// find dashboards to delete since json file is missing
var
dashboardToDelete
[]
int64
for
path
,
provisioningData
:=
range
provisionedDashboardRefs
{
...
...
@@ -123,27 +128,27 @@ func (fr *fileReader) handleMissingDashboardFiles(provisionedDashboardRefs map[s
if
fr
.
Cfg
.
DisableDeletion
{
// If deletion is disabled for the provisioner we just remove provisioning metadata about the dashboard
// so afterwards the dashboard is considered unprovisioned.
for
_
,
dashboardI
d
:=
range
dashboardToDelete
{
fr
.
log
.
Debug
(
"unprovisioning provisioned dashboard. missing on disk"
,
"id"
,
dashboardI
d
)
err
:=
fr
.
dashboardProvisioningService
.
UnprovisionDashboard
(
dashboardI
d
)
for
_
,
dashboardI
D
:=
range
dashboardToDelete
{
fr
.
log
.
Debug
(
"unprovisioning provisioned dashboard. missing on disk"
,
"id"
,
dashboardI
D
)
err
:=
fr
.
dashboardProvisioningService
.
UnprovisionDashboard
(
dashboardI
D
)
if
err
!=
nil
{
fr
.
log
.
Error
(
"failed to unprovision dashboard"
,
"dashboard_id"
,
dashboardI
d
,
"error"
,
err
)
fr
.
log
.
Error
(
"failed to unprovision dashboard"
,
"dashboard_id"
,
dashboardI
D
,
"error"
,
err
)
}
}
}
else
{
// delete dashboard that are missing json file
for
_
,
dashboardI
d
:=
range
dashboardToDelete
{
fr
.
log
.
Debug
(
"deleting provisioned dashboard. missing on disk"
,
"id"
,
dashboardI
d
)
err
:=
fr
.
dashboardProvisioningService
.
DeleteProvisionedDashboard
(
dashboardI
d
,
fr
.
Cfg
.
OrgId
)
for
_
,
dashboardI
D
:=
range
dashboardToDelete
{
fr
.
log
.
Debug
(
"deleting provisioned dashboard. missing on disk"
,
"id"
,
dashboardI
D
)
err
:=
fr
.
dashboardProvisioningService
.
DeleteProvisionedDashboard
(
dashboardI
D
,
fr
.
Cfg
.
OrgId
)
if
err
!=
nil
{
fr
.
log
.
Error
(
"failed to delete dashboard"
,
"id"
,
dashboardI
d
,
"error"
,
err
)
fr
.
log
.
Error
(
"failed to delete dashboard"
,
"id"
,
dashboardI
D
,
"error"
,
err
)
}
}
}
}
// saveDashboard saves or updates the dashboard provisioning file at path.
func
(
fr
*
fileReader
)
saveDashboard
(
path
string
,
folderId
int64
,
fileInfo
os
.
FileInfo
,
provisionedDashboardRefs
map
[
string
]
*
models
.
DashboardProvisioning
)
(
provisioningMetadata
,
error
)
{
func
(
fr
*
FileReader
)
saveDashboard
(
path
string
,
folderID
int64
,
fileInfo
os
.
FileInfo
,
provisionedDashboardRefs
map
[
string
]
*
models
.
DashboardProvisioning
)
(
provisioningMetadata
,
error
)
{
provisioningMetadata
:=
provisioningMetadata
{}
resolvedFileInfo
,
err
:=
resolveSymlink
(
fileInfo
,
path
)
if
err
!=
nil
{
...
...
@@ -153,7 +158,7 @@ func (fr *fileReader) saveDashboard(path string, folderId int64, fileInfo os.Fil
provisionedData
,
alreadyProvisioned
:=
provisionedDashboardRefs
[
path
]
upToDate
:=
alreadyProvisioned
&&
provisionedData
.
Updated
>=
resolvedFileInfo
.
ModTime
()
.
Unix
()
jsonFile
,
err
:=
fr
.
readDashboardFromFile
(
path
,
resolvedFileInfo
.
ModTime
(),
folderI
d
)
jsonFile
,
err
:=
fr
.
readDashboardFromFile
(
path
,
resolvedFileInfo
.
ModTime
(),
folderI
D
)
if
err
!=
nil
{
fr
.
log
.
Error
(
"failed to load dashboard from "
,
"file"
,
path
,
"error"
,
err
)
return
provisioningMetadata
,
nil
...
...
@@ -207,7 +212,7 @@ func getProvisionedDashboardByPath(service dashboards.DashboardProvisioningServi
return
byPath
,
nil
}
func
getOrCreateFolderI
d
(
cfg
*
DashboardsAsConfig
,
service
dashboards
.
DashboardProvisioningService
)
(
int64
,
error
)
{
func
getOrCreateFolderI
D
(
cfg
*
DashboardsAsConfig
,
service
dashboards
.
DashboardProvisioningService
)
(
int64
,
error
)
{
if
cfg
.
Folder
==
""
{
return
0
,
ErrFolderNameMissing
}
...
...
@@ -288,13 +293,13 @@ func validateWalkablePath(fileInfo os.FileInfo) (bool, error) {
return
true
,
nil
}
type
dashboardJ
son
File
struct
{
type
dashboardJ
SON
File
struct
{
dashboard
*
dashboards
.
SaveDashboardDTO
checkSum
string
lastModified
time
.
Time
}
func
(
fr
*
fileReader
)
readDashboardFromFile
(
path
string
,
lastModified
time
.
Time
,
folderId
int64
)
(
*
dashboardJson
File
,
error
)
{
func
(
fr
*
FileReader
)
readDashboardFromFile
(
path
string
,
lastModified
time
.
Time
,
folderID
int64
)
(
*
dashboardJSON
File
,
error
)
{
reader
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -316,19 +321,19 @@ func (fr *fileReader) readDashboardFromFile(path string, lastModified time.Time,
return
nil
,
err
}
dash
,
err
:=
createDashboardJson
(
data
,
lastModified
,
fr
.
Cfg
,
folderI
d
)
dash
,
err
:=
createDashboardJson
(
data
,
lastModified
,
fr
.
Cfg
,
folderI
D
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
dashboardJ
son
File
{
return
&
dashboardJ
SON
File
{
dashboard
:
dash
,
checkSum
:
checkSum
,
lastModified
:
lastModified
,
},
nil
}
func
(
fr
*
f
ileReader
)
resolvedPath
()
string
{
func
(
fr
*
F
ileReader
)
resolvedPath
()
string
{
if
_
,
err
:=
os
.
Stat
(
fr
.
Path
);
os
.
IsNotExist
(
err
)
{
fr
.
log
.
Error
(
"Cannot read directory"
,
"error"
,
err
)
}
...
...
@@ -370,10 +375,10 @@ type provisioningSanityChecker struct {
func
(
checker
provisioningSanityChecker
)
track
(
pm
provisioningMetadata
)
{
if
len
(
pm
.
uid
)
>
0
{
checker
.
uidUsage
[
pm
.
uid
]
+=
1
checker
.
uidUsage
[
pm
.
uid
]
++
}
if
len
(
pm
.
title
)
>
0
{
checker
.
titleUsage
[
pm
.
title
]
+=
1
checker
.
titleUsage
[
pm
.
title
]
++
}
}
...
...
pkg/services/provisioning/dashboards/file_reader_linux_test.go
View file @
80f40040
...
...
@@ -27,7 +27,7 @@ func TestProvsionedSymlinkedFolder(t *testing.T) {
t
.
Error
(
"expected err to be nil"
)
}
want
,
err
:=
filepath
.
Abs
(
containingI
d
)
want
,
err
:=
filepath
.
Abs
(
containingI
D
)
if
err
!=
nil
{
t
.
Errorf
(
"expected err to be nil"
)
...
...
pkg/services/provisioning/dashboards/file_reader_test.go
View file @
80f40040
package
dashboards
import
(
"github.com/grafana/grafana/pkg/util"
"math/rand"
"os"
"path/filepath"
...
...
@@ -9,6 +8,8 @@ import (
"testing"
"time"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
...
...
@@ -21,7 +22,7 @@ var (
defaultDashboards
=
"testdata/test-dashboards/folder-one"
brokenDashboards
=
"testdata/test-dashboards/broken-dashboards"
oneDashboard
=
"testdata/test-dashboards/one-dashboard"
containingI
d
=
"testdata/test-dashboards/containing-id"
containingI
D
=
"testdata/test-dashboards/containing-id"
unprovision
=
"testdata/test-dashboards/unprovision"
fakeService
*
fakeDashboardProvisioningService
...
...
@@ -140,7 +141,7 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey
(
"Overrides id from dashboard.json files"
,
func
()
{
cfg
.
Options
[
"path"
]
=
containingI
d
cfg
.
Options
[
"path"
]
=
containingI
D
reader
,
err
:=
NewDashboardFileReader
(
cfg
,
logger
)
So
(
err
,
ShouldBeNil
)
...
...
@@ -171,8 +172,8 @@ func TestDashboardFileReader(t *testing.T) {
})
Convey
(
"Two dashboard providers should be able to provisioned the same dashboard without uid"
,
func
()
{
cfg1
:=
&
DashboardsAsConfig
{
Name
:
"1"
,
Type
:
"file"
,
OrgId
:
1
,
Folder
:
"f1"
,
Options
:
map
[
string
]
interface
{}{
"path"
:
containingI
d
}}
cfg2
:=
&
DashboardsAsConfig
{
Name
:
"2"
,
Type
:
"file"
,
OrgId
:
1
,
Folder
:
"f2"
,
Options
:
map
[
string
]
interface
{}{
"path"
:
containingI
d
}}
cfg1
:=
&
DashboardsAsConfig
{
Name
:
"1"
,
Type
:
"file"
,
OrgId
:
1
,
Folder
:
"f1"
,
Options
:
map
[
string
]
interface
{}{
"path"
:
containingI
D
}}
cfg2
:=
&
DashboardsAsConfig
{
Name
:
"2"
,
Type
:
"file"
,
OrgId
:
1
,
Folder
:
"f2"
,
Options
:
map
[
string
]
interface
{}{
"path"
:
containingI
D
}}
reader1
,
err
:=
NewDashboardFileReader
(
cfg1
,
logger
)
So
(
err
,
ShouldBeNil
)
...
...
@@ -212,7 +213,7 @@ func TestDashboardFileReader(t *testing.T) {
},
}
_
,
err
:=
getOrCreateFolderI
d
(
cfg
,
fakeService
)
_
,
err
:=
getOrCreateFolderI
D
(
cfg
,
fakeService
)
So
(
err
,
ShouldEqual
,
ErrFolderNameMissing
)
})
...
...
@@ -227,11 +228,11 @@ func TestDashboardFileReader(t *testing.T) {
},
}
folderI
d
,
err
:=
getOrCreateFolderId
(
cfg
,
fakeService
)
folderI
D
,
err
:=
getOrCreateFolderID
(
cfg
,
fakeService
)
So
(
err
,
ShouldBeNil
)
inserted
:=
false
for
_
,
d
:=
range
fakeService
.
inserted
{
if
d
.
Dashboard
.
IsFolder
&&
d
.
Dashboard
.
Id
==
folderI
d
{
if
d
.
Dashboard
.
IsFolder
&&
d
.
Dashboard
.
Id
==
folderI
D
{
inserted
=
true
}
}
...
...
@@ -410,10 +411,10 @@ func (s *fakeDashboardProvisioningService) SaveFolderForProvisionedDashboards(dt
return
dto
.
Dashboard
,
nil
}
func
(
s
*
fakeDashboardProvisioningService
)
UnprovisionDashboard
(
dashboardI
d
int64
)
error
{
func
(
s
*
fakeDashboardProvisioningService
)
UnprovisionDashboard
(
dashboardI
D
int64
)
error
{
for
key
,
val
:=
range
s
.
provisioned
{
for
index
,
dashboard
:=
range
val
{
if
dashboard
.
DashboardId
==
dashboardI
d
{
if
dashboard
.
DashboardId
==
dashboardI
D
{
s
.
provisioned
[
key
]
=
append
(
s
.
provisioned
[
key
][
:
index
],
s
.
provisioned
[
key
][
index
+
1
:
]
...
)
}
}
...
...
@@ -421,21 +422,21 @@ func (s *fakeDashboardProvisioningService) UnprovisionDashboard(dashboardId int6
return
nil
}
func
(
s
*
fakeDashboardProvisioningService
)
DeleteProvisionedDashboard
(
dashboardI
d
int64
,
orgId
int64
)
error
{
err
:=
s
.
UnprovisionDashboard
(
dashboardI
d
)
func
(
s
*
fakeDashboardProvisioningService
)
DeleteProvisionedDashboard
(
dashboardI
D
int64
,
orgID
int64
)
error
{
err
:=
s
.
UnprovisionDashboard
(
dashboardI
D
)
if
err
!=
nil
{
return
err
}
for
index
,
val
:=
range
s
.
inserted
{
if
val
.
Dashboard
.
Id
==
dashboardI
d
{
if
val
.
Dashboard
.
Id
==
dashboardI
D
{
s
.
inserted
=
append
(
s
.
inserted
[
:
index
],
s
.
inserted
[
util
.
MinInt
(
index
+
1
,
len
(
s
.
inserted
))
:
]
...
)
}
}
return
nil
}
func
(
s
*
fakeDashboardProvisioningService
)
GetProvisionedDashboardDataByDashboardId
(
dashboardI
d
int64
)
(
*
models
.
DashboardProvisioning
,
error
)
{
func
(
s
*
fakeDashboardProvisioningService
)
GetProvisionedDashboardDataByDashboardId
(
dashboardI
D
int64
)
(
*
models
.
DashboardProvisioning
,
error
)
{
return
nil
,
nil
}
...
...
pkg/services/provisioning/provisioning.go
View file @
80f40040
...
...
@@ -20,13 +20,13 @@ type ProvisioningService interface {
ProvisionNotifications
()
error
ProvisionDashboards
()
error
GetDashboardProvisionerResolvedPath
(
name
string
)
string
GetAllowU
i
UpdatesFromConfig
(
name
string
)
bool
GetAllowU
I
UpdatesFromConfig
(
name
string
)
bool
}
func
init
()
{
registry
.
RegisterService
(
NewProvisioningServiceImpl
(
func
(
path
string
)
(
dashboards
.
DashboardProvisioner
,
error
)
{
return
dashboards
.
New
DashboardProvisionerImpl
(
path
)
return
dashboards
.
New
(
path
)
},
notifiers
.
Provision
,
datasources
.
Provision
,
...
...
@@ -138,8 +138,8 @@ func (ps *provisioningServiceImpl) GetDashboardProvisionerResolvedPath(name stri
return
ps
.
dashboardProvisioner
.
GetProvisionerResolvedPath
(
name
)
}
func
(
ps
*
provisioningServiceImpl
)
GetAllowU
i
UpdatesFromConfig
(
name
string
)
bool
{
return
ps
.
dashboardProvisioner
.
GetAllowU
i
UpdatesFromConfig
(
name
)
func
(
ps
*
provisioningServiceImpl
)
GetAllowU
I
UpdatesFromConfig
(
name
string
)
bool
{
return
ps
.
dashboardProvisioner
.
GetAllowU
I
UpdatesFromConfig
(
name
)
}
func
(
ps
*
provisioningServiceImpl
)
cancelPolling
()
{
...
...
pkg/services/provisioning/provisioning_mock.go
View file @
80f40040
...
...
@@ -5,7 +5,7 @@ type Calls struct {
ProvisionNotifications
[]
interface
{}
ProvisionDashboards
[]
interface
{}
GetDashboardProvisionerResolvedPath
[]
interface
{}
GetAllowU
i
UpdatesFromConfig
[]
interface
{}
GetAllowU
I
UpdatesFromConfig
[]
interface
{}
}
type
ProvisioningServiceMock
struct
{
...
...
@@ -14,7 +14,7 @@ type ProvisioningServiceMock struct {
ProvisionNotificationsFunc
func
()
error
ProvisionDashboardsFunc
func
()
error
GetDashboardProvisionerResolvedPathFunc
func
(
name
string
)
string
GetAllowU
i
UpdatesFromConfigFunc
func
(
name
string
)
bool
GetAllowU
I
UpdatesFromConfigFunc
func
(
name
string
)
bool
}
func
NewProvisioningServiceMock
()
*
ProvisioningServiceMock
{
...
...
@@ -55,10 +55,10 @@ func (mock *ProvisioningServiceMock) GetDashboardProvisionerResolvedPath(name st
return
""
}
func
(
mock
*
ProvisioningServiceMock
)
GetAllowU
i
UpdatesFromConfig
(
name
string
)
bool
{
mock
.
Calls
.
GetAllowU
iUpdatesFromConfig
=
append
(
mock
.
Calls
.
GetAllowUi
UpdatesFromConfig
,
name
)
if
mock
.
GetAllowU
i
UpdatesFromConfigFunc
!=
nil
{
return
mock
.
GetAllowU
i
UpdatesFromConfigFunc
(
name
)
func
(
mock
*
ProvisioningServiceMock
)
GetAllowU
I
UpdatesFromConfig
(
name
string
)
bool
{
mock
.
Calls
.
GetAllowU
IUpdatesFromConfig
=
append
(
mock
.
Calls
.
GetAllowUI
UpdatesFromConfig
,
name
)
if
mock
.
GetAllowU
I
UpdatesFromConfigFunc
!=
nil
{
return
mock
.
GetAllowU
I
UpdatesFromConfigFunc
(
name
)
}
return
false
}
pkg/services/provisioning/provisioning_test.go
View file @
80f40040
...
...
@@ -75,7 +75,7 @@ type serviceTestStruct struct {
startService
func
()
cancel
func
()
mock
*
dashboards
.
Dashboard
ProvisionerMock
mock
*
dashboards
.
ProvisionerMock
service
*
provisioningServiceImpl
}
...
...
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