mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
@@ -1,8 +1,34 @@
|
||||
package notifiers
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type NotifierBase struct {
|
||||
Name string
|
||||
Type string
|
||||
Name string
|
||||
Type string
|
||||
SeverityFilter models.AlertSeverityType
|
||||
}
|
||||
|
||||
func NewNotifierBase(name, notifierType string, model *simplejson.Json) NotifierBase {
|
||||
base := NotifierBase{Name: name, Type: notifierType}
|
||||
|
||||
severityFilter := models.AlertSeverityType(model.Get("severityFilter").MustString(""))
|
||||
|
||||
if severityFilter == models.AlertSeverityCritical || severityFilter == models.AlertSeverityWarning {
|
||||
base.SeverityFilter = severityFilter
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
|
||||
func (n *NotifierBase) MatchSeverity(result models.AlertSeverityType) bool {
|
||||
if !n.SeverityFilter.IsValid() {
|
||||
return true
|
||||
}
|
||||
|
||||
return n.SeverityFilter == result
|
||||
}
|
||||
|
||||
func (n *NotifierBase) GetType() string {
|
||||
|
||||
36
pkg/services/alerting/notifiers/base_test.go
Normal file
36
pkg/services/alerting/notifiers/base_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package notifiers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestBaseNotifier(t *testing.T) {
|
||||
Convey("Parsing base notification severity", t, func() {
|
||||
|
||||
Convey("matches", func() {
|
||||
json := `
|
||||
{
|
||||
"severityFilter": "critical"
|
||||
}`
|
||||
|
||||
settingsJSON, _ := simplejson.NewJson([]byte(json))
|
||||
not := NewNotifierBase("ops", "email", settingsJSON)
|
||||
So(not.MatchSeverity(m.AlertSeverityCritical), ShouldBeTrue)
|
||||
})
|
||||
|
||||
Convey("does not match", func() {
|
||||
json := `
|
||||
{
|
||||
"severityFilter": "critical"
|
||||
}`
|
||||
|
||||
settingsJSON, _ := simplejson.NewJson([]byte(json))
|
||||
not := NewNotifierBase("ops", "email", settingsJSON)
|
||||
So(not.MatchSeverity(m.AlertSeverityWarning), ShouldBeFalse)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package notifiers
|
||||
@@ -29,12 +29,9 @@ func NewEmailNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
||||
}
|
||||
|
||||
return &EmailNotifier{
|
||||
NotifierBase: NotifierBase{
|
||||
Name: model.Name,
|
||||
Type: model.Type,
|
||||
},
|
||||
Addresses: strings.Split(addressesString, "\n"),
|
||||
log: log.New("alerting.notifier.email"),
|
||||
NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
|
||||
Addresses: strings.Split(addressesString, "\n"),
|
||||
log: log.New("alerting.notifier.email"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -23,12 +23,9 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
||||
}
|
||||
|
||||
return &SlackNotifier{
|
||||
NotifierBase: NotifierBase{
|
||||
Name: model.Name,
|
||||
Type: model.Type,
|
||||
},
|
||||
Url: url,
|
||||
log: log.New("alerting.notifier.slack"),
|
||||
NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
|
||||
Url: url,
|
||||
log: log.New("alerting.notifier.slack"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -20,14 +20,11 @@ func NewWebHookNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
||||
}
|
||||
|
||||
return &WebhookNotifier{
|
||||
NotifierBase: NotifierBase{
|
||||
Name: model.Name,
|
||||
Type: model.Type,
|
||||
},
|
||||
Url: url,
|
||||
User: model.Settings.Get("user").MustString(),
|
||||
Password: model.Settings.Get("password").MustString(),
|
||||
log: log.New("alerting.notifier.webhook"),
|
||||
NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
|
||||
Url: url,
|
||||
User: model.Settings.Get("user").MustString(),
|
||||
Password: model.Settings.Get("password").MustString(),
|
||||
log: log.New("alerting.notifier.webhook"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user