Files
grafana/pkg/api/api_dashboard.go

88 lines
2.1 KiB
Go
Raw Normal View History

2014-08-11 20:18:46 +02:00
package api
import (
2014-08-21 22:09:48 +02:00
log "github.com/alecthomas/log4go"
2014-08-11 20:18:46 +02:00
"github.com/gin-gonic/gin"
2014-08-12 20:45:41 +02:00
"github.com/torkelo/grafana-pro/pkg/models"
2014-08-11 20:18:46 +02:00
)
func init() {
addRoutes(func(self *HttpServer) {
2014-09-19 17:37:18 +02:00
self.addRoute("GET", "/api/dashboards/:slug", self.getDashboard)
self.addRoute("GET", "/api/search/", self.search)
self.addRoute("POST", "/api/dashboard/", self.postDashboard)
self.addRoute("DELETE", "/api/dashboard/:slug", self.deleteDashboard)
2014-08-11 20:18:46 +02:00
})
}
2014-09-19 17:37:18 +02:00
func (self *HttpServer) getDashboard(c *gin.Context, auth *authContext) {
2014-09-17 13:34:42 +02:00
slug := c.Params.ByName("slug")
2014-08-11 20:18:46 +02:00
2014-09-19 17:37:18 +02:00
dash, err := self.store.GetDashboard(slug, auth.getAccountId())
2014-08-11 20:18:46 +02:00
if err != nil {
c.JSON(404, newErrorResponse("Dashboard not found"))
return
}
2014-09-17 13:34:42 +02:00
dash.Data["id"] = dash.Id
2014-08-11 20:18:46 +02:00
c.JSON(200, dash.Data)
}
2014-09-19 17:37:18 +02:00
func (self *HttpServer) deleteDashboard(c *gin.Context, auth *authContext) {
2014-09-17 13:34:42 +02:00
slug := c.Params.ByName("slug")
2014-09-19 17:37:18 +02:00
dash, err := self.store.GetDashboard(slug, auth.getAccountId())
2014-09-17 13:34:42 +02:00
if err != nil {
c.JSON(404, newErrorResponse("Dashboard not found"))
return
}
2014-09-19 17:37:18 +02:00
err = self.store.DeleteDashboard(slug, auth.getAccountId())
2014-09-17 13:34:42 +02:00
if err != nil {
c.JSON(500, newErrorResponse("Failed to delete dashboard: "+err.Error()))
return
}
var resp = map[string]interface{}{"title": dash.Title}
c.JSON(200, resp)
}
2014-09-19 17:37:18 +02:00
func (self *HttpServer) search(c *gin.Context, auth *authContext) {
2014-08-11 20:18:46 +02:00
query := c.Params.ByName("q")
2014-09-19 17:37:18 +02:00
results, err := self.store.Query(query, auth.getAccountId())
2014-08-11 20:18:46 +02:00
if err != nil {
2014-08-21 22:09:48 +02:00
log.Error("Store query error: %v", err)
2014-08-11 20:18:46 +02:00
c.JSON(500, newErrorResponse("Failed"))
return
}
c.JSON(200, results)
}
2014-09-19 17:37:18 +02:00
func (self *HttpServer) postDashboard(c *gin.Context, auth *authContext) {
2014-08-11 20:18:46 +02:00
var command saveDashboardCommand
if c.EnsureBody(&command) {
2014-08-21 22:09:48 +02:00
dashboard := models.NewDashboard("test")
dashboard.Data = command.Dashboard
dashboard.Title = dashboard.Data["title"].(string)
2014-09-19 17:37:18 +02:00
dashboard.AccountId = auth.getAccountId()
dashboard.UpdateSlug()
2014-08-21 22:09:48 +02:00
if dashboard.Data["id"] != nil {
dashboard.Id = dashboard.Data["id"].(string)
}
err := self.store.SaveDashboard(dashboard)
2014-08-11 20:18:46 +02:00
if err == nil {
c.JSON(200, gin.H{"status": "success", "slug": dashboard.Slug})
2014-08-11 20:18:46 +02:00
return
}
}
c.JSON(500, gin.H{"error": "bad request"})
}