mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore: move notifications models into notifications service (#61638)
This commit is contained in:
@@ -175,7 +175,7 @@ func (am *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) error
|
||||
errCnt := 0
|
||||
|
||||
for _, url := range am.URL {
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: strings.TrimSuffix(url, "/") + "/api/v1/alerts",
|
||||
User: am.BasicAuthUser,
|
||||
Password: am.BasicAuthPassword,
|
||||
|
||||
@@ -86,7 +86,7 @@ func (dd *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: dd.URL,
|
||||
Body: string(body),
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
|
||||
json, _ := bodyJSON.MarshalJSON()
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: dn.WebhookURL,
|
||||
HttpMethod: "POST",
|
||||
ContentType: "application/json",
|
||||
@@ -185,7 +185,7 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
|
||||
func (dn *DiscordNotifier) embedImage(cmd *notifications.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
|
||||
// nolint:gosec
|
||||
// We can ignore the gosec G304 warning on this one because `imagePath` comes
|
||||
// from the alert `evalContext` that generates the images.
|
||||
|
||||
@@ -82,8 +82,8 @@ func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
error = evalContext.Error.Error()
|
||||
}
|
||||
|
||||
cmd := &models.SendEmailCommandSync{
|
||||
SendEmailCommand: models.SendEmailCommand{
|
||||
cmd := ¬ifications.SendEmailCommandSync{
|
||||
SendEmailCommand: notifications.SendEmailCommand{
|
||||
Subject: evalContext.GetNotificationTitle(),
|
||||
Data: map[string]interface{}{
|
||||
"Title": evalContext.GetNotificationTitle(),
|
||||
|
||||
@@ -220,7 +220,7 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
}
|
||||
body, _ := json.Marshal(res1D)
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: gcn.URL,
|
||||
HttpMethod: "POST",
|
||||
HttpHeader: headers,
|
||||
|
||||
@@ -173,7 +173,7 @@ func (hc *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
hipURL := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", hc.URL, hc.RoomID, hc.APIKey)
|
||||
data, _ := json.Marshal(&body)
|
||||
hc.log.Info("Request payload", "json", string(data))
|
||||
cmd := &models.SendWebhookSync{Url: hipURL, Body: string(data)}
|
||||
cmd := ¬ifications.SendWebhookSync{Url: hipURL, Body: string(data)}
|
||||
|
||||
if err := hc.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
|
||||
hc.log.Error("Failed to send hipchat notification", "error", err, "webhook", hc.Name)
|
||||
|
||||
@@ -114,7 +114,7 @@ func (kn *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
|
||||
topicURL := kn.Endpoint + "/topics/" + kn.Topic
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: topicURL,
|
||||
Body: string(body),
|
||||
HttpMethod: "POST",
|
||||
|
||||
@@ -82,7 +82,7 @@ func (ln *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
|
||||
form.Add("imageFullsize", evalContext.ImagePublicURL)
|
||||
}
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: lineNotifyURL,
|
||||
HttpMethod: "POST",
|
||||
HttpHeader: map[string]string{
|
||||
|
||||
@@ -195,7 +195,7 @@ func (on *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error
|
||||
|
||||
body, _ := bodyJSON.MarshalJSON()
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: on.APIUrl,
|
||||
Body: string(body),
|
||||
HttpMethod: "POST",
|
||||
@@ -219,7 +219,7 @@ func (on *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error
|
||||
bodyJSON.Set("source", "Grafana")
|
||||
body, _ := bodyJSON.MarshalJSON()
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", on.APIUrl, evalContext.Rule.ID),
|
||||
Body: string(body),
|
||||
HttpMethod: "POST",
|
||||
|
||||
@@ -231,7 +231,7 @@ func (pn *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: pagerdutyEventAPIURL,
|
||||
Body: string(body),
|
||||
HttpMethod: "POST",
|
||||
|
||||
@@ -280,7 +280,7 @@ func (pn *PushoverNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: pushoverEndpoint,
|
||||
HttpMethod: "POST",
|
||||
HttpHeader: headers,
|
||||
|
||||
@@ -138,7 +138,7 @@ func (sn *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
|
||||
body, _ := bodyJSON.MarshalJSON()
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: sn.URL,
|
||||
User: sn.User,
|
||||
Password: sn.Password,
|
||||
|
||||
@@ -188,7 +188,7 @@ func (sn *SensuGoNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: fmt.Sprintf("%s/api/core/v2/namespaces/%s/events", strings.TrimSuffix(sn.URL, "/"), namespace),
|
||||
Body: string(body),
|
||||
HttpMethod: "POST",
|
||||
|
||||
@@ -415,7 +415,7 @@ func (sn *SlackNotifier) slackFileUpload(evalContext *alerting.EvalContext, log
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: "https://slack.com/api/files.upload", Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST",
|
||||
}
|
||||
if err := sn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
|
||||
|
||||
@@ -133,7 +133,7 @@ func (tn *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(&body)
|
||||
cmd := &models.SendWebhookSync{Url: tn.URL, Body: string(data)}
|
||||
cmd := ¬ifications.SendWebhookSync{Url: tn.URL, Body: string(data)}
|
||||
|
||||
if err := tn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
|
||||
tn.log.Error("Failed to send teams notification", "error", err, "webhook", tn.Name)
|
||||
|
||||
@@ -89,7 +89,7 @@ func NewTelegramNotifier(model *models.AlertNotification, fn alerting.GetDecrypt
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (tn *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, sendImageInline bool) (*models.SendWebhookSync, error) {
|
||||
func (tn *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, sendImageInline bool) (*notifications.SendWebhookSync, error) {
|
||||
if sendImageInline {
|
||||
cmd, err := tn.buildMessageInlineImage(evalContext)
|
||||
if err == nil {
|
||||
@@ -102,7 +102,7 @@ func (tn *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, send
|
||||
return tn.buildMessageLinkedImage(evalContext)
|
||||
}
|
||||
|
||||
func (tn *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalContext) (*models.SendWebhookSync, error) {
|
||||
func (tn *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalContext) (*notifications.SendWebhookSync, error) {
|
||||
message := fmt.Sprintf("<b>%s</b>\nState: %s\nMessage: %s\n", evalContext.GetNotificationTitle(), evalContext.Rule.Name, evalContext.Rule.Message)
|
||||
|
||||
ruleURL, err := evalContext.GetRuleURL()
|
||||
@@ -132,7 +132,7 @@ func (tn *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalCo
|
||||
})
|
||||
}
|
||||
|
||||
func (tn *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalContext) (*models.SendWebhookSync, error) {
|
||||
func (tn *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalContext) (*notifications.SendWebhookSync, error) {
|
||||
var imageFile *os.File
|
||||
var err error
|
||||
|
||||
@@ -169,7 +169,7 @@ func (tn *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalCo
|
||||
})
|
||||
}
|
||||
|
||||
func (tn *TelegramNotifier) generateTelegramCmd(message string, messageField string, apiAction string, extraConf func(writer *multipart.Writer)) (*models.SendWebhookSync, error) {
|
||||
func (tn *TelegramNotifier) generateTelegramCmd(message string, messageField string, apiAction string, extraConf func(writer *multipart.Writer)) (*notifications.SendWebhookSync, error) {
|
||||
var body bytes.Buffer
|
||||
w := multipart.NewWriter(&body)
|
||||
defer func() {
|
||||
@@ -203,7 +203,7 @@ func (tn *TelegramNotifier) generateTelegramCmd(message string, messageField str
|
||||
tn.log.Info("Sending telegram notification", "chat_id", tn.ChatID, "bot_token", tn.BotToken, "apiAction", apiAction)
|
||||
url := fmt.Sprintf(telegramAPIURL, tn.BotToken, apiAction)
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: url,
|
||||
Body: body.String(),
|
||||
HttpMethod: "POST",
|
||||
@@ -260,7 +260,7 @@ func appendIfPossible(tlog log.Logger, message string, extra string, sizeLimit i
|
||||
|
||||
// Notify send an alert notification to Telegram.
|
||||
func (tn *TelegramNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
var cmd *models.SendWebhookSync
|
||||
var cmd *notifications.SendWebhookSync
|
||||
var err error
|
||||
if evalContext.ImagePublicURL == "" && tn.UploadImage {
|
||||
cmd, err = tn.buildMessage(evalContext, true)
|
||||
|
||||
@@ -152,7 +152,7 @@ func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error
|
||||
headers := map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: url,
|
||||
Body: body,
|
||||
HttpMethod: "POST",
|
||||
|
||||
@@ -154,7 +154,7 @@ func (vn *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
}
|
||||
|
||||
data, _ := bodyJSON.MarshalJSON()
|
||||
cmd := &models.SendWebhookSync{Url: vn.URL, Body: string(data)}
|
||||
cmd := ¬ifications.SendWebhookSync{Url: vn.URL, Body: string(data)}
|
||||
|
||||
if err := vn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
|
||||
vn.log.Error("Failed to send Victorops notification", "error", err, "webhook", vn.Name)
|
||||
|
||||
@@ -145,7 +145,7 @@ func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
|
||||
bodyJSON, _ := json.Marshal(body)
|
||||
|
||||
cmd := &models.SendWebhookSync{
|
||||
cmd := ¬ifications.SendWebhookSync{
|
||||
Url: wn.URL,
|
||||
User: wn.User,
|
||||
Password: wn.Password,
|
||||
|
||||
Reference in New Issue
Block a user