Metrictank: fix bundled dashboard (#21209)

* update metrictank dashboard

* Restored dashboard datasource variable, and improved error handling in the import api handler

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
Ryan McKinley 2019-12-20 02:42:47 -08:00 committed by Torkel Ödegaard
parent 05d831df29
commit 104c2e3636
3 changed files with 48 additions and 54 deletions

View File

@ -244,7 +244,30 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand)
}
dashboard, err := dashboards.NewService().SaveDashboard(dashItem, allowUiUpdate)
if err != nil {
return dashboardSaveErrorToApiResponse(err)
}
if hs.Cfg.EditorsCanAdmin && newDashboard {
inFolder := cmd.FolderId > 0
err := dashboards.MakeUserAdmin(hs.Bus, cmd.OrgId, cmd.UserId, dashboard.Id, !inFolder)
if err != nil {
hs.log.Error("Could not make user admin", "dashboard", dashboard.Title, "user", c.SignedInUser.UserId, "error", err)
}
}
c.TimeRequest(metrics.MApiDashboardSave)
return JSON(200, util.DynMap{
"status": "success",
"slug": dashboard.Slug,
"version": dashboard.Version,
"id": dashboard.Id,
"uid": dashboard.Uid,
"url": dashboard.GetUrl(),
})
}
func dashboardSaveErrorToApiResponse(err error) Response {
if err == m.ErrDashboardTitleEmpty ||
err == m.ErrDashboardWithSameNameAsFolder ||
err == m.ErrDashboardFolderWithSameNameAsDashboard ||
@ -267,44 +290,28 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand)
return Error(422, validationErr.Error(), nil)
}
if err != nil {
if err == m.ErrDashboardWithSameNameInFolderExists {
return JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
}
if err == m.ErrDashboardVersionMismatch {
return JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
}
if pluginErr, ok := err.(m.UpdatePluginDashboardError); ok {
message := "The dashboard belongs to plugin " + pluginErr.PluginId + "."
// look up plugin name
if pluginDef, exist := plugins.Plugins[pluginErr.PluginId]; exist {
message = "The dashboard belongs to plugin " + pluginDef.Name + "."
}
return JSON(412, util.DynMap{"status": "plugin-dashboard", "message": message})
}
if err == m.ErrDashboardNotFound {
return JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
}
return Error(500, "Failed to save dashboard", err)
if err == m.ErrDashboardWithSameNameInFolderExists {
return JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
}
if hs.Cfg.EditorsCanAdmin && newDashboard {
inFolder := cmd.FolderId > 0
err := dashboards.MakeUserAdmin(hs.Bus, cmd.OrgId, cmd.UserId, dashboard.Id, !inFolder)
if err != nil {
hs.log.Error("Could not make user admin", "dashboard", dashboard.Title, "user", c.SignedInUser.UserId, "error", err)
}
if err == m.ErrDashboardVersionMismatch {
return JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
}
c.TimeRequest(metrics.MApiDashboardSave)
return JSON(200, util.DynMap{
"status": "success",
"slug": dashboard.Slug,
"version": dashboard.Version,
"id": dashboard.Id,
"uid": dashboard.Uid,
"url": dashboard.GetUrl(),
})
if pluginErr, ok := err.(m.UpdatePluginDashboardError); ok {
message := "The dashboard belongs to plugin " + pluginErr.PluginId + "."
// look up plugin name
if pluginDef, exist := plugins.Plugins[pluginErr.PluginId]; exist {
message = "The dashboard belongs to plugin " + pluginDef.Name + "."
}
return JSON(412, util.DynMap{"status": "plugin-dashboard", "message": message})
}
if err == m.ErrDashboardNotFound {
return JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
}
return Error(500, "Failed to save dashboard", err)
}
func GetHomeDashboard(c *m.ReqContext) Response {

View File

@ -179,7 +179,6 @@ func GetPluginMarkdown(c *m.ReqContext) Response {
}
func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
cmd := plugins.ImportDashboardCommand{
OrgId: c.OrgId,
User: c.SignedInUser,
@ -192,7 +191,7 @@ func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Respon
}
if err := bus.Dispatch(&cmd); err != nil {
return Error(500, "Failed to import dashboard", err)
return dashboardSaveErrorToApiResponse(err)
}
return JSON(200, cmd.Result)

View File

@ -1,19 +1,12 @@
{
"__inputs": [],
"__requires": [
"__inputs": [
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "5.2.3"
},
{
"type": "panel",
"id": "graph",
"name": "Graph",
"version": "5.0.0"
"name": "DS_NAME",
"type": "datasource",
"pluginId": "graphite"
}
],
"annotations": {
"list": [
{
@ -31,11 +24,7 @@
]
},
"editable": true,
"gnetId": 279,
"graphTooltip": 1,
"id": 21,
"iteration": 1547213827205,
"links": [],
"panels": [
{
"collapsed": false,
@ -4788,6 +4777,5 @@
},
"timezone": "utc",
"title": "Metrictank",
"uid": "tQW3QShiz",
"version": 5
"uid": "tQW3QShiz"
}