Chore: Remove bus from the alerting service (#44496)

* propagate notificationservice down to the notifiers

* replace dispatch in result handler

* remove dispatch from the rule reader

* remove dispatch from eval context

* remove dispatch from alerting usage

* remove dispatch from alerting usage

* remove dispatch from notifier

* attempt to fix tests in alerting

* hello linter, my old friend; also disable some tests for now

* use mocks to fix the tests

* resolving wire providers

* make linter happy

* remove yet another bus.dispatch

* fix tests using store mock
This commit is contained in:
Serge Zaitsev
2022-02-03 13:26:05 +01:00
committed by GitHub
parent a79c048344
commit 43b15b92ad
66 changed files with 557 additions and 443 deletions

View File

@@ -5,12 +5,13 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/encryption"
"github.com/grafana/grafana/pkg/services/notifications"
"golang.org/x/net/context"
)
// Provision alert notifiers
func Provision(ctx context.Context, configDirectory string, encryptionService encryption.Internal) error {
dc := newNotificationProvisioner(encryptionService, log.New("provisioning.notifiers"))
func Provision(ctx context.Context, configDirectory string, encryptionService encryption.Internal, notificationService *notifications.NotificationService) error {
dc := newNotificationProvisioner(encryptionService, notificationService, log.New("provisioning.notifiers"))
return dc.applyChanges(ctx, configDirectory)
}
@@ -20,12 +21,13 @@ type NotificationProvisioner struct {
cfgProvider *configReader
}
func newNotificationProvisioner(encryptionService encryption.Internal, log log.Logger) NotificationProvisioner {
func newNotificationProvisioner(encryptionService encryption.Internal, notifiationService *notifications.NotificationService, log log.Logger) NotificationProvisioner {
return NotificationProvisioner{
log: log,
cfgProvider: &configReader{
encryptionService: encryptionService,
log: log,
encryptionService: encryptionService,
notificationService: notifiationService,
log: log,
},
}
}

View File

@@ -12,14 +12,16 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/encryption"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/services/provisioning/utils"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/yaml.v2"
)
type configReader struct {
encryptionService encryption.Internal
log log.Logger
encryptionService encryption.Internal
notificationService *notifications.NotificationService
log log.Logger
}
func (cr *configReader) readConfig(ctx context.Context, path string) ([]*notificationsAsConfig, error) {
@@ -175,7 +177,7 @@ func (cr *configReader) validateNotifications(notifications []*notificationsAsCo
Settings: notification.SettingsToJSON(),
SecureSettings: encryptedSecureSettings,
Type: notification.Type,
}, cr.encryptionService.GetDecryptedValue)
}, cr.encryptionService.GetDecryptedValue, cr.notificationService)
if err != nil {
return err

View File

@@ -139,7 +139,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("One configured notification", func(t *testing.T) {
t.Run("no notification in database", func(t *testing.T) {
setup()
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
if err != nil {
@@ -170,7 +170,7 @@ func TestNotificationAsConfig(t *testing.T) {
require.Equal(t, len(notificationsQuery.Result), 1)
t.Run("should update one notification", func(t *testing.T) {
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err = dc.applyChanges(context.Background(), twoNotificationsConfig)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -194,7 +194,7 @@ func TestNotificationAsConfig(t *testing.T) {
})
t.Run("Two notifications with is_default", func(t *testing.T) {
setup()
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err := dc.applyChanges(context.Background(), doubleNotificationsConfig)
t.Run("should both be inserted", func(t *testing.T) {
require.NoError(t, err)
@@ -237,7 +237,7 @@ func TestNotificationAsConfig(t *testing.T) {
require.Equal(t, len(notificationsQuery.Result), 2)
t.Run("should have two new notifications", func(t *testing.T) {
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -271,7 +271,7 @@ func TestNotificationAsConfig(t *testing.T) {
err = sqlStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
require.NoError(t, err)
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err = dc.applyChanges(context.Background(), correctPropertiesWithOrgName)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -290,7 +290,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Config doesn't contain required field", func(t *testing.T) {
setup()
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err := dc.applyChanges(context.Background(), noRequiredFields)
require.NotNil(t, err)
@@ -304,7 +304,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Empty yaml file", func(t *testing.T) {
t.Run("should have not changed repo", func(t *testing.T) {
setup()
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
dc := newNotificationProvisioner(ossencryption.ProvideService(), nil, logger)
err := dc.applyChanges(context.Background(), emptyFile)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)

View File

@@ -9,6 +9,7 @@ import (
plugifaces "github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/encryption"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/services/provisioning/dashboards"
"github.com/grafana/grafana/pkg/services/provisioning/datasources"
"github.com/grafana/grafana/pkg/services/provisioning/notifiers"
@@ -19,12 +20,13 @@ import (
)
func ProvideService(cfg *setting.Cfg, sqlStore *sqlstore.SQLStore, pluginStore plugifaces.Store,
encryptionService encryption.Internal) (*ProvisioningServiceImpl, error) {
encryptionService encryption.Internal, notificatonService *notifications.NotificationService) (*ProvisioningServiceImpl, error) {
s := &ProvisioningServiceImpl{
Cfg: cfg,
SQLStore: sqlStore,
pluginStore: pluginStore,
EncryptionService: encryptionService,
NotificationService: notificatonService,
log: log.New("provisioning"),
newDashboardProvisioner: dashboards.New,
provisionNotifiers: notifiers.Provision,
@@ -59,7 +61,7 @@ func NewProvisioningServiceImpl() *ProvisioningServiceImpl {
// Used for testing purposes
func newProvisioningServiceImpl(
newDashboardProvisioner dashboards.DashboardProvisionerFactory,
provisionNotifiers func(context.Context, string, encryption.Internal) error,
provisionNotifiers func(context.Context, string, encryption.Internal, *notifications.NotificationService) error,
provisionDatasources func(context.Context, string) error,
provisionPlugins func(context.Context, string, plugifaces.Store) error,
) *ProvisioningServiceImpl {
@@ -77,11 +79,12 @@ type ProvisioningServiceImpl struct {
SQLStore *sqlstore.SQLStore
pluginStore plugifaces.Store
EncryptionService encryption.Internal
NotificationService *notifications.NotificationService
log log.Logger
pollingCtxCancel context.CancelFunc
newDashboardProvisioner dashboards.DashboardProvisionerFactory
dashboardProvisioner dashboards.DashboardProvisioner
provisionNotifiers func(context.Context, string, encryption.Internal) error
provisionNotifiers func(context.Context, string, encryption.Internal, *notifications.NotificationService) error
provisionDatasources func(context.Context, string) error
provisionPlugins func(context.Context, string, plugifaces.Store) error
mutex sync.Mutex
@@ -157,7 +160,7 @@ func (ps *ProvisioningServiceImpl) ProvisionPlugins(ctx context.Context) error {
func (ps *ProvisioningServiceImpl) ProvisionNotifications(ctx context.Context) error {
alertNotificationsPath := filepath.Join(ps.Cfg.ProvisioningPath, "notifiers")
if err := ps.provisionNotifiers(ctx, alertNotificationsPath, ps.EncryptionService); err != nil {
if err := ps.provisionNotifiers(ctx, alertNotificationsPath, ps.EncryptionService, ps.NotificationService); err != nil {
err = errutil.Wrap("Alert notification provisioning error", err)
ps.log.Error("Failed to provision alert notifications", "error", err)
return err