mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
Alerting: Improve receiver initialisation errors (#36814)
* Alerting: Improve receiver initialisation errors
This commit is contained in:
parent
0cfc135c4b
commit
f308ba91e3
@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/prometheus/alertmanager/template"
|
"github.com/prometheus/alertmanager/template"
|
||||||
"github.com/prometheus/alertmanager/types"
|
"github.com/prometheus/alertmanager/types"
|
||||||
@ -18,12 +17,12 @@ import (
|
|||||||
// NewAlertmanagerNotifier returns a new Alertmanager notifier.
|
// NewAlertmanagerNotifier returns a new Alertmanager notifier.
|
||||||
func NewAlertmanagerNotifier(model *NotificationChannelConfig, t *template.Template) (*AlertmanagerNotifier, error) {
|
func NewAlertmanagerNotifier(model *NotificationChannelConfig, t *template.Template) (*AlertmanagerNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No settings supplied"}
|
return nil, receiverInitError{Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
urlStr := model.Settings.Get("url").MustString()
|
urlStr := model.Settings.Get("url").MustString()
|
||||||
if urlStr == "" {
|
if urlStr == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
return nil, receiverInitError{Reason: "could not find url property in settings", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
var urls []*url.URL
|
var urls []*url.URL
|
||||||
@ -36,7 +35,7 @@ func NewAlertmanagerNotifier(model *NotificationChannelConfig, t *template.Templ
|
|||||||
uS = strings.TrimSuffix(uS, "/") + "/api/v1/alerts"
|
uS = strings.TrimSuffix(uS, "/") + "/api/v1/alerts"
|
||||||
u, err := url.Parse(uS)
|
u, err := url.Parse(uS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, alerting.ValidationError{Reason: "Invalid url property in settings"}
|
return nil, receiverInitError{Reason: "invalid url property in settings", Cfg: *model, Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
urls = append(urls, u)
|
urls = append(urls, u)
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAlertmanagerNotifier(t *testing.T) {
|
func TestAlertmanagerNotifier(t *testing.T) {
|
||||||
@ -27,8 +26,8 @@ func TestAlertmanagerNotifier(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
receiverName string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Default config with one alert",
|
name: "Default config with one alert",
|
||||||
@ -41,16 +40,30 @@ func TestAlertmanagerNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
receiverName: "Alertmanager",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Default config with one alert with empty receiver name",
|
||||||
|
settings: `{"url": "https://alertmanager.com"}`,
|
||||||
|
alerts: []*types.Alert{
|
||||||
|
{
|
||||||
|
Alert: model.Alert{
|
||||||
|
Labels: model.LabelSet{"__alert_rule_uid__": "rule uid", "alertname": "alert1", "lbl1": "val1"},
|
||||||
|
Annotations: model.LabelSet{"ann1": "annv1"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing: missing URL",
|
name: "Error in initing: missing URL",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find url property in settings"},
|
expInitError: `failed to validate receiver of type "alertmanager": could not find url property in settings`,
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing: invalid URL",
|
name: "Error in initing: invalid URL",
|
||||||
settings: `{
|
settings: `{
|
||||||
"url": "://alertmanager.com"
|
"url": "://alertmanager.com"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Invalid url property in settings"},
|
expInitError: `failed to validate receiver "Alertmanager" of type "alertmanager": invalid url property in settings: parse "://alertmanager.com/api/v1/alerts": missing protocol scheme`,
|
||||||
|
receiverName: "Alertmanager",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
@ -59,15 +72,14 @@ func TestAlertmanagerNotifier(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
m := &NotificationChannelConfig{
|
m := &NotificationChannelConfig{
|
||||||
Name: "Alertmanager",
|
Name: c.receiverName,
|
||||||
Type: "alertmanager",
|
Type: "alertmanager",
|
||||||
Settings: settingsJSON,
|
Settings: settingsJSON,
|
||||||
}
|
}
|
||||||
|
|
||||||
sn, err := NewAlertmanagerNotifier(m, tmpl)
|
sn, err := NewAlertmanagerNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
require.Equal(t, c.expInitError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -85,12 +97,6 @@ func TestAlertmanagerNotifier(t *testing.T) {
|
|||||||
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
||||||
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
||||||
ok, err := sn.Notify(ctx, c.alerts...)
|
ok, err := sn.Notify(ctx, c.alerts...)
|
||||||
if c.expMsgError != nil {
|
|
||||||
require.False(t, ok)
|
|
||||||
require.Error(t, err)
|
|
||||||
require.Equal(t, c.expMsgError, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,12 +20,12 @@ const defaultDingdingMsgType = "link"
|
|||||||
// NewDingDingNotifier is the constructor for the Dingding notifier
|
// NewDingDingNotifier is the constructor for the Dingding notifier
|
||||||
func NewDingDingNotifier(model *NotificationChannelConfig, t *template.Template) (*DingDingNotifier, error) {
|
func NewDingDingNotifier(model *NotificationChannelConfig, t *template.Template) (*DingDingNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
url := model.Settings.Get("url").MustString()
|
url := model.Settings.Get("url").MustString()
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
return nil, receiverInitError{Reason: "could not find url property in settings", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
msgType := model.Settings.Get("msgType").MustString(defaultDingdingMsgType)
|
msgType := model.Settings.Get("msgType").MustString(defaultDingdingMsgType)
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDingdingNotifier(t *testing.T) {
|
func TestDingdingNotifier(t *testing.T) {
|
||||||
@ -29,7 +28,7 @@ func TestDingdingNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg map[string]interface{}
|
expMsg map[string]interface{}
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -51,8 +50,7 @@ func TestDingdingNotifier(t *testing.T) {
|
|||||||
"title": "[FIRING:1] (val1)",
|
"title": "[FIRING:1] (val1)",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -82,12 +80,11 @@ func TestDingdingNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"msgtype": "actionCard",
|
"msgtype": "actionCard",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find url property in settings"},
|
expInitError: `failed to validate receiver "dingding_testing" of type "dingding": could not find url property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,9 +100,8 @@ func TestDingdingNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewDingDingNotifier(m, tmpl)
|
pn, err := NewDingDingNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
@ -29,14 +28,14 @@ type DiscordNotifier struct {
|
|||||||
|
|
||||||
func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template) (*DiscordNotifier, error) {
|
func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template) (*DiscordNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
avatarURL := model.Settings.Get("avatar_url").MustString()
|
avatarURL := model.Settings.Get("avatar_url").MustString()
|
||||||
|
|
||||||
discordURL := model.Settings.Get("url").MustString()
|
discordURL := model.Settings.Get("url").MustString()
|
||||||
if discordURL == "" {
|
if discordURL == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"}
|
return nil, receiverInitError{Reason: "could not find webhook url property in settings", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
content := model.Settings.Get("message").MustString(`{{ template "default.message" . }}`)
|
content := model.Settings.Get("message").MustString(`{{ template "default.message" . }}`)
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDiscordNotifier(t *testing.T) {
|
func TestDiscordNotifier(t *testing.T) {
|
||||||
@ -29,7 +28,7 @@ func TestDiscordNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg map[string]interface{}
|
expMsg map[string]interface{}
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -57,8 +56,7 @@ func TestDiscordNotifier(t *testing.T) {
|
|||||||
}},
|
}},
|
||||||
"username": "Grafana",
|
"username": "Grafana",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
@ -95,13 +93,12 @@ func TestDiscordNotifier(t *testing.T) {
|
|||||||
}},
|
}},
|
||||||
"username": "Grafana",
|
"username": "Grafana",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Error in initialization",
|
name: "Error in initialization",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find webhook url property in settings"},
|
expInitError: `failed to validate receiver "discord_testing" of type "discord": could not find webhook url property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,9 +114,8 @@ func TestDiscordNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dn, err := NewDiscordNotifier(m, tmpl)
|
dn, err := NewDiscordNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
)
|
)
|
||||||
@ -31,14 +30,14 @@ type EmailNotifier struct {
|
|||||||
// for the EmailNotifier.
|
// for the EmailNotifier.
|
||||||
func NewEmailNotifier(model *NotificationChannelConfig, t *template.Template) (*EmailNotifier, error) {
|
func NewEmailNotifier(model *NotificationChannelConfig, t *template.Template) (*EmailNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Reason: "no settings supplied", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
addressesString := model.Settings.Get("addresses").MustString()
|
addressesString := model.Settings.Get("addresses").MustString()
|
||||||
singleEmail := model.Settings.Get("singleEmail").MustBool(false)
|
singleEmail := model.Settings.Get("singleEmail").MustBool(false)
|
||||||
|
|
||||||
if addressesString == "" {
|
if addressesString == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find addresses in settings"}
|
return nil, receiverInitError{Reason: "could not find addresses in settings", Cfg: *model}
|
||||||
}
|
}
|
||||||
|
|
||||||
// split addresses with a few different ways
|
// split addresses with a few different ways
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
@ -29,7 +28,7 @@ type GoogleChatNotifier struct {
|
|||||||
func NewGoogleChatNotifier(model *NotificationChannelConfig, t *template.Template) (*GoogleChatNotifier, error) {
|
func NewGoogleChatNotifier(model *NotificationChannelConfig, t *template.Template) (*GoogleChatNotifier, error) {
|
||||||
url := model.Settings.Get("url").MustString()
|
url := model.Settings.Get("url").MustString()
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find url property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &GoogleChatNotifier{
|
return &GoogleChatNotifier{
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,7 +30,7 @@ func TestGoogleChatNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg *outerStruct
|
expMsg *outerStruct
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -87,8 +86,7 @@ func TestGoogleChatNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Multiple alerts",
|
name: "Multiple alerts",
|
||||||
settings: `{"url": "http://localhost"}`,
|
settings: `{"url": "http://localhost"}`,
|
||||||
@ -146,12 +144,11 @@ func TestGoogleChatNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find url property in settings"},
|
expInitError: `failed to validate receiver "googlechat_testing" of type "googlechat": could not find url property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,9 +164,9 @@ func TestGoogleChatNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewGoogleChatNotifier(m, tmpl)
|
pn, err := NewGoogleChatNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,11 +30,11 @@ type KafkaNotifier struct {
|
|||||||
func NewKafkaNotifier(model *NotificationChannelConfig, t *template.Template) (*KafkaNotifier, error) {
|
func NewKafkaNotifier(model *NotificationChannelConfig, t *template.Template) (*KafkaNotifier, error) {
|
||||||
endpoint := model.Settings.Get("kafkaRestProxy").MustString()
|
endpoint := model.Settings.Get("kafkaRestProxy").MustString()
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find kafka rest proxy endpoint property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find kafka rest proxy endpoint property in settings"}
|
||||||
}
|
}
|
||||||
topic := model.Settings.Get("kafkaTopic").MustString()
|
topic := model.Settings.Get("kafkaTopic").MustString()
|
||||||
if topic == "" {
|
if topic == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find kafka topic property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find kafka topic property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &KafkaNotifier{
|
return &KafkaNotifier{
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKafkaNotifier(t *testing.T) {
|
func TestKafkaNotifier(t *testing.T) {
|
||||||
@ -28,7 +27,7 @@ func TestKafkaNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expUrl, expMsg string
|
expUrl, expMsg string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -60,8 +59,7 @@ func TestKafkaNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`,
|
}`,
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Multiple alerts",
|
name: "Multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -96,16 +94,15 @@ func TestKafkaNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`,
|
}`,
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Endpoint missing",
|
name: "Endpoint missing",
|
||||||
settings: `{"kafkaTopic": "sometopic"}`,
|
settings: `{"kafkaTopic": "sometopic"}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find kafka rest proxy endpoint property in settings"},
|
expInitError: `failed to validate receiver "kafka_testing" of type "kafka": could not find kafka rest proxy endpoint property in settings`,
|
||||||
}, {
|
}, {
|
||||||
name: "Topic missing",
|
name: "Topic missing",
|
||||||
settings: `{"kafkaRestProxy": "http://localhost"}`,
|
settings: `{"kafkaRestProxy": "http://localhost"}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find kafka topic property in settings"},
|
expInitError: `failed to validate receiver "kafka_testing" of type "kafka": could not find kafka topic property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,9 +118,9 @@ func TestKafkaNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewKafkaNotifier(m, tmpl)
|
pn, err := NewKafkaNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,7 +23,7 @@ var (
|
|||||||
func NewLineNotifier(model *NotificationChannelConfig, t *template.Template) (*LineNotifier, error) {
|
func NewLineNotifier(model *NotificationChannelConfig, t *template.Template) (*LineNotifier, error) {
|
||||||
token := model.DecryptedValue("token", model.Settings.Get("token").MustString())
|
token := model.DecryptedValue("token", model.Settings.Get("token").MustString())
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find token in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find token in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &LineNotifier{
|
return &LineNotifier{
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLineNotifier(t *testing.T) {
|
func TestLineNotifier(t *testing.T) {
|
||||||
@ -29,7 +28,7 @@ func TestLineNotifier(t *testing.T) {
|
|||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expHeaders map[string]string
|
expHeaders map[string]string
|
||||||
expMsg string
|
expMsg string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -47,9 +46,8 @@ func TestLineNotifier(t *testing.T) {
|
|||||||
"Authorization": "Bearer sometoken",
|
"Authorization": "Bearer sometoken",
|
||||||
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
||||||
},
|
},
|
||||||
expMsg: "message=%5BFIRING%3A1%5D++%28val1%29%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0ADashboard%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%0APanel%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%3FviewPanel%3Defgh%0A",
|
expMsg: "message=%5BFIRING%3A1%5D++%28val1%29%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0ADashboard%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%0APanel%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%3FviewPanel%3Defgh%0A",
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Multiple alerts",
|
name: "Multiple alerts",
|
||||||
settings: `{"token": "sometoken"}`,
|
settings: `{"token": "sometoken"}`,
|
||||||
@ -70,13 +68,12 @@ func TestLineNotifier(t *testing.T) {
|
|||||||
"Authorization": "Bearer sometoken",
|
"Authorization": "Bearer sometoken",
|
||||||
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
||||||
},
|
},
|
||||||
expMsg: "message=%5BFIRING%3A2%5D++%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval2%0A",
|
expMsg: "message=%5BFIRING%3A2%5D++%0Ahttp%3A%2Flocalhost%2Falerting%2Flist%0A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval2%0A",
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Token missing",
|
name: "Token missing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find token in settings"},
|
expInitError: `failed to validate receiver "line_testing" of type "line": could not find token in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,9 +89,9 @@ func TestLineNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewLineNotifier(m, tmpl)
|
pn, err := NewLineNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -16,7 +16,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -50,7 +49,7 @@ func NewOpsgenieNotifier(model *NotificationChannelConfig, t *template.Template)
|
|||||||
apiKey := model.DecryptedValue("apiKey", model.Settings.Get("apiKey").MustString())
|
apiKey := model.DecryptedValue("apiKey", model.Settings.Get("apiKey").MustString())
|
||||||
apiURL := model.Settings.Get("apiUrl").MustString()
|
apiURL := model.Settings.Get("apiUrl").MustString()
|
||||||
if apiKey == "" {
|
if apiKey == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find api key property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find api key property in settings"}
|
||||||
}
|
}
|
||||||
if apiURL == "" {
|
if apiURL == "" {
|
||||||
apiURL = OpsgenieAlertURL
|
apiURL = OpsgenieAlertURL
|
||||||
@ -58,8 +57,8 @@ func NewOpsgenieNotifier(model *NotificationChannelConfig, t *template.Template)
|
|||||||
|
|
||||||
sendTagsAs := model.Settings.Get("sendTagsAs").MustString(OpsgenieSendTags)
|
sendTagsAs := model.Settings.Get("sendTagsAs").MustString(OpsgenieSendTags)
|
||||||
if sendTagsAs != OpsgenieSendTags && sendTagsAs != OpsgenieSendDetails && sendTagsAs != OpsgenieSendBoth {
|
if sendTagsAs != OpsgenieSendTags && sendTagsAs != OpsgenieSendDetails && sendTagsAs != OpsgenieSendBoth {
|
||||||
return nil, alerting.ValidationError{
|
return nil, receiverInitError{Cfg: *model,
|
||||||
Reason: fmt.Sprintf("Invalid value for sendTagsAs: %q", sendTagsAs),
|
Reason: fmt.Sprintf("invalid value for sendTagsAs: %q", sendTagsAs),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOpsgenieNotifier(t *testing.T) {
|
func TestOpsgenieNotifier(t *testing.T) {
|
||||||
@ -29,7 +28,7 @@ func TestOpsgenieNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg string
|
expMsg string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -136,8 +135,7 @@ func TestOpsgenieNotifier(t *testing.T) {
|
|||||||
"source": "Grafana",
|
"source": "Grafana",
|
||||||
"tags": ["alertname:alert1"]
|
"tags": ["alertname:alert1"]
|
||||||
}`,
|
}`,
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Resolved is not sent when auto close is false",
|
name: "Resolved is not sent when auto close is false",
|
||||||
@ -155,7 +153,7 @@ func TestOpsgenieNotifier(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Error when incorrect settings",
|
name: "Error when incorrect settings",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find api key property in settings"},
|
expInitError: `failed to validate receiver "opsgenie_testing" of type "opsgenie": could not find api key property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,9 +169,9 @@ func TestOpsgenieNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewOpsgenieNotifier(m, tmpl)
|
pn, err := NewOpsgenieNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,12 +44,12 @@ type PagerdutyNotifier struct {
|
|||||||
// NewPagerdutyNotifier is the constructor for the PagerDuty notifier
|
// NewPagerdutyNotifier is the constructor for the PagerDuty notifier
|
||||||
func NewPagerdutyNotifier(model *NotificationChannelConfig, t *template.Template) (*PagerdutyNotifier, error) {
|
func NewPagerdutyNotifier(model *NotificationChannelConfig, t *template.Template) (*PagerdutyNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
key := model.DecryptedValue("integrationKey", model.Settings.Get("integrationKey").MustString())
|
key := model.DecryptedValue("integrationKey", model.Settings.Get("integrationKey").MustString())
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find integration key property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &PagerdutyNotifier{
|
return &PagerdutyNotifier{
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPagerdutyNotifier(t *testing.T) {
|
func TestPagerdutyNotifier(t *testing.T) {
|
||||||
@ -33,7 +32,7 @@ func TestPagerdutyNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg *pagerDutyMessage
|
expMsg *pagerDutyMessage
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -70,8 +69,7 @@ func TestPagerdutyNotifier(t *testing.T) {
|
|||||||
ClientURL: "http://localhost",
|
ClientURL: "http://localhost",
|
||||||
Links: []pagerDutyLink{{HRef: "http://localhost", Text: "External URL"}},
|
Links: []pagerDutyLink{{HRef: "http://localhost", Text: "External URL"}},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -117,12 +115,11 @@ func TestPagerdutyNotifier(t *testing.T) {
|
|||||||
ClientURL: "http://localhost",
|
ClientURL: "http://localhost",
|
||||||
Links: []pagerDutyLink{{HRef: "http://localhost", Text: "External URL"}},
|
Links: []pagerDutyLink{{HRef: "http://localhost", Text: "External URL"}},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find integration key property in settings"},
|
expInitError: `failed to validate receiver "pageduty_testing" of type "pagerduty": could not find integration key property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,9 +135,9 @@ func TestPagerdutyNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewPagerdutyNotifier(m, tmpl)
|
pn, err := NewPagerdutyNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/prometheus/alertmanager/template"
|
"github.com/prometheus/alertmanager/template"
|
||||||
"github.com/prometheus/alertmanager/types"
|
"github.com/prometheus/alertmanager/types"
|
||||||
@ -43,7 +42,7 @@ type PushoverNotifier struct {
|
|||||||
// NewSlackNotifier is the constructor for the Slack notifier
|
// NewSlackNotifier is the constructor for the Slack notifier
|
||||||
func NewPushoverNotifier(model *NotificationChannelConfig, t *template.Template) (*PushoverNotifier, error) {
|
func NewPushoverNotifier(model *NotificationChannelConfig, t *template.Template) (*PushoverNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No settings supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
userKey := model.DecryptedValue("userKey", model.Settings.Get("userKey").MustString())
|
userKey := model.DecryptedValue("userKey", model.Settings.Get("userKey").MustString())
|
||||||
@ -64,10 +63,10 @@ func NewPushoverNotifier(model *NotificationChannelConfig, t *template.Template)
|
|||||||
uploadImage := model.Settings.Get("uploadImage").MustBool(true)
|
uploadImage := model.Settings.Get("uploadImage").MustBool(true)
|
||||||
|
|
||||||
if userKey == "" {
|
if userKey == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "user key not found"}
|
return nil, receiverInitError{Cfg: *model, Reason: "user key not found"}
|
||||||
}
|
}
|
||||||
if APIToken == "" {
|
if APIToken == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "API token not found"}
|
return nil, receiverInitError{Cfg: *model, Reason: "API token not found"}
|
||||||
}
|
}
|
||||||
return &PushoverNotifier{
|
return &PushoverNotifier{
|
||||||
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
|
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
"github.com/prometheus/alertmanager/notify"
|
"github.com/prometheus/alertmanager/notify"
|
||||||
"github.com/prometheus/alertmanager/types"
|
"github.com/prometheus/alertmanager/types"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
@ -34,7 +33,7 @@ func TestPushoverNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg map[string]string
|
expMsg map[string]string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -62,8 +61,7 @@ func TestPushoverNotifier(t *testing.T) {
|
|||||||
"message": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
"message": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
||||||
"html": "1",
|
"html": "1",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
@ -106,21 +104,20 @@ func TestPushoverNotifier(t *testing.T) {
|
|||||||
"expire": "86400",
|
"expire": "86400",
|
||||||
"device": "device",
|
"device": "device",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Missing user key",
|
name: "Missing user key",
|
||||||
settings: `{
|
settings: `{
|
||||||
"apiToken": "<apiToken>"
|
"apiToken": "<apiToken>"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "user key not found"},
|
expInitError: `failed to validate receiver "pushover_testing" of type "pushover": user key not found`,
|
||||||
}, {
|
}, {
|
||||||
name: "Missing api key",
|
name: "Missing api key",
|
||||||
settings: `{
|
settings: `{
|
||||||
"userKey": "<userKey>"
|
"userKey": "<userKey>"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "API token not found"},
|
expInitError: `failed to validate receiver "pushover_testing" of type "pushover": API token not found`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,9 +142,9 @@ func TestPushoverNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewPushoverNotifier(m, tmpl)
|
pn, err := NewPushoverNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/prometheus/alertmanager/template"
|
"github.com/prometheus/alertmanager/template"
|
||||||
"github.com/prometheus/alertmanager/types"
|
"github.com/prometheus/alertmanager/types"
|
||||||
@ -34,17 +33,17 @@ type SensuGoNotifier struct {
|
|||||||
// NewSensuGoNotifier is the constructor for the SensuGo notifier
|
// NewSensuGoNotifier is the constructor for the SensuGo notifier
|
||||||
func NewSensuGoNotifier(model *NotificationChannelConfig, t *template.Template) (*SensuGoNotifier, error) {
|
func NewSensuGoNotifier(model *NotificationChannelConfig, t *template.Template) (*SensuGoNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No settings supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
url := model.Settings.Get("url").MustString()
|
url := model.Settings.Get("url").MustString()
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find URL property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find URL property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
apikey := model.DecryptedValue("apikey", model.Settings.Get("apikey").MustString())
|
apikey := model.DecryptedValue("apikey", model.Settings.Get("apikey").MustString())
|
||||||
if apikey == "" {
|
if apikey == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find the API key property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find the API key property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SensuGoNotifier{
|
return &SensuGoNotifier{
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSensuGoNotifier(t *testing.T) {
|
func TestSensuGoNotifier(t *testing.T) {
|
||||||
@ -30,7 +29,7 @@ func TestSensuGoNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg map[string]interface{}
|
expMsg map[string]interface{}
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -66,8 +65,7 @@ func TestSensuGoNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"ruleUrl": "http://localhost/alerting/list",
|
"ruleUrl": "http://localhost/alerting/list",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -114,20 +112,19 @@ func TestSensuGoNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"ruleUrl": "http://localhost/alerting/list",
|
"ruleUrl": "http://localhost/alerting/list",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing: missing URL",
|
name: "Error in initing: missing URL",
|
||||||
settings: `{
|
settings: `{
|
||||||
"apikey": "<apikey>"
|
"apikey": "<apikey>"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find URL property in settings"},
|
expInitError: `failed to validate receiver "Sensu Go" of type "sensugo": could not find URL property in settings`,
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing: missing API key",
|
name: "Error in initing: missing API key",
|
||||||
settings: `{
|
settings: `{
|
||||||
"url": "http://sensu-api.local:8080"
|
"url": "http://sensu-api.local:8080"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find the API key property in settings"},
|
expInitError: `failed to validate receiver "Sensu Go" of type "sensugo": could not find the API key property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,9 +140,9 @@ func TestSensuGoNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sn, err := NewSensuGoNotifier(m, tmpl)
|
sn, err := NewSensuGoNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
@ -52,7 +51,7 @@ var SlackAPIEndpoint = "https://slack.com/api/chat.postMessage"
|
|||||||
// NewSlackNotifier is the constructor for the Slack notifier
|
// NewSlackNotifier is the constructor for the Slack notifier
|
||||||
func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template) (*SlackNotifier, error) {
|
func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template) (*SlackNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
slackURL := model.DecryptedValue("url", model.Settings.Get("url").MustString())
|
slackURL := model.DecryptedValue("url", model.Settings.Get("url").MustString())
|
||||||
@ -61,24 +60,24 @@ func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template) (*
|
|||||||
}
|
}
|
||||||
apiURL, err := url.Parse(slackURL)
|
apiURL, err := url.Parse(slackURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, alerting.ValidationError{Reason: fmt.Sprintf("invalid URL %q: %s", slackURL, err)}
|
return nil, receiverInitError{Cfg: *model, Reason: fmt.Sprintf("invalid URL %q", slackURL), Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
recipient := strings.TrimSpace(model.Settings.Get("recipient").MustString())
|
recipient := strings.TrimSpace(model.Settings.Get("recipient").MustString())
|
||||||
if recipient != "" {
|
if recipient != "" {
|
||||||
if !reRecipient.MatchString(recipient) {
|
if !reRecipient.MatchString(recipient) {
|
||||||
return nil, alerting.ValidationError{Reason: fmt.Sprintf("recipient on invalid format: %q", recipient)}
|
return nil, receiverInitError{Cfg: *model, Reason: fmt.Sprintf("recipient on invalid format: %q", recipient)}
|
||||||
}
|
}
|
||||||
} else if apiURL.String() == SlackAPIEndpoint {
|
} else if apiURL.String() == SlackAPIEndpoint {
|
||||||
return nil, alerting.ValidationError{
|
return nil, receiverInitError{Cfg: *model,
|
||||||
Reason: "recipient must be specified when using the Slack chat API",
|
Reason: "recipient must be specified when using the Slack chat API",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mentionChannel := model.Settings.Get("mentionChannel").MustString()
|
mentionChannel := model.Settings.Get("mentionChannel").MustString()
|
||||||
if mentionChannel != "" && mentionChannel != "here" && mentionChannel != "channel" {
|
if mentionChannel != "" && mentionChannel != "here" && mentionChannel != "channel" {
|
||||||
return nil, alerting.ValidationError{
|
return nil, receiverInitError{Cfg: *model,
|
||||||
Reason: fmt.Sprintf("Invalid value for mentionChannel: %q", mentionChannel),
|
Reason: fmt.Sprintf("invalid value for mentionChannel: %q", mentionChannel),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +101,7 @@ func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template) (*
|
|||||||
|
|
||||||
token := model.DecryptedValue("token", model.Settings.Get("token").MustString())
|
token := model.DecryptedValue("token", model.Settings.Get("token").MustString())
|
||||||
if token == "" && apiURL.String() == SlackAPIEndpoint {
|
if token == "" && apiURL.String() == SlackAPIEndpoint {
|
||||||
return nil, alerting.ValidationError{
|
return nil, receiverInitError{Cfg: *model,
|
||||||
Reason: "token must be specified when using the Slack chat API",
|
Reason: "token must be specified when using the Slack chat API",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSlackNotifier(t *testing.T) {
|
func TestSlackNotifier(t *testing.T) {
|
||||||
@ -30,7 +29,7 @@ func TestSlackNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg *slackMessage
|
expMsg *slackMessage
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -66,8 +65,7 @@ func TestSlackNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Correct config with webhook",
|
name: "Correct config with webhook",
|
||||||
@ -102,8 +100,7 @@ func TestSlackNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Correct config with multiple alerts and template",
|
name: "Correct config with multiple alerts and template",
|
||||||
@ -145,20 +142,19 @@ func TestSlackNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Missing token",
|
name: "Missing token",
|
||||||
settings: `{
|
settings: `{
|
||||||
"recipient": "#testchannel"
|
"recipient": "#testchannel"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "token must be specified when using the Slack chat API"},
|
expInitError: `failed to validate receiver "slack_testing" of type "slack": token must be specified when using the Slack chat API`,
|
||||||
}, {
|
}, {
|
||||||
name: "Missing recipient",
|
name: "Missing recipient",
|
||||||
settings: `{
|
settings: `{
|
||||||
"token": "1234"
|
"token": "1234"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "recipient must be specified when using the Slack chat API"},
|
expInitError: `failed to validate receiver "slack_testing" of type "slack": recipient must be specified when using the Slack chat API`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,9 +170,9 @@ func TestSlackNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewSlackNotifier(m, tmpl)
|
pn, err := NewSlackNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,12 +27,12 @@ type TeamsNotifier struct {
|
|||||||
// NewTeamsNotifier is the constructor for Teams notifier.
|
// NewTeamsNotifier is the constructor for Teams notifier.
|
||||||
func NewTeamsNotifier(model *NotificationChannelConfig, t *template.Template) (*TeamsNotifier, error) {
|
func NewTeamsNotifier(model *NotificationChannelConfig, t *template.Template) (*TeamsNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
u := model.Settings.Get("url").MustString()
|
u := model.Settings.Get("url").MustString()
|
||||||
if u == "" {
|
if u == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find url property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TeamsNotifier{
|
return &TeamsNotifier{
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTeamsNotifier(t *testing.T) {
|
func TestTeamsNotifier(t *testing.T) {
|
||||||
@ -29,7 +28,7 @@ func TestTeamsNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg map[string]interface{}
|
expMsg map[string]interface{}
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -64,8 +63,7 @@ func TestTeamsNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -106,12 +104,11 @@ func TestTeamsNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find url property in settings"},
|
expInitError: `failed to validate receiver "teams_testing" of type "teams": could not find url property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,9 +124,9 @@ func TestTeamsNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewTeamsNotifier(m, tmpl)
|
pn, err := NewTeamsNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ type TelegramNotifier struct {
|
|||||||
// NewTelegramNotifier is the constructor for the Telegram notifier
|
// NewTelegramNotifier is the constructor for the Telegram notifier
|
||||||
func NewTelegramNotifier(model *NotificationChannelConfig, t *template.Template) (*TelegramNotifier, error) {
|
func NewTelegramNotifier(model *NotificationChannelConfig, t *template.Template) (*TelegramNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
botToken := model.DecryptedValue("bottoken", model.Settings.Get("bottoken").MustString())
|
botToken := model.DecryptedValue("bottoken", model.Settings.Get("bottoken").MustString())
|
||||||
@ -42,11 +41,11 @@ func NewTelegramNotifier(model *NotificationChannelConfig, t *template.Template)
|
|||||||
message := model.Settings.Get("message").MustString(`{{ template "default.message" . }}`)
|
message := model.Settings.Get("message").MustString(`{{ template "default.message" . }}`)
|
||||||
|
|
||||||
if botToken == "" {
|
if botToken == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find Bot Token in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find Bot Token in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
if chatID == "" {
|
if chatID == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find Chat Id in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find Chat Id in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TelegramNotifier{
|
return &TelegramNotifier{
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTelegramNotifier(t *testing.T) {
|
func TestTelegramNotifier(t *testing.T) {
|
||||||
@ -26,7 +25,7 @@ func TestTelegramNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg map[string]string
|
expMsg map[string]string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -49,8 +48,7 @@ func TestTelegramNotifier(t *testing.T) {
|
|||||||
"parse_mode": "html",
|
"parse_mode": "html",
|
||||||
"text": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
"text": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Custom template with multiple alerts",
|
name: "Custom template with multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -77,12 +75,11 @@ func TestTelegramNotifier(t *testing.T) {
|
|||||||
"parse_mode": "html",
|
"parse_mode": "html",
|
||||||
"text": "__Custom Firing__\n2 Firing\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n",
|
"text": "__Custom Firing__\n2 Firing\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: a URL\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find Bot Token in settings"},
|
expInitError: `failed to validate receiver "telegram_testing" of type "telegram": could not find Bot Token in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,9 +95,9 @@ func TestTelegramNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewTelegramNotifier(m, tmpl)
|
pn, err := NewTelegramNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ type ThreemaNotifier struct {
|
|||||||
// NewThreemaNotifier is the constructor for the Threema notifier
|
// NewThreemaNotifier is the constructor for the Threema notifier
|
||||||
func NewThreemaNotifier(model *NotificationChannelConfig, t *template.Template) (*ThreemaNotifier, error) {
|
func NewThreemaNotifier(model *NotificationChannelConfig, t *template.Template) (*ThreemaNotifier, error) {
|
||||||
if model.Settings == nil {
|
if model.Settings == nil {
|
||||||
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
|
return nil, receiverInitError{Cfg: *model, Reason: "no settings supplied"}
|
||||||
}
|
}
|
||||||
|
|
||||||
gatewayID := model.Settings.Get("gateway_id").MustString()
|
gatewayID := model.Settings.Get("gateway_id").MustString()
|
||||||
@ -45,22 +44,22 @@ func NewThreemaNotifier(model *NotificationChannelConfig, t *template.Template)
|
|||||||
|
|
||||||
// Validation
|
// Validation
|
||||||
if gatewayID == "" {
|
if gatewayID == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find Threema Gateway ID in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find Threema Gateway ID in settings"}
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(gatewayID, "*") {
|
if !strings.HasPrefix(gatewayID, "*") {
|
||||||
return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must start with a *"}
|
return nil, receiverInitError{Cfg: *model, Reason: "invalid Threema Gateway ID: Must start with a *"}
|
||||||
}
|
}
|
||||||
if len(gatewayID) != 8 {
|
if len(gatewayID) != 8 {
|
||||||
return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must be 8 characters long"}
|
return nil, receiverInitError{Cfg: *model, Reason: "invalid Threema Gateway ID: Must be 8 characters long"}
|
||||||
}
|
}
|
||||||
if recipientID == "" {
|
if recipientID == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find Threema Recipient ID in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find Threema Recipient ID in settings"}
|
||||||
}
|
}
|
||||||
if len(recipientID) != 8 {
|
if len(recipientID) != 8 {
|
||||||
return nil, alerting.ValidationError{Reason: "Invalid Threema Recipient ID: Must be 8 characters long"}
|
return nil, receiverInitError{Cfg: *model, Reason: "invalid Threema Recipient ID: Must be 8 characters long"}
|
||||||
}
|
}
|
||||||
if apiSecret == "" {
|
if apiSecret == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find Threema API secret in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find Threema API secret in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ThreemaNotifier{
|
return &ThreemaNotifier{
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestThreemaNotifier(t *testing.T) {
|
func TestThreemaNotifier(t *testing.T) {
|
||||||
@ -28,7 +27,7 @@ func TestThreemaNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg string
|
expMsg string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -46,9 +45,8 @@ func TestThreemaNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A1%5D++%28val1%29%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0ADashboard%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%0APanel%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%3FviewPanel%3Defgh%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321",
|
expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A1%5D++%28val1%29%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0ADashboard%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%0APanel%3A+http%3A%2F%2Flocalhost%2Fd%2Fabcd%3FviewPanel%3Defgh%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321",
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Multiple alerts",
|
name: "Multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -69,9 +67,8 @@ func TestThreemaNotifier(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A2%5D++%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval2%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321",
|
expMsg: "from=%2A1234567&secret=supersecret&text=%E2%9A%A0%EF%B8%8F+%5BFIRING%3A2%5D++%0A%0A%2AMessage%3A%2A%0A%2A%2AFiring%2A%2A%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val1%0AAnnotations%3A%0A+-+ann1+%3D+annv1%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval1%0A%0ALabels%3A%0A+-+alertname+%3D+alert1%0A+-+lbl1+%3D+val2%0AAnnotations%3A%0A+-+ann1+%3D+annv2%0ASilence%3A+http%3A%2F%2Flocalhost%2Falerting%2Fsilence%2Fnew%3Falertmanager%3Dgrafana%26matchers%3Dalertname%253Dalert1%252Clbl1%253Dval2%0A%0A%2AURL%3A%2A+http%3A%2Flocalhost%2Falerting%2Flist%0A&to=87654321",
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Invalid gateway id",
|
name: "Invalid gateway id",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -79,7 +76,7 @@ func TestThreemaNotifier(t *testing.T) {
|
|||||||
"recipient_id": "87654321",
|
"recipient_id": "87654321",
|
||||||
"api_secret": "supersecret"
|
"api_secret": "supersecret"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must start with a *"},
|
expInitError: `failed to validate receiver "threema_testing" of type "threema": invalid Threema Gateway ID: Must start with a *`,
|
||||||
}, {
|
}, {
|
||||||
name: "Invalid receipent id",
|
name: "Invalid receipent id",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -87,14 +84,14 @@ func TestThreemaNotifier(t *testing.T) {
|
|||||||
"recipient_id": "8765432",
|
"recipient_id": "8765432",
|
||||||
"api_secret": "supersecret"
|
"api_secret": "supersecret"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Invalid Threema Recipient ID: Must be 8 characters long"},
|
expInitError: `failed to validate receiver "threema_testing" of type "threema": invalid Threema Recipient ID: Must be 8 characters long`,
|
||||||
}, {
|
}, {
|
||||||
name: "No API secret",
|
name: "No API secret",
|
||||||
settings: `{
|
settings: `{
|
||||||
"gateway_id": "*1234567",
|
"gateway_id": "*1234567",
|
||||||
"recipient_id": "87654321"
|
"recipient_id": "87654321"
|
||||||
}`,
|
}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find Threema API secret in settings"},
|
expInitError: `failed to validate receiver "threema_testing" of type "threema": could not find Threema API secret in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,9 +107,9 @@ func TestThreemaNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewThreemaNotifier(m, tmpl)
|
pn, err := NewThreemaNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -26,6 +26,28 @@ const (
|
|||||||
ColorAlertResolved = "#36a64f"
|
ColorAlertResolved = "#36a64f"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type receiverInitError struct {
|
||||||
|
Reason string
|
||||||
|
Err error
|
||||||
|
Cfg NotificationChannelConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e receiverInitError) Error() string {
|
||||||
|
name := ""
|
||||||
|
if e.Cfg.Name != "" {
|
||||||
|
name = fmt.Sprintf("%q ", e.Cfg.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
s := fmt.Sprintf("failed to validate receiver %sof type %q: %s", name, e.Cfg.Type, e.Reason)
|
||||||
|
if e.Err != nil {
|
||||||
|
return fmt.Sprintf("%s: %s", s, e.Err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e receiverInitError) Unwrap() error { return e.Err }
|
||||||
|
|
||||||
func getAlertStatusColor(status model.AlertStatus) string {
|
func getAlertStatusColor(status model.AlertStatus) string {
|
||||||
if status == model.AlertFiring {
|
if status == model.AlertFiring {
|
||||||
return ColorAlertFiring
|
return ColorAlertFiring
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
@ -32,7 +31,7 @@ const (
|
|||||||
func NewVictoropsNotifier(model *NotificationChannelConfig, t *template.Template) (*VictoropsNotifier, error) {
|
func NewVictoropsNotifier(model *NotificationChannelConfig, t *template.Template) (*VictoropsNotifier, error) {
|
||||||
url := model.Settings.Get("url").MustString()
|
url := model.Settings.Get("url").MustString()
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find victorops url property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find victorops url property in settings"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &VictoropsNotifier{
|
return &VictoropsNotifier{
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestVictoropsNotifier(t *testing.T) {
|
func TestVictoropsNotifier(t *testing.T) {
|
||||||
@ -28,7 +27,7 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
settings string
|
settings string
|
||||||
alerts []*types.Alert
|
alerts []*types.Alert
|
||||||
expMsg string
|
expMsg string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -50,8 +49,7 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
"monitoring_tool": "Grafana v",
|
"monitoring_tool": "Grafana v",
|
||||||
"state_message": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n"
|
"state_message": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n"
|
||||||
}`,
|
}`,
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Multiple alerts",
|
name: "Multiple alerts",
|
||||||
settings: `{"url": "http://localhost"}`,
|
settings: `{"url": "http://localhost"}`,
|
||||||
@ -76,12 +74,11 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
"monitoring_tool": "Grafana v",
|
"monitoring_tool": "Grafana v",
|
||||||
"state_message": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n"
|
"state_message": "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n"
|
||||||
}`,
|
}`,
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing, no URL",
|
name: "Error in initing, no URL",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find victorops url property in settings"},
|
expInitError: `failed to validate receiver "victorops_testing" of type "victorops": could not find victorops url property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,9 +94,9 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewVictoropsNotifier(m, tmpl)
|
pn, err := NewVictoropsNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ type WebhookNotifier struct {
|
|||||||
func NewWebHookNotifier(model *NotificationChannelConfig, t *template.Template) (*WebhookNotifier, error) {
|
func NewWebHookNotifier(model *NotificationChannelConfig, t *template.Template) (*WebhookNotifier, error) {
|
||||||
url := model.Settings.Get("url").MustString()
|
url := model.Settings.Get("url").MustString()
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
return nil, receiverInitError{Cfg: *model, Reason: "could not find url property in settings"}
|
||||||
}
|
}
|
||||||
return &WebhookNotifier{
|
return &WebhookNotifier{
|
||||||
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
|
NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/alerting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWebhookNotifier(t *testing.T) {
|
func TestWebhookNotifier(t *testing.T) {
|
||||||
@ -34,7 +33,7 @@ func TestWebhookNotifier(t *testing.T) {
|
|||||||
expUsername string
|
expUsername string
|
||||||
expPassword string
|
expPassword string
|
||||||
expHttpMethod string
|
expHttpMethod string
|
||||||
expInitError error
|
expInitError string
|
||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -88,8 +87,7 @@ func TestWebhookNotifier(t *testing.T) {
|
|||||||
State: "alerting",
|
State: "alerting",
|
||||||
Message: "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
Message: "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Custom config with multiple alerts",
|
name: "Custom config with multiple alerts",
|
||||||
settings: `{
|
settings: `{
|
||||||
@ -166,12 +164,11 @@ func TestWebhookNotifier(t *testing.T) {
|
|||||||
State: "alerting",
|
State: "alerting",
|
||||||
Message: "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n",
|
Message: "**Firing**\n\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval1\n\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matchers=alertname%3Dalert1%2Clbl1%3Dval2\n",
|
||||||
},
|
},
|
||||||
expInitError: nil,
|
expMsgError: nil,
|
||||||
expMsgError: nil,
|
|
||||||
}, {
|
}, {
|
||||||
name: "Error in initing",
|
name: "Error in initing",
|
||||||
settings: `{}`,
|
settings: `{}`,
|
||||||
expInitError: alerting.ValidationError{Reason: "Could not find url property in settings"},
|
expInitError: `failed to validate receiver "webhook_testing" of type "webhook": could not find url property in settings`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,9 +184,9 @@ func TestWebhookNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pn, err := NewWebHookNotifier(m, tmpl)
|
pn, err := NewWebHookNotifier(m, tmpl)
|
||||||
if c.expInitError != nil {
|
if c.expInitError != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, c.expInitError.Error(), err.Error())
|
require.Equal(t, c.expInitError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -61,7 +61,7 @@ func TestAlertmanagerConfigurationIsTransactional(t *testing.T) {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
resp := postRequest(t, alertConfigURL, payload, http.StatusBadRequest) // nolint
|
resp := postRequest(t, alertConfigURL, payload, http.StatusBadRequest) // nolint
|
||||||
require.JSONEq(t, "{\"message\":\"failed to save and apply Alertmanager configuration: alert validation error: token must be specified when using the Slack chat API\"}", getBody(t, resp.Body))
|
require.JSONEq(t, `{"message":"failed to save and apply Alertmanager configuration: failed to validate receiver \"slack.receiver\" of type \"slack\": token must be specified when using the Slack chat API"}`, getBody(t, resp.Body))
|
||||||
|
|
||||||
resp = getRequest(t, alertConfigURL, http.StatusOK) // nolint
|
resp = getRequest(t, alertConfigURL, http.StatusOK) // nolint
|
||||||
require.JSONEq(t, defaultAlertmanagerConfigJSON, getBody(t, resp.Body))
|
require.JSONEq(t, defaultAlertmanagerConfigJSON, getBody(t, resp.Body))
|
||||||
|
Loading…
Reference in New Issue
Block a user