mirror of
https://github.com/grafana/grafana.git
synced 2024-12-29 10:21:41 -06:00
dashboard snapshot bus removal (#44956)
This commit is contained in:
parent
424a79273f
commit
38f93d675b
@ -360,7 +360,7 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
|
||||
// Dashboard snapshots
|
||||
apiRoute.Group("/dashboard/snapshots", func(dashboardRoute routing.RouteRegister) {
|
||||
dashboardRoute.Get("/", routing.Wrap(SearchDashboardSnapshots))
|
||||
dashboardRoute.Get("/", routing.Wrap(hs.SearchDashboardSnapshots))
|
||||
})
|
||||
|
||||
// Playlist
|
||||
@ -515,11 +515,11 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
r.Get("/avatar/:hash", avatarCacheServer.Handler)
|
||||
|
||||
// Snapshots
|
||||
r.Post("/api/snapshots/", reqSnapshotPublicModeOrSignedIn, CreateDashboardSnapshot)
|
||||
r.Post("/api/snapshots/", reqSnapshotPublicModeOrSignedIn, hs.CreateDashboardSnapshot)
|
||||
r.Get("/api/snapshot/shared-options/", reqSignedIn, GetSharingOptions)
|
||||
r.Get("/api/snapshots/:key", routing.Wrap(GetDashboardSnapshot))
|
||||
r.Get("/api/snapshots-delete/:deleteKey", reqSnapshotPublicModeOrSignedIn, routing.Wrap(DeleteDashboardSnapshotByDeleteKey))
|
||||
r.Delete("/api/snapshots/:key", reqEditorRole, routing.Wrap(DeleteDashboardSnapshot))
|
||||
r.Get("/api/snapshots/:key", routing.Wrap(hs.GetDashboardSnapshot))
|
||||
r.Get("/api/snapshots-delete/:deleteKey", reqSnapshotPublicModeOrSignedIn, routing.Wrap(hs.DeleteDashboardSnapshotByDeleteKey))
|
||||
r.Delete("/api/snapshots/:key", reqEditorRole, routing.Wrap(hs.DeleteDashboardSnapshot))
|
||||
|
||||
// Frontend logs
|
||||
sourceMapStore := frontendlogging.NewSourceMapStore(hs.Cfg, hs.pluginStaticRouteResolver, frontendlogging.ReadSourceMapFromFS)
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
@ -76,7 +75,7 @@ func createExternalDashboardSnapshot(cmd models.CreateDashboardSnapshotCommand)
|
||||
}
|
||||
|
||||
// POST /api/snapshots
|
||||
func CreateDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) CreateDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
cmd := models.CreateDashboardSnapshotCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
@ -134,7 +133,7 @@ func CreateDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
metrics.MApiDashboardSnapshotCreate.Inc()
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.CreateDashboardSnapshot(c.Req.Context(), &cmd); err != nil {
|
||||
c.JsonApiErr(500, "Failed to create snapshot", err)
|
||||
return nil
|
||||
}
|
||||
@ -150,7 +149,7 @@ func CreateDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// GET /api/snapshots/:key
|
||||
func GetDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
key := web.Params(c.Req)[":key"]
|
||||
if len(key) == 0 {
|
||||
return response.Error(404, "Snapshot not found", nil)
|
||||
@ -158,7 +157,7 @@ func GetDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
|
||||
query := &models.GetDashboardSnapshotQuery{Key: key}
|
||||
|
||||
err := bus.Dispatch(c.Req.Context(), query)
|
||||
err := hs.SQLStore.GetDashboardSnapshot(query)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to get dashboard snapshot", err)
|
||||
}
|
||||
@ -218,7 +217,7 @@ func deleteExternalDashboardSnapshot(externalUrl string) error {
|
||||
}
|
||||
|
||||
// GET /api/snapshots-delete/:deleteKey
|
||||
func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response {
|
||||
key := web.Params(c.Req)[":deleteKey"]
|
||||
if len(key) == 0 {
|
||||
return response.Error(404, "Snapshot not found", nil)
|
||||
@ -226,7 +225,7 @@ func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response
|
||||
|
||||
query := &models.GetDashboardSnapshotQuery{DeleteKey: key}
|
||||
|
||||
err := bus.Dispatch(c.Req.Context(), query)
|
||||
err := hs.SQLStore.GetDashboardSnapshot(query)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to get dashboard snapshot", err)
|
||||
}
|
||||
@ -240,7 +239,7 @@ func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response
|
||||
|
||||
cmd := &models.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), cmd); err != nil {
|
||||
if err := hs.SQLStore.DeleteDashboardSnapshot(c.Req.Context(), cmd); err != nil {
|
||||
return response.Error(500, "Failed to delete dashboard snapshot", err)
|
||||
}
|
||||
|
||||
@ -251,7 +250,7 @@ func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response
|
||||
}
|
||||
|
||||
// DELETE /api/snapshots/:key
|
||||
func DeleteDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) DeleteDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
key := web.Params(c.Req)[":key"]
|
||||
if len(key) == 0 {
|
||||
return response.Error(404, "Snapshot not found", nil)
|
||||
@ -259,7 +258,7 @@ func DeleteDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
|
||||
query := &models.GetDashboardSnapshotQuery{Key: key}
|
||||
|
||||
err := bus.Dispatch(c.Req.Context(), query)
|
||||
err := hs.SQLStore.GetDashboardSnapshot(query)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to get dashboard snapshot", err)
|
||||
}
|
||||
@ -288,7 +287,7 @@ func DeleteDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
|
||||
cmd := &models.DeleteDashboardSnapshotCommand{DeleteKey: query.Result.DeleteKey}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), cmd); err != nil {
|
||||
if err := hs.SQLStore.DeleteDashboardSnapshot(c.Req.Context(), cmd); err != nil {
|
||||
return response.Error(500, "Failed to delete dashboard snapshot", err)
|
||||
}
|
||||
|
||||
@ -299,7 +298,7 @@ func DeleteDashboardSnapshot(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// GET /api/dashboard/snapshots
|
||||
func SearchDashboardSnapshots(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) SearchDashboardSnapshots(c *models.ReqContext) response.Response {
|
||||
query := c.Query("query")
|
||||
limit := c.QueryInt("limit")
|
||||
|
||||
@ -314,7 +313,7 @@ func SearchDashboardSnapshots(c *models.ReqContext) response.Response {
|
||||
SignedInUser: c.SignedInUser,
|
||||
}
|
||||
|
||||
err := bus.Dispatch(c.Req.Context(), &searchQuery)
|
||||
err := hs.SQLStore.SearchDashboardSnapshots(&searchQuery)
|
||||
if err != nil {
|
||||
return response.Error(500, "Search failed", err)
|
||||
}
|
||||
|
@ -31,11 +31,12 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
|
||||
viewerRole := models.ROLE_VIEWER
|
||||
editorRole := models.ROLE_EDITOR
|
||||
sqlmock := mockstore.NewSQLStoreMock()
|
||||
aclMockResp := []*models.DashboardAclInfoDTO{}
|
||||
hs := &HTTPServer{SQLStore: sqlmock}
|
||||
|
||||
setUpSnapshotTest := func(t *testing.T) *models.DashboardSnapshot {
|
||||
t.Helper()
|
||||
|
||||
mockSnapshotResult := &models.DashboardSnapshot{
|
||||
Id: 1,
|
||||
Key: "12345",
|
||||
@ -45,48 +46,34 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
UserId: 999999,
|
||||
External: true,
|
||||
}
|
||||
sqlmock.ExpectedDashboardSnapshot = mockSnapshotResult
|
||||
sqlmock.ExpectedDashboardAclInfoList = aclMockResp
|
||||
sqlmock.ExpectedTeamsByUser = []*models.TeamDTO{}
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
|
||||
query.Result = mockSnapshotResult
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
// we need it here for now for the guadian service to work
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
query.Result = aclMockResp
|
||||
return nil
|
||||
})
|
||||
|
||||
teamResp := []*models.TeamDTO{}
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetTeamsByUserQuery) error {
|
||||
query.Result = teamResp
|
||||
return nil
|
||||
})
|
||||
|
||||
return mockSnapshotResult
|
||||
}
|
||||
|
||||
t.Run("When user has editor role and is not in the ACL", func(t *testing.T) {
|
||||
mock := mockstore.NewSQLStoreMock()
|
||||
loggedInUserScenarioWithRole(t, "Should not be able to delete snapshot when calling DELETE on",
|
||||
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
mockSnapshotResult := setUpSnapshotTest(t)
|
||||
|
||||
var externalRequest *http.Request
|
||||
mockSnapshotResult := setUpSnapshotTest(t)
|
||||
ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) {
|
||||
externalRequest = req
|
||||
})
|
||||
|
||||
mockSnapshotResult.ExternalDeleteUrl = ts.URL
|
||||
sc.handlerFunc = DeleteDashboardSnapshot
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
assert.Equal(t, 403, sc.resp.Code)
|
||||
require.Nil(t, externalRequest)
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
})
|
||||
|
||||
t.Run("When user is anonymous", func(t *testing.T) {
|
||||
@ -101,7 +88,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
})
|
||||
|
||||
mockSnapshotResult.ExternalDeleteUrl = ts.URL
|
||||
sc.handlerFunc = DeleteDashboardSnapshotByDeleteKey
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{"deleteKey": "12345"}).exec()
|
||||
|
||||
require.Equal(t, 200, sc.resp.Code)
|
||||
@ -122,7 +109,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
{Role: &viewerRole, Permission: models.PERMISSION_VIEW},
|
||||
{Role: &editorRole, Permission: models.PERMISSION_EDIT},
|
||||
}
|
||||
mock := mockstore.NewSQLStoreMock()
|
||||
|
||||
loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot when calling DELETE on", "DELETE",
|
||||
"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
mockSnapshotResult := setUpSnapshotTest(t)
|
||||
@ -132,9 +119,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
rw.WriteHeader(200)
|
||||
externalRequest = req
|
||||
})
|
||||
|
||||
mockSnapshotResult.ExternalDeleteUrl = ts.URL
|
||||
sc.handlerFunc = DeleteDashboardSnapshot
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
@ -145,20 +131,19 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
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())
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
})
|
||||
|
||||
t.Run("When user is editor and creator of the snapshot", func(t *testing.T) {
|
||||
aclMockResp = []*models.DashboardAclInfoDTO{}
|
||||
mock := mockstore.NewSQLStoreMock()
|
||||
aclMockResp := []*models.DashboardAclInfoDTO{}
|
||||
loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot when calling DELETE on",
|
||||
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
mockSnapshotResult := setUpSnapshotTest(t)
|
||||
|
||||
sqlmock.ExpectedDashboardAclInfoList = aclMockResp
|
||||
mockSnapshotResult.UserId = testUserID
|
||||
mockSnapshotResult.External = false
|
||||
|
||||
sc.handlerFunc = DeleteDashboardSnapshot
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
@ -167,12 +152,11 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
|
||||
assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted"))
|
||||
assert.Equal(t, 1, respJSON.Get("id").MustInt())
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
})
|
||||
|
||||
t.Run("When deleting an external snapshot", func(t *testing.T) {
|
||||
aclMockResp = []*models.DashboardAclInfoDTO{}
|
||||
mock := mockstore.NewSQLStoreMock()
|
||||
loggedInUserScenarioWithRole(t,
|
||||
"Should gracefully delete local snapshot when remote snapshot has already been removed when calling DELETE on",
|
||||
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
@ -186,7 +170,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
})
|
||||
|
||||
mockSnapshotResult.ExternalDeleteUrl = ts.URL
|
||||
sc.handlerFunc = DeleteDashboardSnapshot
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
require.NoError(t, writeErr)
|
||||
@ -196,7 +180,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
|
||||
assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted"))
|
||||
assert.Equal(t, 1, respJSON.Get("id").MustInt())
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
|
||||
loggedInUserScenarioWithRole(t,
|
||||
"Should fail to delete local snapshot when an unexpected 500 error occurs when calling DELETE on", "DELETE",
|
||||
@ -212,12 +196,12 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
|
||||
t.Log("Setting external delete URL", "url", ts.URL)
|
||||
mockSnapshotResult.ExternalDeleteUrl = ts.URL
|
||||
sc.handlerFunc = DeleteDashboardSnapshot
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
require.NoError(t, writeErr)
|
||||
assert.Equal(t, 500, sc.resp.Code)
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
|
||||
loggedInUserScenarioWithRole(t,
|
||||
"Should fail to delete local snapshot when an unexpected remote error occurs when calling DELETE on",
|
||||
@ -230,17 +214,17 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
})
|
||||
|
||||
mockSnapshotResult.ExternalDeleteUrl = ts.URL
|
||||
sc.handlerFunc = DeleteDashboardSnapshot
|
||||
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
assert.Equal(t, 500, sc.resp.Code)
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
|
||||
loggedInUserScenarioWithRole(t, "Should be able to read a snapshot's unencrypted data when calling GET on",
|
||||
"GET", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
setUpSnapshotTest(t)
|
||||
|
||||
sc.handlerFunc = GetDashboardSnapshot
|
||||
sc.handlerFunc = hs.GetDashboardSnapshot
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec()
|
||||
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
@ -251,6 +235,6 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
id := dashboard.Get("id")
|
||||
|
||||
assert.Equal(t, int64(100), id.MustInt64())
|
||||
}, mock)
|
||||
}, sqlmock)
|
||||
})
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ type SQLStoreMock struct {
|
||||
ExpectedDashboardAclInfoList []*models.DashboardAclInfoDTO
|
||||
ExpectedUserOrgList []*models.UserOrgDTO
|
||||
ExpectedOrgListResponse OrgListResponse
|
||||
ExpectedDashboardSnapshot *models.DashboardSnapshot
|
||||
ExpectedTeamsByUser []*models.TeamDTO
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
@ -49,6 +51,7 @@ func (m *SQLStoreMock) DeleteDashboardSnapshot(ctx context.Context, cmd *models.
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error {
|
||||
query.Result = m.ExpectedDashboardSnapshot
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
@ -213,7 +216,7 @@ func (m *SQLStoreMock) GetTeamById(ctx context.Context, query *models.GetTeamByI
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) GetTeamsByUser(ctx context.Context, query *models.GetTeamsByUserQuery) error {
|
||||
query.Result = []*models.TeamDTO{}
|
||||
query.Result = m.ExpectedTeamsByUser
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user