provisioning: enables title changes for dashboards

This commit is contained in:
bergquist
2018-01-24 15:22:03 +01:00
parent 77a4ccb822
commit 57e7048b8f
3 changed files with 55 additions and 16 deletions

View File

@@ -30,7 +30,7 @@ type fileReader struct {
log log.Logger log log.Logger
dashboardRepo dashboards.Repository dashboardRepo dashboards.Repository
cache *dashboardCache cache *dashboardCache
createWalk func(fr *fileReader, folderId int64) filepath.WalkFunc createWalk func(fr *fileReader, folderId int64, provisionedDashboards map[string]*models.DashboardProvisioning) filepath.WalkFunc
} }
func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) { func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
@@ -98,7 +98,26 @@ func (fr *fileReader) startWalkingDisk() error {
return err return err
} }
return filepath.Walk(fr.Path, fr.createWalk(fr, folderId)) byPath, err := getProvisionedDashboardByPath(fr.dashboardRepo, fr.Cfg.Name)
if err != nil {
return err
}
return filepath.Walk(fr.Path, fr.createWalk(fr, folderId, byPath))
}
func getProvisionedDashboardByPath(repo dashboards.Repository, name string) (map[string]*models.DashboardProvisioning, error) {
arr, err := repo.GetProvisionedDashboardData(name)
if err != nil {
return nil, err
}
byPath := map[string]*models.DashboardProvisioning{}
for _, pd := range arr {
byPath[pd.ExternalId] = pd
}
return byPath, nil
} }
func getOrCreateFolderId(cfg *DashboardsAsConfig, repo dashboards.Repository) (int64, error) { func getOrCreateFolderId(cfg *DashboardsAsConfig, repo dashboards.Repository) (int64, error) {
@@ -150,7 +169,7 @@ func resolveSymlink(fileinfo os.FileInfo, path string) (os.FileInfo, error) {
return fileinfo, err return fileinfo, err
} }
func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc { func createWalkFn(fr *fileReader, folderId int64, provisionedDashboards map[string]*models.DashboardProvisioning) filepath.WalkFunc {
return func(path string, fileInfo os.FileInfo, err error) error { return func(path string, fileInfo os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@@ -177,18 +196,31 @@ func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc {
return nil return nil
} }
if dash.Dashboard.Id != 0 { var dbDashboard *models.Dashboard
fr.log.Error("Cannot provision dashboard. Please remove the id property from the json file") cmd := &models.GetDashboardQuery{}
return nil provisionedData, allReadyProvisioned := provisionedDashboards[path]
// see if the
if allReadyProvisioned {
dash.Dashboard.Id = provisionedData.DashboardId
dash.Dashboard.Data.Set("id", provisionedData.DashboardId)
cmd.Id = provisionedData.DashboardId
} else {
if dash.Dashboard.Id != 0 {
fr.log.Error("Cannot provision dashboard. Please remove the id property from the json file")
return nil
}
cmd.Slug = dash.Dashboard.Slug
} }
cmd := &models.GetDashboardQuery{Slug: dash.Dashboard.Slug}
err = bus.Dispatch(cmd) err = bus.Dispatch(cmd)
dbDashboard = cmd.Result
// if we don't have the dashboard in the db, save it! // if we don't have the dashboard in the db, save it!
if err == models.ErrDashboardNotFound { if err == models.ErrDashboardNotFound {
fr.log.Debug("saving new dashboard", "file", path) fr.log.Debug("saving new dashboard", "file", path)
err = saveDashboard(fr, path, dash, fileInfo.ModTime()) err = saveDashboard(fr, path, dash)
return err return err
} }
@@ -198,24 +230,30 @@ func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc {
} }
// break if db version is newer then fil version // break if db version is newer then fil version
if cmd.Result.Updated.Unix() >= resolvedFileInfo.ModTime().Unix() { if dbDashboard.Updated.Unix() >= resolvedFileInfo.ModTime().Unix() {
return nil return nil
} }
fr.log.Debug("loading dashboard from disk into database.", "file", path) fr.log.Debug("loading dashboard from disk into database.", "file", path)
err = saveDashboard(fr, path, dash, fileInfo.ModTime()) err = saveDashboard(fr, path, dash)
return err return err
} }
} }
func saveDashboard(fr *fileReader, path string, dash *dashboards.SaveDashboardDTO, modTime time.Time) error {
func saveDashboard(fr *fileReader, path string, dash *dashboards.SaveDashboardDTO) error {
d := &models.DashboardProvisioning{ d := &models.DashboardProvisioning{
ExternalId: path, ExternalId: path,
Name: fr.Cfg.Name, Name: fr.Cfg.Name,
} }
_, err := fr.dashboardRepo.SaveProvisionedDashboard(dash, d)
return err _, err := fr.dashboardRepo.SaveProvisionedDashboard(dash, d)
if err != nil {
return err
}
fr.cache.addDashboardCache(path, dash)
return nil
} }
func validateWalkablePath(fileInfo os.FileInfo) (bool, error) { func validateWalkablePath(fileInfo os.FileInfo) (bool, error) {

View File

@@ -174,13 +174,15 @@ func TestDashboardFileReader(t *testing.T) {
reader, err := NewDashboardFileReader(cfg, log.New("test-logger")) reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
So(err, ShouldBeNil) So(err, ShouldBeNil)
emptyProvisioned := map[string]*models.DashboardProvisioning{}
Convey("should skip dirs that starts with .", func() { Convey("should skip dirs that starts with .", func() {
shouldSkip := reader.createWalk(reader, 0)("path", &FakeFileInfo{isDirectory: true, name: ".folder"}, nil) shouldSkip := reader.createWalk(reader, 0, emptyProvisioned)("path", &FakeFileInfo{isDirectory: true, name: ".folder"}, nil)
So(shouldSkip, ShouldEqual, filepath.SkipDir) So(shouldSkip, ShouldEqual, filepath.SkipDir)
}) })
Convey("should keep walking if file is not .json", func() { Convey("should keep walking if file is not .json", func() {
shouldSkip := reader.createWalk(reader, 0)("path", &FakeFileInfo{isDirectory: true, name: "folder"}, nil) shouldSkip := reader.createWalk(reader, 0, emptyProvisioned)("path", &FakeFileInfo{isDirectory: true, name: "folder"}, nil)
So(shouldSkip, ShouldBeNil) So(shouldSkip, ShouldBeNil)
}) })
}) })

View File

@@ -31,7 +31,6 @@ func saveDashboard(sess *DBSession, cmd *m.SaveDashboardCommand) error {
// try get existing dashboard // try get existing dashboard
var existing, sameTitle m.Dashboard var existing, sameTitle m.Dashboard
if dash.Id > 0 { if dash.Id > 0 {
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing) dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
if err != nil { if err != nil {