dashfolders: create app folder on dashboard import
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user