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) { 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) 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) return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
} }
query := &alertmodels.GetAlertNotificationsQuery{ query := &alertmodels.GetAlertNotificationsQuery{
OrgId: c.OrgID, OrgID: c.OrgID,
Id: notificationId, ID: notificationId,
} }
if query.Id == 0 { if query.ID == 0 {
return response.Error(404, "Alert notification not found", nil) return response.Error(404, "Alert notification not found", nil)
} }
@ -358,11 +358,11 @@ func (hs *HTTPServer) GetAlertNotificationByID(c *contextmodel.ReqContext) respo
// 500: internalServerError // 500: internalServerError
func (hs *HTTPServer) GetAlertNotificationByUID(c *contextmodel.ReqContext) response.Response { func (hs *HTTPServer) GetAlertNotificationByUID(c *contextmodel.ReqContext) response.Response {
query := &alertmodels.GetAlertNotificationsWithUidQuery{ query := &alertmodels.GetAlertNotificationsWithUidQuery{
OrgId: c.OrgID, OrgID: c.OrgID,
Uid: web.Params(c.Req)[":uid"], UID: web.Params(c.Req)[":uid"],
} }
if query.Uid == "" { if query.UID == "" {
return response.Error(404, "Alert notification not found", nil) 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 { if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err) 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) res, err := hs.AlertNotificationService.CreateAlertNotificationCommand(c.Req.Context(), &cmd)
if err != nil { if err != nil {
@ -429,7 +429,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *contextmodel.ReqContext) respon
if err := web.Bind(c.Req, &cmd); err != nil { if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err) return response.Error(http.StatusBadRequest, "bad request data", err)
} }
cmd.OrgId = c.OrgID cmd.OrgID = c.OrgID
err := hs.fillWithSecureSettingsData(c.Req.Context(), &cmd) err := hs.fillWithSecureSettingsData(c.Req.Context(), &cmd)
if err != nil { if err != nil {
@ -448,8 +448,8 @@ func (hs *HTTPServer) UpdateAlertNotification(c *contextmodel.ReqContext) respon
} }
query := alertmodels.GetAlertNotificationsQuery{ query := alertmodels.GetAlertNotificationsQuery{
OrgId: c.OrgID, OrgID: c.OrgID,
Id: cmd.Id, ID: cmd.ID,
} }
res, err := hs.AlertNotificationService.GetAlertNotifications(c.Req.Context(), &query) 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 { if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err) return response.Error(http.StatusBadRequest, "bad request data", err)
} }
cmd.OrgId = c.OrgID cmd.OrgID = c.OrgID
cmd.Uid = web.Params(c.Req)[":uid"] cmd.UID = web.Params(c.Req)[":uid"]
err := hs.fillWithSecureSettingsDataByUID(c.Req.Context(), &cmd) err := hs.fillWithSecureSettingsDataByUID(c.Req.Context(), &cmd)
if err != nil { if err != nil {
@ -493,8 +493,8 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *contextmodel.ReqContext) r
} }
query := alertmodels.GetAlertNotificationsWithUidQuery{ query := alertmodels.GetAlertNotificationsWithUidQuery{
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
Uid: cmd.Uid, UID: cmd.UID,
} }
res, err := hs.AlertNotificationService.GetAlertNotificationsWithUid(c.Req.Context(), &query) 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{ query := &alertmodels.GetAlertNotificationsQuery{
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
Id: cmd.Id, ID: cmd.ID,
} }
res, err := hs.AlertNotificationService.GetAlertNotifications(ctx, query) res, err := hs.AlertNotificationService.GetAlertNotifications(ctx, query)
@ -540,8 +540,8 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *
} }
query := &alertmodels.GetAlertNotificationsWithUidQuery{ query := &alertmodels.GetAlertNotificationsWithUidQuery{
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
Uid: cmd.Uid, UID: cmd.UID,
} }
res, err := hs.AlertNotificationService.GetAlertNotificationsWithUid(ctx, query) res, err := hs.AlertNotificationService.GetAlertNotificationsWithUid(ctx, query)
@ -582,8 +582,8 @@ func (hs *HTTPServer) DeleteAlertNotification(c *contextmodel.ReqContext) respon
} }
cmd := alertmodels.DeleteAlertNotificationCommand{ cmd := alertmodels.DeleteAlertNotificationCommand{
OrgId: c.OrgID, OrgID: c.OrgID,
Id: notificationId, ID: notificationId,
} }
if err := hs.AlertNotificationService.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil { 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 // 500: internalServerError
func (hs *HTTPServer) DeleteAlertNotificationByUID(c *contextmodel.ReqContext) response.Response { func (hs *HTTPServer) DeleteAlertNotificationByUID(c *contextmodel.ReqContext) response.Response {
cmd := alertmodels.DeleteAlertNotificationWithUidCommand{ cmd := alertmodels.DeleteAlertNotificationWithUidCommand{
OrgId: c.OrgID, OrgID: c.OrgID,
Uid: web.Params(c.Req)[":uid"], UID: web.Params(c.Req)[":uid"],
} }
if err := hs.AlertNotificationService.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil { 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{ return response.JSON(http.StatusOK, util.DynMap{
"message": "Notification deleted", "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 { func NewAlertNotification(notification *models.AlertNotification) *AlertNotification {
dto := &AlertNotification{ dto := &AlertNotification{
Id: notification.Id, Id: notification.ID,
Uid: notification.Uid, Uid: notification.UID,
Name: notification.Name, Name: notification.Name,
Type: notification.Type, Type: notification.Type,
IsDefault: notification.IsDefault, IsDefault: notification.IsDefault,
@ -74,8 +74,8 @@ type AlertNotification struct {
func NewAlertNotificationLookup(notification *models.AlertNotification) *AlertNotificationLookup { func NewAlertNotificationLookup(notification *models.AlertNotification) *AlertNotificationLookup {
return &AlertNotificationLookup{ return &AlertNotificationLookup{
Id: notification.Id, Id: notification.ID,
Uid: notification.Uid, Uid: notification.UID,
Name: notification.Name, Name: notification.Name,
Type: notification.Type, Type: notification.Type,
IsDefault: notification.IsDefault, IsDefault: notification.IsDefault,

@ -206,11 +206,11 @@ func TestAlertRuleExtraction(t *testing.T) {
t.Run("Alert notifications are in DB", func(t *testing.T) { t.Run("Alert notifications are in DB", func(t *testing.T) {
sqlStore := sqlStore{db: sqlstore.InitTestDB(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) _, err = sqlStore.CreateAlertNotificationCommand(context.Background(), &firstNotification)
require.Nil(t, err) 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) _, err = sqlStore.CreateAlertNotificationCommand(context.Background(), &secondNotification)
require.Nil(t, err) require.Nil(t, err)

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

@ -156,7 +156,7 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
} }
cmd := &alertmodels.SetAlertNotificationStateToCompleteCommand{ cmd := &alertmodels.SetAlertNotificationStateToCompleteCommand{
Id: notifierState.state.Id, ID: notifierState.state.ID,
Version: notifierState.state.Version, Version: notifierState.state.Version,
} }
@ -166,7 +166,7 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *notifierState) error { func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *notifierState) error {
if !evalContext.IsTestRun { if !evalContext.IsTestRun {
setPendingCmd := &alertmodels.SetAlertNotificationStateToPendingCommand{ setPendingCmd := &alertmodels.SetAlertNotificationStateToPendingCommand{
Id: notifierState.state.Id, ID: notifierState.state.ID,
Version: notifierState.state.Version, Version: notifierState.state.Version,
AlertRuleStateUpdatedVersion: evalContext.Rule.StateChanges, 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) { 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) res, err := n.sqlStore.GetAlertNotificationsWithUidToSend(evalContext.Ctx, query)
if err != nil { if err != nil {
@ -268,19 +268,19 @@ func (n *notificationService) getNeededNotifiers(orgID int64, notificationUids [
for _, notification := range res { for _, notification := range res {
not, err := InitNotifier(notification, n.decryptFn, n.notificationService) not, err := InitNotifier(notification, n.decryptFn, n.notificationService)
if err != nil { 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 continue
} }
query := &alertmodels.GetOrCreateNotificationStateQuery{ query := &alertmodels.GetOrCreateNotificationStateQuery{
NotifierId: notification.Id, NotifierID: notification.ID,
AlertId: evalContext.Rule.ID, AlertID: evalContext.Rule.ID,
OrgId: evalContext.Rule.OrgID, OrgID: evalContext.Rule.OrgID,
} }
state, err := n.sqlStore.GetOrCreateAlertNotificationState(evalContext.Ctx, query) state, err := n.sqlStore.GetOrCreateAlertNotificationState(evalContext.Ctx, query)
if err != nil { 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 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) { store.getAlertNotificationsWithUidToSend = func(ctx context.Context, query *alertmodels.GetAlertNotificationsWithUidToSendQuery) (res []*alertmodels.AlertNotification, err error) {
return []*alertmodels.AlertNotification{ return []*alertmodels.AlertNotification{
{ {
Id: 1, ID: 1,
Type: "test", Type: "test",
Settings: simplejson.NewFromAny(map[string]interface{}{ Settings: simplejson.NewFromAny(map[string]interface{}{
"uploadImage": uploadImage, "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) { store.getOrCreateNotificationState = func(ctx context.Context, query *alertmodels.GetOrCreateNotificationStateQuery) (res *alertmodels.AlertNotificationState, err error) {
return &alertmodels.AlertNotificationState{ return &alertmodels.AlertNotificationState{
AlertId: evalCtx.Rule.ID, AlertID: evalCtx.Rule.ID,
AlertRuleStateUpdatedVersion: 1, AlertRuleStateUpdatedVersion: 1,
Id: 1, ID: 1,
OrgId: evalCtx.Rule.OrgID, OrgID: evalCtx.Rule.OrgID,
State: alertmodels.AlertNotificationStateUnknown, State: alertmodels.AlertNotificationStateUnknown,
}, nil }, nil
} }
@ -282,7 +282,7 @@ func newTestNotifier(model *alertmodels.AlertNotification, _ GetDecryptedValueFn
} }
return &testNotifier{ return &testNotifier{
UID: model.Uid, UID: model.UID,
Name: model.Name, Name: model.Name,
IsDefault: model.IsDefault, IsDefault: model.IsDefault,
Type: model.Type, Type: model.Type,

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

@ -189,7 +189,7 @@ func TestBaseNotifier(t *testing.T) {
bJSON := simplejson.New() bJSON := simplejson.New()
model := &models.AlertNotification{ model := &models.AlertNotification{
Uid: "1", UID: "1",
Name: "name", Name: "name",
Type: "email", Type: "email",
Settings: bJSON, 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) { func getAlertNotificationUIDByIDAndOrgID(ctx context.Context, store AlertStore, notificationID int64, orgID int64) (string, error) {
query := &models.GetAlertNotificationUidQuery{ query := &models.GetAlertNotificationUidQuery{
OrgId: orgID, OrgID: orgID,
Id: notificationID, ID: notificationID,
} }
uid, err := store.GetAlertNotificationUidWithId(ctx, query) uid, err := store.GetAlertNotificationUidWithId(ctx, query)

@ -91,10 +91,10 @@ func TestAlertRuleModel(t *testing.T) {
return &FakeCondition{}, nil 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) _, err := sqlStore.CreateAlertNotificationCommand(context.Background(), &firstNotification)
require.Nil(t, err) 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) _, err = sqlStore.CreateAlertNotificationCommand(context.Background(), &secondNotification)
require.Nil(t, err) 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) { 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"} 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) { 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"} 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{ model := models.AlertNotification{
Id: cmd.Id, ID: cmd.ID,
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
Name: cmd.Name, Name: cmd.Name,
Type: cmd.Type, Type: cmd.Type,
Settings: cmd.Settings, 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) { 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"} 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) { func (s *AlertNotificationService) createNotifier(ctx context.Context, model *models.AlertNotification, secureSettings map[string]string) (Notifier, error) {
secureSettingsMap := map[string]string{} secureSettingsMap := map[string]string{}
if model.Id > 0 { if model.ID > 0 {
query := &models.GetAlertNotificationsQuery{ query := &models.GetAlertNotificationsQuery{
OrgId: model.OrgId, OrgID: model.OrgID,
Id: model.Id, ID: model.ID,
} }
res, err := s.SQLStore.GetAlertNotifications(ctx, query) res, err := s.SQLStore.GetAlertNotifications(ctx, query)
if err != nil { if err != nil {

@ -72,8 +72,8 @@ func TestService(t *testing.T) {
// Delete the created alert notification // Delete the created alert notification
delCmd := models.DeleteAlertNotificationCommand{ delCmd := models.DeleteAlertNotificationCommand{
Id: an.Id, ID: an.ID,
OrgId: an.OrgId, OrgID: an.OrgID,
} }
err = s.DeleteAlertNotification(context.Background(), &delCmd) err = s.DeleteAlertNotification(context.Background(), &delCmd)
require.NoError(t, err) require.NoError(t, err)
@ -90,14 +90,14 @@ func TestService(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Try to update it with an invalid type. // 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) _, err = s.UpdateAlertNotification(ctx, &updateCmd)
require.Error(t, err) require.Error(t, err)
// Delete the created alert notification. // Delete the created alert notification.
delCmd := models.DeleteAlertNotificationCommand{ delCmd := models.DeleteAlertNotificationCommand{
Id: n.Id, ID: n.ID,
OrgId: n.OrgId, OrgID: n.OrgID,
} }
err = s.DeleteAlertNotification(context.Background(), &delCmd) err = s.DeleteAlertNotification(context.Background(), &delCmd)
require.NoError(t, err) require.NoError(t, err)
@ -114,7 +114,7 @@ func TestService(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Update test notification. // 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) n2, err := s.UpdateAlertNotification(ctx, &updateCmd)
require.NoError(t, err) require.NoError(t, err)
@ -124,8 +124,8 @@ func TestService(t *testing.T) {
// Delete the created alert notification. // Delete the created alert notification.
delCmd := models.DeleteAlertNotificationCommand{ delCmd := models.DeleteAlertNotificationCommand{
Id: n.Id, ID: n.ID,
OrgId: n.OrgId, OrgID: n.OrgID,
} }
err = s.DeleteAlertNotification(context.Background(), &delCmd) err = s.DeleteAlertNotification(context.Background(), &delCmd)
require.NoError(t, err) 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) { t.Run("create alert notification should reject an invalid command", func(t *testing.T) {
uid := strings.Repeat("A", 41) 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"}) 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) uid := strings.Repeat("A", 41)
expectedErr := ValidationError{Reason: "Invalid UID: Must be 40 characters or less"} 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) 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) require.ErrorIs(t, err, expectedErr)
}) })
} }

@ -35,7 +35,7 @@ var timeNow = time.Now
func (ss *sqlStore) DeleteAlertNotification(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error { func (ss *sqlStore) DeleteAlertNotification(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error {
return ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) 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 = ?" 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 { if err != nil {
return err return err
} }
@ -48,7 +48,7 @@ func (ss *sqlStore) DeleteAlertNotification(ctx context.Context, cmd *models.Del
return models.ErrAlertNotificationNotFound 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 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) { func (ss *sqlStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *models.DeleteAlertNotificationWithUidCommand) (err error) {
var res *models.AlertNotification var res *models.AlertNotification
if err = ss.db.WithDbSession(ctx, func(sess *db.Session) error { 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) res, err = getAlertNotificationWithUidInternal(ctx, existingNotification, sess)
return err return err
}); err != nil { }); err != nil {
@ -70,10 +70,10 @@ func (ss *sqlStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *mod
return models.ErrAlertNotificationNotFound return models.ErrAlertNotificationNotFound
} }
cmd.DeletedAlertNotificationId = res.Id cmd.DeletedAlertNotificationID = res.ID
deleteCommand := &models.DeleteAlertNotificationCommand{ deleteCommand := &models.DeleteAlertNotificationCommand{
Id: res.Id, ID: res.ID,
OrgId: res.OrgId, OrgID: res.OrgID,
} }
return ss.DeleteAlertNotification(ctx, deleteCommand) 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) { 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 { if cached, found := ss.cache.Get(cacheKey); found {
return cached.(string), nil 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) { func (ss *sqlStore) GetAllAlertNotifications(ctx context.Context, query *models.GetAllAlertNotificationsQuery) (res []*models.AlertNotification, err error) {
res = make([]*models.AlertNotification, 0) res = make([]*models.AlertNotification, 0)
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error { 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 err
} }
return nil return nil
@ -151,14 +151,14 @@ func (ss *sqlStore) GetAlertNotificationsWithUidToSend(ctx context.Context, quer
`) `)
sql.WriteString(` WHERE alert_notification.org_id = ?`) sql.WriteString(` WHERE alert_notification.org_id = ?`)
params = append(params, query.OrgId) params = append(params, query.OrgID)
sql.WriteString(` AND ((alert_notification.is_default = ?)`) sql.WriteString(` AND ((alert_notification.is_default = ?)`)
params = append(params, ss.db.GetDialect().BooleanStr(true)) params = append(params, ss.db.GetDialect().BooleanStr(true))
if len(query.Uids) > 0 { if len(query.UIDs) > 0 {
sql.WriteString(` OR alert_notification.uid IN (?` + strings.Repeat(",?", len(query.Uids)-1) + ")") sql.WriteString(` OR alert_notification.uid IN (?` + strings.Repeat(",?", len(query.UIDs)-1) + ")")
for _, v := range query.Uids { for _, v := range query.UIDs {
params = append(params, v) params = append(params, v)
} }
} }
@ -179,10 +179,10 @@ func getAlertNotificationUidInternal(ctx context.Context, query *models.GetAlert
`) `)
sql.WriteString(` WHERE alert_notification.org_id = ?`) sql.WriteString(` WHERE alert_notification.org_id = ?`)
params = append(params, query.OrgId) params = append(params, query.OrgID)
sql.WriteString(` AND alert_notification.id = ?`) sql.WriteString(` AND alert_notification.id = ?`)
params = append(params, query.Id) params = append(params, query.ID)
results := make([]string, 0) results := make([]string, 0)
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil { 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 = ?`) 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 != "" { if query.Name != "" {
sql.WriteString(` AND alert_notification.name = ?`) sql.WriteString(` AND alert_notification.name = ?`)
params = append(params, query.Name) params = append(params, query.Name)
} }
if query.Id != 0 { if query.ID != 0 {
sql.WriteString(` AND alert_notification.id = ?`) 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 = ?`) 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) results := make([]*models.AlertNotification, 0)
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil { 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) { 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 { err = ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
if cmd.Uid == "" { if cmd.UID == "" {
uid, uidGenerationErr := generateNewAlertNotificationUid(ctx, sess, cmd.OrgId) uid, uidGenerationErr := generateNewAlertNotificationUid(ctx, sess, cmd.OrgID)
if uidGenerationErr != nil { if uidGenerationErr != nil {
return uidGenerationErr 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 { if notification, err := getAlertNotificationWithUidInternal(ctx, existingQuery, sess); err != nil {
return err return err
} else if notification != nil { } else if notification != nil {
@ -297,7 +297,7 @@ func (ss *sqlStore) CreateAlertNotificationCommand(ctx context.Context, cmd *mod
} }
// check if name exists // 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 { if notification, err := getAlertNotificationInternal(ctx, sameNameQuery, sess); err != nil {
return err return err
} else if notification != nil { } else if notification != nil {
@ -324,8 +324,8 @@ func (ss *sqlStore) CreateAlertNotificationCommand(ctx context.Context, cmd *mod
} }
alertNotification := &models.AlertNotification{ alertNotification := &models.AlertNotification{
Uid: cmd.Uid, UID: cmd.UID,
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
Name: cmd.Name, Name: cmd.Name,
Type: cmd.Type, Type: cmd.Type,
Settings: cmd.Settings, 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) { err = ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) (err error) {
current := models.AlertNotification{} 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 return err
} }
if current.Id == 0 { if current.ID == 0 {
return models.ErrAlertNotificationNotFound return models.ErrAlertNotificationNotFound
} }
// check if name exists // 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) notification, err := getAlertNotificationInternal(ctx, sameNameQuery, sess)
if err != nil { if err != nil {
return err 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) 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.SendReminder = cmd.SendReminder
current.DisableResolveMessage = cmd.DisableResolveMessage current.DisableResolveMessage = cmd.DisableResolveMessage
if cmd.Uid != "" { if cmd.UID != "" {
current.Uid = cmd.Uid current.UID = cmd.UID
} }
if current.SendReminder { 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") 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 return err
} else if affected == 0 { } else if affected == 0 {
return fmt.Errorf("could not update alert notification") 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) { 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 { if err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
res, err = getAlertNotificationWithUidInternal(ctx, getAlertNotificationWithUidQuery, sess) res, err = getAlertNotificationWithUidInternal(ctx, getAlertNotificationWithUidQuery, sess)
@ -449,13 +449,13 @@ func (ss *sqlStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *mod
return nil, models.ErrAlertNotificationNotFound return nil, models.ErrAlertNotificationNotFound
} }
if cmd.NewUid == "" { if cmd.NewUID == "" {
cmd.NewUid = cmd.Uid cmd.NewUID = cmd.UID
} }
updateNotification := &models.UpdateAlertNotificationCommand{ updateNotification := &models.UpdateAlertNotificationCommand{
Id: current.Id, ID: current.ID,
Uid: cmd.NewUid, UID: cmd.NewUID,
Name: cmd.Name, Name: cmd.Name,
Type: cmd.Type, Type: cmd.Type,
SendReminder: cmd.SendReminder, SendReminder: cmd.SendReminder,
@ -465,7 +465,7 @@ func (ss *sqlStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *mod
Settings: cmd.Settings, Settings: cmd.Settings,
SecureSettings: cmd.SecureSettings, SecureSettings: cmd.SecureSettings,
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
} }
return ss.UpdateAlertNotification(ctx, updateNotification) 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 { return ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
version := cmd.Version version := cmd.Version
var current models.AlertNotificationState 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 return err
} }
@ -487,13 +487,13 @@ func (ss *sqlStore) SetAlertNotificationStateToCompleteCommand(ctx context.Conte
WHERE WHERE
id = ?` 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 { if err != nil {
return err return err
} }
if current.Version != version { 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 return nil
@ -517,7 +517,7 @@ func (ss *sqlStore) SetAlertNotificationStateToPendingCommand(ctx context.Contex
newVersion, newVersion,
timeNow().Unix(), timeNow().Unix(),
cmd.AlertRuleStateUpdatedVersion, cmd.AlertRuleStateUpdatedVersion,
cmd.Id, cmd.ID,
cmd.Version, cmd.Version,
cmd.AlertRuleStateUpdatedVersion) cmd.AlertRuleStateUpdatedVersion)
@ -553,9 +553,9 @@ func (ss *sqlStore) GetOrCreateAlertNotificationState(ctx context.Context, cmd *
} }
notificationState := &models.AlertNotificationState{ notificationState := &models.AlertNotificationState{
OrgId: cmd.OrgId, OrgID: cmd.OrgID,
AlertId: cmd.AlertId, AlertID: cmd.AlertID,
NotifierId: cmd.NotifierId, NotifierID: cmd.NotifierID,
State: models.AlertNotificationStateUnknown, State: models.AlertNotificationStateUnknown,
UpdatedAt: timeNow().Unix(), 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) { func getAlertNotificationState(ctx context.Context, sess *db.Session, cmd *models.GetOrCreateNotificationStateQuery, nj *models.AlertNotificationState) (bool, error) {
return sess. return sess.
Where("alert_notification_state.org_id = ?", cmd.OrgId). Where("alert_notification_state.org_id = ?", cmd.OrgID).
Where("alert_notification_state.alert_id = ?", cmd.AlertId). Where("alert_notification_state.alert_id = ?", cmd.AlertID).
Where("alert_notification_state.notifier_id = ?", cmd.NotifierId). Where("alert_notification_state.notifier_id = ?", cmd.NotifierID).
Get(nj) Get(nj)
} }

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

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

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

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

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

Loading…
Cancel
Save