| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2015-03-25 16:04:38 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 22:56:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2019-02-24 06:35:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/metrics" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/guardian" | 
					
						
							| 
									
										
										
										
											2015-03-25 16:04:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | var client = &http.Client{ | 
					
						
							|  |  |  | 	Timeout:   time.Second * 5, | 
					
						
							|  |  |  | 	Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | func GetSharingOptions(c *models.ReqContext) { | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	c.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2015-10-14 22:39:57 +08:00
										 |  |  | 		"externalSnapshotURL":  setting.ExternalSnapshotUrl, | 
					
						
							|  |  |  | 		"externalSnapshotName": setting.ExternalSnapshotName, | 
					
						
							|  |  |  | 		"externalEnabled":      setting.ExternalEnabled, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | type CreateExternalSnapshotResponse struct { | 
					
						
							|  |  |  | 	Key       string `json:"key"` | 
					
						
							|  |  |  | 	DeleteKey string `json:"deleteKey"` | 
					
						
							|  |  |  | 	Url       string `json:"url"` | 
					
						
							|  |  |  | 	DeleteUrl string `json:"deleteUrl"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | func createExternalDashboardSnapshot(cmd models.CreateDashboardSnapshotCommand) (*CreateExternalSnapshotResponse, error) { | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 	var createSnapshotResponse CreateExternalSnapshotResponse | 
					
						
							|  |  |  | 	message := map[string]interface{}{ | 
					
						
							|  |  |  | 		"name":      cmd.Name, | 
					
						
							|  |  |  | 		"expires":   cmd.Expires, | 
					
						
							|  |  |  | 		"dashboard": cmd.Dashboard, | 
					
						
							| 
									
										
										
										
											2021-05-14 06:39:43 +08:00
										 |  |  | 		"key":       cmd.Key, | 
					
						
							|  |  |  | 		"deleteKey": cmd.DeleteKey, | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-15 16:32:06 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err := response.Body.Close(); err != nil { | 
					
						
							|  |  |  | 			plog.Warn("Failed to close response body", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if response.StatusCode != 200 { | 
					
						
							| 
									
										
										
										
											2020-11-05 20:07:06 +08:00
										 |  |  | 		return nil, fmt.Errorf("create external snapshot response status code %d", response.StatusCode) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := json.NewDecoder(response.Body).Decode(&createSnapshotResponse); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &createSnapshotResponse, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // POST /api/snapshots
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | func (hs *HTTPServer) CreateDashboardSnapshot(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 	cmd := models.CreateDashboardSnapshotCommand{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-21 15:59:48 +08:00
										 |  |  | 	if cmd.Name == "" { | 
					
						
							|  |  |  | 		cmd.Name = "Unnamed snapshot" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 	var url string | 
					
						
							|  |  |  | 	cmd.ExternalUrl = "" | 
					
						
							|  |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							|  |  |  | 	cmd.UserId = c.UserId | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-25 16:04:38 +08:00
										 |  |  | 	if cmd.External { | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 		if !setting.ExternalEnabled { | 
					
						
							|  |  |  | 			c.JsonApiErr(403, "External dashboard creation is disabled", nil) | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 			return nil | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		response, err := createExternalDashboardSnapshot(cmd) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 			c.JsonApiErr(500, "Failed to create external snapshot", err) | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 			return nil | 
					
						
							| 
									
										
										
										
											2015-03-27 03:59:41 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 		url = response.Url | 
					
						
							|  |  |  | 		cmd.Key = response.Key | 
					
						
							|  |  |  | 		cmd.DeleteKey = response.DeleteKey | 
					
						
							|  |  |  | 		cmd.ExternalUrl = response.Url | 
					
						
							|  |  |  | 		cmd.ExternalDeleteUrl = response.DeleteUrl | 
					
						
							|  |  |  | 		cmd.Dashboard = simplejson.New() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 		metrics.MApiDashboardSnapshotExternal.Inc() | 
					
						
							| 
									
										
										
										
											2015-03-26 19:00:52 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-05-09 17:53:04 +08:00
										 |  |  | 		if cmd.Key == "" { | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 			var err error | 
					
						
							|  |  |  | 			cmd.Key, err = util.GetRandomString(32) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				c.JsonApiErr(500, "Could not generate random string", err) | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 				return nil | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-05-09 17:53:04 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if cmd.DeleteKey == "" { | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 			var err error | 
					
						
							|  |  |  | 			cmd.DeleteKey, err = util.GetRandomString(32) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				c.JsonApiErr(500, "Could not generate random string", err) | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 				return nil | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-05-09 17:53:04 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 		url = setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 		metrics.MApiDashboardSnapshotCreate.Inc() | 
					
						
							| 
									
										
										
										
											2015-03-25 16:04:38 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	if err := hs.DashboardsnapshotsService.CreateDashboardSnapshot(c.Req.Context(), &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 		c.JsonApiErr(500, "Failed to create snapshot", err) | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	c.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2015-03-27 03:34:58 +08:00
										 |  |  | 		"key":       cmd.Key, | 
					
						
							|  |  |  | 		"deleteKey": cmd.DeleteKey, | 
					
						
							| 
									
										
										
										
											2018-12-11 05:36:32 +08:00
										 |  |  | 		"url":       url, | 
					
						
							| 
									
										
										
										
											2015-03-27 03:34:58 +08:00
										 |  |  | 		"deleteUrl": setting.ToAbsUrl("api/snapshots-delete/" + cmd.DeleteKey), | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 		"id":        cmd.Result.Id, | 
					
						
							| 
									
										
										
										
											2015-03-27 03:34:58 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-11-30 02:05:20 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-03-25 16:04:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | // GET /api/snapshots/:key
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | func (hs *HTTPServer) GetDashboardSnapshot(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	key := web.Params(c.Req)[":key"] | 
					
						
							| 
									
										
										
										
											2021-10-06 01:38:09 +08:00
										 |  |  | 	if len(key) == 0 { | 
					
						
							|  |  |  | 		return response.Error(404, "Snapshot not found", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := &models.GetDashboardSnapshotQuery{Key: key} | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	err := hs.DashboardsnapshotsService.GetDashboardSnapshot(c.Req.Context(), query) | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get dashboard snapshot", err) | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-26 19:00:52 +08:00
										 |  |  | 	snapshot := query.Result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// expired snapshots should also be removed from db
 | 
					
						
							|  |  |  | 	if snapshot.Expires.Before(time.Now()) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Dashboard snapshot not found", err) | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	dto := dtos.DashboardFullWithMeta{ | 
					
						
							| 
									
										
										
										
											2021-09-01 19:05:15 +08:00
										 |  |  | 		Dashboard: snapshot.Dashboard, | 
					
						
							| 
									
										
										
										
											2015-03-29 00:53:52 +08:00
										 |  |  | 		Meta: dtos.DashboardMeta{ | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			Type:       models.DashTypeSnapshot, | 
					
						
							| 
									
										
										
										
											2015-03-29 00:53:52 +08:00
										 |  |  | 			IsSnapshot: true, | 
					
						
							|  |  |  | 			Created:    snapshot.Created, | 
					
						
							|  |  |  | 			Expires:    snapshot.Expires, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-03-21 22:56:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	metrics.MApiDashboardSnapshotGet.Inc() | 
					
						
							| 
									
										
										
										
											2015-03-24 23:49:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dto).SetHeader("Cache-Control", "public, max-age=3600") | 
					
						
							| 
									
										
										
										
											2015-03-27 03:34:58 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-26 19:00:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | func deleteExternalDashboardSnapshot(externalUrl string) error { | 
					
						
							|  |  |  | 	response, err := client.Get(externalUrl) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-15 21:29:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err := response.Body.Close(); err != nil { | 
					
						
							|  |  |  | 			plog.Warn("Failed to close response body", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08: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 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 20:07:06 +08:00
										 |  |  | 	return fmt.Errorf("unexpected response when deleting external snapshot, status code: %d", response.StatusCode) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | // GET /api/snapshots-delete/:deleteKey
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | func (hs *HTTPServer) DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	key := web.Params(c.Req)[":deleteKey"] | 
					
						
							| 
									
										
										
										
											2021-10-06 01:38:09 +08:00
										 |  |  | 	if len(key) == 0 { | 
					
						
							|  |  |  | 		return response.Error(404, "Snapshot not found", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := &models.GetDashboardSnapshotQuery{DeleteKey: key} | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	err := hs.DashboardsnapshotsService.GetDashboardSnapshot(c.Req.Context(), query) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get dashboard snapshot", err) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 	if query.Result.External { | 
					
						
							|  |  |  | 		err := deleteExternalDashboardSnapshot(query.Result.ExternalDeleteUrl) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to delete external dashboard", err) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := &models.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey} | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	if err := hs.DashboardsnapshotsService.DeleteDashboardSnapshot(c.Req.Context(), cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to delete dashboard snapshot", err) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 		"message": "Snapshot deleted. It might take an hour before it's cleared from any CDN caches.", | 
					
						
							|  |  |  | 		"id":      query.Result.Id, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DELETE /api/snapshots/:key
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | func (hs *HTTPServer) DeleteDashboardSnapshot(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	key := web.Params(c.Req)[":key"] | 
					
						
							| 
									
										
										
										
											2021-10-06 01:38:09 +08:00
										 |  |  | 	if len(key) == 0 { | 
					
						
							|  |  |  | 		return response.Error(404, "Snapshot not found", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := &models.GetDashboardSnapshotQuery{Key: key} | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	err := hs.DashboardsnapshotsService.GetDashboardSnapshot(c.Req.Context(), query) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get dashboard snapshot", err) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if query.Result == nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Failed to get dashboard snapshot", nil) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 19:05:15 +08:00
										 |  |  | 	dashboardID := query.Result.Dashboard.Get("id").MustInt64() | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 23:43:32 +08:00
										 |  |  | 	guardian := guardian.New(c.Req.Context(), dashboardID, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	canEdit, err := guardian.CanEdit() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Error while checking permissions for snapshot", err) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !canEdit && query.Result.UserId != c.SignedInUser.UserId { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(403, "Access denied to this snapshot", nil) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 	if query.Result.External { | 
					
						
							|  |  |  | 		err := deleteExternalDashboardSnapshot(query.Result.ExternalDeleteUrl) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to delete external dashboard", err) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := &models.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey} | 
					
						
							| 
									
										
										
										
											2015-03-24 23:49:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	if err := hs.DashboardsnapshotsService.DeleteDashboardSnapshot(c.Req.Context(), cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to delete dashboard snapshot", err) | 
					
						
							| 
									
										
										
										
											2015-03-27 03:34:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 		"message": "Snapshot deleted. It might take an hour before it's cleared from any CDN caches.", | 
					
						
							|  |  |  | 		"id":      query.Result.Id, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-19 17:37:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | // GET /api/dashboard/snapshots
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | func (hs *HTTPServer) SearchDashboardSnapshots(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2016-01-19 21:05:24 +08:00
										 |  |  | 	query := c.Query("query") | 
					
						
							|  |  |  | 	limit := c.QueryInt("limit") | 
					
						
							| 
									
										
										
										
											2016-01-19 17:37:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 21:05:24 +08:00
										 |  |  | 	if limit == 0 { | 
					
						
							|  |  |  | 		limit = 1000 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-19 17:37:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	searchQuery := models.GetDashboardSnapshotsQuery{ | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 		Name:         query, | 
					
						
							|  |  |  | 		Limit:        limit, | 
					
						
							|  |  |  | 		OrgId:        c.OrgId, | 
					
						
							|  |  |  | 		SignedInUser: c.SignedInUser, | 
					
						
							| 
									
										
										
										
											2016-01-19 21:05:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-19 17:37:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	err := hs.DashboardsnapshotsService.SearchDashboardSnapshots(c.Req.Context(), &searchQuery) | 
					
						
							| 
									
										
										
										
											2016-01-19 21:05:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Search failed", err) | 
					
						
							| 
									
										
										
										
											2016-01-19 21:05:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-19 17:37:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	dtos := make([]*models.DashboardSnapshotDTO, len(searchQuery.Result)) | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:44 +08:00
										 |  |  | 	for i, snapshot := range searchQuery.Result { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		dtos[i] = &models.DashboardSnapshotDTO{ | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:44 +08:00
										 |  |  | 			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, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dtos) | 
					
						
							| 
									
										
										
										
											2016-01-19 17:37:36 +08:00
										 |  |  | } |