forked from thbkrkr/toctoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
173 lines (144 loc) · 3.72 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"errors"
"sort"
"time"
"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
}
ID, err := extractHostAndService(data)
if err != nil {
logrus.WithError(err).WithField("body", data).Error("Fail to extract host and service while handling state event")
c.JSON(400, gin.H{"message": err.Error()})
return
}
status, err := extractStatus(data)
if err != nil {
logrus.WithError(err).WithField("body", data).Error("Fail to extract status while handling state event")
c.JSON(400, gin.H{"message": err.Error()})
return
}
event := types.Event{
ID: ID,
Status: status,
Timestamp: time.Now(),
Value: data,
}
mutex.Lock()
defer mutex.Unlock()
if _, ok := events[ns]; !ok {
events[ns] = map[string]types.Event{}
}
events[ns][ID] = event
}
func extractHostAndService(obj map[string]interface{}) (string, error) {
host := obj["Host"]
if host == nil {
host = obj["Node"]
}
if host == nil {
return "", errors.New("Property 'Host' not found")
}
service := obj["Service"]
if service == nil {
return "", errors.New("Property 'Service' not found")
}
return service.(string) + "/" + host.(string), nil
}
func extractStatus(obj map[string]interface{}) (string, error) {
status := obj["Status"]
if status == nil {
status = obj["State"]
}
if status == nil {
return "", errors.New("Property 'Status' not found")
}
return status.(string), nil
}
// Health return 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 return 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]interface{}{}
for _, event := range evs {
host := event.GetHost()
service := event.GetService()
if _, ok := eventsMap[host]; !ok {
eventsMap[host] = map[string]interface{}{}
}
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 Delete 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",
})
}