Commit 4356e980 by Daniel Lee

dashfolders: create app folder on dashboard import

parent 07cd1826
......@@ -49,6 +49,30 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
if dashboard, err = loadPluginDashboard(cmd.PluginId, cmd.Path); err != nil {
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 {
dashboard = m.NewDashboardFromJson(cmd.Dashboard)
}
......@@ -69,6 +93,7 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
UserId: cmd.UserId,
Overwrite: cmd.Overwrite,
PluginId: cmd.PluginId,
FolderId: dashboard.FolderId,
}
if err := bus.Dispatch(&saveCmd); err != nil {
......
......@@ -22,10 +22,19 @@ func TestDashboardImport(t *testing.T) {
So(err, ShouldBeNil)
folderId := int64(1000)
var importedDash *m.Dashboard
var createdFolder *m.Dashboard
bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
importedDash = cmd.GetDashboardModel()
cmd.Result = importedDash
if cmd.IsFolder {
createdFolder = cmd.GetDashboardModel()
createdFolder.Id = folderId
cmd.Result = createdFolder
} else {
importedDash = cmd.GetDashboardModel()
cmd.Result = importedDash
}
return nil
})
......@@ -54,21 +63,28 @@ func TestDashboardImport(t *testing.T) {
panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
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() {
template, _ := simplejson.NewJson([]byte(`{
"__inputs": [
{
"name": "DS_NAME",
"type": "datasource"
}
],
"test": {
"prop": "${DS_NAME}"
}
}`))
"__inputs": [
{
"name": "DS_NAME",
"type": "datasource"
}
],
"test": {
"prop": "${DS_NAME}"
}
}`))
evaluator := &DashTemplateEvaluator{
template: template,
......
......@@ -108,3 +108,13 @@ func loadPluginDashboard(pluginId, path string) (*m.Dashboard, error) {
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 {
func GetDashboardsByPluginId(query *m.GetDashboardsByPluginIdQuery) error {
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
if err != nil {
......
......@@ -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
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 {
setting.AutoAssignOrg = true
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