-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
133 lines (110 loc) · 2.85 KB
/
api.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
package main
import (
"sort"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/thbkrkr/toctoc/types"
)
// HandleEvent valids and stores an event representing a state of a service for a given host
func HandleEvent(c *gin.Context) {
ns := c.Param("ns")
var data map[string]interface{}
err := c.BindJSON(&data)
if err != nil {
c.JSON(400, gin.H{"message": "Fail to parse event"})
return
}
event, err := types.ParseEvent(defaultCheckTTL, data)
if err != nil {
logrus.WithError(err).WithField("body", data).Error("Fail to parse event")
c.JSON(400, gin.H{"message": err.Error()})
return
}
mutex.Lock()
defer mutex.Unlock()
if _, ok := events[ns]; !ok {
events[ns] = map[string]types.Event{}
}
events[ns][event.ID] = event
}
// Health returns all events with status 500 if at least one event is in error
func Health(c *gin.Context) {
mutex.RLock()
defer mutex.RUnlock()
evs := events[c.Param("ns")]
eventsArr := []interface{}{}
status := 200
for _, event := range evs {
eventsArr = append(eventsArr, event)
if event.Status == types.StatusKO {
status = 500
}
}
c.JSON(status, eventsArr)
}
// Services returns the list of services and a map of event states indexed by host and service
func Services(c *gin.Context) {
mutex.RLock()
defer mutex.RUnlock()
evs := events[c.Param("ns")]
servicesMap := map[string]int{}
eventsMap := map[string]map[string]types.Event{}
for _, event := range evs {
if event.ID == "" {
logrus.WithField("event", event).Warn("event is nil")
continue
}
host := event.GetHost()
service := event.GetService()
if _, ok := eventsMap[host]; !ok {
eventsMap[host] = map[string]types.Event{}
}
servicesMap[service] = 1
eventsMap[host][service] = event
}
services := make([]string, len(servicesMap))
i := 0
for service := range servicesMap {
services[i] = service
i++
}
sort.Strings(services)
c.JSON(200, gin.H{
"services": services,
"status": eventsMap,
})
}
// DeleteService deletes all events related to a given service
func DeleteService(c *gin.Context) {
ns := c.Param("ns")
service := c.Param("service")
evs := events[ns]
mutex.Lock()
defer mutex.Unlock()
for id, event := range evs {
if event.GetService() == service {
logrus.Info("deleteService for host: ", event.GetHost(), " service: ", event.GetService())
delete(events[ns], id)
}
}
c.JSON(200, gin.H{
"message": "Service " + service + " removed",
})
}
// DeleteHost Delete all events related to a given host
func DeleteHost(c *gin.Context) {
ns := c.Param("ns")
host := c.Param("host")
evs := events[ns]
mutex.Lock()
defer mutex.Unlock()
for id, event := range evs {
if event.GetHost() == host {
logrus.Info("deleteHost for host: ", event.GetHost(), " service: ", event.GetService())
delete(events[ns], id)
}
}
c.JSON(200, gin.H{
"message": "Host " + host + " removed",
})
}