Use org service instead of sqlstore (#56407)

* Use org service instead of sqlstore

* Remove methods from sqlstore

* Remove commented out code

* Fix lint

* Fix lint 2
This commit is contained in:
idafurjes
2022-10-13 08:40:46 -04:00
committed by GitHub
parent b0cb02568a
commit ef651daed2
20 changed files with 164 additions and 267 deletions
@@ -24,14 +24,10 @@ type Manager interface {
GetAlertNotificationsWithUidToSend(ctx context.Context, query *models.GetAlertNotificationsWithUidToSendQuery) error
UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error
}
type SQLStore interface {
GetOrgById(c context.Context, cmd *models.GetOrgByIdQuery) error
GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error
}
// Provision alert notifiers
func Provision(ctx context.Context, configDirectory string, alertingService Manager, orgService org.Service, sqlstore SQLStore, encryptionService encryption.Internal, notificationService *notifications.NotificationService) error {
dc := newNotificationProvisioner(orgService, sqlstore, alertingService, encryptionService, notificationService, log.New("provisioning.notifiers"))
func Provision(ctx context.Context, configDirectory string, alertingService Manager, orgService org.Service, encryptionService encryption.Internal, notificationService *notifications.NotificationService) error {
dc := newNotificationProvisioner(orgService, alertingService, encryptionService, notificationService, log.New("provisioning.notifiers"))
return dc.applyChanges(ctx, configDirectory)
}
@@ -40,11 +36,10 @@ type NotificationProvisioner struct {
log log.Logger
cfgProvider *configReader
alertingManager Manager
sqlstore SQLStore
orgService org.Service
}
func newNotificationProvisioner(orgService org.Service, store SQLStore, alertingManager Manager, encryptionService encryption.Internal, notifiationService *notifications.NotificationService, log log.Logger) NotificationProvisioner {
func newNotificationProvisioner(orgService org.Service, alertingManager Manager, encryptionService encryption.Internal, notifiationService *notifications.NotificationService, log log.Logger) NotificationProvisioner {
return NotificationProvisioner{
log: log,
alertingManager: alertingManager,
@@ -52,9 +47,8 @@ func newNotificationProvisioner(orgService org.Service, store SQLStore, alerting
encryptionService: encryptionService,
notificationService: notifiationService,
log: log,
orgStore: store,
orgService: orgService,
},
sqlstore: store,
orgService: orgService,
}
}
@@ -13,6 +13,7 @@ import (
"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/org"
"github.com/grafana/grafana/pkg/services/provisioning/utils"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/yaml.v2"
@@ -21,7 +22,7 @@ import (
type configReader struct {
encryptionService encryption.Internal
notificationService *notifications.NotificationService
orgStore utils.OrgStore
orgService org.Service
log log.Logger
}
@@ -94,7 +95,7 @@ func (cr *configReader) checkOrgIDAndOrgName(ctx context.Context, notifications
notification.OrgID = 0
}
} else {
if err := utils.CheckOrgExists(ctx, cr.orgStore, notification.OrgID); err != nil {
if err := utils.CheckOrgExists(ctx, cr.orgService, notification.OrgID); err != nil {
return fmt.Errorf("failed to provision %q notification: %w", notification.Name, err)
}
}
@@ -34,6 +34,7 @@ var (
func TestNotificationAsConfig(t *testing.T) {
var sqlStore *sqlstore.SQLStore
var orgFake org.Service
var ns *alerting.AlertNotificationService
logger := log.New("fake.log")
orgService := orgtest.NewOrgServiceFake()
@@ -44,6 +45,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Testing notification as configuration", func(t *testing.T) {
setup := func() {
sqlStore = sqlstore.InitTestDB(t)
orgFake = orgtest.NewOrgServiceFake()
nm := &notifications.NotificationService{}
ns = alerting.ProvideService(sqlStore, encryptionService, nm)
@@ -70,7 +72,7 @@ func TestNotificationAsConfig(t *testing.T) {
setup()
_ = os.Setenv("TEST_VAR", "default")
cfgProvider := &configReader{
orgStore: sqlStore,
orgService: orgFake,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -150,7 +152,7 @@ func TestNotificationAsConfig(t *testing.T) {
setup()
fakeAlertNotification := &fakeAlertNotification{}
fakeAlertNotification.ExpectedAlertNotification = &models.AlertNotification{OrgId: 1}
dc := newNotificationProvisioner(orgService, sqlStore, fakeAlertNotification, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, fakeAlertNotification, encryptionService, nil, logger)
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
if err != nil {
@@ -176,7 +178,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(orgService, sqlStore, &fakeAlertNotification{}, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, &fakeAlertNotification{}, encryptionService, nil, logger)
err = dc.applyChanges(context.Background(), twoNotificationsConfig)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -186,7 +188,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Two notifications with is_default", func(t *testing.T) {
setup()
dc := newNotificationProvisioner(orgService, sqlStore, &fakeAlertNotification{}, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, &fakeAlertNotification{}, encryptionService, nil, logger)
err := dc.applyChanges(context.Background(), doubleNotificationsConfig)
t.Run("should both be inserted", func(t *testing.T) {
require.NoError(t, err)
@@ -221,7 +223,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(orgService, sqlStore, &fakeAlertNotification{}, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, &fakeAlertNotification{}, encryptionService, nil, logger)
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -233,25 +235,16 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Can read correct properties with orgName instead of orgId", func(t *testing.T) {
setup()
existingOrg1 := models.GetOrgByNameQuery{Name: "Main Org. 1"}
err := sqlStore.GetOrgByNameHandler(context.Background(), &existingOrg1)
require.NoError(t, err)
require.NotNil(t, existingOrg1.Result)
existingOrg2 := models.GetOrgByNameQuery{Name: "Main Org. 2"}
err = sqlStore.GetOrgByNameHandler(context.Background(), &existingOrg2)
require.NoError(t, err)
require.NotNil(t, existingOrg2.Result)
existingNotificationCmd := models.CreateAlertNotificationCommand{
Name: "default-notification-delete",
OrgId: existingOrg2.Result.Id,
OrgId: 1,
Uid: "notifier2",
Type: "slack",
}
err = ns.SQLStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
err := ns.SQLStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
require.NoError(t, err)
dc := newNotificationProvisioner(orgService, sqlStore, &fakeAlertNotification{}, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, &fakeAlertNotification{}, encryptionService, nil, logger)
err = dc.applyChanges(context.Background(), correctPropertiesWithOrgName)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -260,7 +253,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Config doesn't contain required field", func(t *testing.T) {
setup()
dc := newNotificationProvisioner(orgService, sqlStore, &fakeAlertNotification{}, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, &fakeAlertNotification{}, encryptionService, nil, logger)
err := dc.applyChanges(context.Background(), noRequiredFields)
require.NotNil(t, err)
@@ -274,7 +267,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(orgService, sqlStore, &fakeAlertNotification{}, encryptionService, nil, logger)
dc := newNotificationProvisioner(orgService, &fakeAlertNotification{}, encryptionService, nil, logger)
err := dc.applyChanges(context.Background(), emptyFile)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
@@ -288,7 +281,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Broken yaml should return error", func(t *testing.T) {
reader := &configReader{
orgStore: sqlStore,
orgService: orgFake,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -299,7 +292,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Skip invalid directory", func(t *testing.T) {
cfgProvider := &configReader{
orgStore: sqlStore,
orgService: orgFake,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -313,7 +306,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Unknown notifier should return error", func(t *testing.T) {
cfgProvider := &configReader{
orgStore: sqlStore,
orgService: orgFake,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -324,7 +317,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Read incorrect properties", func(t *testing.T) {
cfgProvider := &configReader{
orgStore: sqlStore,
orgService: orgFake,
encryptionService: encryptionService,
log: log.New("test logger"),
}