From d92beeee3dad366352379fc95dc40f387dd4cb9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agn=C3=A8s=20Toulet?= <35176601+AgnesToulet@users.noreply.github.com> Date: Fri, 4 Dec 2020 16:22:58 +0100 Subject: [PATCH] API: add ID to snapshot API responses (#29600) * API: add ID to snapshot API responses * API: update snapshot tests --- docs/sources/http_api/snapshot.md | 9 +++++---- pkg/api/dashboard_snapshot.go | 11 +++++++++-- pkg/api/dashboard_snapshot_test.go | 8 ++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/sources/http_api/snapshot.md b/docs/sources/http_api/snapshot.md index 778087b620e..a602fa37bbb 100644 --- a/docs/sources/http_api/snapshot.md +++ b/docs/sources/http_api/snapshot.md @@ -68,7 +68,8 @@ JSON Body schema: "deleteKey":"XXXXXXX", "deleteUrl":"myurl/api/snapshots-delete/XXXXXXX", "key":"YYYYYYY", - "url":"myurl/dashboard/snapshot/YYYYYYY" + "url":"myurl/dashboard/snapshot/YYYYYYY", + "id": 1, } ``` @@ -192,7 +193,7 @@ Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk HTTP/1.1 200 Content-Type: application/json -{"message":"Snapshot deleted. It might take an hour before it's cleared from any CDN caches."} +{"message":"Snapshot deleted. It might take an hour before it's cleared from any CDN caches.", "id": 1} ``` ## Delete Snapshot by deleteKey @@ -214,5 +215,5 @@ Accept: application/json HTTP/1.1 200 Content-Type: application/json -{"message":"Snapshot deleted. It might take an hour before it's cleared from any CDN caches."} -``` \ No newline at end of file +{"message":"Snapshot deleted. It might take an hour before it's cleared from any CDN caches.", "id": 1} +``` diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 2449522531b..2aa18b44c25 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -132,6 +132,7 @@ func CreateDashboardSnapshot(c *models.ReqContext, cmd models.CreateDashboardSna "deleteKey": cmd.DeleteKey, "url": url, "deleteUrl": setting.ToAbsUrl("api/snapshots-delete/" + cmd.DeleteKey), + "id": cmd.Result.Id, }) } @@ -223,7 +224,10 @@ func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) Response { 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."}) + return JSON(200, util.DynMap{ + "message": "Snapshot deleted. It might take an hour before it's cleared from any CDN caches.", + "id": query.Result.Id, + }) } // DELETE /api/snapshots/:key @@ -269,7 +273,10 @@ func DeleteDashboardSnapshot(c *models.ReqContext) Response { 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."}) + return JSON(200, util.DynMap{ + "message": "Snapshot deleted. It might take an hour before it's cleared from any CDN caches.", + "id": query.Result.Id, + }) } // GET /api/dashboard/snapshots diff --git a/pkg/api/dashboard_snapshot_test.go b/pkg/api/dashboard_snapshot_test.go index 9689e02a215..6dbc39332a4 100644 --- a/pkg/api/dashboard_snapshot_test.go +++ b/pkg/api/dashboard_snapshot_test.go @@ -109,6 +109,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) { require.NoError(t, err) assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) + assert.Equal(t, 1, respJSON.Get("id").MustInt()) assert.Equal(t, http.MethodGet, externalRequest.Method) assert.Equal(t, ts.URL, fmt.Sprintf("http://%s", externalRequest.Host)) @@ -141,6 +142,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) { require.NoError(t, err) assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) + assert.Equal(t, 1, respJSON.Get("id").MustInt()) assert.Equal(t, ts.URL, fmt.Sprintf("http://%s", externalRequest.Host)) assert.Equal(t, "/", externalRequest.URL.EscapedPath()) }) @@ -163,6 +165,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) { require.NoError(t, err) assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) + assert.Equal(t, 1, respJSON.Get("id").MustInt()) }) }) @@ -186,6 +189,11 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) { require.NoError(t, writeErr) assert.Equal(t, 200, sc.resp.Code) + respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) + require.NoError(t, err) + + assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) + assert.Equal(t, 1, respJSON.Get("id").MustInt()) }) loggedInUserScenarioWithRole(t,