Chore: Remove Dispatch and AddHandler (#42603)

* Remove Dispatch

* Remove context.TODO()

* Remove AddHandler and Dispatch
This commit is contained in:
idafurjes
2021-12-02 18:08:59 +01:00
committed by GitHub
parent e79fe8e84e
commit c80e7764d8
28 changed files with 83 additions and 129 deletions
@@ -204,7 +204,7 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo
// fetch key
keyQuery := models.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
if err := bus.Dispatch(&keyQuery); err != nil {
if err := bus.DispatchCtx(reqContext.Req.Context(), &keyQuery); err != nil {
reqContext.JsonApiErr(401, InvalidAPIKey, err)
return true
}
@@ -246,7 +246,7 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo
//Use service account linked to API key as the signed in user
query := models.GetSignedInUserQuery{UserId: apikey.ServiceAccountId, OrgId: apikey.OrgId}
if err := bus.Dispatch(&query); err != nil {
if err := bus.DispatchCtx(reqContext.Req.Context(), &query); err != nil {
reqContext.Logger.Error(
"Failed to link API key to service account in",
"id", query.UserId,
+3 -3
View File
@@ -33,7 +33,7 @@ type DashboardProvisioningService interface {
SaveFolderForProvisionedDashboards(context.Context, *SaveDashboardDTO) (*models.Dashboard, error)
GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error)
GetProvisionedDashboardDataByDashboardID(dashboardID int64) (*models.DashboardProvisioning, error)
UnprovisionDashboard(dashboardID int64) error
UnprovisionDashboard(ctx context.Context, dashboardID int64) error
DeleteProvisionedDashboard(ctx context.Context, dashboardID int64, orgID int64) error
}
@@ -346,9 +346,9 @@ func (dr *dashboardServiceImpl) ImportDashboard(ctx context.Context, dto *SaveDa
// UnprovisionDashboard removes info about dashboard being provisioned. Used after provisioning configs are changed
// and provisioned dashboards are left behind but not deleted.
func (dr *dashboardServiceImpl) UnprovisionDashboard(dashboardId int64) error {
func (dr *dashboardServiceImpl) UnprovisionDashboard(ctx context.Context, dashboardId int64) error {
cmd := &models.UnprovisionDashboardCommand{Id: dashboardId}
return bus.Dispatch(cmd)
return bus.DispatchCtx(ctx, cmd)
}
type FakeDashboardService struct {
+1 -1
View File
@@ -209,7 +209,7 @@ func (dr *dashboardServiceImpl) DeleteFolder(ctx context.Context, uid string, fo
}
deleteCmd := models.DeleteDashboardCommand{OrgId: dr.orgId, Id: dashFolder.Id, ForceDeleteFolderRules: forceDeleteRules}
if err := bus.Dispatch(&deleteCmd); err != nil {
if err := bus.DispatchCtx(ctx, &deleteCmd); err != nil {
return nil, toFolderError(err)
}
+3 -3
View File
@@ -134,7 +134,7 @@ func (g *dashboardGuardianImpl) checkAcl(permission models.PermissionType, acl [
}
// load teams
teams, err := g.getTeams()
teams, err := g.getTeams(g.ctx)
if err != nil {
return false, err
}
@@ -248,14 +248,14 @@ func (g *dashboardGuardianImpl) GetACLWithoutDuplicates() ([]*models.DashboardAc
return result, nil
}
func (g *dashboardGuardianImpl) getTeams() ([]*models.TeamDTO, error) {
func (g *dashboardGuardianImpl) getTeams(ctx context.Context) ([]*models.TeamDTO, error) {
if g.teams != nil {
return g.teams, nil
}
query := models.GetTeamsByUserQuery{OrgId: g.orgId, UserId: g.user.UserId}
// TODO: Use bus.DispatchCtx(g.Ctx, &query) when GetTeamsByUserQuery supports context.
err := bus.Dispatch(&query)
err := bus.DispatchCtx(ctx, &query)
g.teams = query.Result
return query.Result, err
@@ -200,7 +200,7 @@ func (fr *FileReader) handleMissingDashboardFiles(ctx context.Context, provision
// so afterwards the dashboard is considered unprovisioned.
for _, dashboardID := range dashboardsToDelete {
fr.log.Debug("unprovisioning provisioned dashboard. missing on disk", "id", dashboardID)
err := fr.dashboardProvisioningService.UnprovisionDashboard(dashboardID)
err := fr.dashboardProvisioningService.UnprovisionDashboard(ctx, dashboardID)
if err != nil {
fr.log.Error("failed to unprovision dashboard", "dashboard_id", dashboardID, "error", err)
}
@@ -608,7 +608,7 @@ func (s *fakeDashboardProvisioningService) SaveFolderForProvisionedDashboards(ct
return dto.Dashboard, nil
}
func (s *fakeDashboardProvisioningService) UnprovisionDashboard(dashboardID int64) error {
func (s *fakeDashboardProvisioningService) UnprovisionDashboard(ctx context.Context, dashboardID int64) error {
for key, val := range s.provisioned {
for index, dashboard := range val {
if dashboard.DashboardId == dashboardID {
@@ -620,7 +620,7 @@ func (s *fakeDashboardProvisioningService) UnprovisionDashboard(dashboardID int6
}
func (s *fakeDashboardProvisioningService) DeleteProvisionedDashboard(ctx context.Context, dashboardID int64, orgID int64) error {
err := s.UnprovisionDashboard(dashboardID)
err := s.UnprovisionDashboard(ctx, dashboardID)
if err != nil {
return err
}
@@ -35,7 +35,7 @@ func (dc *NotificationProvisioner) apply(ctx context.Context, cfg *notifications
return err
}
if err := dc.mergeNotifications(cfg.Notifications); err != nil {
if err := dc.mergeNotifications(ctx, cfg.Notifications); err != nil {
return err
}
@@ -48,7 +48,7 @@ func (dc *NotificationProvisioner) deleteNotifications(ctx context.Context, noti
if notification.OrgID == 0 && notification.OrgName != "" {
getOrg := &models.GetOrgByNameQuery{Name: notification.OrgName}
if err := bus.Dispatch(getOrg); err != nil {
if err := bus.DispatchCtx(ctx, getOrg); err != nil {
return err
}
notification.OrgID = getOrg.Result.Id
@@ -73,11 +73,11 @@ func (dc *NotificationProvisioner) deleteNotifications(ctx context.Context, noti
return nil
}
func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*notificationFromConfig) error {
func (dc *NotificationProvisioner) mergeNotifications(ctx context.Context, notificationToMerge []*notificationFromConfig) error {
for _, notification := range notificationToMerge {
if notification.OrgID == 0 && notification.OrgName != "" {
getOrg := &models.GetOrgByNameQuery{Name: notification.OrgName}
if err := bus.Dispatch(getOrg); err != nil {
if err := bus.DispatchCtx(ctx, getOrg); err != nil {
return err
}
notification.OrgID = getOrg.Result.Id
@@ -86,7 +86,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not
}
cmd := &models.GetAlertNotificationsWithUidQuery{OrgId: notification.OrgID, Uid: notification.UID}
err := bus.Dispatch(cmd)
err := bus.DispatchCtx(ctx, cmd)
if err != nil {
return err
}
@@ -106,7 +106,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not
SendReminder: notification.SendReminder,
}
if err := bus.Dispatch(insertCmd); err != nil {
if err := bus.DispatchCtx(ctx, insertCmd); err != nil {
return err
}
} else {
@@ -124,7 +124,7 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not
SendReminder: notification.SendReminder,
}
if err := bus.Dispatch(updateCmd); err != nil {
if err := bus.DispatchCtx(ctx, updateCmd); err != nil {
return err
}
}
@@ -1,6 +1,7 @@
package plugins
import (
"context"
"errors"
"github.com/grafana/grafana/pkg/bus"
@@ -11,13 +12,13 @@ import (
// Provision scans a directory for provisioning config files
// and provisions the app in those files.
func Provision(configDirectory string, pluginStore plugins.Store) error {
func Provision(ctx context.Context, configDirectory string, pluginStore plugins.Store) error {
logger := log.New("provisioning.plugins")
ap := PluginProvisioner{
log: logger,
cfgProvider: newConfigReader(logger, pluginStore),
}
return ap.applyChanges(configDirectory)
return ap.applyChanges(ctx, configDirectory)
}
// PluginProvisioner is responsible for provisioning apps based on
@@ -27,11 +28,11 @@ type PluginProvisioner struct {
cfgProvider configReader
}
func (ap *PluginProvisioner) apply(cfg *pluginsAsConfig) error {
func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) error {
for _, app := range cfg.Apps {
if app.OrgID == 0 && app.OrgName != "" {
getOrgQuery := &models.GetOrgByNameQuery{Name: app.OrgName}
if err := bus.Dispatch(getOrgQuery); err != nil {
if err := bus.DispatchCtx(ctx, getOrgQuery); err != nil {
return err
}
app.OrgID = getOrgQuery.Result.Id
@@ -40,7 +41,7 @@ func (ap *PluginProvisioner) apply(cfg *pluginsAsConfig) error {
}
query := &models.GetPluginSettingByIdQuery{OrgId: app.OrgID, PluginId: app.PluginID}
err := bus.Dispatch(query)
err := bus.DispatchCtx(ctx, query)
if err != nil {
if !errors.Is(err, models.ErrPluginSettingNotFound) {
return err
@@ -59,7 +60,7 @@ func (ap *PluginProvisioner) apply(cfg *pluginsAsConfig) error {
SecureJsonData: app.SecureJSONData,
PluginVersion: app.PluginVersion,
}
if err := bus.Dispatch(cmd); err != nil {
if err := bus.DispatchCtx(ctx, cmd); err != nil {
return err
}
}
@@ -67,14 +68,14 @@ func (ap *PluginProvisioner) apply(cfg *pluginsAsConfig) error {
return nil
}
func (ap *PluginProvisioner) applyChanges(configPath string) error {
func (ap *PluginProvisioner) applyChanges(ctx context.Context, configPath string) error {
configs, err := ap.cfgProvider.readConfig(configPath)
if err != nil {
return err
}
for _, cfg := range configs {
if err := ap.apply(cfg); err != nil {
if err := ap.apply(ctx, cfg); err != nil {
return err
}
}
@@ -16,7 +16,7 @@ func TestPluginProvisioner(t *testing.T) {
expectedErr := errors.New("test")
reader := &testConfigReader{err: expectedErr}
ap := PluginProvisioner{log: log.New("test"), cfgProvider: reader}
err := ap.applyChanges("")
err := ap.applyChanges(context.Background(), "")
require.Equal(t, expectedErr, err)
})
@@ -59,7 +59,7 @@ func TestPluginProvisioner(t *testing.T) {
}
reader := &testConfigReader{result: cfg}
ap := PluginProvisioner{log: log.New("test"), cfgProvider: reader}
err := ap.applyChanges("")
err := ap.applyChanges(context.Background(), "")
require.NoError(t, err)
require.Len(t, sentCommands, 4)
+6 -6
View File
@@ -38,7 +38,7 @@ type ProvisioningService interface {
registry.BackgroundService
RunInitProvisioners(ctx context.Context) error
ProvisionDatasources(ctx context.Context) error
ProvisionPlugins() error
ProvisionPlugins(ctx context.Context) error
ProvisionNotifications(ctx context.Context) error
ProvisionDashboards(ctx context.Context) error
GetDashboardProvisionerResolvedPath(name string) string
@@ -61,7 +61,7 @@ func newProvisioningServiceImpl(
newDashboardProvisioner dashboards.DashboardProvisionerFactory,
provisionNotifiers func(context.Context, string, encryption.Internal) error,
provisionDatasources func(context.Context, string) error,
provisionPlugins func(string, plugifaces.Store) error,
provisionPlugins func(context.Context, string, plugifaces.Store) error,
) *ProvisioningServiceImpl {
return &ProvisioningServiceImpl{
log: log.New("provisioning"),
@@ -83,7 +83,7 @@ type ProvisioningServiceImpl struct {
dashboardProvisioner dashboards.DashboardProvisioner
provisionNotifiers func(context.Context, string, encryption.Internal) error
provisionDatasources func(context.Context, string) error
provisionPlugins func(string, plugifaces.Store) error
provisionPlugins func(context.Context, string, plugifaces.Store) error
mutex sync.Mutex
}
@@ -93,7 +93,7 @@ func (ps *ProvisioningServiceImpl) RunInitProvisioners(ctx context.Context) erro
return err
}
err = ps.ProvisionPlugins()
err = ps.ProvisionPlugins(ctx)
if err != nil {
return err
}
@@ -141,9 +141,9 @@ func (ps *ProvisioningServiceImpl) ProvisionDatasources(ctx context.Context) err
return errutil.Wrap("Datasource provisioning error", err)
}
func (ps *ProvisioningServiceImpl) ProvisionPlugins() error {
func (ps *ProvisioningServiceImpl) ProvisionPlugins(ctx context.Context) error {
appPath := filepath.Join(ps.Cfg.ProvisioningPath, "plugins")
err := ps.provisionPlugins(appPath, ps.pluginStore)
err := ps.provisionPlugins(ctx, appPath, ps.pluginStore)
return errutil.Wrap("app provisioning error", err)
}
@@ -47,7 +47,7 @@ func (mock *ProvisioningServiceMock) ProvisionDatasources(ctx context.Context) e
return nil
}
func (mock *ProvisioningServiceMock) ProvisionPlugins() error {
func (mock *ProvisioningServiceMock) ProvisionPlugins(ctx context.Context) error {
mock.Calls.ProvisionPlugins = append(mock.Calls.ProvisionPlugins, nil)
if mock.ProvisionPluginsFunc != nil {
return mock.ProvisionPluginsFunc()
+4 -2
View File
@@ -1,11 +1,13 @@
package teamguardian
import (
"context"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
)
func CanAdmin(bus bus.Bus, orgId int64, teamId int64, user *models.SignedInUser) error {
func CanAdmin(ctx context.Context, bus bus.Bus, orgId int64, teamId int64, user *models.SignedInUser) error {
if user.OrgRole == models.ROLE_ADMIN {
return nil
}
@@ -20,7 +22,7 @@ func CanAdmin(bus bus.Bus, orgId int64, teamId int64, user *models.SignedInUser)
UserId: user.UserId,
}
if err := bus.Dispatch(&cmd); err != nil {
if err := bus.DispatchCtx(ctx, &cmd); err != nil {
return err
}
+4 -4
View File
@@ -36,7 +36,7 @@ func TestUpdateTeam(t *testing.T) {
return nil
})
err := CanAdmin(bus.GetBus(), testTeam.OrgId, testTeam.Id, &editor)
err := CanAdmin(context.Background(), bus.GetBus(), testTeam.OrgId, testTeam.Id, &editor)
require.Equal(t, models.ErrNotAllowedToUpdateTeam, err)
})
})
@@ -53,7 +53,7 @@ func TestUpdateTeam(t *testing.T) {
return nil
})
err := CanAdmin(bus.GetBus(), testTeam.OrgId, testTeam.Id, &editor)
err := CanAdmin(context.Background(), bus.GetBus(), testTeam.OrgId, testTeam.Id, &editor)
require.NoError(t, err)
})
})
@@ -75,14 +75,14 @@ func TestUpdateTeam(t *testing.T) {
return nil
})
err := CanAdmin(bus.GetBus(), testTeamOtherOrg.OrgId, testTeamOtherOrg.Id, &editor)
err := CanAdmin(context.Background(), bus.GetBus(), testTeamOtherOrg.OrgId, testTeamOtherOrg.Id, &editor)
require.Equal(t, models.ErrNotAllowedToUpdateTeamInDifferentOrg, err)
})
})
t.Run("Given an org admin and a team", func(t *testing.T) {
t.Run("Should be able to update the team", func(t *testing.T) {
err := CanAdmin(bus.GetBus(), testTeam.OrgId, testTeam.Id, &admin)
err := CanAdmin(context.Background(), bus.GetBus(), testTeam.OrgId, testTeam.Id, &admin)
require.NoError(t, err)
})
})