backend/dashboardsnapshot service: move models (#50898)

* backend/dashboard snapshots: refactor leftover models and mocks
* Move all dashboard snapshot-related models into the dashboardsnapshotservice package
* Remove leftover dashboard-related mocks from the mockstore
This commit is contained in:
Kristin Laemmert
2022-06-17 09:09:01 -04:00
committed by GitHub
parent c6ab87008a
commit 72f934de01
13 changed files with 196 additions and 133 deletions
@@ -28,7 +28,7 @@ func ProvideStore(db db.DB) *DashboardSnapshotStore {
// DeleteExpiredSnapshots removes snapshots with old expiry dates.
// SnapShotRemoveExpired is deprecated and should be removed in the future.
// Snapshot expiry is decided by the user when they share the snapshot.
func (d *DashboardSnapshotStore) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
func (d *DashboardSnapshotStore) DeleteExpiredSnapshots(ctx context.Context, cmd *dashboardsnapshots.DeleteExpiredSnapshotsCommand) error {
return d.store.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
if !setting.SnapShotRemoveExpired {
d.log.Warn("[Deprecated] The snapshot_remove_expired setting is outdated. Please remove from your config.")
@@ -46,15 +46,14 @@ func (d *DashboardSnapshotStore) DeleteExpiredSnapshots(ctx context.Context, cmd
})
}
func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error {
func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.CreateDashboardSnapshotCommand) error {
return d.store.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
// never
var expires = time.Now().Add(time.Hour * 24 * 365 * 50)
if cmd.Expires > 0 {
expires = time.Now().Add(time.Second * time.Duration(cmd.Expires))
}
snapshot := &models.DashboardSnapshot{
snapshot := &dashboardsnapshots.DashboardSnapshot{
Name: cmd.Name,
Key: cmd.Key,
DeleteKey: cmd.DeleteKey,
@@ -76,7 +75,7 @@ func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cm
})
}
func (d *DashboardSnapshotStore) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
func (d *DashboardSnapshotStore) DeleteDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.DeleteDashboardSnapshotCommand) error {
return d.store.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
var rawSQL = "DELETE FROM dashboard_snapshot WHERE delete_key=?"
_, err := sess.Exec(rawSQL, cmd.DeleteKey)
@@ -84,10 +83,10 @@ func (d *DashboardSnapshotStore) DeleteDashboardSnapshot(ctx context.Context, cm
})
}
func (d *DashboardSnapshotStore) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
return d.store.WithDbSession(ctx, func(dbSess *sqlstore.DBSession) error {
snapshot := models.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey}
has, err := dbSess.Get(&snapshot)
func (d *DashboardSnapshotStore) GetDashboardSnapshot(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotQuery) error {
return d.store.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
snapshot := dashboardsnapshots.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey}
has, err := sess.Get(&snapshot)
if err != nil {
return err
@@ -102,9 +101,9 @@ func (d *DashboardSnapshotStore) GetDashboardSnapshot(ctx context.Context, query
// SearchDashboardSnapshots returns a list of all snapshots for admins
// for other roles, it returns snapshots created by the user
func (d *DashboardSnapshotStore) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error {
func (d *DashboardSnapshotStore) SearchDashboardSnapshots(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotsQuery) error {
return d.store.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
var snapshots = make(models.DashboardSnapshotsList, 0)
var snapshots = make(dashboardsnapshots.DashboardSnapshotsList, 0)
if query.Limit > 0 {
sess.Limit(query.Limit)
}
@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
"github.com/grafana/grafana/pkg/services/sqlstore"
@@ -38,7 +39,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
encryptedDashboard, err := secretsService.Encrypt(context.Background(), rawDashboard, secrets.WithoutScope())
require.NoError(t, err)
cmd := models.CreateDashboardSnapshotCommand{
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: "hej",
DashboardEncrypted: encryptedDashboard,
UserId: 1000,
@@ -49,7 +50,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
require.NoError(t, err)
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
query := models.GetDashboardSnapshotQuery{Key: "hej"}
query := dashboardsnapshots.GetDashboardSnapshotQuery{Key: "hej"}
err := dashStore.GetDashboardSnapshot(context.Background(), &query)
require.NoError(t, err)
@@ -68,7 +69,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
})
t.Run("And the user has the admin role", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
@@ -82,7 +83,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
})
t.Run("And the user has the editor role and has created a snapshot", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000},
}
@@ -96,7 +97,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
})
t.Run("And the user has the editor role and has not created any snapshot", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2},
}
@@ -110,7 +111,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
})
t.Run("And the user is anonymous", func(t *testing.T) {
cmd := models.CreateDashboardSnapshotCommand{
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: "strangesnapshotwithuserid0",
DeleteKey: "adeletekey",
Dashboard: simplejson.NewFromAny(map[string]interface{}{
@@ -123,7 +124,7 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
}
@@ -161,10 +162,10 @@ func TestIntegrationDeleteExpiredSnapshots(t *testing.T) {
createTestSnapshot(t, dashStore, "key2", -1200)
createTestSnapshot(t, dashStore, "key3", -1200)
err := dashStore.DeleteExpiredSnapshots(context.Background(), &models.DeleteExpiredSnapshotsCommand{})
err := dashStore.DeleteExpiredSnapshots(context.Background(), &dashboardsnapshots.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
query := models.GetDashboardSnapshotsQuery{
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
@@ -174,10 +175,10 @@ func TestIntegrationDeleteExpiredSnapshots(t *testing.T) {
assert.Len(t, query.Result, 1)
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
err = dashStore.DeleteExpiredSnapshots(context.Background(), &models.DeleteExpiredSnapshotsCommand{})
err = dashStore.DeleteExpiredSnapshots(context.Background(), &dashboardsnapshots.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
query = models.GetDashboardSnapshotsQuery{
query = dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
@@ -189,8 +190,8 @@ func TestIntegrationDeleteExpiredSnapshots(t *testing.T) {
})
}
func createTestSnapshot(t *testing.T, dashStore *DashboardSnapshotStore, key string, expires int64) *models.DashboardSnapshot {
cmd := models.CreateDashboardSnapshotCommand{
func createTestSnapshot(t *testing.T, dashStore *DashboardSnapshotStore, key string, expires int64) *dashboardsnapshots.DashboardSnapshot {
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: key,
DeleteKey: "delete" + key,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
+108
View File
@@ -0,0 +1,108 @@
package dashboardsnapshots
import (
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
)
// DashboardSnapshot model
type DashboardSnapshot struct {
Id int64
Name string
Key string
DeleteKey string
OrgId int64
UserId int64
External bool
ExternalUrl string
ExternalDeleteUrl string
Expires time.Time
Created time.Time
Updated time.Time
Dashboard *simplejson.Json
DashboardEncrypted []byte
}
// DashboardSnapshotDTO without dashboard map
type DashboardSnapshotDTO struct {
Id int64 `json:"id"`
Name string `json:"name"`
Key string `json:"key"`
OrgId int64 `json:"orgId"`
UserId int64 `json:"userId"`
External bool `json:"external"`
ExternalUrl string `json:"externalUrl"`
Expires time.Time `json:"expires"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
// -----------------
// COMMANDS
// swagger:model
type CreateDashboardSnapshotCommand struct {
// The complete dashboard model.
// required:true
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
// Snapshot name
// required:false
Name string `json:"name"`
// When the snapshot should expire in seconds in seconds. Default is never to expire.
// required:false
// default:0
Expires int64 `json:"expires"`
// these are passed when storing an external snapshot ref
// Save the snapshot on an external server rather than locally.
// required:false
// default: false
External bool `json:"external"`
ExternalUrl string `json:"-"`
ExternalDeleteUrl string `json:"-"`
// Define the unique key. Required if `external` is `true`.
// required:false
Key string `json:"key"`
// Unique key used to delete the snapshot. It is different from the `key` so that only the creator can delete the snapshot. Required if `external` is `true`.
// required:false
DeleteKey string `json:"deleteKey"`
OrgId int64 `json:"-"`
UserId int64 `json:"-"`
DashboardEncrypted []byte `json:"-"`
Result *DashboardSnapshot
}
type DeleteDashboardSnapshotCommand struct {
DeleteKey string `json:"-"`
}
type DeleteExpiredSnapshotsCommand struct {
DeletedRows int64
}
type GetDashboardSnapshotQuery struct {
Key string
DeleteKey string
Result *DashboardSnapshot
}
type DashboardSnapshotsList []*DashboardSnapshotDTO
type GetDashboardSnapshotsQuery struct {
Name string
Limit int
OrgId int64
SignedInUser *models.SignedInUser
Result DashboardSnapshotsList
}
+6 -7
View File
@@ -2,14 +2,13 @@ package dashboardsnapshots
import (
"context"
"github.com/grafana/grafana/pkg/models"
)
//go:generate mockery --name Service --structname MockService --inpackage --filename service_mock.go
type Service interface {
CreateDashboardSnapshot(context.Context, *models.CreateDashboardSnapshotCommand) error
DeleteDashboardSnapshot(context.Context, *models.DeleteDashboardSnapshotCommand) error
DeleteExpiredSnapshots(context.Context, *models.DeleteExpiredSnapshotsCommand) error
GetDashboardSnapshot(context.Context, *models.GetDashboardSnapshotQuery) error
SearchDashboardSnapshots(context.Context, *models.GetDashboardSnapshotsQuery) error
CreateDashboardSnapshot(context.Context, *CreateDashboardSnapshotCommand) error
DeleteDashboardSnapshot(context.Context, *DeleteDashboardSnapshotCommand) error
DeleteExpiredSnapshots(context.Context, *DeleteExpiredSnapshotsCommand) error
GetDashboardSnapshot(context.Context, *GetDashboardSnapshotQuery) error
SearchDashboardSnapshots(context.Context, *GetDashboardSnapshotsQuery) error
}
@@ -4,7 +4,6 @@ import (
"context"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
"github.com/grafana/grafana/pkg/services/secrets"
)
@@ -26,7 +25,7 @@ func ProvideService(store dashboardsnapshots.Store, secretsService secrets.Servi
return s
}
func (s *ServiceImpl) CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error {
func (s *ServiceImpl) CreateDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.CreateDashboardSnapshotCommand) error {
marshalledData, err := cmd.Dashboard.Encode()
if err != nil {
return err
@@ -42,7 +41,7 @@ func (s *ServiceImpl) CreateDashboardSnapshot(ctx context.Context, cmd *models.C
return s.store.CreateDashboardSnapshot(ctx, cmd)
}
func (s *ServiceImpl) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
func (s *ServiceImpl) GetDashboardSnapshot(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotQuery) error {
err := s.store.GetDashboardSnapshot(ctx, query)
if err != nil {
return err
@@ -65,14 +64,14 @@ func (s *ServiceImpl) GetDashboardSnapshot(ctx context.Context, query *models.Ge
return err
}
func (s *ServiceImpl) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
func (s *ServiceImpl) DeleteDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.DeleteDashboardSnapshotCommand) error {
return s.store.DeleteDashboardSnapshot(ctx, cmd)
}
func (s *ServiceImpl) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error {
func (s *ServiceImpl) SearchDashboardSnapshots(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotsQuery) error {
return s.store.SearchDashboardSnapshots(ctx, query)
}
func (s *ServiceImpl) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
func (s *ServiceImpl) DeleteExpiredSnapshots(ctx context.Context, cmd *dashboardsnapshots.DeleteExpiredSnapshotsCommand) error {
return s.store.DeleteExpiredSnapshots(ctx, cmd)
}
@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
dashsnapdb "github.com/grafana/grafana/pkg/services/dashboardsnapshots/database"
"github.com/grafana/grafana/pkg/services/secrets/database"
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
@@ -36,7 +36,7 @@ func TestDashboardSnapshotsService(t *testing.T) {
t.Run("create dashboard snapshot should encrypt the dashboard", func(t *testing.T) {
ctx := context.Background()
cmd := models.CreateDashboardSnapshotCommand{
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: dashboardKey,
DeleteKey: dashboardKey,
Dashboard: dashboard,
@@ -54,7 +54,7 @@ func TestDashboardSnapshotsService(t *testing.T) {
t.Run("get dashboard snapshot should return the dashboard decrypted", func(t *testing.T) {
ctx := context.Background()
query := models.GetDashboardSnapshotQuery{
query := dashboardsnapshots.GetDashboardSnapshotQuery{
Key: dashboardKey,
DeleteKey: dashboardKey,
}
@@ -0,0 +1,95 @@
// Code generated by mockery v2.12.2. DO NOT EDIT.
package dashboardsnapshots
import (
context "context"
testing "testing"
mock "github.com/stretchr/testify/mock"
)
// MockService is an autogenerated mock type for the Service type
type MockService struct {
mock.Mock
}
// CreateDashboardSnapshot provides a mock function with given fields: _a0, _a1
func (_m *MockService) CreateDashboardSnapshot(_a0 context.Context, _a1 *CreateDashboardSnapshotCommand) error {
ret := _m.Called(_a0, _a1)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *CreateDashboardSnapshotCommand) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
}
return r0
}
// DeleteDashboardSnapshot provides a mock function with given fields: _a0, _a1
func (_m *MockService) DeleteDashboardSnapshot(_a0 context.Context, _a1 *DeleteDashboardSnapshotCommand) error {
ret := _m.Called(_a0, _a1)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *DeleteDashboardSnapshotCommand) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
}
return r0
}
// DeleteExpiredSnapshots provides a mock function with given fields: _a0, _a1
func (_m *MockService) DeleteExpiredSnapshots(_a0 context.Context, _a1 *DeleteExpiredSnapshotsCommand) error {
ret := _m.Called(_a0, _a1)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *DeleteExpiredSnapshotsCommand) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
}
return r0
}
// GetDashboardSnapshot provides a mock function with given fields: _a0, _a1
func (_m *MockService) GetDashboardSnapshot(_a0 context.Context, _a1 *GetDashboardSnapshotQuery) error {
ret := _m.Called(_a0, _a1)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *GetDashboardSnapshotQuery) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
}
return r0
}
// SearchDashboardSnapshots provides a mock function with given fields: _a0, _a1
func (_m *MockService) SearchDashboardSnapshots(_a0 context.Context, _a1 *GetDashboardSnapshotsQuery) error {
ret := _m.Called(_a0, _a1)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *GetDashboardSnapshotsQuery) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
}
return r0
}
// NewMockService creates a new instance of MockService. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockService(t testing.TB) *MockService {
mock := &MockService{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
+5 -7
View File
@@ -2,14 +2,12 @@ package dashboardsnapshots
import (
"context"
"github.com/grafana/grafana/pkg/models"
)
type Store interface {
CreateDashboardSnapshot(context.Context, *models.CreateDashboardSnapshotCommand) error
DeleteDashboardSnapshot(context.Context, *models.DeleteDashboardSnapshotCommand) error
DeleteExpiredSnapshots(context.Context, *models.DeleteExpiredSnapshotsCommand) error
GetDashboardSnapshot(context.Context, *models.GetDashboardSnapshotQuery) error
SearchDashboardSnapshots(context.Context, *models.GetDashboardSnapshotsQuery) error
CreateDashboardSnapshot(context.Context, *CreateDashboardSnapshotCommand) error
DeleteDashboardSnapshot(context.Context, *DeleteDashboardSnapshotCommand) error
DeleteExpiredSnapshots(context.Context, *DeleteExpiredSnapshotsCommand) error
GetDashboardSnapshot(context.Context, *GetDashboardSnapshotQuery) error
SearchDashboardSnapshots(context.Context, *GetDashboardSnapshotsQuery) error
}