Skip to content

Commit

Permalink
Adding notification webhook structure (#120)
Browse files Browse the repository at this point in the history
* Adding notification webhook structure

* Adding test for webhook notification

* Amending test values

---------

Co-authored-by: CB Hoffman <[email protected]>
  • Loading branch information
cbhoffman and CB Hoffman authored Dec 13, 2023
1 parent 598ab18 commit 3b4f115
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
52 changes: 52 additions & 0 deletions alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,58 @@ func TestClient_CreateAlertRuleWithNotifications(t *testing.T) {
assert.Equal(t, &expected, res)
}

func TestClient_CreateAlertRuleWithWebhookNotifications(t *testing.T) {
setup()
out := `{"alertRuleId": 1, "ruleName": "test", "notifications":{"webhook":[{"integrationId": "1", "integrationName": "Teams Channel", "integrationType":"WEBHOOK"},{"integrationId": "2", "integrationName": "Teams Channel", "integrationType":"WEBHOOK"}]}}`
mux.HandleFunc("/alert-rules/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(out))
})

var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
u := AlertRule{
RuleName: String("test"),
Notifications: &Notification{
Webhook: &[]NotificationWebhook{
{
IntegrationID: String("1"),
IntegrationName: String("Teams Channel"),
IntegrationType: String("WEBHOOK"),
},
{
IntegrationID: String("2"),
IntegrationName: String("Teams Channel"),
IntegrationType: String("WEBHOOK"),
},
},
},
}
res, err := client.CreateAlertRule(u)
if err != nil {
t.Fatal(err)
}
expected := AlertRule{
RuleID: Int64(1),
RuleName: String("test"),
Notifications: &Notification{
Webhook: &[]NotificationWebhook{
{
IntegrationID: String("1"),
IntegrationName: String("Teams Channel"),
IntegrationType: String("WEBHOOK"),
},
{
IntegrationID: String("2"),
IntegrationName: String("Teams Channel"),
IntegrationType: String("WEBHOOK"),
},
},
},
}
assert.Equal(t, &expected, res)
}

func TestClient_AlertJsonError(t *testing.T) {
out := `{"alertRules": [test]}`
setup()
Expand Down
9 changes: 9 additions & 0 deletions alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ type NotificationThirdParty struct {
Channel *string `json:"channel,omitempty"`
}

// NotificationWebhook - Alert Rule Notification Webhook structure
type NotificationWebhook struct {
IntegrationID *string `json:"integrationId,omitempty"`
IntegrationName *string `json:"integrationName,omitempty"`
IntegrationType *string `json:"integrationType,omitempty"`
Target *string `json:"target,omitempty"`
}

// Notification - Alert Rule Notification structure
type Notification struct {
Email *NotificationEmail `json:"email,omitempty"`
ThirdParty *[]NotificationThirdParty `json:"thirdParty,omitempty"`
Webhook *[]NotificationWebhook `json:"webhook,omitempty"`
}

// AlertRule - An alert rule
Expand Down

0 comments on commit 3b4f115

Please sign in to comment.