Commit 368a4d7a by Marcus Efraimsson

dashfolders: don't create app folder on dashboard import if already exists

parent 4356e980
...@@ -50,29 +50,9 @@ func ImportDashboard(cmd *ImportDashboardCommand) error { ...@@ -50,29 +50,9 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
return err return err
} }
var plugin *PluginBase if err = createDashboardFolderForPlugin(cmd, dashboard); err != nil {
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 return err
} }
dashboard.FolderId = saveCmd.Result.Id
} else { } else {
dashboard = m.NewDashboardFromJson(cmd.Dashboard) dashboard = m.NewDashboardFromJson(cmd.Dashboard)
} }
...@@ -113,6 +93,63 @@ func ImportDashboard(cmd *ImportDashboardCommand) error { ...@@ -113,6 +93,63 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
return nil return nil
} }
func createDashboardFolderForPlugin(cmd *ImportDashboardCommand, dashboard *m.Dashboard) error {
var err error
var plugin *PluginBase
if plugin, err = getPlugin(cmd.PluginId); err != nil {
return err
}
var pluginType string
if plugin.Type == "datasource" {
pluginType = "Datasource"
} else if plugin.Type == "app" {
pluginType = "App"
}
folderTitle := fmt.Sprint(pluginType, ": ", plugin.Name)
folderDash := simplejson.NewFromAny(map[string]interface{}{
"schemaVersion": 16,
"title": folderTitle,
"editable": true,
"hideControls": true,
})
saveCmd := m.SaveDashboardCommand{
Dashboard: folderDash,
OrgId: cmd.OrgId,
UserId: cmd.UserId,
PluginId: cmd.PluginId,
IsFolder: true,
}
dashModel := saveCmd.GetDashboardModel()
getDashboardQuery := m.GetDashboardQuery{
OrgId: cmd.OrgId,
Slug: dashModel.Slug,
}
if err := bus.Dispatch(&getDashboardQuery); err != nil {
return err
}
if getDashboardQuery.Result != nil {
dashboard.FolderId = getDashboardQuery.Result.Id
return nil
}
if err := bus.Dispatch(&saveCmd); err != nil {
return err
}
dashboard.FolderId = saveCmd.Result.Id
return nil
}
type DashTemplateEvaluator struct { type DashTemplateEvaluator struct {
template *simplejson.Json template *simplejson.Json
inputs []ImportDashboardInput inputs []ImportDashboardInput
......
...@@ -38,6 +38,10 @@ func TestDashboardImport(t *testing.T) { ...@@ -38,6 +38,10 @@ func TestDashboardImport(t *testing.T) {
return nil return nil
}) })
bus.AddHandler("test", func(cmd *m.GetDashboardQuery) error {
return nil
})
cmd := ImportDashboardCommand{ cmd := ImportDashboardCommand{
PluginId: "test-app", PluginId: "test-app",
Path: "dashboards/connections.json", Path: "dashboards/connections.json",
...@@ -68,11 +72,76 @@ func TestDashboardImport(t *testing.T) { ...@@ -68,11 +72,76 @@ func TestDashboardImport(t *testing.T) {
}) })
Convey("should create app folder", func() { Convey("should create app folder", func() {
So(createdFolder.Title, ShouldEqual, "Test App") So(createdFolder.Title, ShouldEqual, "App: Test App")
So(createdFolder.Id, ShouldEqual, folderId) So(createdFolder.Id, ShouldEqual, folderId)
}) })
}) })
Convey("When re-importing plugin dashboard", t, func() {
setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app")
err := Init()
So(err, ShouldBeNil)
folderId := int64(1000)
var importedDash *m.Dashboard
var createdFolder *m.Dashboard
bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
if cmd.IsFolder {
cmd.Result = cmd.GetDashboardModel()
} else {
importedDash = cmd.GetDashboardModel()
cmd.Result = importedDash
}
return nil
})
bus.AddHandler("test", func(cmd *m.GetDashboardQuery) error {
cmd.Result = &m.Dashboard{
Id: 1000,
Title: "Something",
}
return nil
})
cmd := ImportDashboardCommand{
PluginId: "test-app",
Path: "dashboards/connections.json",
OrgId: 1,
UserId: 1,
Inputs: []ImportDashboardInput{
{Name: "*", Type: "datasource", Value: "graphite"},
},
}
err = ImportDashboard(&cmd)
So(err, ShouldBeNil)
Convey("should install dashboard", func() {
So(importedDash, ShouldNotBeNil)
resultStr, _ := importedDash.Data.EncodePretty()
expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
expectedJson, _ := simplejson.NewJson(expectedBytes)
expectedStr, _ := expectedJson.EncodePretty()
So(string(resultStr), ShouldEqual, string(expectedStr))
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 not create app folder", func() {
So(createdFolder, ShouldBeNil)
})
})
Convey("When evaling dashboard template", t, func() { Convey("When evaling dashboard template", t, func() {
template, _ := simplejson.NewJson([]byte(`{ template, _ := simplejson.NewJson([]byte(`{
"__inputs": [ "__inputs": [
......
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