Chore: Add context to org (#40685)
* Add context to org * Rebase * Fix rebase
This commit is contained in:
@@ -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"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Provision alert notifiers
|
||||
func Provision(configDirectory string, encryptionService encryption.Service) error {
|
||||
func Provision(ctx context.Context, configDirectory string, encryptionService encryption.Service) error {
|
||||
dc := newNotificationProvisioner(encryptionService, log.New("provisioning.notifiers"))
|
||||
return dc.applyChanges(configDirectory)
|
||||
return dc.applyChanges(ctx, configDirectory)
|
||||
}
|
||||
|
||||
// NotificationProvisioner is responsible for provsioning alert notifiers
|
||||
@@ -132,8 +133,8 @@ func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*not
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dc *NotificationProvisioner) applyChanges(configPath string) error {
|
||||
configs, err := dc.cfgProvider.readConfig(configPath)
|
||||
func (dc *NotificationProvisioner) applyChanges(ctx context.Context, configPath string) error {
|
||||
configs, err := dc.cfgProvider.readConfig(ctx, configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ type configReader struct {
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func (cr *configReader) readConfig(path string) ([]*notificationsAsConfig, error) {
|
||||
func (cr *configReader) readConfig(ctx context.Context, path string) ([]*notificationsAsConfig, error) {
|
||||
var notifications []*notificationsAsConfig
|
||||
cr.log.Debug("Looking for alert notification provisioning files", "path", path)
|
||||
|
||||
@@ -51,7 +51,7 @@ func (cr *configReader) readConfig(path string) ([]*notificationsAsConfig, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := cr.checkOrgIDAndOrgName(notifications); err != nil {
|
||||
if err := cr.checkOrgIDAndOrgName(ctx, notifications); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func (cr *configReader) parseNotificationConfig(path string, file os.FileInfo) (
|
||||
return cfg.mapToNotificationFromConfig(), nil
|
||||
}
|
||||
|
||||
func (cr *configReader) checkOrgIDAndOrgName(notifications []*notificationsAsConfig) error {
|
||||
func (cr *configReader) checkOrgIDAndOrgName(ctx context.Context, notifications []*notificationsAsConfig) error {
|
||||
for i := range notifications {
|
||||
for _, notification := range notifications[i].Notifications {
|
||||
if notification.OrgID < 1 {
|
||||
@@ -91,7 +91,7 @@ func (cr *configReader) checkOrgIDAndOrgName(notifications []*notificationsAsCon
|
||||
notification.OrgID = 0
|
||||
}
|
||||
} else {
|
||||
if err := utils.CheckOrgExists(notification.OrgID); err != nil {
|
||||
if err := utils.CheckOrgExists(ctx, notification.OrgID); err != nil {
|
||||
return fmt.Errorf("failed to provision %q notification: %w", notification.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package notifiers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -40,7 +41,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
|
||||
for i := 1; i < 5; i++ {
|
||||
orgCommand := models.CreateOrgCommand{Name: fmt.Sprintf("Main Org. %v", i)}
|
||||
err := sqlstore.CreateOrg(&orgCommand)
|
||||
err := sqlstore.CreateOrg(context.Background(), &orgCommand)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -65,7 +66,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
log: log.New("test logger"),
|
||||
}
|
||||
|
||||
cfg, err := cfgProvider.readConfig(correctProperties)
|
||||
cfg, err := cfgProvider.readConfig(context.Background(), correctProperties)
|
||||
_ = os.Unsetenv("TEST_VAR")
|
||||
if err != nil {
|
||||
t.Fatalf("readConfig return an error %v", err)
|
||||
@@ -140,7 +141,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
setup()
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
|
||||
err := dc.applyChanges(twoNotificationsConfig)
|
||||
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -170,7 +171,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
|
||||
t.Run("should update one notification", func(t *testing.T) {
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
err = dc.applyChanges(twoNotificationsConfig)
|
||||
err = dc.applyChanges(context.Background(), twoNotificationsConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -194,7 +195,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
t.Run("Two notifications with is_default", func(t *testing.T) {
|
||||
setup()
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
err := dc.applyChanges(doubleNotificationsConfig)
|
||||
err := dc.applyChanges(context.Background(), doubleNotificationsConfig)
|
||||
t.Run("should both be inserted", func(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgId: 1}
|
||||
@@ -237,7 +238,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
|
||||
t.Run("should have two new notifications", func(t *testing.T) {
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
err := dc.applyChanges(twoNotificationsConfig)
|
||||
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -253,11 +254,11 @@ 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.GetOrgByName(&existingOrg1)
|
||||
err := sqlstore.GetOrgByName(context.Background(), &existingOrg1)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, existingOrg1.Result)
|
||||
existingOrg2 := models.GetOrgByNameQuery{Name: "Main Org. 2"}
|
||||
err = sqlstore.GetOrgByName(&existingOrg2)
|
||||
err = sqlstore.GetOrgByName(context.Background(), &existingOrg2)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, existingOrg2.Result)
|
||||
|
||||
@@ -271,7 +272,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
err = dc.applyChanges(correctPropertiesWithOrgName)
|
||||
err = dc.applyChanges(context.Background(), correctPropertiesWithOrgName)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -290,7 +291,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
t.Run("Config doesn't contain required field", func(t *testing.T) {
|
||||
setup()
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
err := dc.applyChanges(noRequiredFields)
|
||||
err := dc.applyChanges(context.Background(), noRequiredFields)
|
||||
require.NotNil(t, err)
|
||||
|
||||
errString := err.Error()
|
||||
@@ -304,7 +305,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
t.Run("should have not changed repo", func(t *testing.T) {
|
||||
setup()
|
||||
dc := newNotificationProvisioner(ossencryption.ProvideService(), logger)
|
||||
err := dc.applyChanges(emptyFile)
|
||||
err := dc.applyChanges(context.Background(), emptyFile)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -321,7 +322,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
log: log.New("test logger"),
|
||||
}
|
||||
|
||||
_, err := reader.readConfig(brokenYaml)
|
||||
_, err := reader.readConfig(context.Background(), brokenYaml)
|
||||
require.NotNil(t, err)
|
||||
})
|
||||
|
||||
@@ -331,7 +332,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
log: log.New("test logger"),
|
||||
}
|
||||
|
||||
cfg, err := cfgProvider.readConfig(emptyFolder)
|
||||
cfg, err := cfgProvider.readConfig(context.Background(), emptyFolder)
|
||||
if err != nil {
|
||||
t.Fatalf("readConfig return an error %v", err)
|
||||
}
|
||||
@@ -343,7 +344,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
encryptionService: ossencryption.ProvideService(),
|
||||
log: log.New("test logger"),
|
||||
}
|
||||
_, err := cfgProvider.readConfig(unknownNotifier)
|
||||
_, err := cfgProvider.readConfig(context.Background(), unknownNotifier)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Error(), `unsupported notification type "nonexisting"`)
|
||||
})
|
||||
@@ -353,7 +354,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
encryptionService: ossencryption.ProvideService(),
|
||||
log: log.New("test logger"),
|
||||
}
|
||||
_, err := cfgProvider.readConfig(incorrectSettings)
|
||||
_, err := cfgProvider.readConfig(context.Background(), incorrectSettings)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Error(), "alert validation error: token must be specified when using the Slack chat API")
|
||||
})
|
||||
@@ -362,7 +363,7 @@ func TestNotificationAsConfig(t *testing.T) {
|
||||
|
||||
func setupBusHandlers(sqlStore *sqlstore.SQLStore) {
|
||||
bus.AddHandler("getOrg", func(q *models.GetOrgByNameQuery) error {
|
||||
return sqlstore.GetOrgByName(q)
|
||||
return sqlstore.GetOrgByName(context.Background(), q)
|
||||
})
|
||||
|
||||
bus.AddHandler("getAlertNotifications", func(q *models.GetAlertNotificationsWithUidQuery) error {
|
||||
|
||||
Reference in New Issue
Block a user