@@ -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.
2931type 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 ),
0 commit comments