Commit 104c2e36 by Ryan McKinley Committed by Torkel Ödegaard

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>
parent 05d831df
...@@ -244,7 +244,30 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand) ...@@ -244,7 +244,30 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand)
} }
dashboard, err := dashboards.NewService().SaveDashboard(dashItem, allowUiUpdate) 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 || if err == m.ErrDashboardTitleEmpty ||
err == m.ErrDashboardWithSameNameAsFolder || err == m.ErrDashboardWithSameNameAsFolder ||
err == m.ErrDashboardFolderWithSameNameAsDashboard || err == m.ErrDashboardFolderWithSameNameAsDashboard ||
...@@ -267,44 +290,28 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand) ...@@ -267,44 +290,28 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand)
return Error(422, validationErr.Error(), nil) return Error(422, validationErr.Error(), nil)
} }
if err != nil { if err == m.ErrDashboardWithSameNameInFolderExists {
if err == m.ErrDashboardWithSameNameInFolderExists { return JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
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 hs.Cfg.EditorsCanAdmin && newDashboard { if err == m.ErrDashboardVersionMismatch {
inFolder := cmd.FolderId > 0 return JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
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 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})
} }
c.TimeRequest(metrics.MApiDashboardSave) if err == m.ErrDashboardNotFound {
return JSON(200, util.DynMap{ return JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
"status": "success", }
"slug": dashboard.Slug,
"version": dashboard.Version, return Error(500, "Failed to save dashboard", err)
"id": dashboard.Id,
"uid": dashboard.Uid,
"url": dashboard.GetUrl(),
})
} }
func GetHomeDashboard(c *m.ReqContext) Response { func GetHomeDashboard(c *m.ReqContext) Response {
......
...@@ -179,7 +179,6 @@ func GetPluginMarkdown(c *m.ReqContext) Response { ...@@ -179,7 +179,6 @@ func GetPluginMarkdown(c *m.ReqContext) Response {
} }
func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response { func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
cmd := plugins.ImportDashboardCommand{ cmd := plugins.ImportDashboardCommand{
OrgId: c.OrgId, OrgId: c.OrgId,
User: c.SignedInUser, User: c.SignedInUser,
...@@ -192,7 +191,7 @@ func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Respon ...@@ -192,7 +191,7 @@ func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Respon
} }
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
return Error(500, "Failed to import dashboard", err) return dashboardSaveErrorToApiResponse(err)
} }
return JSON(200, cmd.Result) return JSON(200, cmd.Result)
......
{ {
"__inputs": [], "__inputs": [
"__requires": [
{ {
"type": "grafana", "name": "DS_NAME",
"id": "grafana", "type": "datasource",
"name": "Grafana", "pluginId": "graphite"
"version": "5.2.3"
},
{
"type": "panel",
"id": "graph",
"name": "Graph",
"version": "5.0.0"
} }
], ],
"annotations": { "annotations": {
"list": [ "list": [
{ {
...@@ -31,11 +24,7 @@ ...@@ -31,11 +24,7 @@
] ]
}, },
"editable": true, "editable": true,
"gnetId": 279,
"graphTooltip": 1, "graphTooltip": 1,
"id": 21,
"iteration": 1547213827205,
"links": [],
"panels": [ "panels": [
{ {
"collapsed": false, "collapsed": false,
...@@ -4788,6 +4777,5 @@ ...@@ -4788,6 +4777,5 @@
}, },
"timezone": "utc", "timezone": "utc",
"title": "Metrictank", "title": "Metrictank",
"uid": "tQW3QShiz", "uid": "tQW3QShiz"
"version": 5
} }
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