2015-03-21 07:53:16 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-12-10 15:36:32 -06:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-03-25 03:04:38 -05:00
|
|
|
"time"
|
|
|
|
|
2015-03-21 09:56:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2015-03-21 07:53:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2018-12-10 15:36:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2015-03-21 07:53:16 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2018-02-20 16:26:08 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2015-03-25 03:04:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-03-21 07:53:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2018-12-10 15:36:32 -06:00
|
|
|
var client = &http.Client{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
|
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetSharingOptions(c *m.ReqContext) {
|
2015-10-14 09:39:57 -05:00
|
|
|
c.JSON(200, util.DynMap{
|
|
|
|
"externalSnapshotURL": setting.ExternalSnapshotUrl,
|
|
|
|
"externalSnapshotName": setting.ExternalSnapshotName,
|
|
|
|
"externalEnabled": setting.ExternalEnabled,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:36:32 -06:00
|
|
|
type CreateExternalSnapshotResponse struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
DeleteKey string `json:"deleteKey"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
DeleteUrl string `json:"deleteUrl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func createExternalDashboardSnapshot(cmd m.CreateDashboardSnapshotCommand) (*CreateExternalSnapshotResponse, error) {
|
|
|
|
var createSnapshotResponse CreateExternalSnapshotResponse
|
|
|
|
message := map[string]interface{}{
|
|
|
|
"name": cmd.Name,
|
|
|
|
"expires": cmd.Expires,
|
|
|
|
"dashboard": cmd.Dashboard,
|
|
|
|
}
|
|
|
|
|
|
|
|
messageBytes, err := simplejson.NewFromAny(message).Encode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.Post(setting.ExternalSnapshotUrl+"/api/snapshots", "application/json", bytes.NewBuffer(messageBytes))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-18 07:32:49 -06:00
|
|
|
defer response.Body.Close()
|
2018-12-10 15:36:32 -06:00
|
|
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("Create external snapshot response status code %d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(&createSnapshotResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &createSnapshotResponse, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/snapshots
|
2018-03-07 10:54:50 -06:00
|
|
|
func CreateDashboardSnapshot(c *m.ReqContext, cmd m.CreateDashboardSnapshotCommand) {
|
2016-04-21 02:59:48 -05:00
|
|
|
if cmd.Name == "" {
|
|
|
|
cmd.Name = "Unnamed snapshot"
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:36:32 -06:00
|
|
|
var url string
|
|
|
|
cmd.ExternalUrl = ""
|
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
cmd.UserId = c.UserId
|
|
|
|
|
2015-03-25 03:04:38 -05:00
|
|
|
if cmd.External {
|
2018-12-10 15:36:32 -06:00
|
|
|
if !setting.ExternalEnabled {
|
|
|
|
c.JsonApiErr(403, "External dashboard creation is disabled", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := createExternalDashboardSnapshot(cmd)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to create external snaphost", err)
|
2015-03-26 14:59:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:36:32 -06:00
|
|
|
url = response.Url
|
|
|
|
cmd.Key = response.Key
|
|
|
|
cmd.DeleteKey = response.DeleteKey
|
|
|
|
cmd.ExternalUrl = response.Url
|
|
|
|
cmd.ExternalDeleteUrl = response.DeleteUrl
|
|
|
|
cmd.Dashboard = simplejson.New()
|
|
|
|
|
2017-09-06 04:23:52 -05:00
|
|
|
metrics.M_Api_Dashboard_Snapshot_External.Inc()
|
2015-03-26 06:00:52 -05:00
|
|
|
} else {
|
2015-03-26 14:59:41 -05:00
|
|
|
cmd.Key = util.GetRandomString(32)
|
|
|
|
cmd.DeleteKey = util.GetRandomString(32)
|
2018-12-10 15:36:32 -06:00
|
|
|
url = setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key)
|
|
|
|
|
2017-09-06 04:23:52 -05:00
|
|
|
metrics.M_Api_Dashboard_Snapshot_Create.Inc()
|
2015-03-25 03:04:38 -05:00
|
|
|
}
|
2015-03-21 07:53:16 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to create snaphost", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-26 14:34:58 -05:00
|
|
|
c.JSON(200, util.DynMap{
|
|
|
|
"key": cmd.Key,
|
|
|
|
"deleteKey": cmd.DeleteKey,
|
2018-12-10 15:36:32 -06:00
|
|
|
"url": url,
|
2015-03-26 14:34:58 -05:00
|
|
|
"deleteUrl": setting.ToAbsUrl("api/snapshots-delete/" + cmd.DeleteKey),
|
|
|
|
})
|
2015-03-25 03:04:38 -05:00
|
|
|
}
|
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
// GET /api/snapshots/:key
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetDashboardSnapshot(c *m.ReqContext) {
|
2015-05-04 01:36:44 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:00:52 -05:00
|
|
|
snapshot := query.Result
|
|
|
|
|
|
|
|
// expired snapshots should also be removed from db
|
|
|
|
if snapshot.Expires.Before(time.Now()) {
|
2015-07-07 14:51:38 -05:00
|
|
|
c.JsonApiErr(404, "Dashboard snapshot not found", err)
|
2015-03-26 06:00:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-04 01:36:44 -05:00
|
|
|
dto := dtos.DashboardFullWithMeta{
|
|
|
|
Dashboard: snapshot.Dashboard,
|
2015-03-28 11:53:52 -05:00
|
|
|
Meta: dtos.DashboardMeta{
|
2015-05-13 02:58:45 -05:00
|
|
|
Type: m.DashTypeSnapshot,
|
2015-03-28 11:53:52 -05:00
|
|
|
IsSnapshot: true,
|
|
|
|
Created: snapshot.Created,
|
|
|
|
Expires: snapshot.Expires,
|
|
|
|
},
|
2015-03-21 09:56:26 -05:00
|
|
|
}
|
|
|
|
|
2017-09-06 04:23:52 -05:00
|
|
|
metrics.M_Api_Dashboard_Snapshot_Get.Inc()
|
2015-03-24 10:49:12 -05:00
|
|
|
|
2015-03-26 14:34:58 -05:00
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
c.JSON(200, dto)
|
|
|
|
}
|
2015-03-26 06:00:52 -05:00
|
|
|
|
2018-12-10 15:40:26 -06:00
|
|
|
func deleteExternalDashboardSnapshot(externalUrl string) error {
|
|
|
|
response, err := client.Get(externalUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-18 07:32:49 -06:00
|
|
|
defer response.Body.Close()
|
2018-12-10 15:40:26 -06:00
|
|
|
|
|
|
|
if response.StatusCode == 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gracefully ignore "snapshot not found" errors as they could have already
|
|
|
|
// been removed either via the cleanup script or by request.
|
|
|
|
if response.StatusCode == 500 {
|
|
|
|
var respJson map[string]interface{}
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(&respJson); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if respJson["message"] == "Failed to get dashboard snapshot" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Unexpected response when deleting external snapshot. Status code: %d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:40:26 -06:00
|
|
|
if query.Result.External {
|
|
|
|
err := deleteExternalDashboardSnapshot(query.Result.ExternalDeleteUrl)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to delete external dashboard", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
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 {
|
2015-03-26 14:34:58 -05:00
|
|
|
key := c.Params(":key")
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
query := &m.GetDashboardSnapshotQuery{Key: key}
|
2018-02-20 16:26:08 -06:00
|
|
|
|
|
|
|
err := bus.Dispatch(query)
|
|
|
|
if err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get dashboard snapshot", err)
|
2018-02-20 16:26:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if query.Result == nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Failed to get dashboard snapshot", nil)
|
2018-02-20 16:26:08 -06:00
|
|
|
}
|
|
|
|
dashboard := query.Result.Dashboard
|
2018-03-22 06:37:35 -05:00
|
|
|
dashboardID := dashboard.Get("id").MustInt64()
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
guardian := guardian.New(dashboardID, c.OrgId, c.SignedInUser)
|
2018-02-20 16:26:08 -06:00
|
|
|
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)
|
2018-02-20 16:26:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-02-20 16:26:08 -06:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:40:26 -06:00
|
|
|
if query.Result.External {
|
|
|
|
err := deleteExternalDashboardSnapshot(query.Result.ExternalDeleteUrl)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to delete external dashboard", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
cmd := &m.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey}
|
2015-03-24 10:49:12 -05:00
|
|
|
|
2015-03-26 14:34:58 -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)
|
2015-03-26 14:34:58 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
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
|
|
|
}
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2018-02-20 16:26:08 -06: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 03:37:36 -06:00
|
|
|
|
2016-01-19 07:05:24 -06:00
|
|
|
if limit == 0 {
|
|
|
|
limit = 1000
|
|
|
|
}
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2016-01-19 07:05:24 -06:00
|
|
|
searchQuery := m.GetDashboardSnapshotsQuery{
|
2018-02-20 16:26:08 -06:00
|
|
|
Name: query,
|
|
|
|
Limit: limit,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
SignedInUser: c.SignedInUser,
|
2016-01-19 07:05:24 -06:00
|
|
|
}
|
2016-01-19 03:37:36 -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-19 03:37:36 -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)
|
2016-01-19 03:37:36 -06:00
|
|
|
}
|