Skip to content

Commit 1fb8d85

Browse files
yhabteaboxzi
authored andcommitted
Use the newly introduced notifications event utils from igl
1 parent 7d721de commit 1fb8d85

File tree

13 files changed

+88
-236
lines changed

13 files changed

+88
-236
lines changed

internal/channel/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (c *Channel) Notify(contact *recipient.Contact, i contracts.Incident, ev *e
186186
Incident: &plugin.Incident{
187187
Id: i.ID(),
188188
Url: incidentUrl.String(),
189-
Severity: i.SeverityString(),
189+
Severity: i.IncidentSeverity(),
190190
},
191191
Event: &plugin.Event{
192192
Time: ev.Time,

internal/contracts/contracts.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package contracts
22

33
import (
44
"fmt"
5+
"github.com/icinga/icinga-go-library/notifications/event"
56
"github.com/icinga/icinga-notifications/internal/object"
67
)
78

@@ -10,5 +11,5 @@ type Incident interface {
1011

1112
ID() int64
1213
IncidentObject() *object.Object
13-
SeverityString() string
14+
IncidentSeverity() event.Severity
1415
}

internal/event/event.go

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/icinga/icinga-go-library/database"
13+
baseEv "github.com/icinga/icinga-go-library/notifications/event"
1314
"github.com/icinga/icinga-go-library/types"
1415
"github.com/jmoiron/sqlx"
1516
)
@@ -23,46 +24,19 @@ var ErrSuperfluousMuteUnmuteEvent = errors.New("ignoring superfluous (un)mute ev
2324

2425
// Event received of a specified Type for internal processing.
2526
//
26-
// The JSON struct tags are being used to unmarshal a JSON representation received from the listener.Listener. Some
27-
// fields are being omitted as they are only allowed to be populated from within icinga-notifications. Currently, there
28-
// is no Event being marshalled into its JSON representation.
27+
// This is a representation of an event received from an external source with additional metadata with sole
28+
// purpose of being used for internal processing. All the JSON serializable fields are these inherited from
29+
// the base event type, and are used to decode the request body. Currently, there is no Event being marshalled
30+
// into its JSON representation.
2931
type Event struct {
3032
Time time.Time `json:"-"`
3133
SourceId int64 `json:"-"`
32-
33-
Name string `json:"name"`
34-
URL string `json:"url"`
35-
Tags map[string]string `json:"tags"`
36-
37-
Type string `json:"type"`
38-
Severity Severity `json:"severity"`
39-
Username string `json:"username"`
40-
Message string `json:"message"`
41-
42-
Mute types.Bool `json:"mute"`
43-
MuteReason string `json:"mute_reason"`
44-
45-
ID int64 `json:"-"`
34+
ID int64 `json:"-"`
4635

4736
MatchedRules map[int64]struct{} `json:"-"` // MatchedRules contains the event rule IDs received from source.
48-
}
4937

50-
// Please keep the following types in alphabetically order and, even more important, make sure that the database type
51-
// event_type reflects the same values.
52-
const (
53-
TypeAcknowledgementCleared = "acknowledgement-cleared"
54-
TypeAcknowledgementSet = "acknowledgement-set"
55-
TypeCustom = "custom"
56-
TypeDowntimeEnd = "downtime-end"
57-
TypeDowntimeRemoved = "downtime-removed"
58-
TypeDowntimeStart = "downtime-start"
59-
TypeFlappingEnd = "flapping-end"
60-
TypeFlappingStart = "flapping-start"
61-
TypeIncidentAge = "incident-age"
62-
TypeMute = "mute"
63-
TypeState = "state"
64-
TypeUnmute = "unmute"
65-
)
38+
*baseEv.Event `json:",inline"`
39+
}
6640

6741
// Validate validates the current event state.
6842
// Returns an error if it detects a misconfigured field.
@@ -81,39 +55,24 @@ func (e *Event) Validate() error {
8155
return fmt.Errorf("invalid event: source ID must not be empty")
8256
}
8357

84-
if e.Severity != SeverityNone && e.Type != TypeState {
85-
return fmt.Errorf("invalid event: if 'severity' is set, 'type' must be set to %q", TypeState)
58+
if e.Severity != baseEv.SeverityNone && e.Type != baseEv.TypeState {
59+
return fmt.Errorf("invalid event: if 'severity' is set, 'type' must be set to %q", baseEv.TypeState)
8660
}
87-
if e.Type == TypeMute && (!e.Mute.Valid || !e.Mute.Bool) {
88-
return fmt.Errorf("invalid event: 'mute' must be true if 'type' is set to %q", TypeMute)
61+
if e.Type == baseEv.TypeMute && (!e.Mute.Valid || !e.Mute.Bool) {
62+
return fmt.Errorf("invalid event: 'mute' must be true if 'type' is set to %q", baseEv.TypeMute)
8963
}
90-
if e.Type == TypeUnmute && (!e.Mute.Valid || e.Mute.Bool) {
91-
return fmt.Errorf("invalid event: 'mute' must be false if 'type' is set to %q", TypeUnmute)
64+
if e.Type == baseEv.TypeUnmute && (!e.Mute.Valid || e.Mute.Bool) {
65+
return fmt.Errorf("invalid event: 'mute' must be false if 'type' is set to %q", baseEv.TypeUnmute)
9266
}
9367
if e.Mute.Valid && e.Mute.Bool && e.MuteReason == "" {
9468
return fmt.Errorf("invalid event: 'mute_reason' must not be empty if 'mute' is set")
9569
}
9670

97-
switch e.Type {
98-
case "":
99-
return fmt.Errorf("invalid event: 'type' must not be empty")
100-
case
101-
TypeAcknowledgementCleared,
102-
TypeAcknowledgementSet,
103-
TypeCustom,
104-
TypeDowntimeEnd,
105-
TypeDowntimeRemoved,
106-
TypeDowntimeStart,
107-
TypeFlappingEnd,
108-
TypeFlappingStart,
109-
TypeIncidentAge,
110-
TypeMute,
111-
TypeState,
112-
TypeUnmute:
113-
return nil
114-
default:
115-
return fmt.Errorf("invalid event: unsupported event type %q", e.Type)
71+
if e.Type == baseEv.TypeUnknown {
72+
return fmt.Errorf("invalid event: unsupported or empty event type %q", e.Type)
11673
}
74+
75+
return nil
11776
}
11877

11978
// SetMute alters the event mute and mute reason.
@@ -163,7 +122,7 @@ func (e *Event) FullString() string {
163122
}
164123
_, _ = fmt.Fprintf(&b, " Time: %s\n", e.Time)
165124
_, _ = fmt.Fprintf(&b, " SourceId: %d\n", e.SourceId)
166-
if e.Type != "" {
125+
if e.Type != baseEv.TypeUnknown {
167126
_, _ = fmt.Fprintf(&b, " Type: %q\n", e.Type)
168127
}
169128
if e.Severity != 0 {
@@ -199,7 +158,7 @@ type EventRow struct {
199158
Time types.UnixMilli `db:"time"`
200159
ObjectID types.Binary `db:"object_id"`
201160
Type types.String `db:"type"`
202-
Severity Severity `db:"severity"`
161+
Severity baseEv.Severity `db:"severity"`
203162
Username types.String `db:"username"`
204163
Message types.String `db:"message"`
205164
Mute types.Bool `db:"mute"`
@@ -215,7 +174,7 @@ func NewEventRow(e *Event, objectId types.Binary) *EventRow {
215174
return &EventRow{
216175
Time: types.UnixMilli(e.Time),
217176
ObjectID: objectId,
218-
Type: types.MakeString(e.Type, types.TransformEmptyStringToNull),
177+
Type: types.MakeString(e.Type.String(), types.TransformEmptyStringToNull),
219178
Severity: e.Severity,
220179
Username: types.MakeString(e.Username, types.TransformEmptyStringToNull),
221180
Message: types.MakeString(e.Message, types.TransformEmptyStringToNull),

internal/event/severity.go

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

internal/incident/db_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package incident
33
import (
44
"context"
55
"github.com/icinga/icinga-go-library/database"
6+
baseEv "github.com/icinga/icinga-go-library/notifications/event"
67
"github.com/icinga/icinga-go-library/types"
7-
"github.com/icinga/icinga-notifications/internal/event"
88
"github.com/icinga/icinga-notifications/internal/recipient"
99
"github.com/jmoiron/sqlx"
1010
)
@@ -74,8 +74,8 @@ type HistoryRow struct {
7474
Time types.UnixMilli `db:"time"`
7575
Type HistoryEventType `db:"type"`
7676
ChannelID types.Int `db:"channel_id"`
77-
NewSeverity event.Severity `db:"new_severity"`
78-
OldSeverity event.Severity `db:"old_severity"`
77+
NewSeverity baseEv.Severity `db:"new_severity"`
78+
OldSeverity baseEv.Severity `db:"old_severity"`
7979
NewRecipientRole ContactRole `db:"new_recipient_role"`
8080
OldRecipientRole ContactRole `db:"old_recipient_role"`
8181
Message types.String `db:"message"`

0 commit comments

Comments
 (0)