Commit 03c23dd9 by Carl Bergquist Committed by GitHub

Merge pull request #9901 from grafana/fix_annotations_api

fix: return id from api when creating new annotation, fixes #9798
parents dcffcbb6 909601d6
...@@ -89,7 +89,7 @@ Content-Type: application/json ...@@ -89,7 +89,7 @@ Content-Type: application/json
## Create Annotation ## Create Annotation
Creates an annotation in the Grafana database. The `dashboardId` and `panelId` fields are optional. If they are not specified then a global annotation is created and can be queried in any dashboard that adds the Grafana annotations data source. Creates an annotation in the Grafana database. The `dashboardId` and `panelId` fields are optional. If they are not specified then a global annotation is created and can be queried in any dashboard that adds the Grafana annotations data source. When creating a region annotation the response will include both `id` and `endId`, if not only `id`.
`POST /api/annotations` `POST /api/annotations`
...@@ -117,7 +117,11 @@ Content-Type: application/json ...@@ -117,7 +117,11 @@ Content-Type: application/json
HTTP/1.1 200 HTTP/1.1 200
Content-Type: application/json Content-Type: application/json
{"message":"Annotation added"} {
"message":"Annotation added",
"id": 1,
"endId": 2
}
``` ```
## Create Annotation in Graphite format ## Create Annotation in Graphite format
...@@ -148,7 +152,10 @@ Content-Type: application/json ...@@ -148,7 +152,10 @@ Content-Type: application/json
HTTP/1.1 200 HTTP/1.1 200
Content-Type: application/json Content-Type: application/json
{"message":"Graphite annotation added"} {
"message":"Graphite annotation added",
"id": 1
}
``` ```
## Update Annotation ## Update Annotation
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/services/annotations" "github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/util"
) )
func GetAnnotations(c *middleware.Context) Response { func GetAnnotations(c *middleware.Context) Response {
...@@ -75,9 +76,11 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response ...@@ -75,9 +76,11 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response
return ApiError(500, "Failed to save annotation", err) return ApiError(500, "Failed to save annotation", err)
} }
startID := item.Id
// handle regions // handle regions
if cmd.IsRegion { if cmd.IsRegion {
item.RegionId = item.Id item.RegionId = startID
if item.Data == nil { if item.Data == nil {
item.Data = simplejson.New() item.Data = simplejson.New()
...@@ -93,9 +96,18 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response ...@@ -93,9 +96,18 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response
if err := repo.Save(&item); err != nil { if err := repo.Save(&item); err != nil {
return ApiError(500, "Failed save annotation for region end time", err) return ApiError(500, "Failed save annotation for region end time", err)
} }
return Json(200, util.DynMap{
"message": "Annotation added",
"id": startID,
"endId": item.Id,
})
} }
return ApiSuccess("Annotation added") return Json(200, util.DynMap{
"message": "Annotation added",
"id": startID,
})
} }
func formatGraphiteAnnotation(what string, data string) string { func formatGraphiteAnnotation(what string, data string) string {
...@@ -154,7 +166,10 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati ...@@ -154,7 +166,10 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati
return ApiError(500, "Failed to save Graphite annotation", err) return ApiError(500, "Failed to save Graphite annotation", err)
} }
return ApiSuccess("Graphite annotation added") return Json(200, util.DynMap{
"message": "Graphite annotation added",
"id": item.Id,
})
} }
func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response { func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
......
...@@ -199,10 +199,10 @@ func (hs *HttpServer) registerRoutes() { ...@@ -199,10 +199,10 @@ func (hs *HttpServer) registerRoutes() {
// Data sources // Data sources
apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) { apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) {
datasourceRoute.Get("/", wrap(GetDataSources)) datasourceRoute.Get("/", wrap(GetDataSources))
datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), AddDataSource) datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), wrap(AddDataSource))
datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource)) datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
datasourceRoute.Delete("/:id", DeleteDataSourceById) datasourceRoute.Delete("/:id", wrap(DeleteDataSourceById))
datasourceRoute.Delete("/name/:name", DeleteDataSourceByName) datasourceRoute.Delete("/name/:name", wrap(DeleteDataSourceByName))
datasourceRoute.Get("/:id", wrap(GetDataSourceById)) datasourceRoute.Get("/:id", wrap(GetDataSourceById))
datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName)) datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName))
}, reqOrgAdmin) }, reqOrgAdmin)
......
...@@ -69,80 +69,70 @@ func GetDataSourceById(c *middleware.Context) Response { ...@@ -69,80 +69,70 @@ func GetDataSourceById(c *middleware.Context) Response {
return Json(200, &dtos) return Json(200, &dtos)
} }
func DeleteDataSourceById(c *middleware.Context) { func DeleteDataSourceById(c *middleware.Context) Response {
id := c.ParamsInt64(":id") id := c.ParamsInt64(":id")
if id <= 0 { if id <= 0 {
c.JsonApiErr(400, "Missing valid datasource id", nil) return ApiError(400, "Missing valid datasource id", nil)
return
} }
ds, err := getRawDataSourceById(id, c.OrgId) ds, err := getRawDataSourceById(id, c.OrgId)
if err != nil { if err != nil {
c.JsonApiErr(400, "Failed to delete datasource", nil) return ApiError(400, "Failed to delete datasource", nil)
return
} }
if ds.ReadOnly { if ds.ReadOnly {
c.JsonApiErr(403, "Cannot delete read-only data source", nil) return ApiError(403, "Cannot delete read-only data source", nil)
return
} }
cmd := &m.DeleteDataSourceByIdCommand{Id: id, OrgId: c.OrgId} cmd := &m.DeleteDataSourceByIdCommand{Id: id, OrgId: c.OrgId}
err = bus.Dispatch(cmd) err = bus.Dispatch(cmd)
if err != nil { if err != nil {
c.JsonApiErr(500, "Failed to delete datasource", err) return ApiError(500, "Failed to delete datasource", err)
return
} }
c.JsonOK("Data source deleted") return ApiSuccess("Data source deleted")
} }
func DeleteDataSourceByName(c *middleware.Context) { func DeleteDataSourceByName(c *middleware.Context) Response {
name := c.Params(":name") name := c.Params(":name")
if name == "" { if name == "" {
c.JsonApiErr(400, "Missing valid datasource name", nil) return ApiError(400, "Missing valid datasource name", nil)
return
} }
getCmd := &m.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId} getCmd := &m.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
if err := bus.Dispatch(getCmd); err != nil { if err := bus.Dispatch(getCmd); err != nil {
c.JsonApiErr(500, "Failed to delete datasource", err) return ApiError(500, "Failed to delete datasource", err)
return
} }
if getCmd.Result.ReadOnly { if getCmd.Result.ReadOnly {
c.JsonApiErr(403, "Cannot delete read-only data source", nil) return ApiError(403, "Cannot delete read-only data source", nil)
return
} }
cmd := &m.DeleteDataSourceByNameCommand{Name: name, OrgId: c.OrgId} cmd := &m.DeleteDataSourceByNameCommand{Name: name, OrgId: c.OrgId}
err := bus.Dispatch(cmd) err := bus.Dispatch(cmd)
if err != nil { if err != nil {
c.JsonApiErr(500, "Failed to delete datasource", err) return ApiError(500, "Failed to delete datasource", err)
return
} }
c.JsonOK("Data source deleted") return ApiSuccess("Data source deleted")
} }
func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) { func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) Response {
cmd.OrgId = c.OrgId cmd.OrgId = c.OrgId
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrDataSourceNameExists { if err == m.ErrDataSourceNameExists {
c.JsonApiErr(409, err.Error(), err) return ApiError(409, err.Error(), err)
return
} }
c.JsonApiErr(500, "Failed to add datasource", err) return ApiError(500, "Failed to add datasource", err)
return
} }
ds := convertModelToDtos(cmd.Result) ds := convertModelToDtos(cmd.Result)
c.JSON(200, util.DynMap{ return Json(200, util.DynMap{
"message": "Datasource added", "message": "Datasource added",
"id": cmd.Result.Id, "id": cmd.Result.Id,
"name": cmd.Result.Name, "name": cmd.Result.Name,
......
...@@ -37,16 +37,18 @@ func TestAnnotations(t *testing.T) { ...@@ -37,16 +37,18 @@ func TestAnnotations(t *testing.T) {
repo := SqlAnnotationRepo{} repo := SqlAnnotationRepo{}
Convey("Can save annotation", func() { Convey("Can save annotation", func() {
err := repo.Save(&annotations.Item{ annotation := &annotations.Item{
OrgId: 1, OrgId: 1,
UserId: 1, UserId: 1,
DashboardId: 1, DashboardId: 1,
Text: "hello", Text: "hello",
Epoch: 10, Epoch: 10,
Tags: []string{"outage", "error", "type:outage", "server:server-1"}, Tags: []string{"outage", "error", "type:outage", "server:server-1"},
}) }
err := repo.Save(annotation)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(annotation.Id, ShouldBeGreaterThan, 0)
Convey("Can query for annotation", func() { Convey("Can query for annotation", func() {
items, err := repo.Find(&annotations.ItemQuery{ items, err := repo.Find(&annotations.ItemQuery{
......
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