Chore: Remove Result field from dashboard snapshot mode (#62089)

Chore: Remove Result field from dashboard snapshot mode;
This commit is contained in:
idafurjes
2023-01-25 15:09:44 +01:00
committed by GitHub
parent 13de1afcbe
commit 529e6c379f
12 changed files with 220 additions and 182 deletions
@@ -45,8 +45,9 @@ func (d *DashboardSnapshotStore) DeleteExpiredSnapshots(ctx context.Context, cmd
})
}
func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.CreateDashboardSnapshotCommand) error {
return d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.CreateDashboardSnapshotCommand) (*dashboardsnapshots.DashboardSnapshot, error) {
var result *dashboardsnapshots.DashboardSnapshot
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
var expires = time.Now().Add(time.Hour * 24 * 365 * 50)
if cmd.Expires > 0 {
expires = time.Now().Add(time.Second * time.Duration(cmd.Expires))
@@ -56,11 +57,11 @@ func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cm
Name: cmd.Name,
Key: cmd.Key,
DeleteKey: cmd.DeleteKey,
OrgId: cmd.OrgId,
UserId: cmd.UserId,
OrgID: cmd.OrgID,
UserID: cmd.UserID,
External: cmd.External,
ExternalUrl: cmd.ExternalUrl,
ExternalDeleteUrl: cmd.ExternalDeleteUrl,
ExternalURL: cmd.ExternalURL,
ExternalDeleteURL: cmd.ExternalDeleteURL,
Dashboard: simplejson.New(),
DashboardEncrypted: cmd.DashboardEncrypted,
Expires: expires,
@@ -68,10 +69,14 @@ func (d *DashboardSnapshotStore) CreateDashboardSnapshot(ctx context.Context, cm
Updated: time.Now(),
}
_, err := sess.Insert(snapshot)
cmd.Result = snapshot
result = snapshot
return err
})
if err != nil {
return nil, err
}
return result, nil
}
func (d *DashboardSnapshotStore) DeleteDashboardSnapshot(ctx context.Context, cmd *dashboardsnapshots.DeleteDashboardSnapshotCommand) error {
@@ -82,8 +87,9 @@ func (d *DashboardSnapshotStore) DeleteDashboardSnapshot(ctx context.Context, cm
})
}
func (d *DashboardSnapshotStore) GetDashboardSnapshot(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotQuery) error {
return d.store.WithDbSession(ctx, func(sess *db.Session) error {
func (d *DashboardSnapshotStore) GetDashboardSnapshot(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotQuery) (*dashboardsnapshots.DashboardSnapshot, error) {
var queryResult *dashboardsnapshots.DashboardSnapshot
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
snapshot := dashboardsnapshots.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey}
has, err := sess.Get(&snapshot)
@@ -93,15 +99,20 @@ func (d *DashboardSnapshotStore) GetDashboardSnapshot(ctx context.Context, query
return dashboardsnapshots.ErrBaseNotFound.Errorf("dashboard snapshot not found")
}
query.Result = &snapshot
queryResult = &snapshot
return nil
})
if err != nil {
return nil, err
}
return queryResult, nil
}
// 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 *dashboardsnapshots.GetDashboardSnapshotsQuery) error {
return d.store.WithDbSession(ctx, func(sess *db.Session) error {
func (d *DashboardSnapshotStore) SearchDashboardSnapshots(ctx context.Context, query *dashboardsnapshots.GetDashboardSnapshotsQuery) (dashboardsnapshots.DashboardSnapshotsList, error) {
var queryResult dashboardsnapshots.DashboardSnapshotsList
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
var snapshots = make(dashboardsnapshots.DashboardSnapshotsList, 0)
if query.Limit > 0 {
sess.Limit(query.Limit)
@@ -115,16 +126,20 @@ func (d *DashboardSnapshotStore) SearchDashboardSnapshots(ctx context.Context, q
// admins can see all snapshots, everyone else can only see their own snapshots
switch {
case query.SignedInUser.OrgRole == org.RoleAdmin:
sess.Where("org_id = ?", query.OrgId)
sess.Where("org_id = ?", query.OrgID)
case !query.SignedInUser.IsAnonymous:
sess.Where("org_id = ? AND user_id = ?", query.OrgId, query.SignedInUser.UserID)
sess.Where("org_id = ? AND user_id = ?", query.OrgID, query.SignedInUser.UserID)
default:
query.Result = snapshots
queryResult = snapshots
return nil
}
err := sess.Find(&snapshots)
query.Result = snapshots
queryResult = snapshots
return err
})
if err != nil {
return dashboardsnapshots.DashboardSnapshotsList{}, err
}
return queryResult, nil
}
@@ -43,23 +43,23 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: "hej",
DashboardEncrypted: encryptedDashboard,
UserId: 1000,
OrgId: 1,
UserID: 1000,
OrgID: 1,
}
err = dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
result, err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotQuery{Key: "hej"}
err := dashStore.GetDashboardSnapshot(context.Background(), &query)
queryResult, err := dashStore.GetDashboardSnapshot(context.Background(), &query)
require.NoError(t, err)
assert.NotNil(t, query.Result)
assert.NotNil(t, queryResult)
decryptedDashboard, err := secretsService.Decrypt(
context.Background(),
query.Result.DashboardEncrypted,
queryResult.DashboardEncrypted,
)
require.NoError(t, err)
@@ -71,43 +71,43 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
t.Run("And the user has the admin role", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
assert.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
assert.NotNil(t, queryResult)
assert.Len(t, queryResult, 1)
})
})
t.Run("And the user has the editor role and has created a snapshot", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, UserID: 1000},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
require.NotNil(t, queryResult)
assert.Len(t, queryResult, 1)
})
})
t.Run("And the user has the editor role and has not created any snapshot", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, UserID: 2},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
require.NotNil(t, queryResult)
assert.Empty(t, queryResult)
})
})
@@ -118,29 +118,29 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 0,
OrgId: 1,
UserID: 0,
OrgID: 1,
}
err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
_, err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, IsAnonymous: true, UserID: 0},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
require.NotNil(t, queryResult)
assert.Empty(t, queryResult)
})
})
t.Run("Should have encrypted dashboard data", func(t *testing.T) {
decryptedDashboard, err := secretsService.Decrypt(
context.Background(),
cmd.Result.DashboardEncrypted,
result.DashboardEncrypted,
)
require.NoError(t, err)
@@ -167,27 +167,27 @@ func TestIntegrationDeleteExpiredSnapshots(t *testing.T) {
require.NoError(t, err)
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
assert.Len(t, query.Result, 1)
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
assert.Len(t, queryResult, 1)
assert.Equal(t, nonExpiredSnapshot.Key, queryResult[0].Key)
err = dashStore.DeleteExpiredSnapshots(context.Background(), &dashboardsnapshots.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
query = dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
require.Len(t, query.Result, 1)
require.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
require.Len(t, queryResult, 1)
require.Equal(t, nonExpiredSnapshot.Key, queryResult[0].Key)
})
}
@@ -198,22 +198,22 @@ func createTestSnapshot(t *testing.T, dashStore *DashboardSnapshotStore, key str
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 1000,
OrgId: 1,
UserID: 1000,
OrgID: 1,
Expires: expires,
}
err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
result, err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
// Set expiry date manually - to be able to create expired snapshots
if expires < 0 {
expireDate := time.Now().Add(time.Second * time.Duration(expires))
err = dashStore.store.WithDbSession(context.Background(), func(sess *db.Session) error {
_, err := sess.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, cmd.Result.Id)
_, err := sess.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, result.ID)
return err
})
require.NoError(t, err)
}
return cmd.Result
return result
}