From c2c5bdc6ebecb8ecd457d828ebb687e230120495 Mon Sep 17 00:00:00 2001 From: Rambatino Date: Wed, 24 Jul 2024 09:31:47 +0100 Subject: [PATCH] fix(notifiers): added customWebhook type --- axiom/monitors_integration_test.go | 1 - axiom/notifiers.go | 11 ++++++ axiom/notifiers_integration_test.go | 60 +++++++++++++++++++++++++++++ axiom/notifiers_test.go | 53 +++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) diff --git a/axiom/monitors_integration_test.go b/axiom/monitors_integration_test.go index 0045af27..f4a8607f 100644 --- a/axiom/monitors_integration_test.go +++ b/axiom/monitors_integration_test.go @@ -136,6 +136,5 @@ func (s *MonitorsTestSuite) TestCreateMatchMonitor() { }) s.Require().NoError(err) s.Require().NotNil(monitor) - s.Equal(axiom.MonitorTypeMatchEvent.String(), monitor.Type.String()) } diff --git a/axiom/notifiers.go b/axiom/notifiers.go index 45eaff80..fd858a9c 100644 --- a/axiom/notifiers.go +++ b/axiom/notifiers.go @@ -42,6 +42,8 @@ type NotifierProperties struct { Webhook *WebhookConfig `json:"webhook,omitempty"` // MicrosoftTeams configuration. MicrosoftTeams *MicrosoftTeams `json:"microsoftTeams,omitempty"` + // CustomWebhook configuration. + CustomWebhook *CustomWebhook `json:"customWebhook,omitempty"` } type DiscordConfig struct { @@ -90,6 +92,15 @@ type MicrosoftTeams struct { URL string `json:"microsoftTeamsUrl,omitempty"` } +type CustomWebhook struct { + // URL is the destination URL for the webhook. + URL string + // Headers is a map of header keys and values to include in the webhook request. + Headers map[string]string + // Body is the body of the webhook request. + Body string +} + // Axiom API Reference: /v2/notifiers type NotifiersService service diff --git a/axiom/notifiers_integration_test.go b/axiom/notifiers_integration_test.go index e8290cd7..9631e0f4 100644 --- a/axiom/notifiers_integration_test.go +++ b/axiom/notifiers_integration_test.go @@ -89,3 +89,63 @@ func (s *NotifiersTestSuite) Test() { s.Contains(notifiers, s.notifier) } + +func (s *NotifiersTestSuite) TestCreateCustomWebhookNotifier() { + // Create a custom webhook notifier. + notifier, err := s.client.Notifiers.Create(s.ctx, axiom.Notifier{ + Name: "Custom Webhook Notifier", + Properties: axiom.NotifierProperties{ + CustomWebhook: &axiom.CustomWebhook{ + URL: "http://example.com/webhook", + Headers: map[string]string{ + "Authorization": "Bearer token", + }, + Body: "{\"key\":\"value\"}", + }, + }, + }) + s.Require().NoError(err) + s.Require().NotNil(notifier) + + s.notifier = notifier + + // Get the notifier and make sure it matches what we have created. + notifier, err = s.client.Notifiers.Get(s.ctx, s.notifier.ID) + s.Require().NoError(err) + s.Require().NotNil(notifier) + + s.Equal(s.notifier, notifier) + + // Update the custom webhook notifier. + notifier, err = s.client.Notifiers.Update(s.ctx, s.notifier.ID, axiom.Notifier{ + Name: "Updated Custom Webhook Notifier", + Properties: axiom.NotifierProperties{ + CustomWebhook: &axiom.CustomWebhook{ + URL: "http://example.com/updated-webhook", + Headers: map[string]string{ + "Authorization": "Bearer new-token", + }, + Body: "{\"key\":\"new-value\"}", + }, + }, + }) + s.Require().NoError(err) + s.Require().NotNil(notifier) + + s.notifier = notifier + + // Get the notifier and make sure it matches what we have updated it to. + notifier, err = s.client.Notifiers.Get(s.ctx, s.notifier.ID) + s.Require().NoError(err) + s.Require().NotNil(notifier) + + s.Equal(s.notifier, notifier) + + // List all notifiers and make sure the created notifier is part of that + // list. + notifiers, err := s.client.Notifiers.List(s.ctx) + s.Require().NoError(err) + s.Require().NotEmpty(notifiers) + + s.Contains(notifiers, s.notifier) +} diff --git a/axiom/notifiers_test.go b/axiom/notifiers_test.go index ee042277..b80c10f4 100644 --- a/axiom/notifiers_test.go +++ b/axiom/notifiers_test.go @@ -184,3 +184,56 @@ func TestNotifiersService_Delete(t *testing.T) { err := client.Notifiers.Delete(context.Background(), "testID") require.NoError(t, err) } + +func TestNotifiersService_Create_CustomWebhook(t *testing.T) { + exp := &Notifier{ + ID: "test", + Name: "test", + Properties: NotifierProperties{ + CustomWebhook: &CustomWebhook{ + URL: "http://example.com/webhook", + Headers: map[string]string{ + "Authorization": "Bearer token", + }, + Body: "{\"key\":\"value\"}", + }, + }, + } + hf := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + assert.Equal(t, mediaTypeJSON, r.Header.Get("Content-Type")) + + w.Header().Set("Content-Type", mediaTypeJSON) + _, err := fmt.Fprint(w, `{ + "id": "test", + "name": "test", + "properties": { + "customWebhook": { + "url": "http://example.com/webhook", + "headers": { + "Authorization": "Bearer token" + }, + "body": "{\"key\":\"value\"}" + } + } + }`) + assert.NoError(t, err) + } + client := setup(t, "/v2/notifiers", hf) + + res, err := client.Notifiers.Create(context.Background(), Notifier{ + Name: "test", + Properties: NotifierProperties{ + CustomWebhook: &CustomWebhook{ + URL: "http://example.com/webhook", + Headers: map[string]string{ + "Authorization": "Bearer token", + }, + Body: "{\"key\":\"value\"}", + }, + }, + }) + + require.NoError(t, err) + assert.Equal(t, exp, res) +}