forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
134 lines (108 loc) · 3.64 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package actions
import (
"context"
"github.com/sensu/sensu-go/backend/messaging"
"github.com/sensu/sensu-go/backend/store"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)
// EventController expose actions in which a viewer can perform.
type EventController struct {
store store.EventStore
bus messaging.MessageBus
}
// NewEventController returns new EventController
func NewEventController(store store.EventStore, bus messaging.MessageBus) EventController {
return EventController{
store: store,
bus: bus,
}
}
// List returns resources available to the viewer filter by given params.
func (a EventController) List(ctx context.Context, pred *store.SelectionPredicate) ([]corev2.Resource, error) {
var results []*corev2.Event
var err error
// Fetch from store
if pred.Subcollection != "" {
results, err = a.store.GetEventsByEntity(ctx, pred.Subcollection, pred)
} else {
results, err = a.store.GetEvents(ctx, pred)
}
if err != nil {
return nil, NewError(InternalErr, err)
}
resources := make([]corev2.Resource, len(results))
for i, v := range results {
resources[i] = corev2.Resource(v)
}
return resources, nil
}
// Get returns resource associated with given parameters if available to the
// viewer.
func (a EventController) Get(ctx context.Context, entity, check string) (*corev2.Event, error) {
// Get (for events) requires both an entity and check
if entity == "" || check == "" {
return nil, NewErrorf(InvalidArgument, "Get() requires both an entity and a check")
}
result, err := a.store.GetEventByEntityCheck(ctx, entity, check)
if err != nil {
return nil, NewError(InternalErr, err)
}
if result == nil {
return nil, NewErrorf(NotFound)
}
return result, nil
}
// Delete destroys the event indicated by the supplied entity and check.
func (a EventController) Delete(ctx context.Context, entity, check string) error {
// Destroy (for events) requires both an entity and check
if entity == "" || check == "" {
return NewErrorf(InvalidArgument, "Delete() requires both an entity and a check")
}
result, err := a.store.GetEventByEntityCheck(ctx, entity, check)
if err != nil {
return NewError(InternalErr, err)
}
if result == nil {
return NewErrorf(NotFound)
}
if err := a.store.DeleteEventByEntityCheck(ctx, entity, check); err != nil {
return NewError(InternalErr, err)
}
return nil
}
// Create creates the event indicated by the supplied entity and check.
// If an event already exists for the entity and check, it updates that event.
func (a EventController) Create(ctx context.Context, event *corev2.Event) error {
if err := event.Validate(); err != nil {
return NewError(InvalidArgument, err)
}
// Verify if we already have an existing event for this entity/check pair.
// Doesn't apply to metric events.
if event.HasCheck() {
check := event.Check
entity := event.Entity
e, err := a.store.GetEventByEntityCheck(ctx, entity.Name, check.Name)
if err != nil {
return NewError(InternalErr, err)
} else if e != nil {
return NewErrorf(AlreadyExistsErr)
}
}
// Publish to event pipeline
if err := a.bus.Publish(messaging.TopicEventRaw, event); err != nil {
return NewError(InternalErr, err)
}
return nil
}
// CreateOrReplace creates the event indicated by the supplied entity and check.
// If an event already exists for the entity and check, it updates that event.
func (a EventController) CreateOrReplace(ctx context.Context, event *corev2.Event) error {
if err := event.Validate(); err != nil {
return NewError(InvalidArgument, err)
}
// Publish to event pipeline
if err := a.bus.Publish(messaging.TopicEventRaw, event); err != nil {
return NewError(InternalErr, err)
}
return nil
}