Chore: Add context for dashboards (#39844)
* Add context for dashboards * Remove GetDashboardCtx * Remove ctx.TODO
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -258,7 +259,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
err = SaveAlerts(&cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = DeleteDashboard(&models.DeleteDashboardCommand{
|
||||
err = DeleteDashboard(context.Background(), &models.DeleteDashboardCommand{
|
||||
OrgId: 1,
|
||||
Id: testDash.Id,
|
||||
})
|
||||
|
||||
@@ -26,18 +26,17 @@ var shadowSearchCounter = prometheus.NewCounterVec(
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDashboard)
|
||||
bus.AddHandler("sql", GetDashboards)
|
||||
bus.AddHandlerCtx("sql", GetDashboardCtx)
|
||||
bus.AddHandler("sql", DeleteDashboard)
|
||||
bus.AddHandler("sql", SearchDashboards)
|
||||
bus.AddHandler("sql", GetDashboardTags)
|
||||
bus.AddHandler("sql", GetDashboardSlugById)
|
||||
bus.AddHandler("sql", GetDashboardsByPluginId)
|
||||
bus.AddHandler("sql", GetDashboardPermissionsForUser)
|
||||
bus.AddHandler("sql", GetDashboardsBySlug)
|
||||
bus.AddHandler("sql", HasEditPermissionInFolders)
|
||||
bus.AddHandler("sql", HasAdminPermissionInFolders)
|
||||
bus.AddHandlerCtx("sql", GetDashboard)
|
||||
bus.AddHandlerCtx("sql", GetDashboards)
|
||||
bus.AddHandlerCtx("sql", DeleteDashboard)
|
||||
bus.AddHandlerCtx("sql", SearchDashboards)
|
||||
bus.AddHandlerCtx("sql", GetDashboardTags)
|
||||
bus.AddHandlerCtx("sql", GetDashboardSlugById)
|
||||
bus.AddHandlerCtx("sql", GetDashboardsByPluginId)
|
||||
bus.AddHandlerCtx("sql", GetDashboardPermissionsForUser)
|
||||
bus.AddHandlerCtx("sql", GetDashboardsBySlug)
|
||||
bus.AddHandlerCtx("sql", HasEditPermissionInFolders)
|
||||
bus.AddHandlerCtx("sql", HasAdminPermissionInFolders)
|
||||
|
||||
prometheus.MustRegister(shadowSearchCounter)
|
||||
}
|
||||
@@ -232,12 +231,7 @@ func (ss *SQLStore) GetFolderByTitle(orgID int64, title string) (*models.Dashboa
|
||||
return &dashboard, nil
|
||||
}
|
||||
|
||||
// TODO: Remove me
|
||||
func GetDashboard(query *models.GetDashboardQuery) error {
|
||||
return GetDashboardCtx(context.TODO(), query)
|
||||
}
|
||||
|
||||
func GetDashboardCtx(ctx context.Context, query *models.GetDashboardQuery) error {
|
||||
func GetDashboard(ctx context.Context, query *models.GetDashboardQuery) error {
|
||||
return withDbSession(ctx, x, func(dbSession *DBSession) error {
|
||||
if query.Id == 0 && len(query.Slug) == 0 && len(query.Uid) == 0 {
|
||||
return models.ErrDashboardIdentifierNotSet
|
||||
@@ -340,7 +334,7 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func SearchDashboards(query *search.FindPersistedDashboardsQuery) error {
|
||||
func SearchDashboards(ctx context.Context, query *search.FindPersistedDashboardsQuery) error {
|
||||
res, err := findDashboards(query)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -400,7 +394,7 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard
|
||||
}
|
||||
}
|
||||
|
||||
func GetDashboardTags(query *models.GetDashboardTagsQuery) error {
|
||||
func GetDashboardTags(ctx context.Context, query *models.GetDashboardTagsQuery) error {
|
||||
sql := `SELECT
|
||||
COUNT(*) as count,
|
||||
term
|
||||
@@ -416,7 +410,7 @@ func GetDashboardTags(query *models.GetDashboardTagsQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteDashboard(cmd *models.DeleteDashboardCommand) error {
|
||||
func DeleteDashboard(ctx context.Context, cmd *models.DeleteDashboardCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
return deleteDashboard(cmd, sess)
|
||||
})
|
||||
@@ -515,7 +509,7 @@ func deleteDashboard(cmd *models.DeleteDashboardCommand, sess *DBSession) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDashboards(query *models.GetDashboardsQuery) error {
|
||||
func GetDashboards(ctx context.Context, query *models.GetDashboardsQuery) error {
|
||||
if len(query.DashboardIds) == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
@@ -529,7 +523,7 @@ func GetDashboards(query *models.GetDashboardsQuery) error {
|
||||
|
||||
// GetDashboardPermissionsForUser returns the maximum permission the specified user has for a dashboard(s)
|
||||
// The function takes in a list of dashboard ids and the user id and role
|
||||
func GetDashboardPermissionsForUser(query *models.GetDashboardPermissionsForUserQuery) error {
|
||||
func GetDashboardPermissionsForUser(ctx context.Context, query *models.GetDashboardPermissionsForUserQuery) error {
|
||||
if len(query.DashboardIds) == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
@@ -597,7 +591,7 @@ func GetDashboardPermissionsForUser(query *models.GetDashboardPermissionsForUser
|
||||
return err
|
||||
}
|
||||
|
||||
func GetDashboardsByPluginId(query *models.GetDashboardsByPluginIdQuery) error {
|
||||
func GetDashboardsByPluginId(ctx context.Context, query *models.GetDashboardsByPluginIdQuery) error {
|
||||
var dashboards = make([]*models.Dashboard, 0)
|
||||
whereExpr := "org_id=? AND plugin_id=? AND is_folder=" + dialect.BooleanStr(false)
|
||||
|
||||
@@ -610,7 +604,7 @@ type DashboardSlugDTO struct {
|
||||
Slug string
|
||||
}
|
||||
|
||||
func GetDashboardSlugById(query *models.GetDashboardSlugByIdQuery) error {
|
||||
func GetDashboardSlugById(ctx context.Context, query *models.GetDashboardSlugByIdQuery) error {
|
||||
var rawSQL = `SELECT slug from dashboard WHERE Id=?`
|
||||
var slug = DashboardSlugDTO{}
|
||||
|
||||
@@ -626,7 +620,7 @@ func GetDashboardSlugById(query *models.GetDashboardSlugByIdQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDashboardsBySlug(query *models.GetDashboardsBySlugQuery) error {
|
||||
func GetDashboardsBySlug(ctx context.Context, query *models.GetDashboardsBySlugQuery) error {
|
||||
var dashboards []*models.Dashboard
|
||||
|
||||
if err := x.Where("org_id=? AND slug=?", query.OrgId, query.Slug).Find(&dashboards); err != nil {
|
||||
@@ -805,7 +799,7 @@ func (ss *SQLStore) ValidateDashboardBeforeSave(dashboard *models.Dashboard, ove
|
||||
return isParentFolderChanged, nil
|
||||
}
|
||||
|
||||
func HasEditPermissionInFolders(query *models.HasEditPermissionInFoldersQuery) error {
|
||||
func HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error {
|
||||
if query.SignedInUser.HasRole(models.ROLE_EDITOR) {
|
||||
query.Result = true
|
||||
return nil
|
||||
@@ -830,7 +824,7 @@ func HasEditPermissionInFolders(query *models.HasEditPermissionInFoldersQuery) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func HasAdminPermissionInFolders(query *models.HasAdminPermissionInFoldersQuery) error {
|
||||
func HasAdminPermissionInFolders(ctx context.Context, query *models.HasAdminPermissionInFoldersQuery) error {
|
||||
if query.SignedInUser.HasRole(models.ROLE_ADMIN) {
|
||||
query.Result = true
|
||||
return nil
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@@ -32,7 +33,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
So(query.Result[0].ID, ShouldEqual, folder.Id)
|
||||
@@ -55,7 +56,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER},
|
||||
OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -74,7 +75,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
So(query.Result[0].ID, ShouldEqual, folder.Id)
|
||||
@@ -93,7 +94,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
So(query.Result[0].ID, ShouldEqual, folder.Id)
|
||||
@@ -115,7 +116,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &search.FindPersistedDashboardsQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
So(query.Result[0].ID, ShouldEqual, dashInRoot.Id)
|
||||
@@ -129,7 +130,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
|
||||
Convey("should be able to search for child dashboard but not folder", func() {
|
||||
query := &search.FindPersistedDashboardsQuery{SignedInUser: &models.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
So(query.Result[0].ID, ShouldEqual, childDash.Id)
|
||||
@@ -148,7 +149,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder.Id, dashInRoot.Id, childDash.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 3)
|
||||
So(query.Result[0].ID, ShouldEqual, folder.Id)
|
||||
@@ -178,7 +179,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
},
|
||||
OrgId: 1,
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 4)
|
||||
So(query.Result[0].ID, ShouldEqual, folder1.Id)
|
||||
@@ -204,7 +205,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder1.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
So(query.Result[0].ID, ShouldEqual, dashInRoot.Id)
|
||||
@@ -219,7 +220,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 4)
|
||||
So(query.Result[0].ID, ShouldEqual, folder2.Id)
|
||||
@@ -243,7 +244,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
||||
}
|
||||
err := SearchDashboards(query)
|
||||
err := SearchDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 4)
|
||||
So(query.Result[0].ID, ShouldEqual, folder2.Id)
|
||||
@@ -273,7 +274,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
Type: "dash-folder",
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -289,7 +290,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgRole: models.ROLE_ADMIN,
|
||||
}
|
||||
|
||||
err := GetDashboardPermissionsForUser(&query)
|
||||
err := GetDashboardPermissionsForUser(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -303,7 +304,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasEditPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: adminUser.Id, OrgId: 1, OrgRole: models.ROLE_ADMIN},
|
||||
}
|
||||
err := HasEditPermissionInFolders(query)
|
||||
err := HasEditPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeTrue)
|
||||
})
|
||||
@@ -312,7 +313,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasAdminPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: adminUser.Id, OrgId: 1, OrgRole: models.ROLE_ADMIN},
|
||||
}
|
||||
err := HasAdminPermissionInFolders(query)
|
||||
err := HasAdminPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeTrue)
|
||||
})
|
||||
@@ -326,7 +327,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
Convey("Should have write access to all dashboard folders with default ACL", func() {
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -342,7 +343,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgRole: models.ROLE_EDITOR,
|
||||
}
|
||||
|
||||
err := GetDashboardPermissionsForUser(&query)
|
||||
err := GetDashboardPermissionsForUser(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -358,7 +359,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = SearchDashboards(&query)
|
||||
err = SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -369,7 +370,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasEditPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: editorUser.Id, OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
err := HasEditPermissionInFolders(query)
|
||||
err := HasEditPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeTrue)
|
||||
})
|
||||
@@ -378,7 +379,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasAdminPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: adminUser.Id, OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
err := HasAdminPermissionInFolders(query)
|
||||
err := HasAdminPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeFalse)
|
||||
})
|
||||
@@ -392,7 +393,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
Convey("Should have no write access to any dashboard folders with default ACL", func() {
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
@@ -406,7 +407,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
OrgRole: models.ROLE_VIEWER,
|
||||
}
|
||||
|
||||
err := GetDashboardPermissionsForUser(&query)
|
||||
err := GetDashboardPermissionsForUser(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -422,7 +423,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = SearchDashboards(&query)
|
||||
err = SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -433,7 +434,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasEditPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: viewerUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER},
|
||||
}
|
||||
err := HasEditPermissionInFolders(query)
|
||||
err := HasEditPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeFalse)
|
||||
})
|
||||
@@ -442,7 +443,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasAdminPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: adminUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER},
|
||||
}
|
||||
err := HasAdminPermissionInFolders(query)
|
||||
err := HasAdminPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeFalse)
|
||||
})
|
||||
@@ -457,7 +458,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasEditPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: viewerUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER},
|
||||
}
|
||||
err := HasEditPermissionInFolders(query)
|
||||
err := HasEditPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeTrue)
|
||||
})
|
||||
@@ -473,7 +474,7 @@ func TestDashboardFolderDataAccess(t *testing.T) {
|
||||
query := &models.HasEditPermissionInFoldersQuery{
|
||||
SignedInUser: &models.SignedInUser{UserId: viewerUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER},
|
||||
}
|
||||
err := HasEditPermissionInFolders(query)
|
||||
err := HasEditPermissionInFolders(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldBeTrue)
|
||||
})
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", UnprovisionDashboard)
|
||||
bus.AddHandler("sql", DeleteOrphanedProvisionedDashboards)
|
||||
bus.AddHandlerCtx("sql", UnprovisionDashboard)
|
||||
bus.AddHandlerCtx("sql", DeleteOrphanedProvisionedDashboards)
|
||||
}
|
||||
|
||||
type DashboardExtras struct {
|
||||
@@ -80,14 +80,14 @@ func (ss *SQLStore) GetProvisionedDashboardData(name string) ([]*models.Dashboar
|
||||
|
||||
// UnprovisionDashboard removes row in dashboard_provisioning for the dashboard making it seem as if manually created.
|
||||
// The dashboard will still have `created_by = -1` to see it was not created by any particular user.
|
||||
func UnprovisionDashboard(cmd *models.UnprovisionDashboardCommand) error {
|
||||
func UnprovisionDashboard(ctx context.Context, cmd *models.UnprovisionDashboardCommand) error {
|
||||
if _, err := x.Where("dashboard_id = ?", cmd.Id).Delete(&models.DashboardProvisioning{}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteOrphanedProvisionedDashboards(cmd *models.DeleteOrphanedProvisionedDashboardsCommand) error {
|
||||
func DeleteOrphanedProvisionedDashboards(ctx context.Context, cmd *models.DeleteOrphanedProvisionedDashboardsCommand) error {
|
||||
var result []*models.DashboardProvisioning
|
||||
|
||||
convertedReaderNames := make([]interface{}, len(cmd.ReaderNames))
|
||||
@@ -101,7 +101,7 @@ func DeleteOrphanedProvisionedDashboards(cmd *models.DeleteOrphanedProvisionedDa
|
||||
}
|
||||
|
||||
for _, deleteDashCommand := range result {
|
||||
err := DeleteDashboard(&models.DeleteDashboardCommand{Id: deleteDashCommand.DashboardId})
|
||||
err := DeleteDashboard(ctx, &models.DeleteDashboardCommand{Id: deleteDashCommand.DashboardId})
|
||||
if err != nil && !errors.Is(err, models.ErrDashboardNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -74,15 +75,15 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
query := &models.GetDashboardsQuery{DashboardIds: []int64{anotherDash.Id}}
|
||||
err = GetDashboards(query)
|
||||
err = GetDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldNotBeNil)
|
||||
|
||||
deleteCmd := &models.DeleteOrphanedProvisionedDashboardsCommand{ReaderNames: []string{"default"}}
|
||||
So(DeleteOrphanedProvisionedDashboards(deleteCmd), ShouldBeNil)
|
||||
So(DeleteOrphanedProvisionedDashboards(context.Background(), deleteCmd), ShouldBeNil)
|
||||
|
||||
query = &models.GetDashboardsQuery{DashboardIds: []int64{dash.Id, anotherDash.Id}}
|
||||
err = GetDashboards(query)
|
||||
err = GetDashboards(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -117,7 +118,7 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
So(DeleteDashboard(deleteCmd), ShouldBeNil)
|
||||
So(DeleteDashboard(context.Background(), deleteCmd), ShouldBeNil)
|
||||
|
||||
data, err := sqlStore.GetProvisionedDataByDashboardID(dash.Id)
|
||||
So(err, ShouldBeNil)
|
||||
@@ -129,7 +130,7 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
Id: dashId,
|
||||
}
|
||||
|
||||
So(UnprovisionDashboard(unprovisionCmd), ShouldBeNil)
|
||||
So(UnprovisionDashboard(context.Background(), unprovisionCmd), ShouldBeNil)
|
||||
|
||||
data, err := sqlStore.GetProvisionedDataByDashboardID(dashId)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
@@ -56,7 +56,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardCtx(context.Background(), &query)
|
||||
err := GetDashboard(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(query.Result.Title, ShouldEqual, "test dash 23")
|
||||
@@ -72,7 +72,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardCtx(context.Background(), &query)
|
||||
err := GetDashboard(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(query.Result.Title, ShouldEqual, "test dash 23")
|
||||
@@ -88,7 +88,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardCtx(context.Background(), &query)
|
||||
err := GetDashboard(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(query.Result.Title, ShouldEqual, "test dash 23")
|
||||
@@ -103,14 +103,14 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardCtx(context.Background(), &query)
|
||||
err := GetDashboard(context.Background(), &query)
|
||||
So(err, ShouldEqual, models.ErrDashboardIdentifierNotSet)
|
||||
})
|
||||
|
||||
Convey("Should be able to delete dashboard", func() {
|
||||
dash := insertTestDashboard(t, sqlStore, "delete me", 1, 0, false, "delete this")
|
||||
|
||||
err := DeleteDashboard(&models.DeleteDashboardCommand{
|
||||
err := DeleteDashboard(context.Background(), &models.DeleteDashboardCommand{
|
||||
Id: dash.Id,
|
||||
OrgId: 1,
|
||||
})
|
||||
@@ -191,7 +191,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err = GetDashboardCtx(context.Background(), &query)
|
||||
err = GetDashboard(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result.FolderId, ShouldEqual, 0)
|
||||
So(query.Result.CreatedBy, ShouldEqual, savedDash.CreatedBy)
|
||||
@@ -204,19 +204,19 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
emptyFolder := insertTestDashboard(t, sqlStore, "2 test dash folder", 1, 0, true, "prod", "webapp")
|
||||
|
||||
deleteCmd := &models.DeleteDashboardCommand{Id: emptyFolder.Id}
|
||||
err := DeleteDashboard(deleteCmd)
|
||||
err := DeleteDashboard(context.Background(), deleteCmd)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Should be not able to delete a dashboard if force delete rules is disabled", func() {
|
||||
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: false}
|
||||
err := DeleteDashboard(deleteCmd)
|
||||
err := DeleteDashboard(context.Background(), deleteCmd)
|
||||
So(errors.Is(err, models.ErrFolderContainsAlertRules), ShouldBeTrue)
|
||||
})
|
||||
|
||||
Convey("Should be able to delete a dashboard folder and its children if force delete rules is enabled", func() {
|
||||
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: true}
|
||||
err := DeleteDashboard(deleteCmd)
|
||||
err := DeleteDashboard(context.Background(), deleteCmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
query := search.FindPersistedDashboardsQuery{
|
||||
@@ -225,7 +225,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{},
|
||||
}
|
||||
|
||||
err = SearchDashboards(&query)
|
||||
err = SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
@@ -277,7 +277,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
Convey("Should be able to get dashboard tags", func() {
|
||||
query := models.GetDashboardTagsQuery{OrgId: 1}
|
||||
|
||||
err := GetDashboardTags(&query)
|
||||
err := GetDashboardTags(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -290,7 +290,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -307,7 +307,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -322,7 +322,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -337,7 +337,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 3)
|
||||
@@ -351,7 +351,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -371,7 +371,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -403,7 +403,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
SignedInUser: &models.SignedInUser{UserId: 10, OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
||||
IsStarred: true,
|
||||
}
|
||||
err := SearchDashboards(&query)
|
||||
err := SearchDashboards(context.Background(), &query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -425,7 +425,7 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardsByPluginId(&query)
|
||||
err := GetDashboardsByPluginId(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
|
||||
@@ -52,7 +52,7 @@ func TestGetDashboardVersion(t *testing.T) {
|
||||
Uid: savedDash.Uid,
|
||||
}
|
||||
|
||||
err = GetDashboardCtx(context.Background(), &dashCmd)
|
||||
err = GetDashboard(context.Background(), &dashCmd)
|
||||
So(err, ShouldBeNil)
|
||||
eq := reflect.DeepEqual(dashCmd.Result.Data, query.Result.Data)
|
||||
So(eq, ShouldEqual, true)
|
||||
|
||||
Reference in New Issue
Block a user