From d1eceedf55b0f6d1e88cffdeeb07b5502ad78743 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 18 Oct 2016 16:18:16 +0200 Subject: [PATCH] feat(webhook): add httpmethod to webhook closes #6255 --- pkg/models/notifications.go | 18 ++++++++------- pkg/services/alerting/notifiers/webhook.go | 19 ++++++++------- pkg/services/notifications/notifications.go | 18 ++++++++------- pkg/services/notifications/webhook.go | 17 +++++++++----- .../alerting/notification_edit_ctrl.ts | 2 +- .../alerting/partials/notification_edit.html | 23 +++++++++++-------- 6 files changed, 57 insertions(+), 40 deletions(-) diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index 759efe41a02..abbc6ec7e27 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -17,17 +17,19 @@ type SendEmailCommandSync struct { } type SendWebhook struct { - Url string - User string - Password string - Body string + Url string + User string + Password string + Body string + HttpMethod string } type SendWebhookSync struct { - Url string - User string - Password string - Body string + Url string + User string + Password string + Body string + HttpMethod string } type SendResetPasswordEmailCommand struct { diff --git a/pkg/services/alerting/notifiers/webhook.go b/pkg/services/alerting/notifiers/webhook.go index 979ce2e8a98..fb236c91c13 100644 --- a/pkg/services/alerting/notifiers/webhook.go +++ b/pkg/services/alerting/notifiers/webhook.go @@ -24,16 +24,18 @@ func NewWebHookNotifier(model *m.AlertNotification) (alerting.Notifier, error) { Url: url, User: model.Settings.Get("user").MustString(), Password: model.Settings.Get("password").MustString(), + HttpMethod: model.Settings.Get("httpMethod").MustString("POST"), log: log.New("alerting.notifier.webhook"), }, nil } type WebhookNotifier struct { NotifierBase - Url string - User string - Password string - log log.Logger + Url string + User string + Password string + HttpMethod string + log log.Logger } func (this *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error { @@ -59,10 +61,11 @@ func (this *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error { body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: this.Url, - User: this.User, - Password: this.Password, - Body: string(body), + Url: this.Url, + User: this.User, + Password: this.Password, + Body: string(body), + HttpMethod: this.HttpMethod, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { diff --git a/pkg/services/notifications/notifications.go b/pkg/services/notifications/notifications.go index f7762e2d3d0..9aa30b94edd 100644 --- a/pkg/services/notifications/notifications.go +++ b/pkg/services/notifications/notifications.go @@ -61,19 +61,21 @@ func Init() error { func SendWebhookSync(ctx context.Context, cmd *m.SendWebhookSync) error { return sendWebRequestSync(ctx, &Webhook{ - Url: cmd.Url, - User: cmd.User, - Password: cmd.Password, - Body: cmd.Body, + Url: cmd.Url, + User: cmd.User, + Password: cmd.Password, + Body: cmd.Body, + HttpMethod: cmd.HttpMethod, }) } func sendWebhook(cmd *m.SendWebhook) error { addToWebhookQueue(&Webhook{ - Url: cmd.Url, - User: cmd.User, - Password: cmd.Password, - Body: cmd.Body, + Url: cmd.Url, + User: cmd.User, + Password: cmd.Password, + Body: cmd.Body, + HttpMethod: cmd.HttpMethod, }) return nil diff --git a/pkg/services/notifications/webhook.go b/pkg/services/notifications/webhook.go index d5b8a718d52..de1303d8131 100644 --- a/pkg/services/notifications/webhook.go +++ b/pkg/services/notifications/webhook.go @@ -15,10 +15,11 @@ import ( ) type Webhook struct { - Url string - User string - Password string - Body string + Url string + User string + Password string + Body string + HttpMethod string } var webhookQueue chan *Webhook @@ -44,13 +45,17 @@ func processWebhookQueue() { } func sendWebRequestSync(ctx context.Context, webhook *Webhook) error { - webhookLog.Debug("Sending webhook", "url", webhook.Url) + webhookLog.Debug("Sending webhook", "url", webhook.Url, "http method", webhook.HttpMethod) client := &http.Client{ Timeout: time.Duration(10 * time.Second), } - request, err := http.NewRequest(http.MethodPost, webhook.Url, bytes.NewReader([]byte(webhook.Body))) + if webhook.HttpMethod == "" { + webhook.HttpMethod = http.MethodPost + } + + request, err := http.NewRequest(webhook.HttpMethod, webhook.Url, bytes.NewReader([]byte(webhook.Body))) if webhook.User != "" && webhook.Password != "" { request.Header.Add("Authorization", util.GetBasicAuthHeader(webhook.User, webhook.Password)) } diff --git a/public/app/features/alerting/notification_edit_ctrl.ts b/public/app/features/alerting/notification_edit_ctrl.ts index 19b804f4697..c5f24650845 100644 --- a/public/app/features/alerting/notification_edit_ctrl.ts +++ b/public/app/features/alerting/notification_edit_ctrl.ts @@ -18,7 +18,7 @@ export class AlertNotificationEditCtrl { this.model = { type: 'email', settings: { - severityFilter: 'none' + httpMethod: 'POST' }, isDefault: false }; diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html index 817035d21a3..c6c422b3598 100644 --- a/public/app/features/alerting/partials/notification_edit.html +++ b/public/app/features/alerting/partials/notification_edit.html @@ -32,19 +32,24 @@

Webhook settings

- Url + Url
-
-
- Username - -
-
- Password - +
+ Http Method +
+
+
+ Username + +
+
+ Password + +