grafana/pkg/api/dashboard_snapshot.go

189 lines
4.9 KiB
Go
Raw Normal View History

2015-03-21 07:53:16 -05:00
package api
import (
"time"
"github.com/grafana/grafana/pkg/api/dtos"
2015-03-21 07:53:16 -05:00
"github.com/grafana/grafana/pkg/bus"
2015-03-24 10:49:12 -05:00
"github.com/grafana/grafana/pkg/metrics"
2015-03-21 07:53:16 -05:00
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/setting"
2015-03-21 07:53:16 -05:00
"github.com/grafana/grafana/pkg/util"
)
2018-03-07 10:54:50 -06:00
func GetSharingOptions(c *m.ReqContext) {
c.JSON(200, util.DynMap{
"externalSnapshotURL": setting.ExternalSnapshotUrl,
"externalSnapshotName": setting.ExternalSnapshotName,
"externalEnabled": setting.ExternalEnabled,
})
}
2018-03-07 10:54:50 -06:00
func CreateDashboardSnapshot(c *m.ReqContext, cmd m.CreateDashboardSnapshotCommand) {
if cmd.Name == "" {
cmd.Name = "Unnamed snapshot"
}
if cmd.External {
// external snapshot ref requires key and delete key
2015-03-27 00:47:58 -05:00
if cmd.Key == "" || cmd.DeleteKey == "" {
c.JsonApiErr(400, "Missing key and delete key for external snapshot", nil)
return
}
cmd.OrgId = -1
cmd.UserId = -1
2017-09-06 04:23:52 -05:00
metrics.M_Api_Dashboard_Snapshot_External.Inc()
} else {
cmd.Key = util.GetRandomString(32)
cmd.DeleteKey = util.GetRandomString(32)
cmd.OrgId = c.OrgId
cmd.UserId = c.UserId
2017-09-06 04:23:52 -05:00
metrics.M_Api_Dashboard_Snapshot_Create.Inc()
}
2015-03-21 07:53:16 -05:00
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to create snaphost", err)
return
}
c.JSON(200, util.DynMap{
"key": cmd.Key,
"deleteKey": cmd.DeleteKey,
"url": setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key),
"deleteUrl": setting.ToAbsUrl("api/snapshots-delete/" + cmd.DeleteKey),
})
}
// GET /api/snapshots/:key
2018-03-07 10:54:50 -06:00
func GetDashboardSnapshot(c *m.ReqContext) {
key := c.Params(":key")
2015-03-21 07:53:16 -05:00
query := &m.GetDashboardSnapshotQuery{Key: key}
err := bus.Dispatch(query)
if err != nil {
c.JsonApiErr(500, "Failed to get dashboard snapshot", err)
return
}
snapshot := query.Result
// expired snapshots should also be removed from db
if snapshot.Expires.Before(time.Now()) {
c.JsonApiErr(404, "Dashboard snapshot not found", err)
return
}
dto := dtos.DashboardFullWithMeta{
Dashboard: snapshot.Dashboard,
Meta: dtos.DashboardMeta{
Type: m.DashTypeSnapshot,
IsSnapshot: true,
Created: snapshot.Created,
Expires: snapshot.Expires,
},
}
2017-09-06 04:23:52 -05:00
metrics.M_Api_Dashboard_Snapshot_Get.Inc()
2015-03-24 10:49:12 -05:00
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
c.JSON(200, dto)
}
// GET /api/snapshots-delete/:deleteKey
func DeleteDashboardSnapshotByDeleteKey(c *m.ReqContext) Response {
key := c.Params(":deleteKey")
query := &m.GetDashboardSnapshotQuery{DeleteKey: key}
err := bus.Dispatch(query)
if err != nil {
return Error(500, "Failed to get dashboard snapshot", err)
}
cmd := &m.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey}
if err := bus.Dispatch(cmd); err != nil {
return Error(500, "Failed to delete dashboard snapshot", err)
}
return JSON(200, util.DynMap{"message": "Snapshot deleted. It might take an hour before it's cleared from any CDN caches."})
}
// DELETE /api/snapshots/:key
2018-03-07 10:54:50 -06:00
func DeleteDashboardSnapshot(c *m.ReqContext) Response {
key := c.Params(":key")
query := &m.GetDashboardSnapshotQuery{Key: key}
err := bus.Dispatch(query)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get dashboard snapshot", err)
}
if query.Result == nil {
2018-03-22 16:13:46 -05:00
return Error(404, "Failed to get dashboard snapshot", nil)
}
dashboard := query.Result.Dashboard
2018-03-22 06:37:35 -05:00
dashboardID := dashboard.Get("id").MustInt64()
2018-03-22 06:37:35 -05:00
guardian := guardian.New(dashboardID, c.OrgId, c.SignedInUser)
canEdit, err := guardian.CanEdit()
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Error while checking permissions for snapshot", err)
}
if !canEdit && query.Result.UserId != c.SignedInUser.UserId {
2018-03-22 16:13:46 -05:00
return Error(403, "Access denied to this snapshot", nil)
}
cmd := &m.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey}
2015-03-24 10:49:12 -05:00
if err := bus.Dispatch(cmd); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to delete dashboard snapshot", err)
}
return JSON(200, util.DynMap{"message": "Snapshot deleted. It might take an hour before it's cleared from any CDN caches."})
2015-03-21 07:53:16 -05:00
}
// GET /api/dashboard/snapshots
2018-03-07 10:54:50 -06:00
func SearchDashboardSnapshots(c *m.ReqContext) Response {
2016-01-19 07:05:24 -06:00
query := c.Query("query")
limit := c.QueryInt("limit")
2016-01-19 07:05:24 -06:00
if limit == 0 {
limit = 1000
}
2016-01-19 07:05:24 -06:00
searchQuery := m.GetDashboardSnapshotsQuery{
Name: query,
Limit: limit,
OrgId: c.OrgId,
SignedInUser: c.SignedInUser,
2016-01-19 07:05:24 -06:00
}
2016-01-19 07:05:24 -06:00
err := bus.Dispatch(&searchQuery)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Search failed", err)
2016-01-19 07:05:24 -06:00
}
2016-01-20 01:23:44 -06:00
dtos := make([]*m.DashboardSnapshotDTO, len(searchQuery.Result))
for i, snapshot := range searchQuery.Result {
dtos[i] = &m.DashboardSnapshotDTO{
Id: snapshot.Id,
Name: snapshot.Name,
Key: snapshot.Key,
OrgId: snapshot.OrgId,
UserId: snapshot.UserId,
External: snapshot.External,
ExternalUrl: snapshot.ExternalUrl,
Expires: snapshot.Expires,
Created: snapshot.Created,
Updated: snapshot.Updated,
}
}
2018-03-22 16:13:46 -05:00
return JSON(200, dtos)
}