Skip to content

Commit 20db236

Browse files
committed
notifications: Move Rule Version and IDs to Event
Remove the custom HTTP headers for the rules and their version and introduce them in the event.Event struct. This eases both serialization, deserialization and the HTTP code.
1 parent eb4e314 commit 20db236

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

notifications/event/event.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ type Event struct {
3838
// as Icinga Notifications requires a reason for muting an object. Otherwise, it will be omitted
3939
// from the encoded JSON.
4040
MuteReason string `json:"mute_reason,omitempty"`
41+
42+
// RulesVersion and RuleIds are the source rules matching for this Event.
43+
RulesVersion string `json:"rules_version"`
44+
RuleIds []int64 `json:"rule_ids"`
4145
}

notifications/event/event_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,32 @@ func TestEvent(t *testing.T) {
1818
t.Parallel()
1919

2020
event := &Event{
21-
Name: "TestEvent",
22-
URL: "example.com",
23-
Tags: map[string]string{"tag1": "value1"},
24-
Type: TypeState,
25-
Severity: SeverityOK,
26-
Username: "testuser",
27-
Message: "Test",
21+
Name: "TestEvent",
22+
URL: "example.com",
23+
Tags: map[string]string{"tag1": "value1"},
24+
Type: TypeState,
25+
Severity: SeverityOK,
26+
Username: "testuser",
27+
Message: "Test",
28+
RulesVersion: "0x1",
29+
RuleIds: []int64{1, 2, 3, 6},
2830
}
2931

3032
data, err := json.Marshal(event)
3133
require.NoError(t, err)
3234

33-
expected := `{"name":"TestEvent","url":"example.com","tags":{"tag1":"value1"},"type":"state","severity":"ok","username":"testuser","message":"Test"}`
35+
expected := `
36+
{
37+
"name":"TestEvent",
38+
"url":"example.com",
39+
"tags":{"tag1":"value1"},
40+
"type":"state",
41+
"severity":"ok",
42+
"username":"testuser",
43+
"message":"Test",
44+
"rules_version": "0x1",
45+
"rule_ids": [1, 2, 3, 6]
46+
}`
3447
assert.JSONEq(t, expected, string(data), "JSON encoding does not match expected output")
3548
})
3649

notifications/source/client.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"iter"
1010
"net/http"
1111
"net/url"
12-
"strconv"
1312
"strings"
1413

1514
"github.com/icinga/icinga-go-library/notifications/event"
@@ -88,9 +87,8 @@ func NewClient(cfg Config, projectName string) (*Client, error) {
8887

8988
// ProcessEvent submits an event to the Icinga Notifications /process-event API endpoint.
9089
//
91-
// It serializes the event into JSON and sends it as a POST request to the process event endpoint.
92-
// The given ruleVersion is transmitted as [XIcingaRulesVersion] header along with the [XIcingaRulesId] header,
93-
// containing a comma-separated list of rule IDs.
90+
// It serializes the event into JSON and sends it as a POST request to the process event endpoint. In most cases, the
91+
// Event.RulesVersion and Event.RuleIds must be set.
9492
//
9593
// It may return an ErrRulesOutdated error, implying that the provided ruleVersion does not match the current rules
9694
// version in Icinga Notifications daemon. In this case, it will also return the current rules specific to your source
@@ -99,7 +97,7 @@ func NewClient(cfg Config, projectName string) (*Client, error) {
9997
// you're using an outdated event rules config.
10098
//
10199
// If the request fails or the response is not as expected, it returns an error; otherwise, it returns nil.
102-
func (c *Client) ProcessEvent(ctx context.Context, ev *event.Event, ruleVersion string, ruleIDs ...int64) (*RulesInfo, error) {
100+
func (c *Client) ProcessEvent(ctx context.Context, ev *event.Event) (*RulesInfo, error) {
103101
body, err := json.Marshal(ev)
104102
if err != nil {
105103
return nil, errors.Wrap(err, "cannot encode event to JSON")
@@ -110,15 +108,8 @@ func (c *Client) ProcessEvent(ctx context.Context, ev *event.Event, ruleVersion
110108
return nil, errors.Wrap(err, "cannot create HTTP request")
111109
}
112110

113-
ruleIdsStrArr := make([]string, 0, len(ruleIDs))
114-
for _, ruleId := range ruleIDs {
115-
ruleIdsStrArr = append(ruleIdsStrArr, strconv.FormatInt(ruleId, 10))
116-
}
117-
118111
req.Header.Add("Content-Type", "application/json")
119112
req.Header.Add("Accept", "application/json")
120-
req.Header.Add(XIcingaRulesVersion, ruleVersion)
121-
req.Header.Add(XIcingaRulesId, strings.Join(ruleIdsStrArr, ","))
122113

123114
resp, err := c.client.Do(req)
124115
if err != nil {

notifications/source/xhttp_headers.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)