2016-09-08 04:25:45 -05:00
package api
import (
2022-03-18 11:33:21 -05:00
"context"
2021-03-29 08:47:16 -05:00
"errors"
2021-11-29 03:18:01 -06:00
"net/http"
2022-01-14 10:55:57 -06:00
"strconv"
2017-10-12 03:12:15 -05:00
"strings"
2016-09-08 04:25:45 -05:00
"github.com/grafana/grafana/pkg/api/dtos"
2021-01-15 07:43:20 -06:00
"github.com/grafana/grafana/pkg/api/response"
2022-03-18 11:33:21 -05:00
"github.com/grafana/grafana/pkg/services/accesscontrol"
2016-09-08 04:25:45 -05:00
"github.com/grafana/grafana/pkg/services/annotations"
2023-10-09 09:07:28 -05:00
"github.com/grafana/grafana/pkg/services/auth/identity"
2023-01-27 01:50:36 -06:00
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
2022-05-04 09:12:09 -05:00
"github.com/grafana/grafana/pkg/services/dashboards"
2023-11-17 03:57:25 -06:00
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
2017-12-20 17:52:21 -06:00
"github.com/grafana/grafana/pkg/services/guardian"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/user"
2017-11-16 07:24:56 -06:00
"github.com/grafana/grafana/pkg/util"
2021-11-29 03:18:01 -06:00
"github.com/grafana/grafana/pkg/web"
2016-09-08 04:25:45 -05:00
)
2022-07-27 08:54:37 -05:00
// swagger:route GET /annotations annotations getAnnotations
//
// Find Annotations.
//
// Starting in Grafana v6.4 regions annotations are now returned in one entity that now includes the timeEnd property.
//
// Responses:
// 200: getAnnotationsResponse
// 401: unauthorisedError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) GetAnnotations ( c * contextmodel . ReqContext ) response . Response {
2016-09-08 04:25:45 -05:00
query := & annotations . ItemQuery {
2022-04-11 07:18:38 -05:00
From : c . QueryInt64 ( "from" ) ,
To : c . QueryInt64 ( "to" ) ,
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-02-03 10:23:09 -06:00
UserID : c . QueryInt64 ( "userId" ) ,
AlertID : c . QueryInt64 ( "alertId" ) ,
DashboardID : c . QueryInt64 ( "dashboardId" ) ,
DashboardUID : c . Query ( "dashboardUID" ) ,
PanelID : c . QueryInt64 ( "panelId" ) ,
2022-04-11 07:18:38 -05:00
Limit : c . QueryInt64 ( "limit" ) ,
Tags : c . QueryStrings ( "tags" ) ,
Type : c . Query ( "type" ) ,
MatchAny : c . QueryBool ( "matchAny" ) ,
SignedInUser : c . SignedInUser ,
2016-09-08 04:25:45 -05:00
}
2022-07-26 20:52:34 -05:00
// When dashboard UID present in the request, we ignore dashboard ID
2023-02-03 10:23:09 -06:00
if query . DashboardUID != "" {
2023-10-06 04:34:36 -05:00
dq := dashboards . GetDashboardQuery { UID : query . DashboardUID , OrgID : c . SignedInUser . GetOrgID ( ) }
2023-01-25 03:36:26 -06:00
dqResult , err := hs . DashboardService . GetDashboard ( c . Req . Context ( ) , & dq )
2022-07-26 20:52:34 -05:00
if err != nil {
2023-03-20 11:36:49 -05:00
return response . Error ( http . StatusBadRequest , "Invalid dashboard UID in annotation request" , err )
2022-08-26 17:09:56 -05:00
} else {
2023-02-03 10:23:09 -06:00
query . DashboardID = dqResult . ID
2022-07-26 20:52:34 -05:00
}
}
2022-09-19 02:54:37 -05:00
items , err := hs . annotationsRepo . Find ( c . Req . Context ( ) , query )
2016-09-08 04:25:45 -05:00
if err != nil {
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusInternalServerError , "Failed to get annotations" , err )
2016-09-08 04:25:45 -05:00
}
2022-05-02 04:35:36 -05:00
// since there are several annotations per dashboard, we can cache dashboard uid
dashboardCache := make ( map [ int64 ] * string )
2016-09-08 04:25:45 -05:00
for _ , item := range items {
2017-10-07 03:31:39 -05:00
if item . Email != "" {
2024-01-23 05:36:22 -06:00
item . AvatarURL = dtos . GetGravatarUrl ( hs . Cfg , item . Email )
2017-10-07 03:31:39 -05:00
}
2022-05-02 04:35:36 -05:00
2023-02-03 10:23:09 -06:00
if item . DashboardID != 0 {
if val , ok := dashboardCache [ item . DashboardID ] ; ok {
2022-05-02 04:35:36 -05:00
item . DashboardUID = val
} else {
2023-10-06 04:34:36 -05:00
query := dashboards . GetDashboardQuery { ID : item . DashboardID , OrgID : c . SignedInUser . GetOrgID ( ) }
2023-01-25 03:36:26 -06:00
queryResult , err := hs . DashboardService . GetDashboard ( c . Req . Context ( ) , & query )
if err == nil && queryResult != nil {
item . DashboardUID = & queryResult . UID
2023-02-03 10:23:09 -06:00
dashboardCache [ item . DashboardID ] = & queryResult . UID
2022-05-02 04:35:36 -05:00
}
}
}
2016-09-08 04:25:45 -05:00
}
2022-04-15 07:01:58 -05:00
return response . JSON ( http . StatusOK , items )
2016-09-08 04:25:45 -05:00
}
2016-10-14 02:33:16 -05:00
2022-03-23 16:39:00 -05:00
type AnnotationError struct {
2017-10-18 03:13:02 -05:00
message string
}
2022-03-23 16:39:00 -05:00
func ( e * AnnotationError ) Error ( ) string {
2017-10-18 03:13:02 -05:00
return e . message
}
2022-07-27 08:54:37 -05:00
// swagger:route POST /annotations annotations postAnnotation
//
// Create Annotation.
//
// Creates an annotation in the Grafana database. The dashboardId and panelId fields are optional. If they are not specified then an organization annotation is created and can be queried in any dashboard that adds the Grafana annotations data source. When creating a region annotation include the timeEnd property.
// The format for `time` and `timeEnd` should be epoch numbers in millisecond resolution.
// The response for this HTTP request is slightly different in versions prior to v6.4. In prior versions you would also get an endId if you where creating a region. But in 6.4 regions are represented using a single event with time and timeEnd properties.
//
// Responses:
// 200: postAnnotationResponse
// 400: badRequestError
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) PostAnnotation ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
cmd := dtos . PostAnnotationsCmd { }
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
2022-03-18 11:33:21 -05:00
2022-05-02 04:35:36 -05:00
// overwrite dashboardId when dashboardUID is not empty
if cmd . DashboardUID != "" {
2023-10-06 04:34:36 -05:00
query := dashboards . GetDashboardQuery { OrgID : c . SignedInUser . GetOrgID ( ) , UID : cmd . DashboardUID }
2023-01-25 03:36:26 -06:00
queryResult , err := hs . DashboardService . GetDashboard ( c . Req . Context ( ) , & query )
2022-05-02 04:35:36 -05:00
if err == nil {
2023-01-25 03:36:26 -06:00
cmd . DashboardId = queryResult . ID
2022-05-02 04:35:36 -05:00
}
}
2022-04-20 02:43:42 -05:00
if canSave , err := hs . canCreateAnnotation ( c , cmd . DashboardId ) ; err != nil || ! canSave {
2023-11-23 04:47:37 -06:00
if ! hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
return dashboardGuardianResponse ( err )
} else if err != nil {
return response . Error ( http . StatusInternalServerError , "Error while checking annotation permissions" , err )
} else {
return response . Error ( http . StatusForbidden , "Access denied to save the annotation" , nil )
}
2017-12-20 17:52:21 -06:00
}
2017-10-18 03:13:02 -05:00
if cmd . Text == "" {
2022-03-23 16:39:00 -05:00
err := & AnnotationError { "text field should not be empty" }
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusBadRequest , "Failed to save annotation" , err )
2017-10-18 03:13:02 -05:00
}
2023-10-09 09:07:28 -05:00
userID , err := identity . UserIdentifier ( c . SignedInUser . GetNamespacedID ( ) )
if err != nil {
return response . Error ( http . StatusInternalServerError , "Failed to save annotation" , err )
}
2017-04-10 12:22:58 -05:00
item := annotations . Item {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-10-09 09:07:28 -05:00
UserID : userID ,
2023-02-03 10:23:09 -06:00
DashboardID : cmd . DashboardId ,
PanelID : cmd . PanelId ,
2018-03-22 10:21:47 -05:00
Epoch : cmd . Time ,
2019-08-16 03:49:30 -05:00
EpochEnd : cmd . TimeEnd ,
2017-04-10 12:22:58 -05:00
Text : cmd . Text ,
2017-10-07 03:31:39 -05:00
Data : cmd . Data ,
Tags : cmd . Tags ,
2017-04-10 12:22:58 -05:00
}
2022-09-19 02:54:37 -05:00
if err := hs . annotationsRepo . Save ( c . Req . Context ( ) , & item ) ; err != nil {
2021-03-29 08:47:16 -05:00
if errors . Is ( err , annotations . ErrTimerangeMissing ) {
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusBadRequest , "Failed to save annotation" , err )
2021-03-29 08:47:16 -05:00
}
2024-02-27 10:39:51 -06:00
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to save annotation" , err )
2017-04-10 12:22:58 -05:00
}
2023-02-03 10:23:09 -06:00
startID := item . ID
2017-11-16 07:24:56 -06:00
2022-04-15 07:01:58 -05:00
return response . JSON ( http . StatusOK , util . DynMap {
2017-11-16 07:24:56 -06:00
"message" : "Annotation added" ,
"id" : startID ,
} )
2017-04-10 12:22:58 -05:00
}
2017-10-12 03:12:15 -05:00
func formatGraphiteAnnotation ( what string , data string ) string {
2017-10-18 03:13:02 -05:00
text := what
if data != "" {
text = text + "\n" + data
}
return text
2017-10-12 03:12:15 -05:00
}
2022-07-27 08:54:37 -05:00
// swagger:route POST /annotations/graphite annotations postGraphiteAnnotation
//
// Create Annotation in Graphite format.
//
// Creates an annotation by using Graphite-compatible event format. The `when` and `data` fields are optional. If `when` is not specified then the current time will be used as annotation’ s timestamp. The `tags` field can also be in prior to Graphite `0.10.0` format (string with multiple tags being separated by a space).
//
// Responses:
// 200: postAnnotationResponse
// 400: badRequestError
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) PostGraphiteAnnotation ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
cmd := dtos . PostGraphiteAnnotationsCmd { }
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
2017-10-18 03:13:02 -05:00
if cmd . What == "" {
2022-03-23 16:39:00 -05:00
err := & AnnotationError { "what field should not be empty" }
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusBadRequest , "Failed to save Graphite annotation" , err )
2017-10-18 03:13:02 -05:00
}
2017-10-12 03:12:15 -05:00
text := formatGraphiteAnnotation ( cmd . What , cmd . Data )
// Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
var tagsArray [ ] string
switch tags := cmd . Tags . ( type ) {
case string :
2017-10-18 03:13:02 -05:00
if tags != "" {
tagsArray = strings . Split ( tags , " " )
} else {
tagsArray = [ ] string { }
}
2023-08-30 10:46:47 -05:00
case [ ] any :
2017-10-12 03:12:15 -05:00
for _ , t := range tags {
if tagStr , ok := t . ( string ) ; ok {
tagsArray = append ( tagsArray , tagStr )
} else {
2022-03-23 16:39:00 -05:00
err := & AnnotationError { "tag should be a string" }
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusBadRequest , "Failed to save Graphite annotation" , err )
2017-10-12 03:12:15 -05:00
}
}
default :
2022-03-23 16:39:00 -05:00
err := & AnnotationError { "unsupported tags format" }
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusBadRequest , "Failed to save Graphite annotation" , err )
2017-10-12 03:12:15 -05:00
}
2023-10-09 09:07:28 -05:00
userID , err := identity . UserIdentifier ( c . SignedInUser . GetNamespacedID ( ) )
if err != nil {
return response . Error ( http . StatusInternalServerError , "Failed to save Graphite annotation" , err )
}
2017-10-12 03:12:15 -05:00
item := annotations . Item {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-10-09 09:07:28 -05:00
UserID : userID ,
2018-04-09 06:58:09 -05:00
Epoch : cmd . When * 1000 ,
2017-10-12 03:12:15 -05:00
Text : text ,
Tags : tagsArray ,
}
2022-09-21 07:04:01 -05:00
if err := hs . annotationsRepo . Save ( c . Req . Context ( ) , & item ) ; err != nil {
2024-02-27 10:39:51 -06:00
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to save Graphite annotation" , err )
2017-10-12 03:12:15 -05:00
}
2022-04-15 07:01:58 -05:00
return response . JSON ( http . StatusOK , util . DynMap {
2017-11-16 07:24:56 -06:00
"message" : "Graphite annotation added" ,
2023-02-03 10:23:09 -06:00
"id" : item . ID ,
2017-11-16 07:24:56 -06:00
} )
2017-10-12 03:12:15 -05:00
}
2022-07-27 08:54:37 -05:00
// swagger:route PUT /annotations/{annotation_id} annotations updateAnnotation
//
// Update Annotation.
//
// Updates all properties of an annotation that matches the specified id. To only update certain property, consider using the Patch Annotation operation.
//
// Responses:
// 200: okResponse
// 400: badRequestError
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) UpdateAnnotation ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
cmd := dtos . UpdateAnnotationsCmd { }
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
2022-01-14 10:55:57 -06:00
annotationID , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":annotationId" ] , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "annotationId is invalid" , err )
}
2017-10-07 03:31:39 -05:00
2022-09-19 02:54:37 -05:00
annotation , resp := findAnnotationByID ( c . Req . Context ( ) , hs . annotationsRepo , annotationID , c . SignedInUser )
2022-02-11 12:43:29 -06:00
if resp != nil {
2017-12-20 17:52:21 -06:00
return resp
}
2023-11-23 04:47:37 -06:00
if ! hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
if canSave , err := hs . canSaveAnnotation ( c , annotation ) ; err != nil || ! canSave {
return dashboardGuardianResponse ( err )
}
2022-02-11 12:43:29 -06:00
}
2023-10-09 09:07:28 -05:00
userID , err := identity . UserIdentifier ( c . SignedInUser . GetNamespacedID ( ) )
if err != nil {
return response . Error ( http . StatusInternalServerError ,
"Failed to update annotation" , err )
}
2017-10-07 03:31:39 -05:00
item := annotations . Item {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-10-09 09:07:28 -05:00
UserID : userID ,
2023-02-03 10:23:09 -06:00
ID : annotationID ,
2019-08-16 03:49:30 -05:00
Epoch : cmd . Time ,
EpochEnd : cmd . TimeEnd ,
Text : cmd . Text ,
Tags : cmd . Tags ,
2022-12-26 08:53:52 -06:00
Data : annotation . Data ,
}
if cmd . Data != nil {
item . Data = cmd . Data
2017-10-07 03:31:39 -05:00
}
2022-09-19 02:54:37 -05:00
if err := hs . annotationsRepo . Update ( c . Req . Context ( ) , & item ) ; err != nil {
2024-02-27 10:39:51 -06:00
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to update annotation" , err )
2017-10-07 03:31:39 -05:00
}
2021-01-15 07:43:20 -06:00
return response . Success ( "Annotation updated" )
2017-10-07 03:31:39 -05:00
}
2022-07-27 08:54:37 -05:00
// swagger:route PATCH /annotations/{annotation_id} annotations patchAnnotation
//
2022-09-12 02:40:35 -05:00
// Patch Annotation.
2022-07-27 08:54:37 -05:00
//
// Updates one or more properties of an annotation that matches the specified ID.
// This operation currently supports updating of the `text`, `tags`, `time` and `timeEnd` properties.
// This is available in Grafana 6.0.0-beta2 and above.
//
// Responses:
// 200: okResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) PatchAnnotation ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
cmd := dtos . PatchAnnotationsCmd { }
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
2022-01-14 10:55:57 -06:00
annotationID , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":annotationId" ] , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "annotationId is invalid" , err )
}
2019-01-27 05:49:22 -06:00
2022-09-19 02:54:37 -05:00
annotation , resp := findAnnotationByID ( c . Req . Context ( ) , hs . annotationsRepo , annotationID , c . SignedInUser )
2022-02-11 12:43:29 -06:00
if resp != nil {
2019-01-27 05:49:22 -06:00
return resp
}
2023-11-23 04:47:37 -06:00
if ! hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
if canSave , err := hs . canSaveAnnotation ( c , annotation ) ; err != nil || ! canSave {
return dashboardGuardianResponse ( err )
}
2019-01-27 05:49:22 -06:00
}
2023-10-09 09:07:28 -05:00
userID , err := identity . UserIdentifier ( c . SignedInUser . GetNamespacedID ( ) )
if err != nil {
return response . Error ( http . StatusInternalServerError ,
"Failed to update annotation" , err )
}
2019-01-27 05:49:22 -06:00
existing := annotations . Item {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-10-09 09:07:28 -05:00
UserID : userID ,
2023-02-03 10:23:09 -06:00
ID : annotationID ,
2022-02-11 12:43:29 -06:00
Epoch : annotation . Time ,
EpochEnd : annotation . TimeEnd ,
Text : annotation . Text ,
Tags : annotation . Tags ,
2022-12-26 08:53:52 -06:00
Data : annotation . Data ,
2019-01-27 05:49:22 -06:00
}
if cmd . Tags != nil {
existing . Tags = cmd . Tags
}
if cmd . Text != "" && cmd . Text != existing . Text {
existing . Text = cmd . Text
}
if cmd . Time > 0 && cmd . Time != existing . Epoch {
existing . Epoch = cmd . Time
}
2019-08-16 03:49:30 -05:00
if cmd . TimeEnd > 0 && cmd . TimeEnd != existing . EpochEnd {
existing . EpochEnd = cmd . TimeEnd
2019-01-27 05:49:22 -06:00
}
2022-12-26 08:53:52 -06:00
if cmd . Data != nil {
existing . Data = cmd . Data
}
2022-09-19 02:54:37 -05:00
if err := hs . annotationsRepo . Update ( c . Req . Context ( ) , & existing ) ; err != nil {
2024-02-27 10:39:51 -06:00
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to update annotation" , err )
2019-01-27 05:49:22 -06:00
}
2021-01-15 07:43:20 -06:00
return response . Success ( "Annotation patched" )
2019-01-27 05:49:22 -06:00
}
2022-07-27 08:54:37 -05:00
// swagger:route POST /annotations/mass-delete annotations massDeleteAnnotations
//
// Delete multiple annotations.
//
// Responses:
// 200: okResponse
// 401: unauthorisedError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) MassDeleteAnnotations ( c * contextmodel . ReqContext ) response . Response {
2022-03-23 16:39:00 -05:00
cmd := dtos . MassDeleteAnnotationsCmd { }
err := web . Bind ( c . Req , & cmd )
if err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
2022-05-02 04:35:36 -05:00
if cmd . DashboardUID != "" {
2023-10-06 04:34:36 -05:00
query := dashboards . GetDashboardQuery { OrgID : c . SignedInUser . GetOrgID ( ) , UID : cmd . DashboardUID }
2023-01-25 03:36:26 -06:00
queryResult , err := hs . DashboardService . GetDashboard ( c . Req . Context ( ) , & query )
2022-05-02 04:35:36 -05:00
if err == nil {
2023-01-25 03:36:26 -06:00
cmd . DashboardId = queryResult . ID
2022-05-02 04:35:36 -05:00
}
}
2022-03-23 16:39:00 -05:00
if ( cmd . DashboardId != 0 && cmd . PanelId == 0 ) || ( cmd . PanelId != 0 && cmd . DashboardId == 0 ) {
err := & AnnotationError { message : "DashboardId and PanelId are both required for mass delete" }
2021-11-29 03:18:01 -06:00
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
2022-03-23 16:39:00 -05:00
var deleteParams * annotations . DeleteParams
2016-10-14 02:33:16 -05:00
2022-04-21 07:31:02 -05:00
// validations only for RBAC. A user can mass delete all annotations in a (dashboard + panel) or a specific annotation
2022-03-23 16:39:00 -05:00
// if has access to that dashboard.
2023-05-31 03:58:57 -05:00
var dashboardId int64
2022-03-23 16:39:00 -05:00
2023-05-31 03:58:57 -05:00
if cmd . AnnotationId != 0 {
annotation , respErr := findAnnotationByID ( c . Req . Context ( ) , hs . annotationsRepo , cmd . AnnotationId , c . SignedInUser )
if respErr != nil {
return respErr
2022-03-23 16:39:00 -05:00
}
2023-05-31 03:58:57 -05:00
dashboardId = annotation . DashboardID
deleteParams = & annotations . DeleteParams {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-05-31 03:58:57 -05:00
ID : cmd . AnnotationId ,
2022-03-23 16:39:00 -05:00
}
2023-05-31 03:58:57 -05:00
} else {
dashboardId = cmd . DashboardId
2022-03-23 16:39:00 -05:00
deleteParams = & annotations . DeleteParams {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-02-03 10:23:09 -06:00
DashboardID : cmd . DashboardId ,
PanelID : cmd . PanelId ,
2022-03-23 16:39:00 -05:00
}
}
2023-05-31 03:58:57 -05:00
canSave , err := hs . canMassDeleteAnnotations ( c , dashboardId )
if err != nil || ! canSave {
2023-11-23 04:47:37 -06:00
if ! hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
return dashboardGuardianResponse ( err )
} else if err != nil {
return response . Error ( http . StatusInternalServerError , "Error while checking annotation permissions" , err )
} else {
return response . Error ( http . StatusForbidden , "Access denied to mass delete annotations" , nil )
}
2023-05-31 03:58:57 -05:00
}
2022-09-19 02:54:37 -05:00
err = hs . annotationsRepo . Delete ( c . Req . Context ( ) , deleteParams )
2016-10-14 02:33:16 -05:00
if err != nil {
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusInternalServerError , "Failed to delete annotations" , err )
2016-10-14 02:33:16 -05:00
}
2021-01-15 07:43:20 -06:00
return response . Success ( "Annotations deleted" )
2016-10-14 02:33:16 -05:00
}
2017-10-07 03:31:39 -05:00
2022-07-27 08:54:37 -05:00
// swagger:route GET /annotations/{annotation_id} annotations getAnnotationByID
//
2022-09-12 02:40:35 -05:00
// Get Annotation by ID.
2022-07-27 08:54:37 -05:00
//
// Responses:
// 200: getAnnotationByIDResponse
// 401: unauthorisedError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) GetAnnotationByID ( c * contextmodel . ReqContext ) response . Response {
2022-05-16 10:16:36 -05:00
annotationID , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":annotationId" ] , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "annotationId is invalid" , err )
}
2022-09-19 02:54:37 -05:00
annotation , resp := findAnnotationByID ( c . Req . Context ( ) , hs . annotationsRepo , annotationID , c . SignedInUser )
2022-05-16 10:16:36 -05:00
if resp != nil {
return resp
}
if annotation . Email != "" {
2024-01-23 05:36:22 -06:00
annotation . AvatarURL = dtos . GetGravatarUrl ( hs . Cfg , annotation . Email )
2022-05-16 10:16:36 -05:00
}
2024-02-27 10:39:51 -06:00
return response . JSON ( http . StatusOK , annotation )
2022-05-16 10:16:36 -05:00
}
2022-07-27 08:54:37 -05:00
// swagger:route DELETE /annotations/{annotation_id} annotations deleteAnnotationByID
//
// Delete Annotation By ID.
//
// Deletes the annotation that matches the specified ID.
//
// Responses:
// 200: okResponse
// 401: unauthorisedError
// 403: forbiddenError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) DeleteAnnotationByID ( c * contextmodel . ReqContext ) response . Response {
2022-01-14 10:55:57 -06:00
annotationID , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":annotationId" ] , 10 , 64 )
if err != nil {
return response . Error ( http . StatusBadRequest , "annotationId is invalid" , err )
}
2017-10-07 03:31:39 -05:00
2023-11-23 04:47:37 -06:00
if ! hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
annotation , resp := findAnnotationByID ( c . Req . Context ( ) , hs . annotationsRepo , annotationID , c . SignedInUser )
if resp != nil {
return resp
}
2017-12-20 17:52:21 -06:00
2023-11-23 04:47:37 -06:00
if canSave , err := hs . canSaveAnnotation ( c , annotation ) ; err != nil || ! canSave {
return dashboardGuardianResponse ( err )
}
2022-02-11 12:43:29 -06:00
}
2022-09-19 02:54:37 -05:00
err = hs . annotationsRepo . Delete ( c . Req . Context ( ) , & annotations . DeleteParams {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2023-02-03 10:23:09 -06:00
ID : annotationID ,
2017-10-07 03:31:39 -05:00
} )
if err != nil {
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusInternalServerError , "Failed to delete annotation" , err )
2017-10-07 03:31:39 -05:00
}
2021-01-15 07:43:20 -06:00
return response . Success ( "Annotation deleted" )
2017-10-07 03:31:39 -05:00
}
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) canSaveAnnotation ( c * contextmodel . ReqContext , annotation * annotations . ItemDTO ) ( bool , error ) {
2022-04-20 02:43:42 -05:00
if annotation . GetType ( ) == annotations . Dashboard {
2023-02-03 10:23:09 -06:00
return canEditDashboard ( c , annotation . DashboardID )
2022-04-20 02:43:42 -05:00
} else {
return true , nil
}
}
2023-01-27 01:50:36 -06:00
func canEditDashboard ( c * contextmodel . ReqContext , dashboardID int64 ) ( bool , error ) {
2023-10-06 04:34:36 -05:00
guard , err := guardian . New ( c . Req . Context ( ) , dashboardID , c . SignedInUser . GetOrgID ( ) , c . SignedInUser )
2022-12-15 08:34:17 -06:00
if err != nil {
return false , err
}
2022-03-18 11:33:21 -05:00
if canEdit , err := guard . CanEdit ( ) ; err != nil || ! canEdit {
return false , err
2017-12-20 17:52:21 -06:00
}
return true , nil
}
2022-08-10 04:56:48 -05:00
func findAnnotationByID ( ctx context . Context , repo annotations . Repository , annotationID int64 , user * user . SignedInUser ) ( * annotations . ItemDTO , response . Response ) {
2022-04-11 07:18:38 -05:00
query := & annotations . ItemQuery {
2023-02-03 10:23:09 -06:00
AnnotationID : annotationID ,
OrgID : user . OrgID ,
2022-04-11 07:18:38 -05:00
SignedInUser : user ,
}
items , err := repo . Find ( ctx , query )
2017-12-20 17:52:21 -06:00
2022-02-11 12:43:29 -06:00
if err != nil {
2024-02-27 10:39:51 -06:00
return nil , response . Error ( http . StatusInternalServerError , "Failed to find annotation" , err )
2022-02-11 12:43:29 -06:00
}
2017-12-20 17:52:21 -06:00
2022-02-11 12:43:29 -06:00
if len ( items ) == 0 {
2024-02-27 10:39:51 -06:00
return nil , response . Error ( http . StatusNotFound , "Annotation not found" , nil )
2017-12-20 17:52:21 -06:00
}
2022-02-11 12:43:29 -06:00
return items [ 0 ] , nil
2017-12-20 17:52:21 -06:00
}
2021-06-30 06:42:54 -05:00
2022-07-27 08:54:37 -05:00
// swagger:route GET /annotations/tags annotations getAnnotationTags
//
// Find Annotations Tags.
//
// Find all the event tags created in the annotations.
//
// Responses:
// 200: getAnnotationTagsResponse
// 401: unauthorisedError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) GetAnnotationTags ( c * contextmodel . ReqContext ) response . Response {
2021-06-30 06:42:54 -05:00
query := & annotations . TagsQuery {
2023-10-06 04:34:36 -05:00
OrgID : c . SignedInUser . GetOrgID ( ) ,
2021-06-30 06:42:54 -05:00
Tag : c . Query ( "tag" ) ,
Limit : c . QueryInt64 ( "limit" ) ,
}
2022-09-19 02:54:37 -05:00
result , err := hs . annotationsRepo . FindTags ( c . Req . Context ( ) , query )
2021-06-30 06:42:54 -05:00
if err != nil {
2024-02-27 10:39:51 -06:00
return response . Error ( http . StatusInternalServerError , "Failed to find annotation tags" , err )
2021-06-30 06:42:54 -05:00
}
2022-04-15 07:01:58 -05:00
return response . JSON ( http . StatusOK , annotations . GetAnnotationTagsResponse { Result : result } )
2021-06-30 06:42:54 -05:00
}
2022-03-18 11:33:21 -05:00
2022-05-02 02:29:30 -05:00
// AnnotationTypeScopeResolver provides an ScopeAttributeResolver able to
2022-03-18 11:33:21 -05:00
// resolve annotation types. Scope "annotations:id:<id>" will be translated to "annotations:type:<type>,
// where <type> is the type of annotation with id <id>.
2023-11-17 03:57:25 -06:00
// If annotationPermissionUpdate feature toggle is enabled, dashboard annotation scope will be resolved to the corresponding
// dashboard and folder scopes (eg, "dashboards:uid:<annotation_dashboard_uid>", "folders:uid:<parent_folder_uid>" etc).
2024-01-09 12:38:06 -06:00
func AnnotationTypeScopeResolver ( annotationsRepo annotations . Repository , features featuremgmt . FeatureToggles , dashSvc dashboards . DashboardService , folderSvc folder . Service ) ( string , accesscontrol . ScopeAttributeResolver ) {
2022-05-02 02:29:30 -05:00
prefix := accesscontrol . ScopeAnnotationsProvider . GetResourceScope ( "" )
return prefix , accesscontrol . ScopeAttributeResolverFunc ( func ( ctx context . Context , orgID int64 , initialScope string ) ( [ ] string , error ) {
2022-03-18 11:33:21 -05:00
scopeParts := strings . Split ( initialScope , ":" )
if scopeParts [ 0 ] != accesscontrol . ScopeAnnotationsRoot || len ( scopeParts ) != 3 {
2022-05-02 02:29:30 -05:00
return nil , accesscontrol . ErrInvalidScope
2022-03-18 11:33:21 -05:00
}
annotationIdStr := scopeParts [ 2 ]
annotationId , err := strconv . Atoi ( annotationIdStr )
if err != nil {
2022-05-02 02:29:30 -05:00
return nil , accesscontrol . ErrInvalidScope
2022-03-18 11:33:21 -05:00
}
2022-04-11 07:18:38 -05:00
// tempUser is used to resolve annotation type.
// The annotation doesn't get returned to the real user, so real user's permissions don't matter here.
2022-08-10 04:56:48 -05:00
tempUser := & user . SignedInUser {
2022-08-11 06:28:55 -05:00
OrgID : orgID ,
2022-04-11 07:18:38 -05:00
Permissions : map [ int64 ] map [ string ] [ ] string {
orgID : {
2022-05-04 09:12:09 -05:00
dashboards . ActionDashboardsRead : { dashboards . ScopeDashboardsAll } ,
2022-04-11 07:18:38 -05:00
accesscontrol . ActionAnnotationsRead : { accesscontrol . ScopeAnnotationsAll } ,
} ,
} ,
}
2023-11-17 03:57:25 -06:00
if features . IsEnabled ( ctx , featuremgmt . FlagAnnotationPermissionUpdate ) {
tempUser = & user . SignedInUser {
OrgID : orgID ,
Permissions : map [ int64 ] map [ string ] [ ] string {
orgID : {
accesscontrol . ActionAnnotationsRead : { accesscontrol . ScopeAnnotationsTypeOrganization , dashboards . ScopeDashboardsAll } ,
} ,
} ,
}
}
2022-09-19 02:54:37 -05:00
annotation , resp := findAnnotationByID ( ctx , annotationsRepo , int64 ( annotationId ) , tempUser )
2022-03-18 11:33:21 -05:00
if resp != nil {
2022-05-02 02:29:30 -05:00
return nil , errors . New ( "could not resolve annotation type" )
2022-03-18 11:33:21 -05:00
}
2023-11-17 03:57:25 -06:00
if ! features . IsEnabled ( ctx , featuremgmt . FlagAnnotationPermissionUpdate ) {
switch annotation . GetType ( ) {
case annotations . Organization :
return [ ] string { accesscontrol . ScopeAnnotationsTypeOrganization } , nil
case annotations . Dashboard :
return [ ] string { accesscontrol . ScopeAnnotationsTypeDashboard } , nil
}
}
if annotation . DashboardID == 0 {
2022-05-02 02:29:30 -05:00
return [ ] string { accesscontrol . ScopeAnnotationsTypeOrganization } , nil
2022-03-18 11:33:21 -05:00
} else {
2023-11-17 03:57:25 -06:00
dashboard , err := dashSvc . GetDashboard ( ctx , & dashboards . GetDashboardQuery { ID : annotation . DashboardID , OrgID : orgID } )
if err != nil {
return nil , err
}
scopes := [ ] string { dashboards . ScopeDashboardsProvider . GetResourceScopeUID ( dashboard . UID ) }
// Append dashboard parent scopes if dashboard is in a folder or the general scope if dashboard is not in a folder
if dashboard . FolderUID != "" {
scopes = append ( scopes , dashboards . ScopeFoldersProvider . GetResourceScopeUID ( dashboard . FolderUID ) )
inheritedScopes , err := dashboards . GetInheritedScopes ( ctx , orgID , dashboard . FolderUID , folderSvc )
if err != nil {
return nil , err
}
scopes = append ( scopes , inheritedScopes ... )
} else {
scopes = append ( scopes , dashboards . ScopeFoldersProvider . GetResourceScopeUID ( folder . GeneralFolderUID ) )
}
return scopes , nil
2022-03-18 11:33:21 -05:00
}
2022-05-02 02:29:30 -05:00
} )
2022-03-18 11:33:21 -05:00
}
2022-03-21 12:28:39 -05:00
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) canCreateAnnotation ( c * contextmodel . ReqContext , dashboardId int64 ) ( bool , error ) {
2023-11-23 04:47:37 -06:00
if hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
if dashboardId != 0 {
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsCreate , dashboards . ScopeDashboardsProvider . GetResourceScope ( strconv . FormatInt ( dashboardId , 10 ) ) )
return hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
} else { // organization annotations
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsCreate , accesscontrol . ScopeAnnotationsTypeOrganization )
return hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
}
}
2022-04-20 02:43:42 -05:00
if dashboardId != 0 {
2023-05-31 03:58:57 -05:00
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsCreate , accesscontrol . ScopeAnnotationsTypeDashboard )
if canSave , err := hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator ) ; err != nil || ! canSave {
return canSave , err
2022-04-20 02:43:42 -05:00
}
2022-12-15 08:34:17 -06:00
2022-04-20 02:43:42 -05:00
return canEditDashboard ( c , dashboardId )
} else { // organization annotations
2023-05-31 03:58:57 -05:00
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsCreate , accesscontrol . ScopeAnnotationsTypeOrganization )
return hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
2022-04-20 02:43:42 -05:00
}
2022-03-21 12:28:39 -05:00
}
2022-03-23 16:39:00 -05:00
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) canMassDeleteAnnotations ( c * contextmodel . ReqContext , dashboardID int64 ) ( bool , error ) {
2023-11-23 04:47:37 -06:00
if hs . Features . IsEnabled ( c . Req . Context ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
if dashboardID == 0 {
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsDelete , accesscontrol . ScopeAnnotationsTypeOrganization )
return hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
} else {
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsDelete , dashboards . ScopeDashboardsProvider . GetResourceScope ( strconv . FormatInt ( dashboardID , 10 ) ) )
return hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
}
}
2022-03-23 16:39:00 -05:00
if dashboardID == 0 {
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsDelete , accesscontrol . ScopeAnnotationsTypeOrganization )
return hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
} else {
evaluator := accesscontrol . EvalPermission ( accesscontrol . ActionAnnotationsDelete , accesscontrol . ScopeAnnotationsTypeDashboard )
canSave , err := hs . AccessControl . Evaluate ( c . Req . Context ( ) , c . SignedInUser , evaluator )
if err != nil || ! canSave {
return false , err
}
2022-04-20 02:43:42 -05:00
canSave , err = canEditDashboard ( c , dashboardID )
2022-03-23 16:39:00 -05:00
if err != nil || ! canSave {
return false , err
}
}
return true , nil
}
2022-07-27 08:54:37 -05:00
// swagger:parameters getAnnotationByID
type GetAnnotationByIDParams struct {
// in:path
// required:true
AnnotationID string ` json:"annotation_id" `
}
// swagger:parameters deleteAnnotationByID
type DeleteAnnotationByIDParams struct {
// in:path
// required:true
AnnotationID string ` json:"annotation_id" `
}
// swagger:parameters getAnnotations
type GetAnnotationsParams struct {
// Find annotations created after specific epoch datetime in milliseconds.
// in:query
// required:false
From int64 ` json:"from" `
// Find annotations created before specific epoch datetime in milliseconds.
// in:query
// required:false
To int64 ` json:"to" `
// Limit response to annotations created by specific user.
// in:query
// required:false
UserID int64 ` json:"userId" `
// Find annotations for a specified alert.
// in:query
// required:false
AlertID int64 ` json:"alertId" `
// Find annotations that are scoped to a specific dashboard
// in:query
// required:false
DashboardID int64 ` json:"dashboardId" `
// Find annotations that are scoped to a specific dashboard
// in:query
// required:false
DashboardUID string ` json:"dashboardUID" `
// Find annotations that are scoped to a specific panel
// in:query
// required:false
PanelID int64 ` json:"panelId" `
// Max limit for results returned.
// in:query
// required:false
Limit int64 ` json:"limit" `
// Use this to filter organization annotations. Organization annotations are annotations from an annotation data source that are not connected specifically to a dashboard or panel. You can filter by multiple tags.
// in:query
// required:false
// type: array
// collectionFormat: multi
Tags [ ] string ` json:"tags" `
// Return alerts or user created annotations
// in:query
// required:false
// Description:
// * `alert`
// * `annotation`
// enum: alert,annotation
Type string ` json:"type" `
// Match any or all tags
// in:query
// required:false
MatchAny bool ` json:"matchAny" `
}
// swagger:parameters getAnnotationTags
type GetAnnotationTagsParams struct {
// Tag is a string that you can use to filter tags.
// in:query
// required:false
Tag string ` json:"tag" `
// Max limit for results returned.
// in:query
// required:false
// default: 100
Limit string ` json:"limit" `
}
// swagger:parameters massDeleteAnnotations
type MassDeleteAnnotationsParams struct {
// in:body
// required:true
Body dtos . MassDeleteAnnotationsCmd ` json:"body" `
}
// swagger:parameters postAnnotation
type PostAnnotationParams struct {
// in:body
// required:true
Body dtos . PostAnnotationsCmd ` json:"body" `
}
// swagger:parameters postGraphiteAnnotation
type PostGraphiteAnnotationParams struct {
// in:body
// required:true
Body dtos . PostGraphiteAnnotationsCmd ` json:"body" `
}
// swagger:parameters updateAnnotation
type UpdateAnnotationParams struct {
// in:path
// required:true
AnnotationID string ` json:"annotation_id" `
// in:body
// required:true
Body dtos . UpdateAnnotationsCmd ` json:"body" `
}
// swagger:parameters patchAnnotation
type PatchAnnotationParams struct {
// in:path
// required:true
AnnotationID string ` json:"annotation_id" `
// in:body
// required:true
Body dtos . PatchAnnotationsCmd ` json:"body" `
}
// swagger:response getAnnotationsResponse
type GetAnnotationsResponse struct {
// The response message
// in: body
Body [ ] * annotations . ItemDTO ` json:"body" `
}
// swagger:response getAnnotationByIDResponse
type GetAnnotationByIDResponse struct {
// The response message
// in: body
Body * annotations . ItemDTO ` json:"body" `
}
// swagger:response postAnnotationResponse
type PostAnnotationResponse struct {
// The response message
// in: body
Body struct {
// ID Identifier of the created annotation.
// required: true
// example: 65
ID int64 ` json:"id" `
// Message Message of the created annotation.
// required: true
Message string ` json:"message" `
} ` json:"body" `
}
// swagger:response getAnnotationTagsResponse
type GetAnnotationTagsResponse struct {
// The response message
// in: body
Body annotations . GetAnnotationTagsResponse ` json:"body" `
}