-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager.go
108 lines (94 loc) · 2.64 KB
/
manager.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
package notifiers
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/orange-cloudfoundry/statusetat/config"
"github.com/orange-cloudfoundry/statusetat/emitter"
"github.com/orange-cloudfoundry/statusetat/models"
"github.com/orange-cloudfoundry/statusetat/storages"
)
type ToNotifie struct {
Notifier Notifier
For config.ForComponent
}
var toNotifies = []ToNotifie{}
var notifiers = []Notifier{}
var metadataFields = make(models.MetadataFields, 0)
func RegisterNotifier(notifier Notifier) {
notifiers = append(notifiers, notifier)
}
func AddNotifier(name string, params map[string]interface{}, forComp config.ForComponent, baseInfo config.BaseInfo) error {
for _, n := range notifiers {
if n.Name() == name {
notifier, err := n.Creator(params, baseInfo)
if err != nil {
return err
}
if metanotif, ok := notifier.(NotifierMetadataField); ok {
for _, field := range metanotif.MetadataFields() {
err := field.Validate()
if err != nil {
return err
}
metadataFields = append(metadataFields, field)
}
}
toNotifies = append(toNotifies, ToNotifie{
Notifier: notifier,
For: forComp,
})
return nil
}
}
return fmt.Errorf("Could not find notifier with name '%s' .", name)
}
func MetadataFields() models.MetadataFields {
return metadataFields
}
func PreCheckers(components models.Components) []NotifierPreCheck {
notifiers := make([]NotifierPreCheck, 0)
for _, tn := range toNotifies {
preChecker, ok := tn.Notifier.(NotifierPreCheck)
if !ok || !tn.For.MatchComponents(components) {
continue
}
notifiers = append(notifiers, preChecker)
}
return notifiers
}
func ListAll() map[string][]Notifier {
notifierMap := make(map[string][]Notifier)
for _, toNotif := range toNotifies {
notifier := toNotif.Notifier
if ls, ok := notifierMap[notifier.Name()]; ok {
notifierMap[notifier.Name()] = append(ls, notifier)
continue
}
notifierMap[notifier.Name()] = []Notifier{notifier}
}
return notifierMap
}
func Notify(store storages.Store) {
if len(toNotifies) == 0 {
return
}
for event := range emitter.On() {
subscribers, err := store.Subscribers()
if err != nil {
log.Warningf("Could not retrieve list of subscribers: %s", err.Error())
}
notifyReq := emitter.ToNotifyRequest(event)
notifyReq.Subscribers = subscribers
for _, toNotif := range toNotifies {
n := toNotif.Notifier
if !toNotif.For.MatchComponents(*notifyReq.Incident.Components) {
continue
}
entry := log.WithField("notifier", n.Name()).WithField("id", n.Id())
err := n.Notify(notifyReq)
if err != nil {
entry.Errorf("Could not send notify: %s", err.Error())
}
}
}
}