Commit 4356e980 by Daniel Lee

dashfolders: create app folder on dashboard import

parent 07cd1826
...@@ -49,6 +49,30 @@ func ImportDashboard(cmd *ImportDashboardCommand) error { ...@@ -49,6 +49,30 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
if dashboard, err = loadPluginDashboard(cmd.PluginId, cmd.Path); err != nil { if dashboard, err = loadPluginDashboard(cmd.PluginId, cmd.Path); err != nil {
return err return err
} }
var plugin *PluginBase
if plugin, err = getPlugin(cmd.PluginId); err != nil {
return err
}
folderDash := simplejson.NewFromAny(map[string]interface{}{
"title": plugin.Name,
})
saveCmd := m.SaveDashboardCommand{
Dashboard: folderDash,
OrgId: cmd.OrgId,
UserId: cmd.UserId,
PluginId: cmd.PluginId,
IsFolder: true,
}
if err := bus.Dispatch(&saveCmd); err != nil {
return err
}
dashboard.FolderId = saveCmd.Result.Id
} else { } else {
dashboard = m.NewDashboardFromJson(cmd.Dashboard) dashboard = m.NewDashboardFromJson(cmd.Dashboard)
} }
...@@ -69,6 +93,7 @@ func ImportDashboard(cmd *ImportDashboardCommand) error { ...@@ -69,6 +93,7 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
UserId: cmd.UserId, UserId: cmd.UserId,
Overwrite: cmd.Overwrite, Overwrite: cmd.Overwrite,
PluginId: cmd.PluginId, PluginId: cmd.PluginId,
FolderId: dashboard.FolderId,
} }
if err := bus.Dispatch(&saveCmd); err != nil { if err := bus.Dispatch(&saveCmd); err != nil {
......
...@@ -22,10 +22,19 @@ func TestDashboardImport(t *testing.T) { ...@@ -22,10 +22,19 @@ func TestDashboardImport(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
folderId := int64(1000)
var importedDash *m.Dashboard var importedDash *m.Dashboard
var createdFolder *m.Dashboard
bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error { bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
importedDash = cmd.GetDashboardModel() if cmd.IsFolder {
cmd.Result = importedDash createdFolder = cmd.GetDashboardModel()
createdFolder.Id = folderId
cmd.Result = createdFolder
} else {
importedDash = cmd.GetDashboardModel()
cmd.Result = importedDash
}
return nil return nil
}) })
...@@ -54,21 +63,28 @@ func TestDashboardImport(t *testing.T) { ...@@ -54,21 +63,28 @@ func TestDashboardImport(t *testing.T) {
panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0) panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
So(panel.Get("datasource").MustString(), ShouldEqual, "graphite") So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
So(importedDash.FolderId, ShouldEqual, folderId)
})
Convey("should create app folder", func() {
So(createdFolder.Title, ShouldEqual, "Test App")
So(createdFolder.Id, ShouldEqual, folderId)
}) })
}) })
Convey("When evaling dashboard template", t, func() { Convey("When evaling dashboard template", t, func() {
template, _ := simplejson.NewJson([]byte(`{ template, _ := simplejson.NewJson([]byte(`{
"__inputs": [ "__inputs": [
{ {
"name": "DS_NAME", "name": "DS_NAME",
"type": "datasource" "type": "datasource"
} }
], ],
"test": { "test": {
"prop": "${DS_NAME}" "prop": "${DS_NAME}"
} }
}`)) }`))
evaluator := &DashTemplateEvaluator{ evaluator := &DashTemplateEvaluator{
template: template, template: template,
......
...@@ -108,3 +108,13 @@ func loadPluginDashboard(pluginId, path string) (*m.Dashboard, error) { ...@@ -108,3 +108,13 @@ func loadPluginDashboard(pluginId, path string) (*m.Dashboard, error) {
return m.NewDashboardFromJson(data), nil return m.NewDashboardFromJson(data), nil
} }
func getPlugin(pluginId string) (*PluginBase, error) {
plugin, exists := Plugins[pluginId]
if !exists {
return nil, PluginNotFoundError{pluginId}
}
return plugin, nil
}
...@@ -340,7 +340,7 @@ func GetDashboards(query *m.GetDashboardsQuery) error { ...@@ -340,7 +340,7 @@ func GetDashboards(query *m.GetDashboardsQuery) error {
func GetDashboardsByPluginId(query *m.GetDashboardsByPluginIdQuery) error { func GetDashboardsByPluginId(query *m.GetDashboardsByPluginIdQuery) error {
var dashboards = make([]*m.Dashboard, 0) var dashboards = make([]*m.Dashboard, 0)
err := x.Where("org_id=? AND plugin_id=?", query.OrgId, query.PluginId).Find(&dashboards) err := x.Where("org_id=? AND plugin_id=? AND is_folder=0", query.OrgId, query.PluginId).Find(&dashboards)
query.Result = dashboards query.Result = dashboards
if err != nil { if err != nil {
......
...@@ -458,6 +458,25 @@ func TestDashboardDataAccess(t *testing.T) { ...@@ -458,6 +458,25 @@ func TestDashboardDataAccess(t *testing.T) {
}) })
}) })
}) })
Convey("Given a plugin with imported dashboards", func() {
pluginId := "test-app"
appFolder := insertTestDashboardForPlugin("app-test", 1, 0, true, pluginId)
insertTestDashboardForPlugin("app-dash1", 1, appFolder.Id, false, pluginId)
insertTestDashboardForPlugin("app-dash2", 1, appFolder.Id, false, pluginId)
Convey("Should return imported dashboard", func() {
query := m.GetDashboardsByPluginIdQuery{
PluginId: pluginId,
OrgId: 1,
}
err := GetDashboardsByPluginId(&query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
})
})
}) })
} }
...@@ -479,6 +498,24 @@ func insertTestDashboard(title string, orgId int64, folderId int64, isFolder boo ...@@ -479,6 +498,24 @@ func insertTestDashboard(title string, orgId int64, folderId int64, isFolder boo
return cmd.Result return cmd.Result
} }
func insertTestDashboardForPlugin(title string, orgId int64, folderId int64, isFolder bool, pluginId string) *m.Dashboard {
cmd := m.SaveDashboardCommand{
OrgId: orgId,
FolderId: folderId,
IsFolder: isFolder,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": nil,
"title": title,
}),
PluginId: pluginId,
}
err := SaveDashboard(&cmd)
So(err, ShouldBeNil)
return cmd.Result
}
func createUser(name string, role string, isAdmin bool) m.User { func createUser(name string, role string, isAdmin bool) m.User {
setting.AutoAssignOrg = true setting.AutoAssignOrg = true
setting.AutoAssignOrgRole = role setting.AutoAssignOrgRole = role
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment