Chore: Add context for dashboards (#39844)

* Add context for dashboards

* Remove GetDashboardCtx

* Remove ctx.TODO
This commit is contained in:
idafurjes 2021-10-05 13:26:24 +02:00 committed by GitHub
parent 697a90699b
commit 2759b16ef5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 112 additions and 113 deletions

View File

@ -9,7 +9,7 @@ import (
)
func (hs *HTTPServer) AdminProvisioningReloadDashboards(c *models.ReqContext) response.Response {
err := hs.ProvisioningService.ProvisionDashboards()
err := hs.ProvisioningService.ProvisionDashboards(c.Req.Context())
if err != nil && !errors.Is(err, context.Canceled) {
return response.Error(500, "", err)
}

View File

@ -196,7 +196,7 @@ func OrgAdminFolderAdminOrTeamAdmin(c *models.ReqContext) {
}
hasAdminPermissionInFoldersQuery := models.HasAdminPermissionInFoldersQuery{SignedInUser: c.SignedInUser}
if err := sqlstore.HasAdminPermissionInFolders(&hasAdminPermissionInFoldersQuery); err != nil {
if err := sqlstore.HasAdminPermissionInFolders(c.Req.Context(), &hasAdminPermissionInFoldersQuery); err != nil {
c.JsonApiErr(500, "Failed to check if user is a folder admin", err)
}

View File

@ -1,6 +1,7 @@
package manager
import (
"context"
"testing"
"github.com/grafana/grafana/pkg/services/sqlstore"
@ -25,7 +26,7 @@ func TestGetPluginDashboards(t *testing.T) {
err := pm.init()
require.NoError(t, err)
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
if query.Slug == "nginx-connections" {
dash := models.NewDashboard("Nginx Connections")
dash.Data.Set("revision", "1.1")
@ -36,7 +37,7 @@ func TestGetPluginDashboards(t *testing.T) {
return models.ErrDashboardNotFound
})
bus.AddHandler("test", func(query *models.GetDashboardsByPluginIdQuery) error {
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardsByPluginIdQuery) error {
var data = simplejson.New()
data.Set("title", "Nginx Connections")
data.Set("revision", 22)

View File

@ -474,7 +474,7 @@ func (sch *schedule) ruleRoutine(grafanaCtx context.Context, key models.AlertRul
return err
}
processedStates := sch.stateManager.ProcessEvalResults(alertRule, results)
processedStates := sch.stateManager.ProcessEvalResults(context.Background(), alertRule, results)
sch.saveAlertStates(processedStates)
alerts := FromAlertStateToPostableAlerts(processedStates, sch.stateManager, sch.appURL)

View File

@ -138,12 +138,12 @@ func (st *Manager) RemoveByRuleUID(orgID int64, ruleUID string) {
st.cache.removeByRuleUID(orgID, ruleUID)
}
func (st *Manager) ProcessEvalResults(alertRule *ngModels.AlertRule, results eval.Results) []*State {
func (st *Manager) ProcessEvalResults(ctx context.Context, alertRule *ngModels.AlertRule, results eval.Results) []*State {
st.log.Debug("state manager processing evaluation results", "uid", alertRule.UID, "resultCount", len(results))
var states []*State
processedResults := make(map[string]*State, len(results))
for _, result := range results {
s := st.setNextState(alertRule, result)
s := st.setNextState(ctx, alertRule, result)
states = append(states, s)
processedResults[s.CacheId] = s
}
@ -152,7 +152,7 @@ func (st *Manager) ProcessEvalResults(alertRule *ngModels.AlertRule, results eva
}
//Set the current state based on evaluation results
func (st *Manager) setNextState(alertRule *ngModels.AlertRule, result eval.Result) *State {
func (st *Manager) setNextState(ctx context.Context, alertRule *ngModels.AlertRule, result eval.Result) *State {
currentState := st.getOrCreate(alertRule, result)
currentState.LastEvaluationTime = result.EvaluatedAt
@ -185,7 +185,7 @@ func (st *Manager) setNextState(alertRule *ngModels.AlertRule, result eval.Resul
st.set(currentState)
if oldState != currentState.State {
go st.createAlertAnnotation(currentState.State, alertRule, result, oldState)
go st.createAlertAnnotation(ctx, currentState.State, alertRule, result, oldState)
}
return currentState
}
@ -233,7 +233,7 @@ func translateInstanceState(state ngModels.InstanceStateType) eval.State {
}
}
func (st *Manager) createAlertAnnotation(new eval.State, alertRule *ngModels.AlertRule, result eval.Result, oldState eval.State) {
func (st *Manager) createAlertAnnotation(ctx context.Context, new eval.State, alertRule *ngModels.AlertRule, result eval.Result, oldState eval.State) {
st.log.Debug("alert state changed creating annotation", "alertRuleUID", alertRule.UID, "newState", new.String())
dashUid, ok := alertRule.Annotations["__dashboardUid__"]
if !ok {
@ -253,7 +253,7 @@ func (st *Manager) createAlertAnnotation(new eval.State, alertRule *ngModels.Ale
OrgId: alertRule.OrgID,
}
err = sqlstore.GetDashboardCtx(context.TODO(), query)
err = sqlstore.GetDashboard(ctx, query)
if err != nil {
st.log.Error("error getting dashboard for alert annotation", "dashboardUID", dashUid, "alertRuleUID", alertRule.UID, "error", err.Error())
return

View File

@ -1,6 +1,7 @@
package state_test
import (
"context"
"testing"
"time"
@ -854,7 +855,7 @@ func TestProcessEvalResults(t *testing.T) {
st := state.NewManager(log.New("test_state_manager"), testMetrics.GetStateMetrics(), nil, nil, nil)
t.Run(tc.desc, func(t *testing.T) {
for _, res := range tc.evalResults {
_ = st.ProcessEvalResults(tc.alertRule, res)
_ = st.ProcessEvalResults(context.Background(), tc.alertRule, res)
}
for _, s := range tc.expectedStates {
cachedState, err := st.Get(s.OrgID, s.AlertRuleUID, s.CacheId)
@ -954,7 +955,7 @@ func TestStaleResultsHandler(t *testing.T) {
// We have loaded the expected number of entries from the db
assert.Equal(t, tc.startingStateCount, len(existingStatesForRule))
for _, res := range tc.evalResults {
st.ProcessEvalResults(rule, res)
st.ProcessEvalResults(context.Background(), rule, res)
for _, s := range tc.expectedStates {
cachedState, err := st.Get(s.OrgID, s.AlertRuleUID, s.CacheId)
require.NoError(t, err)

View File

@ -19,7 +19,7 @@ type DashboardProvisioner interface {
PollChanges(ctx context.Context)
GetProvisionerResolvedPath(name string) string
GetAllowUIUpdatesFromConfig(name string) bool
CleanUpOrphanedDashboards()
CleanUpOrphanedDashboards(ctx context.Context)
}
// DashboardProvisionerFactory creates DashboardProvisioners based on input
@ -77,14 +77,14 @@ func (provider *Provisioner) Provision(ctx context.Context) error {
}
// CleanUpOrphanedDashboards deletes provisioned dashboards missing a linked reader.
func (provider *Provisioner) CleanUpOrphanedDashboards() {
func (provider *Provisioner) CleanUpOrphanedDashboards(ctx context.Context) {
currentReaders := make([]string, len(provider.fileReaders))
for index, reader := range provider.fileReaders {
currentReaders[index] = reader.Cfg.Name
}
if err := bus.Dispatch(&models.DeleteOrphanedProvisionedDashboardsCommand{ReaderNames: currentReaders}); err != nil {
if err := bus.DispatchCtx(ctx, &models.DeleteOrphanedProvisionedDashboardsCommand{ReaderNames: currentReaders}); err != nil {
provider.log.Warn("Failed to delete orphaned provisioned dashboards", "err", err)
}
}

View File

@ -62,4 +62,4 @@ func (dpm *ProvisionerMock) GetAllowUIUpdatesFromConfig(name string) bool {
}
// CleanUpOrphanedDashboards not implemented for mocks
func (dpm *ProvisionerMock) CleanUpOrphanedDashboards() {}
func (dpm *ProvisionerMock) CleanUpOrphanedDashboards(ctx context.Context) {}

View File

@ -38,7 +38,7 @@ type ProvisioningService interface {
ProvisionDatasources() error
ProvisionPlugins() error
ProvisionNotifications() error
ProvisionDashboards() error
ProvisionDashboards(ctx context.Context) error
GetDashboardProvisionerResolvedPath(name string) string
GetAllowUIUpdatesFromConfig(name string) bool
}
@ -104,7 +104,7 @@ func (ps *ProvisioningServiceImpl) RunInitProvisioners() error {
}
func (ps *ProvisioningServiceImpl) Run(ctx context.Context) error {
err := ps.ProvisionDashboards()
err := ps.ProvisionDashboards(ctx)
if err != nil {
ps.log.Error("Failed to provision dashboard", "error", err)
return err
@ -150,7 +150,7 @@ func (ps *ProvisioningServiceImpl) ProvisionNotifications() error {
return errutil.Wrap("Alert notification provisioning error", err)
}
func (ps *ProvisioningServiceImpl) ProvisionDashboards() error {
func (ps *ProvisioningServiceImpl) ProvisionDashboards(ctx context.Context) error {
dashboardPath := filepath.Join(ps.Cfg.ProvisioningPath, "dashboards")
dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath, ps.SQLStore)
if err != nil {
@ -161,9 +161,9 @@ func (ps *ProvisioningServiceImpl) ProvisionDashboards() error {
defer ps.mutex.Unlock()
ps.cancelPolling()
dashProvisioner.CleanUpOrphanedDashboards()
dashProvisioner.CleanUpOrphanedDashboards(ctx)
err = dashProvisioner.Provision(context.TODO())
err = dashProvisioner.Provision(ctx)
if err != nil {
// If we fail to provision with the new provisioner, the mutex will unlock and the polling will restart with the
// old provisioner as we did not switch them yet.

View File

@ -63,7 +63,7 @@ func (mock *ProvisioningServiceMock) ProvisionNotifications() error {
return nil
}
func (mock *ProvisioningServiceMock) ProvisionDashboards() error {
func (mock *ProvisioningServiceMock) ProvisionDashboards(ctx context.Context) error {
mock.Calls.ProvisionDashboards = append(mock.Calls.ProvisionDashboards, nil)
if mock.ProvisionDashboardsFunc != nil {
return mock.ProvisionDashboardsFunc()

View File

@ -15,14 +15,14 @@ import (
func TestProvisioningServiceImpl(t *testing.T) {
t.Run("Restart dashboard provisioning and stop service", func(t *testing.T) {
serviceTest := setup()
err := serviceTest.service.ProvisionDashboards()
err := serviceTest.service.ProvisionDashboards(context.Background())
assert.Nil(t, err)
serviceTest.startService()
serviceTest.waitForPollChanges()
assert.Equal(t, 1, len(serviceTest.mock.Calls.PollChanges), "PollChanges should have been called")
err = serviceTest.service.ProvisionDashboards()
err = serviceTest.service.ProvisionDashboards(context.Background())
assert.Nil(t, err)
serviceTest.waitForPollChanges()
@ -42,7 +42,7 @@ func TestProvisioningServiceImpl(t *testing.T) {
t.Run("Failed reloading does not stop polling with old provisioned", func(t *testing.T) {
serviceTest := setup()
err := serviceTest.service.ProvisionDashboards()
err := serviceTest.service.ProvisionDashboards(context.Background())
assert.Nil(t, err)
serviceTest.startService()
serviceTest.waitForPollChanges()
@ -51,7 +51,7 @@ func TestProvisioningServiceImpl(t *testing.T) {
serviceTest.mock.ProvisionFunc = func(ctx context.Context) error {
return errors.New("Test error")
}
err = serviceTest.service.ProvisionDashboards()
err = serviceTest.service.ProvisionDashboards(context.Background())
assert.NotNil(t, err)
serviceTest.waitForPollChanges()

View File

@ -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,
})

View File

@ -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

View File

@ -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)
})

View File

@ -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
}

View File

@ -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)

View File

@ -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)
})

View File

@ -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)