Chore: rename Id to ID in alert notification models (#62868)

pull/62882/head
idafurjes 2 years ago committed by GitHub
parent 2e98f5063b
commit 00d954320f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      pkg/api/alerting.go
  2. 8
      pkg/api/dtos/alerting.go
  3. 4
      pkg/services/alerting/extractor_test.go
  4. 68
      pkg/services/alerting/models/alert_notification.go
  5. 16
      pkg/services/alerting/notifier.go
  6. 10
      pkg/services/alerting/notifier_test.go
  7. 2
      pkg/services/alerting/notifiers/base.go
  8. 2
      pkg/services/alerting/notifiers/base_test.go
  9. 4
      pkg/services/alerting/rule.go
  10. 4
      pkg/services/alerting/rule_test.go
  11. 16
      pkg/services/alerting/service.go
  12. 22
      pkg/services/alerting/service_test.go
  13. 98
      pkg/services/alerting/store_notification.go
  14. 132
      pkg/services/alerting/store_notification_test.go
  15. 4
      pkg/services/alerting/test_notification.go
  16. 14
      pkg/services/provisioning/notifiers/alert_notifications.go
  17. 24
      pkg/services/provisioning/notifiers/config_reader_test.go
  18. 4
      pkg/services/sqlstore/migrations/ualert/migration_test.go

@ -302,7 +302,7 @@ func (hs *HTTPServer) GetAlertNotifications(c *contextmodel.ReqContext) response
}
func (hs *HTTPServer) getAlertNotificationsInternal(c *contextmodel.ReqContext) ([]*alertmodels.AlertNotification, error) {
query := &alertmodels.GetAllAlertNotificationsQuery{OrgId: c.OrgID}
query := &alertmodels.GetAllAlertNotificationsQuery{OrgID: c.OrgID}
return hs.AlertNotificationService.GetAllAlertNotifications(c.Req.Context(), query)
}
@ -324,11 +324,11 @@ func (hs *HTTPServer) GetAlertNotificationByID(c *contextmodel.ReqContext) respo
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
}
query := &alertmodels.GetAlertNotificationsQuery{
OrgId: c.OrgID,
Id: notificationId,
OrgID: c.OrgID,
ID: notificationId,
}
if query.Id == 0 {
if query.ID == 0 {
return response.Error(404, "Alert notification not found", nil)
}
@ -358,11 +358,11 @@ func (hs *HTTPServer) GetAlertNotificationByID(c *contextmodel.ReqContext) respo
// 500: internalServerError
func (hs *HTTPServer) GetAlertNotificationByUID(c *contextmodel.ReqContext) response.Response {
query := &alertmodels.GetAlertNotificationsWithUidQuery{
OrgId: c.OrgID,
Uid: web.Params(c.Req)[":uid"],
OrgID: c.OrgID,
UID: web.Params(c.Req)[":uid"],
}
if query.Uid == "" {
if query.UID == "" {
return response.Error(404, "Alert notification not found", nil)
}
@ -395,7 +395,7 @@ func (hs *HTTPServer) CreateAlertNotification(c *contextmodel.ReqContext) respon
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
cmd.OrgId = c.OrgID
cmd.OrgID = c.OrgID
res, err := hs.AlertNotificationService.CreateAlertNotificationCommand(c.Req.Context(), &cmd)
if err != nil {
@ -429,7 +429,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *contextmodel.ReqContext) respon
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
cmd.OrgId = c.OrgID
cmd.OrgID = c.OrgID
err := hs.fillWithSecureSettingsData(c.Req.Context(), &cmd)
if err != nil {
@ -448,8 +448,8 @@ func (hs *HTTPServer) UpdateAlertNotification(c *contextmodel.ReqContext) respon
}
query := alertmodels.GetAlertNotificationsQuery{
OrgId: c.OrgID,
Id: cmd.Id,
OrgID: c.OrgID,
ID: cmd.ID,
}
res, err := hs.AlertNotificationService.GetAlertNotifications(c.Req.Context(), &query)
@ -477,8 +477,8 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *contextmodel.ReqContext) r
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
cmd.OrgId = c.OrgID
cmd.Uid = web.Params(c.Req)[":uid"]
cmd.OrgID = c.OrgID
cmd.UID = web.Params(c.Req)[":uid"]
err := hs.fillWithSecureSettingsDataByUID(c.Req.Context(), &cmd)
if err != nil {
@ -493,8 +493,8 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *contextmodel.ReqContext) r
}
query := alertmodels.GetAlertNotificationsWithUidQuery{
OrgId: cmd.OrgId,
Uid: cmd.Uid,
OrgID: cmd.OrgID,
UID: cmd.UID,
}
res, err := hs.AlertNotificationService.GetAlertNotificationsWithUid(c.Req.Context(), &query)
@ -511,8 +511,8 @@ func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *alert
}
query := &alertmodels.GetAlertNotificationsQuery{
OrgId: cmd.OrgId,
Id: cmd.Id,
OrgID: cmd.OrgID,
ID: cmd.ID,
}
res, err := hs.AlertNotificationService.GetAlertNotifications(ctx, query)
@ -540,8 +540,8 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *
}
query := &alertmodels.GetAlertNotificationsWithUidQuery{
OrgId: cmd.OrgId,
Uid: cmd.Uid,
OrgID: cmd.OrgID,
UID: cmd.UID,
}
res, err := hs.AlertNotificationService.GetAlertNotificationsWithUid(ctx, query)
@ -582,8 +582,8 @@ func (hs *HTTPServer) DeleteAlertNotification(c *contextmodel.ReqContext) respon
}
cmd := alertmodels.DeleteAlertNotificationCommand{
OrgId: c.OrgID,
Id: notificationId,
OrgID: c.OrgID,
ID: notificationId,
}
if err := hs.AlertNotificationService.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil {
@ -610,8 +610,8 @@ func (hs *HTTPServer) DeleteAlertNotification(c *contextmodel.ReqContext) respon
// 500: internalServerError
func (hs *HTTPServer) DeleteAlertNotificationByUID(c *contextmodel.ReqContext) response.Response {
cmd := alertmodels.DeleteAlertNotificationWithUidCommand{
OrgId: c.OrgID,
Uid: web.Params(c.Req)[":uid"],
OrgID: c.OrgID,
UID: web.Params(c.Req)[":uid"],
}
if err := hs.AlertNotificationService.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
@ -623,7 +623,7 @@ func (hs *HTTPServer) DeleteAlertNotificationByUID(c *contextmodel.ReqContext) r
return response.JSON(http.StatusOK, util.DynMap{
"message": "Notification deleted",
"id": cmd.DeletedAlertNotificationId,
"id": cmd.DeletedAlertNotificationID,
})
}

@ -34,8 +34,8 @@ func formatShort(interval time.Duration) string {
func NewAlertNotification(notification *models.AlertNotification) *AlertNotification {
dto := &AlertNotification{
Id: notification.Id,
Uid: notification.Uid,
Id: notification.ID,
Uid: notification.UID,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
@ -74,8 +74,8 @@ type AlertNotification struct {
func NewAlertNotificationLookup(notification *models.AlertNotification) *AlertNotificationLookup {
return &AlertNotificationLookup{
Id: notification.Id,
Uid: notification.Uid,
Id: notification.ID,
Uid: notification.UID,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,

@ -206,11 +206,11 @@ func TestAlertRuleExtraction(t *testing.T) {
t.Run("Alert notifications are in DB", func(t *testing.T) {
sqlStore := sqlStore{db: sqlstore.InitTestDB(t)}
firstNotification := models.CreateAlertNotificationCommand{Uid: "notifier1", OrgId: 1, Name: "1"}
firstNotification := models.CreateAlertNotificationCommand{UID: "notifier1", OrgID: 1, Name: "1"}
_, err = sqlStore.CreateAlertNotificationCommand(context.Background(), &firstNotification)
require.Nil(t, err)
secondNotification := models.CreateAlertNotificationCommand{Uid: "notifier2", OrgId: 1, Name: "2"}
secondNotification := models.CreateAlertNotificationCommand{UID: "notifier2", OrgID: 1, Name: "2"}
_, err = sqlStore.CreateAlertNotificationCommand(context.Background(), &secondNotification)
require.Nil(t, err)

@ -26,9 +26,9 @@ var (
)
type AlertNotification struct {
Id int64 `json:"id"`
Uid string `json:"-"`
OrgId int64 `json:"-"`
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
UID string `json:"-" xorm:"uid"`
OrgID int64 `json:"-" xorm:"org_id"`
Name string `json:"name"`
Type string `json:"type"`
SendReminder bool `json:"sendReminder"`
@ -42,7 +42,7 @@ type AlertNotification struct {
}
type CreateAlertNotificationCommand struct {
Uid string `json:"uid"`
UID string `json:"uid"`
Name string `json:"name" binding:"Required"`
Type string `json:"type" binding:"Required"`
SendReminder bool `json:"sendReminder"`
@ -52,13 +52,13 @@ type CreateAlertNotificationCommand struct {
Settings *simplejson.Json `json:"settings"`
SecureSettings map[string]string `json:"secureSettings"`
OrgId int64 `json:"-"`
OrgID int64 `json:"-"`
EncryptedSecureSettings map[string][]byte `json:"-"`
}
type UpdateAlertNotificationCommand struct {
Id int64 `json:"id" binding:"Required"`
Uid string `json:"uid"`
ID int64 `json:"id" binding:"Required"`
UID string `json:"uid"`
Name string `json:"name" binding:"Required"`
Type string `json:"type" binding:"Required"`
SendReminder bool `json:"sendReminder"`
@ -68,13 +68,13 @@ type UpdateAlertNotificationCommand struct {
Settings *simplejson.Json `json:"settings" binding:"Required"`
SecureSettings map[string]string `json:"secureSettings"`
OrgId int64 `json:"-"`
OrgID int64 `json:"-"`
EncryptedSecureSettings map[string][]byte `json:"-"`
}
type UpdateAlertNotificationWithUidCommand struct {
Uid string `json:"-"`
NewUid string `json:"uid"`
UID string `json:"-"`
NewUID string `json:"uid"`
Name string `json:"name" binding:"Required"`
Type string `json:"type" binding:"Required"`
SendReminder bool `json:"sendReminder"`
@ -84,50 +84,50 @@ type UpdateAlertNotificationWithUidCommand struct {
Settings *simplejson.Json `json:"settings" binding:"Required"`
SecureSettings map[string]string `json:"secureSettings"`
OrgId int64 `json:"-"`
OrgID int64 `json:"-"`
}
type DeleteAlertNotificationCommand struct {
Id int64
OrgId int64
ID int64
OrgID int64
}
type DeleteAlertNotificationWithUidCommand struct {
Uid string
OrgId int64
UID string
OrgID int64
DeletedAlertNotificationId int64
DeletedAlertNotificationID int64
}
type GetAlertNotificationUidQuery struct {
Id int64
OrgId int64
ID int64
OrgID int64
}
type GetAlertNotificationsQuery struct {
Name string
Id int64
OrgId int64
ID int64
OrgID int64
}
type GetAlertNotificationsWithUidQuery struct {
Uid string
OrgId int64
UID string
OrgID int64
}
type GetAlertNotificationsWithUidToSendQuery struct {
Uids []string
OrgId int64
UIDs []string
OrgID int64
}
type GetAllAlertNotificationsQuery struct {
OrgId int64
OrgID int64
}
type AlertNotificationState struct {
Id int64
OrgId int64
AlertId int64
NotifierId int64
ID int64 `xorm:"pk autoincr 'id'"`
OrgID int64 `xorm:"org_id"`
AlertID int64 `xorm:"alert_id"`
NotifierID int64 `xorm:"notifier_id"`
State AlertNotificationStateType
Version int64
UpdatedAt int64
@ -135,7 +135,7 @@ type AlertNotificationState struct {
}
type SetAlertNotificationStateToPendingCommand struct {
Id int64
ID int64
AlertRuleStateUpdatedVersion int64
Version int64
@ -143,12 +143,12 @@ type SetAlertNotificationStateToPendingCommand struct {
}
type SetAlertNotificationStateToCompleteCommand struct {
Id int64
ID int64
Version int64
}
type GetOrCreateNotificationStateQuery struct {
OrgId int64
AlertId int64
NotifierId int64
OrgID int64
AlertID int64
NotifierID int64
}

@ -156,7 +156,7 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
}
cmd := &alertmodels.SetAlertNotificationStateToCompleteCommand{
Id: notifierState.state.Id,
ID: notifierState.state.ID,
Version: notifierState.state.Version,
}
@ -166,7 +166,7 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *notifierState) error {
if !evalContext.IsTestRun {
setPendingCmd := &alertmodels.SetAlertNotificationStateToPendingCommand{
Id: notifierState.state.Id,
ID: notifierState.state.ID,
Version: notifierState.state.Version,
AlertRuleStateUpdatedVersion: evalContext.Rule.StateChanges,
}
@ -257,7 +257,7 @@ func (n *notificationService) renderAndUploadImage(evalCtx *EvalContext, timeout
}
func (n *notificationService) getNeededNotifiers(orgID int64, notificationUids []string, evalContext *EvalContext) (notifierStateSlice, error) {
query := &alertmodels.GetAlertNotificationsWithUidToSendQuery{OrgId: orgID, Uids: notificationUids}
query := &alertmodels.GetAlertNotificationsWithUidToSendQuery{OrgID: orgID, UIDs: notificationUids}
res, err := n.sqlStore.GetAlertNotificationsWithUidToSend(evalContext.Ctx, query)
if err != nil {
@ -268,19 +268,19 @@ func (n *notificationService) getNeededNotifiers(orgID int64, notificationUids [
for _, notification := range res {
not, err := InitNotifier(notification, n.decryptFn, n.notificationService)
if err != nil {
n.log.Error("Could not create notifier", "notifier", notification.Uid, "error", err)
n.log.Error("Could not create notifier", "notifier", notification.UID, "error", err)
continue
}
query := &alertmodels.GetOrCreateNotificationStateQuery{
NotifierId: notification.Id,
AlertId: evalContext.Rule.ID,
OrgId: evalContext.Rule.OrgID,
NotifierID: notification.ID,
AlertID: evalContext.Rule.ID,
OrgID: evalContext.Rule.OrgID,
}
state, err := n.sqlStore.GetOrCreateAlertNotificationState(evalContext.Ctx, query)
if err != nil {
n.log.Error("Could not get notification state.", "notifier", notification.Id, "error", err)
n.log.Error("Could not get notification state.", "notifier", notification.ID, "error", err)
continue
}

@ -185,7 +185,7 @@ func notificationServiceScenario(t *testing.T, name string, evalCtx *EvalContext
store.getAlertNotificationsWithUidToSend = func(ctx context.Context, query *alertmodels.GetAlertNotificationsWithUidToSendQuery) (res []*alertmodels.AlertNotification, err error) {
return []*alertmodels.AlertNotification{
{
Id: 1,
ID: 1,
Type: "test",
Settings: simplejson.NewFromAny(map[string]interface{}{
"uploadImage": uploadImage,
@ -196,10 +196,10 @@ func notificationServiceScenario(t *testing.T, name string, evalCtx *EvalContext
store.getOrCreateNotificationState = func(ctx context.Context, query *alertmodels.GetOrCreateNotificationStateQuery) (res *alertmodels.AlertNotificationState, err error) {
return &alertmodels.AlertNotificationState{
AlertId: evalCtx.Rule.ID,
AlertID: evalCtx.Rule.ID,
AlertRuleStateUpdatedVersion: 1,
Id: 1,
OrgId: evalCtx.Rule.OrgID,
ID: 1,
OrgID: evalCtx.Rule.OrgID,
State: alertmodels.AlertNotificationStateUnknown,
}, nil
}
@ -282,7 +282,7 @@ func newTestNotifier(model *alertmodels.AlertNotification, _ GetDecryptedValueFn
}
return &testNotifier{
UID: model.Uid,
UID: model.UID,
Name: model.Name,
IsDefault: model.IsDefault,
Type: model.Type,

@ -38,7 +38,7 @@ func NewNotifierBase(model *models.AlertNotification, notificationService notifi
}
return NotifierBase{
UID: model.Uid,
UID: model.UID,
Name: model.Name,
IsDefault: model.IsDefault,
Type: model.Type,

@ -189,7 +189,7 @@ func TestBaseNotifier(t *testing.T) {
bJSON := simplejson.New()
model := &models.AlertNotification{
Uid: "1",
UID: "1",
Name: "name",
Type: "email",
Settings: bJSON,

@ -222,8 +222,8 @@ func translateNotificationIDToUID(ctx context.Context, store AlertStore, id int6
func getAlertNotificationUIDByIDAndOrgID(ctx context.Context, store AlertStore, notificationID int64, orgID int64) (string, error) {
query := &models.GetAlertNotificationUidQuery{
OrgId: orgID,
Id: notificationID,
OrgID: orgID,
ID: notificationID,
}
uid, err := store.GetAlertNotificationUidWithId(ctx, query)

@ -91,10 +91,10 @@ func TestAlertRuleModel(t *testing.T) {
return &FakeCondition{}, nil
})
firstNotification := models.CreateAlertNotificationCommand{Uid: "notifier1", OrgId: 1, Name: "1"}
firstNotification := models.CreateAlertNotificationCommand{UID: "notifier1", OrgID: 1, Name: "1"}
_, err := sqlStore.CreateAlertNotificationCommand(context.Background(), &firstNotification)
require.Nil(t, err)
secondNotification := models.CreateAlertNotificationCommand{Uid: "notifier2", OrgId: 1, Name: "2"}
secondNotification := models.CreateAlertNotificationCommand{UID: "notifier2", OrgID: 1, Name: "2"}
_, err = sqlStore.CreateAlertNotificationCommand(context.Background(), &secondNotification)
require.Nil(t, err)

@ -34,7 +34,7 @@ func (s *AlertNotificationService) GetAlertNotifications(ctx context.Context, qu
}
func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) (res *models.AlertNotification, err error) {
if util.IsShortUIDTooLong(cmd.Uid) {
if util.IsShortUIDTooLong(cmd.UID) {
return nil, ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
}
@ -57,7 +57,7 @@ func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Co
}
func (s *AlertNotificationService) UpdateAlertNotification(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) (res *models.AlertNotification, err error) {
if util.IsShortUIDTooLong(cmd.Uid) {
if util.IsShortUIDTooLong(cmd.UID) {
return nil, ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
}
@ -67,8 +67,8 @@ func (s *AlertNotificationService) UpdateAlertNotification(ctx context.Context,
}
model := models.AlertNotification{
Id: cmd.Id,
OrgId: cmd.OrgId,
ID: cmd.ID,
OrgID: cmd.OrgID,
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,
@ -106,7 +106,7 @@ func (s *AlertNotificationService) GetAlertNotificationsWithUid(ctx context.Cont
}
func (s *AlertNotificationService) UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) (res *models.AlertNotification, err error) {
if util.IsShortUIDTooLong(cmd.Uid) || util.IsShortUIDTooLong(cmd.NewUid) {
if util.IsShortUIDTooLong(cmd.UID) || util.IsShortUIDTooLong(cmd.NewUID) {
return nil, ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
}
@ -124,10 +124,10 @@ func (s *AlertNotificationService) GetAlertNotificationsWithUidToSend(ctx contex
func (s *AlertNotificationService) createNotifier(ctx context.Context, model *models.AlertNotification, secureSettings map[string]string) (Notifier, error) {
secureSettingsMap := map[string]string{}
if model.Id > 0 {
if model.ID > 0 {
query := &models.GetAlertNotificationsQuery{
OrgId: model.OrgId,
Id: model.Id,
OrgID: model.OrgID,
ID: model.ID,
}
res, err := s.SQLStore.GetAlertNotifications(ctx, query)
if err != nil {

@ -72,8 +72,8 @@ func TestService(t *testing.T) {
// Delete the created alert notification
delCmd := models.DeleteAlertNotificationCommand{
Id: an.Id,
OrgId: an.OrgId,
ID: an.ID,
OrgID: an.OrgID,
}
err = s.DeleteAlertNotification(context.Background(), &delCmd)
require.NoError(t, err)
@ -90,14 +90,14 @@ func TestService(t *testing.T) {
require.NoError(t, err)
// Try to update it with an invalid type.
updateCmd := models.UpdateAlertNotificationCommand{Id: n.Id, Settings: simplejson.New(), SecureSettings: ss, Type: "invalid"}
updateCmd := models.UpdateAlertNotificationCommand{ID: n.ID, Settings: simplejson.New(), SecureSettings: ss, Type: "invalid"}
_, err = s.UpdateAlertNotification(ctx, &updateCmd)
require.Error(t, err)
// Delete the created alert notification.
delCmd := models.DeleteAlertNotificationCommand{
Id: n.Id,
OrgId: n.OrgId,
ID: n.ID,
OrgID: n.OrgID,
}
err = s.DeleteAlertNotification(context.Background(), &delCmd)
require.NoError(t, err)
@ -114,7 +114,7 @@ func TestService(t *testing.T) {
require.NoError(t, err)
// Update test notification.
updateCmd := models.UpdateAlertNotificationCommand{Id: n.Id, Settings: simplejson.New(), SecureSettings: ss, Type: nType}
updateCmd := models.UpdateAlertNotificationCommand{ID: n.ID, Settings: simplejson.New(), SecureSettings: ss, Type: nType}
n2, err := s.UpdateAlertNotification(ctx, &updateCmd)
require.NoError(t, err)
@ -124,8 +124,8 @@ func TestService(t *testing.T) {
// Delete the created alert notification.
delCmd := models.DeleteAlertNotificationCommand{
Id: n.Id,
OrgId: n.OrgId,
ID: n.ID,
OrgID: n.OrgID,
}
err = s.DeleteAlertNotification(context.Background(), &delCmd)
require.NoError(t, err)
@ -134,7 +134,7 @@ func TestService(t *testing.T) {
t.Run("create alert notification should reject an invalid command", func(t *testing.T) {
uid := strings.Repeat("A", 41)
_, err := s.CreateAlertNotificationCommand(context.Background(), &models.CreateAlertNotificationCommand{Uid: uid})
_, err := s.CreateAlertNotificationCommand(context.Background(), &models.CreateAlertNotificationCommand{UID: uid})
require.ErrorIs(t, err, ValidationError{Reason: "Invalid UID: Must be 40 characters or less"})
})
@ -144,10 +144,10 @@ func TestService(t *testing.T) {
uid := strings.Repeat("A", 41)
expectedErr := ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
_, err := s.UpdateAlertNotification(ctx, &models.UpdateAlertNotificationCommand{Uid: uid})
_, err := s.UpdateAlertNotification(ctx, &models.UpdateAlertNotificationCommand{UID: uid})
require.ErrorIs(t, err, expectedErr)
_, err = s.UpdateAlertNotificationWithUid(ctx, &models.UpdateAlertNotificationWithUidCommand{NewUid: uid})
_, err = s.UpdateAlertNotificationWithUid(ctx, &models.UpdateAlertNotificationWithUidCommand{NewUID: uid})
require.ErrorIs(t, err, expectedErr)
})
}

@ -35,7 +35,7 @@ var timeNow = time.Now
func (ss *sqlStore) DeleteAlertNotification(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error {
return ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
res, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
res, err := sess.Exec(sql, cmd.OrgID, cmd.ID)
if err != nil {
return err
}
@ -48,7 +48,7 @@ func (ss *sqlStore) DeleteAlertNotification(ctx context.Context, cmd *models.Del
return models.ErrAlertNotificationNotFound
}
if _, err := sess.Exec("DELETE FROM alert_notification_state WHERE alert_notification_state.org_id = ? AND alert_notification_state.notifier_id = ?", cmd.OrgId, cmd.Id); err != nil {
if _, err := sess.Exec("DELETE FROM alert_notification_state WHERE alert_notification_state.org_id = ? AND alert_notification_state.notifier_id = ?", cmd.OrgID, cmd.ID); err != nil {
return err
}
@ -59,7 +59,7 @@ func (ss *sqlStore) DeleteAlertNotification(ctx context.Context, cmd *models.Del
func (ss *sqlStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *models.DeleteAlertNotificationWithUidCommand) (err error) {
var res *models.AlertNotification
if err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
existingNotification := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
existingNotification := &models.GetAlertNotificationsWithUidQuery{OrgID: cmd.OrgID, UID: cmd.UID}
res, err = getAlertNotificationWithUidInternal(ctx, existingNotification, sess)
return err
}); err != nil {
@ -70,10 +70,10 @@ func (ss *sqlStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *mod
return models.ErrAlertNotificationNotFound
}
cmd.DeletedAlertNotificationId = res.Id
cmd.DeletedAlertNotificationID = res.ID
deleteCommand := &models.DeleteAlertNotificationCommand{
Id: res.Id,
OrgId: res.OrgId,
ID: res.ID,
OrgID: res.OrgID,
}
return ss.DeleteAlertNotification(ctx, deleteCommand)
}
@ -87,7 +87,7 @@ func (ss *sqlStore) GetAlertNotifications(ctx context.Context, query *models.Get
}
func (ss *sqlStore) GetAlertNotificationUidWithId(ctx context.Context, query *models.GetAlertNotificationUidQuery) (res string, err error) {
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
cacheKey := newAlertNotificationUidCacheKey(query.OrgID, query.ID)
if cached, found := ss.cache.Get(cacheKey); found {
return cached.(string), nil
@ -119,7 +119,7 @@ func (ss *sqlStore) GetAlertNotificationsWithUid(ctx context.Context, query *mod
func (ss *sqlStore) GetAllAlertNotifications(ctx context.Context, query *models.GetAllAlertNotificationsQuery) (res []*models.AlertNotification, err error) {
res = make([]*models.AlertNotification, 0)
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
if err := sess.Where("org_id = ?", query.OrgId).Asc("name").Find(&res); err != nil {
if err := sess.Where("org_id = ?", query.OrgID).Asc("name").Find(&res); err != nil {
return err
}
return nil
@ -151,14 +151,14 @@ func (ss *sqlStore) GetAlertNotificationsWithUidToSend(ctx context.Context, quer
`)
sql.WriteString(` WHERE alert_notification.org_id = ?`)
params = append(params, query.OrgId)
params = append(params, query.OrgID)
sql.WriteString(` AND ((alert_notification.is_default = ?)`)
params = append(params, ss.db.GetDialect().BooleanStr(true))
if len(query.Uids) > 0 {
sql.WriteString(` OR alert_notification.uid IN (?` + strings.Repeat(",?", len(query.Uids)-1) + ")")
for _, v := range query.Uids {
if len(query.UIDs) > 0 {
sql.WriteString(` OR alert_notification.uid IN (?` + strings.Repeat(",?", len(query.UIDs)-1) + ")")
for _, v := range query.UIDs {
params = append(params, v)
}
}
@ -179,10 +179,10 @@ func getAlertNotificationUidInternal(ctx context.Context, query *models.GetAlert
`)
sql.WriteString(` WHERE alert_notification.org_id = ?`)
params = append(params, query.OrgId)
params = append(params, query.OrgID)
sql.WriteString(` AND alert_notification.id = ?`)
params = append(params, query.Id)
params = append(params, query.ID)
results := make([]string, 0)
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil {
@ -219,17 +219,17 @@ func getAlertNotificationInternal(ctx context.Context, query *models.GetAlertNot
`)
sql.WriteString(` WHERE alert_notification.org_id = ?`)
params = append(params, query.OrgId)
params = append(params, query.OrgID)
if query.Name != "" || query.Id != 0 {
if query.Name != "" || query.ID != 0 {
if query.Name != "" {
sql.WriteString(` AND alert_notification.name = ?`)
params = append(params, query.Name)
}
if query.Id != 0 {
if query.ID != 0 {
sql.WriteString(` AND alert_notification.id = ?`)
params = append(params, query.Id)
params = append(params, query.ID)
}
}
@ -266,7 +266,7 @@ func getAlertNotificationWithUidInternal(ctx context.Context, query *models.GetA
`)
sql.WriteString(` WHERE alert_notification.org_id = ? AND alert_notification.uid = ?`)
params = append(params, query.OrgId, query.Uid)
params = append(params, query.OrgID, query.UID)
results := make([]*models.AlertNotification, 0)
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil {
@ -281,15 +281,15 @@ func getAlertNotificationWithUidInternal(ctx context.Context, query *models.GetA
func (ss *sqlStore) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) (res *models.AlertNotification, err error) {
err = ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
if cmd.Uid == "" {
uid, uidGenerationErr := generateNewAlertNotificationUid(ctx, sess, cmd.OrgId)
if cmd.UID == "" {
uid, uidGenerationErr := generateNewAlertNotificationUid(ctx, sess, cmd.OrgID)
if uidGenerationErr != nil {
return uidGenerationErr
}
cmd.Uid = uid
cmd.UID = uid
}
existingQuery := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
existingQuery := &models.GetAlertNotificationsWithUidQuery{OrgID: cmd.OrgID, UID: cmd.UID}
if notification, err := getAlertNotificationWithUidInternal(ctx, existingQuery, sess); err != nil {
return err
} else if notification != nil {
@ -297,7 +297,7 @@ func (ss *sqlStore) CreateAlertNotificationCommand(ctx context.Context, cmd *mod
}
// check if name exists
sameNameQuery := &models.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
sameNameQuery := &models.GetAlertNotificationsQuery{OrgID: cmd.OrgID, Name: cmd.Name}
if notification, err := getAlertNotificationInternal(ctx, sameNameQuery, sess); err != nil {
return err
} else if notification != nil {
@ -324,8 +324,8 @@ func (ss *sqlStore) CreateAlertNotificationCommand(ctx context.Context, cmd *mod
}
alertNotification := &models.AlertNotification{
Uid: cmd.Uid,
OrgId: cmd.OrgId,
UID: cmd.UID,
OrgID: cmd.OrgID,
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,
@ -368,22 +368,22 @@ func (ss *sqlStore) UpdateAlertNotification(ctx context.Context, cmd *models.Upd
err = ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) (err error) {
current := models.AlertNotification{}
if _, err = sess.ID(cmd.Id).Get(&current); err != nil {
if _, err = sess.ID(cmd.ID).Get(&current); err != nil {
return err
}
if current.Id == 0 {
if current.ID == 0 {
return models.ErrAlertNotificationNotFound
}
// check if name exists
sameNameQuery := &models.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
sameNameQuery := &models.GetAlertNotificationsQuery{OrgID: cmd.OrgID, Name: cmd.Name}
notification, err := getAlertNotificationInternal(ctx, sameNameQuery, sess)
if err != nil {
return err
}
if notification != nil && notification.Id != current.Id {
if notification != nil && notification.ID != current.ID {
return fmt.Errorf("alert notification name %q already exists", cmd.Name)
}
@ -403,8 +403,8 @@ func (ss *sqlStore) UpdateAlertNotification(ctx context.Context, cmd *models.Upd
current.SendReminder = cmd.SendReminder
current.DisableResolveMessage = cmd.DisableResolveMessage
if cmd.Uid != "" {
current.Uid = cmd.Uid
if cmd.UID != "" {
current.UID = cmd.UID
}
if current.SendReminder {
@ -422,7 +422,7 @@ func (ss *sqlStore) UpdateAlertNotification(ctx context.Context, cmd *models.Upd
sess.UseBool("is_default", "send_reminder", "disable_resolve_message")
if affected, err := sess.ID(cmd.Id).Update(current); err != nil {
if affected, err := sess.ID(cmd.ID).Update(current); err != nil {
return err
} else if affected == 0 {
return fmt.Errorf("could not update alert notification")
@ -435,7 +435,7 @@ func (ss *sqlStore) UpdateAlertNotification(ctx context.Context, cmd *models.Upd
}
func (ss *sqlStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) (res *models.AlertNotification, err error) {
getAlertNotificationWithUidQuery := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
getAlertNotificationWithUidQuery := &models.GetAlertNotificationsWithUidQuery{OrgID: cmd.OrgID, UID: cmd.UID}
if err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
res, err = getAlertNotificationWithUidInternal(ctx, getAlertNotificationWithUidQuery, sess)
@ -449,13 +449,13 @@ func (ss *sqlStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *mod
return nil, models.ErrAlertNotificationNotFound
}
if cmd.NewUid == "" {
cmd.NewUid = cmd.Uid
if cmd.NewUID == "" {
cmd.NewUID = cmd.UID
}
updateNotification := &models.UpdateAlertNotificationCommand{
Id: current.Id,
Uid: cmd.NewUid,
ID: current.ID,
UID: cmd.NewUID,
Name: cmd.Name,
Type: cmd.Type,
SendReminder: cmd.SendReminder,
@ -465,7 +465,7 @@ func (ss *sqlStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *mod
Settings: cmd.Settings,
SecureSettings: cmd.SecureSettings,
OrgId: cmd.OrgId,
OrgID: cmd.OrgID,
}
return ss.UpdateAlertNotification(ctx, updateNotification)
@ -475,7 +475,7 @@ func (ss *sqlStore) SetAlertNotificationStateToCompleteCommand(ctx context.Conte
return ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
version := cmd.Version
var current models.AlertNotificationState
if _, err := sess.ID(cmd.Id).Get(&current); err != nil {
if _, err := sess.ID(cmd.ID).Get(&current); err != nil {
return err
}
@ -487,13 +487,13 @@ func (ss *sqlStore) SetAlertNotificationStateToCompleteCommand(ctx context.Conte
WHERE
id = ?`
_, err := sess.Exec(sql, models.AlertNotificationStateCompleted, newVersion, timeNow().Unix(), cmd.Id)
_, err := sess.Exec(sql, models.AlertNotificationStateCompleted, newVersion, timeNow().Unix(), cmd.ID)
if err != nil {
return err
}
if current.Version != version {
ss.log.Error("notification state out of sync. the notification is marked as complete but has been modified between set as pending and completion.", "notifierId", current.NotifierId)
ss.log.Error("notification state out of sync. the notification is marked as complete but has been modified between set as pending and completion.", "notifierId", current.NotifierID)
}
return nil
@ -517,7 +517,7 @@ func (ss *sqlStore) SetAlertNotificationStateToPendingCommand(ctx context.Contex
newVersion,
timeNow().Unix(),
cmd.AlertRuleStateUpdatedVersion,
cmd.Id,
cmd.ID,
cmd.Version,
cmd.AlertRuleStateUpdatedVersion)
@ -553,9 +553,9 @@ func (ss *sqlStore) GetOrCreateAlertNotificationState(ctx context.Context, cmd *
}
notificationState := &models.AlertNotificationState{
OrgId: cmd.OrgId,
AlertId: cmd.AlertId,
NotifierId: cmd.NotifierId,
OrgID: cmd.OrgID,
AlertID: cmd.AlertID,
NotifierID: cmd.NotifierID,
State: models.AlertNotificationStateUnknown,
UpdatedAt: timeNow().Unix(),
}
@ -587,8 +587,8 @@ func (ss *sqlStore) GetOrCreateAlertNotificationState(ctx context.Context, cmd *
func getAlertNotificationState(ctx context.Context, sess *db.Session, cmd *models.GetOrCreateNotificationStateQuery, nj *models.AlertNotificationState) (bool, error) {
return sess.
Where("alert_notification_state.org_id = ?", cmd.OrgId).
Where("alert_notification_state.alert_id = ?", cmd.AlertId).
Where("alert_notification_state.notifier_id = ?", cmd.NotifierId).
Where("alert_notification_state.org_id = ?", cmd.OrgID).
Where("alert_notification_state.alert_id = ?", cmd.AlertID).
Where("alert_notification_state.notifier_id = ?", cmd.NotifierID).
Get(nj)
}

@ -40,7 +40,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
defer func() { timeNow = oldTimeNow }()
t.Run("Get no existing state should create a new state", func(t *testing.T) {
query := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
query := &models.GetOrCreateNotificationStateQuery{AlertID: alertID, OrgID: orgID, NotifierID: notifierID}
res, err := store.GetOrCreateAlertNotificationState(context.Background(), query)
require.Nil(t, err)
require.NotNil(t, res)
@ -49,11 +49,11 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
require.Equal(t, now.Unix(), res.UpdatedAt)
t.Run("Get existing state should not create a new state", func(t *testing.T) {
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
query2 := &models.GetOrCreateNotificationStateQuery{AlertID: alertID, OrgID: orgID, NotifierID: notifierID}
res2, err := store.GetOrCreateAlertNotificationState(context.Background(), query2)
require.Nil(t, err)
require.NotNil(t, res2)
require.Equal(t, res.Id, res2.Id)
require.Equal(t, res.ID, res2.ID)
require.Equal(t, now.Unix(), res2.UpdatedAt)
})
@ -61,7 +61,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
s := *res
cmd := models.SetAlertNotificationStateToPendingCommand{
Id: s.Id,
ID: s.ID,
Version: s.Version,
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
}
@ -70,7 +70,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
require.Nil(t, err)
require.Equal(t, int64(1), cmd.ResultVersion)
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
query2 := &models.GetOrCreateNotificationStateQuery{AlertID: alertID, OrgID: orgID, NotifierID: notifierID}
res2, err := store.GetOrCreateAlertNotificationState(context.Background(), query2)
require.Nil(t, err)
require.Equal(t, int64(1), res2.Version)
@ -80,13 +80,13 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("Update existing state to completed should update database", func(t *testing.T) {
s := *res
setStateCmd := models.SetAlertNotificationStateToCompleteCommand{
Id: s.Id,
ID: s.ID,
Version: cmd.ResultVersion,
}
err := store.SetAlertNotificationStateToCompleteCommand(context.Background(), &setStateCmd)
require.Nil(t, err)
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
query3 := &models.GetOrCreateNotificationStateQuery{AlertID: alertID, OrgID: orgID, NotifierID: notifierID}
res3, err := store.GetOrCreateAlertNotificationState(context.Background(), query3)
require.Nil(t, err)
require.Equal(t, int64(2), res3.Version)
@ -98,13 +98,13 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
s := *res
unknownVersion := int64(1000)
cmd := models.SetAlertNotificationStateToCompleteCommand{
Id: s.Id,
ID: s.ID,
Version: unknownVersion,
}
err := store.SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
require.Nil(t, err)
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
query3 := &models.GetOrCreateNotificationStateQuery{AlertID: alertID, OrgID: orgID, NotifierID: notifierID}
res3, err := store.GetOrCreateAlertNotificationState(context.Background(), query3)
require.Nil(t, err)
require.Equal(t, unknownVersion+1, res3.Version)
@ -117,7 +117,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
s := *res
s.Version = 1000
cmd := models.SetAlertNotificationStateToPendingCommand{
Id: s.NotifierId,
ID: s.NotifierID,
Version: s.Version,
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
}
@ -128,7 +128,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("Updating existing state to pending with incorrect version since alert rule state update version is higher", func(t *testing.T) {
s := *res
cmd := models.SetAlertNotificationStateToPendingCommand{
Id: s.Id,
ID: s.ID,
Version: s.Version,
AlertRuleStateUpdatedVersion: 1000,
}
@ -142,7 +142,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
s := *res
s.Version = 1000
cmd := models.SetAlertNotificationStateToPendingCommand{
Id: s.Id,
ID: s.ID,
Version: s.Version,
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
}
@ -155,7 +155,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("Alert notifications should be empty", func(t *testing.T) {
setup()
cmd := &models.GetAlertNotificationsQuery{
OrgId: 2,
OrgID: 2,
Name: "email",
}
@ -169,7 +169,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
cmd := &models.CreateAlertNotificationCommand{
Name: "ops",
Type: "email",
OrgId: 1,
OrgID: 1,
SendReminder: true,
Settings: simplejson.New(),
}
@ -192,7 +192,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
cmd := &models.CreateAlertNotificationCommand{
Name: "ops update",
Type: "email",
OrgId: 1,
OrgID: 1,
SendReminder: false,
Settings: simplejson.New(),
}
@ -201,7 +201,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
require.Nil(t, err)
updateCmd := &models.UpdateAlertNotificationCommand{
Id: res.Id,
ID: res.ID,
SendReminder: true,
}
@ -225,7 +225,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
cmd := &models.CreateAlertNotificationCommand{
Name: "ops",
Type: "email",
OrgId: 1,
OrgID: 1,
SendReminder: true,
Frequency: "10s",
Settings: simplejson.New(),
@ -233,12 +233,12 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
res, err := store.CreateAlertNotificationCommand(context.Background(), cmd)
require.Nil(t, err)
require.NotEqual(t, 0, res.Id)
require.NotEqual(t, 0, res.OrgId)
require.NotEqual(t, 0, res.ID)
require.NotEqual(t, 0, res.OrgID)
require.Equal(t, "email", res.Type)
require.Equal(t, 10*time.Second, res.Frequency)
require.False(t, res.DisableResolveMessage)
require.NotEmpty(t, res.Uid)
require.NotEmpty(t, res.UID)
t.Run("Cannot save Alert Notification with the same name", func(t *testing.T) {
_, err = store.CreateAlertNotificationCommand(context.Background(), cmd)
@ -248,11 +248,11 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
anotherUidCmd := &models.CreateAlertNotificationCommand{
Name: cmd.Name,
Type: cmd.Type,
OrgId: 1,
OrgID: 1,
SendReminder: cmd.SendReminder,
Frequency: cmd.Frequency,
Settings: cmd.Settings,
Uid: "notifier1",
UID: "notifier1",
}
_, err = store.CreateAlertNotificationCommand(context.Background(), anotherUidCmd)
require.Error(t, err)
@ -261,11 +261,11 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
anotherUidCmd := &models.CreateAlertNotificationCommand{
Name: "another ops",
Type: cmd.Type,
OrgId: 1,
OrgID: 1,
SendReminder: cmd.SendReminder,
Frequency: cmd.Frequency,
Settings: cmd.Settings,
Uid: "notifier2",
UID: "notifier2",
}
_, err = store.CreateAlertNotificationCommand(context.Background(), anotherUidCmd)
require.Nil(t, err)
@ -275,12 +275,12 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
newCmd := &models.UpdateAlertNotificationCommand{
Name: "NewName",
Type: "webhook",
OrgId: res.OrgId,
OrgID: res.OrgID,
SendReminder: true,
DisableResolveMessage: true,
Frequency: "60s",
Settings: simplejson.New(),
Id: res.Id,
ID: res.ID,
}
newres, err := store.UpdateAlertNotification(context.Background(), newCmd)
require.Nil(t, err)
@ -293,10 +293,10 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
newCmd := &models.UpdateAlertNotificationCommand{
Name: "NewName",
Type: "webhook",
OrgId: res.OrgId,
OrgID: res.OrgID,
SendReminder: false,
Settings: simplejson.New(),
Id: res.Id,
ID: res.ID,
}
newres, err := store.UpdateAlertNotification(context.Background(), newCmd)
require.Nil(t, err)
@ -306,12 +306,12 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("Can search using an array of ids", func(t *testing.T) {
setup()
cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgID: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgID: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgID: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgID: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgID: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
res1, err := store.CreateAlertNotificationCommand(context.Background(), &cmd1)
require.NoError(t, err)
@ -326,8 +326,8 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("search", func(t *testing.T) {
query := &models.GetAlertNotificationsWithUidToSendQuery{
Uids: []string{res1.Uid, res2.Uid, "112341231"},
OrgId: 1,
UIDs: []string{res1.UID, res2.UID, "112341231"},
OrgID: 1,
}
res, err := store.GetAlertNotificationsWithUidToSend(context.Background(), query)
@ -337,7 +337,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("all", func(t *testing.T) {
query := &models.GetAllAlertNotificationsQuery{
OrgId: 1,
OrgID: 1,
}
res, err := store.GetAllAlertNotifications(context.Background(), query)
@ -350,28 +350,28 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
})
})
t.Run("Notification Uid by Id Caching", func(t *testing.T) {
t.Run("Notification UID by ID Caching", func(t *testing.T) {
setup()
notification := &models.CreateAlertNotificationCommand{Uid: "aNotificationUid", OrgId: 1, Name: "aNotificationUid"}
notification := &models.CreateAlertNotificationCommand{UID: "aNotificationUid", OrgID: 1, Name: "aNotificationUid"}
_, err := store.CreateAlertNotificationCommand(context.Background(), notification)
require.Nil(t, err)
byUidQuery := &models.GetAlertNotificationsWithUidQuery{
Uid: notification.Uid,
OrgId: notification.OrgId,
UID: notification.UID,
OrgID: notification.OrgID,
}
res, notificationByUidErr := store.GetAlertNotificationsWithUid(context.Background(), byUidQuery)
require.Nil(t, notificationByUidErr)
t.Run("Can cache notification Uid", func(t *testing.T) {
t.Run("Can cache notification UID", func(t *testing.T) {
byIdQuery := &models.GetAlertNotificationUidQuery{
Id: res.Id,
OrgId: res.OrgId,
ID: res.ID,
OrgID: res.OrgID,
}
cacheKey := newAlertNotificationUidCacheKey(byIdQuery.OrgId, byIdQuery.Id)
cacheKey := newAlertNotificationUidCacheKey(byIdQuery.OrgID, byIdQuery.ID)
resultBeforeCaching, foundBeforeCaching := store.cache.Get(cacheKey)
require.False(t, foundBeforeCaching)
@ -382,15 +382,15 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
resultAfterCaching, foundAfterCaching := store.cache.Get(cacheKey)
require.True(t, foundAfterCaching)
require.Equal(t, notification.Uid, resultAfterCaching)
require.Equal(t, notification.UID, resultAfterCaching)
})
t.Run("Retrieves from cache when exists", func(t *testing.T) {
query := &models.GetAlertNotificationUidQuery{
Id: 999,
OrgId: 100,
ID: 999,
OrgID: 100,
}
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
cacheKey := newAlertNotificationUidCacheKey(query.OrgID, query.ID)
store.cache.Set(cacheKey, "a-cached-uid", -1)
res, err := store.GetAlertNotificationUidWithId(context.Background(), query)
@ -400,8 +400,8 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
t.Run("Returns an error without populating cache when the notification doesn't exist in the database", func(t *testing.T) {
query := &models.GetAlertNotificationUidQuery{
Id: -1,
OrgId: 100,
ID: -1,
OrgID: 100,
}
res, err := store.GetAlertNotificationUidWithId(context.Background(), query)
@ -409,7 +409,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
require.Error(t, err)
require.True(t, errors.Is(err, models.ErrAlertNotificationFailedTranslateUniqueID))
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
cacheKey := newAlertNotificationUidCacheKey(query.OrgID, query.ID)
result, found := store.cache.Get(cacheKey)
require.False(t, found)
require.Nil(t, result)
@ -421,12 +421,12 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
updateCmd := &models.UpdateAlertNotificationCommand{
Name: "NewName",
Type: "webhook",
OrgId: 1,
OrgID: 1,
SendReminder: true,
DisableResolveMessage: true,
Frequency: "60s",
Settings: simplejson.New(),
Id: 1,
ID: 1,
}
_, err := store.UpdateAlertNotification(context.Background(), updateCmd)
require.Equal(t, models.ErrAlertNotificationNotFound, err)
@ -435,13 +435,13 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
updateWithUidCmd := &models.UpdateAlertNotificationWithUidCommand{
Name: "NewName",
Type: "webhook",
OrgId: 1,
OrgID: 1,
SendReminder: true,
DisableResolveMessage: true,
Frequency: "60s",
Settings: simplejson.New(),
Uid: "uid",
NewUid: "newUid",
UID: "uid",
NewUID: "newUid",
}
_, err := store.UpdateAlertNotificationWithUid(context.Background(), updateWithUidCmd)
require.Equal(t, models.ErrAlertNotificationNotFound, err)
@ -453,7 +453,7 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
cmd := &models.CreateAlertNotificationCommand{
Name: "ops update",
Type: "email",
OrgId: 1,
OrgID: 1,
SendReminder: false,
Settings: simplejson.New(),
}
@ -462,8 +462,8 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
require.Nil(t, err)
deleteCmd := &models.DeleteAlertNotificationCommand{
Id: res.Id,
OrgId: 1,
ID: res.ID,
OrgID: 1,
}
err = store.DeleteAlertNotification(context.Background(), deleteCmd)
require.Nil(t, err)
@ -473,29 +473,29 @@ func TestIntegrationAlertNotificationSQLAccess(t *testing.T) {
require.Nil(t, err)
deleteWithUidCmd := &models.DeleteAlertNotificationWithUidCommand{
Uid: res.Uid,
OrgId: 1,
UID: res.UID,
OrgID: 1,
}
err = store.DeleteAlertNotificationWithUid(context.Background(), deleteWithUidCmd)
require.Nil(t, err)
require.Equal(t, res.Id, deleteWithUidCmd.DeletedAlertNotificationId)
require.Equal(t, res.ID, deleteWithUidCmd.DeletedAlertNotificationID)
})
})
t.Run("Cannot delete non-existing Alert Notification", func(t *testing.T) {
setup()
deleteCmd := &models.DeleteAlertNotificationCommand{
Id: 1,
OrgId: 1,
ID: 1,
OrgID: 1,
}
err := store.DeleteAlertNotification(context.Background(), deleteCmd)
require.Equal(t, models.ErrAlertNotificationNotFound, err)
t.Run("using UID", func(t *testing.T) {
deleteWithUidCmd := &models.DeleteAlertNotificationWithUidCommand{
Uid: "uid",
OrgId: 1,
UID: "uid",
OrgID: 1,
}
err = store.DeleteAlertNotificationWithUid(context.Background(), deleteWithUidCmd)
require.Equal(t, models.ErrAlertNotificationNotFound, err)

@ -33,8 +33,8 @@ func (s *AlertNotificationService) HandleNotificationTestCommand(ctx context.Con
notificationSvc := newNotificationService(nil, nil, nil, nil)
model := models.AlertNotification{
Id: cmd.ID,
OrgId: cmd.OrgID,
ID: cmd.ID,
OrgID: cmd.OrgID,
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,

@ -80,7 +80,7 @@ func (dc *NotificationProvisioner) deleteNotifications(ctx context.Context, noti
notification.OrgID = 1
}
getNotification := &models.GetAlertNotificationsWithUidQuery{Uid: notification.UID, OrgId: notification.OrgID}
getNotification := &models.GetAlertNotificationsWithUidQuery{UID: notification.UID, OrgID: notification.OrgID}
res, err := dc.alertingManager.GetAlertNotificationsWithUid(ctx, getNotification)
if err != nil {
@ -88,7 +88,7 @@ func (dc *NotificationProvisioner) deleteNotifications(ctx context.Context, noti
}
if res != nil {
cmd := &models.DeleteAlertNotificationWithUidCommand{Uid: res.Uid, OrgId: getNotification.OrgId}
cmd := &models.DeleteAlertNotificationWithUidCommand{UID: res.UID, OrgID: getNotification.OrgID}
if err := dc.alertingManager.DeleteAlertNotificationWithUid(ctx, cmd); err != nil {
return err
}
@ -111,7 +111,7 @@ func (dc *NotificationProvisioner) mergeNotifications(ctx context.Context, notif
notification.OrgID = 1
}
cmd := &models.GetAlertNotificationsWithUidQuery{OrgId: notification.OrgID, Uid: notification.UID}
cmd := &models.GetAlertNotificationsWithUidQuery{OrgID: notification.OrgID, UID: notification.UID}
res, err := dc.alertingManager.GetAlertNotificationsWithUid(ctx, cmd)
if err != nil {
return err
@ -120,13 +120,13 @@ func (dc *NotificationProvisioner) mergeNotifications(ctx context.Context, notif
if res == nil {
dc.log.Debug("inserting alert notification from configuration", "name", notification.Name, "uid", notification.UID)
insertCmd := &models.CreateAlertNotificationCommand{
Uid: notification.UID,
UID: notification.UID,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Settings: notification.SettingsToJSON(),
SecureSettings: notification.SecureSettings,
OrgId: notification.OrgID,
OrgID: notification.OrgID,
DisableResolveMessage: notification.DisableResolveMessage,
Frequency: notification.Frequency,
SendReminder: notification.SendReminder,
@ -139,13 +139,13 @@ func (dc *NotificationProvisioner) mergeNotifications(ctx context.Context, notif
} else {
dc.log.Debug("updating alert notification from configuration", "name", notification.Name)
updateCmd := &models.UpdateAlertNotificationWithUidCommand{
Uid: notification.UID,
UID: notification.UID,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Settings: notification.SettingsToJSON(),
SecureSettings: notification.SecureSettings,
OrgId: notification.OrgID,
OrgID: notification.OrgID,
DisableResolveMessage: notification.DisableResolveMessage,
Frequency: notification.Frequency,
SendReminder: notification.SendReminder,

@ -151,7 +151,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("no notification in database", func(t *testing.T) {
setup()
fakeAlertNotification := &fakeAlertNotification{}
fakeAlertNotification.ExpectedAlertNotification = &models.AlertNotification{OrgId: 1}
fakeAlertNotification.ExpectedAlertNotification = &models.AlertNotification{OrgID: 1}
dc := newNotificationProvisioner(orgService, fakeAlertNotification, encryptionService, nil, logger)
err := dc.applyChanges(context.Background(), twoNotificationsConfig)
@ -164,14 +164,14 @@ func TestNotificationAsConfig(t *testing.T) {
setup()
existingNotificationCmd := models.CreateAlertNotificationCommand{
Name: "channel1",
OrgId: 1,
Uid: "notifier1",
OrgID: 1,
UID: "notifier1",
Type: "slack",
}
res, err := ns.SQLStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
require.NoError(t, err)
require.NotNil(t, res)
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgId: 1}
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgID: 1}
results, err := ns.SQLStore.GetAllAlertNotifications(context.Background(), &notificationsQuery)
require.NoError(t, err)
require.NotNil(t, results)
@ -201,22 +201,22 @@ func TestNotificationAsConfig(t *testing.T) {
setup()
existingNotificationCmd := models.CreateAlertNotificationCommand{
Name: "channel0",
OrgId: 1,
Uid: "notifier0",
OrgID: 1,
UID: "notifier0",
Type: "slack",
}
_, err := ns.SQLStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
require.NoError(t, err)
existingNotificationCmd = models.CreateAlertNotificationCommand{
Name: "channel3",
OrgId: 1,
Uid: "notifier3",
OrgID: 1,
UID: "notifier3",
Type: "slack",
}
_, err = ns.SQLStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
require.NoError(t, err)
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgId: 1}
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgID: 1}
res, err := ns.GetAllAlertNotifications(context.Background(), &notificationsQuery)
require.NoError(t, err)
require.NotNil(t, res)
@ -237,8 +237,8 @@ func TestNotificationAsConfig(t *testing.T) {
existingNotificationCmd := models.CreateAlertNotificationCommand{
Name: "default-notification-delete",
OrgId: 1,
Uid: "notifier2",
OrgID: 1,
UID: "notifier2",
Type: "slack",
}
_, err := ns.SQLStore.CreateAlertNotificationCommand(context.Background(), &existingNotificationCmd)
@ -272,7 +272,7 @@ func TestNotificationAsConfig(t *testing.T) {
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgId: 1}
notificationsQuery := models.GetAllAlertNotificationsQuery{OrgID: 1}
res, err := ns.GetAllAlertNotifications(context.Background(), &notificationsQuery)
require.NoError(t, err)
require.Empty(t, res)

@ -619,8 +619,8 @@ func createAlertNotificationWithReminder(t *testing.T, orgId int64, uid string,
}
return &models.AlertNotification{
OrgId: orgId,
Uid: uid,
OrgID: orgId,
UID: uid,
Name: uid, // Same as uid to make testing easier.
Type: channelType,
DisableResolveMessage: false,

Loading…
Cancel
Save