This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
alert.go
75 lines (70 loc) · 2.17 KB
/
alert.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
package handler
import (
"fmt"
"net/http"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-graylog"
"github.com/suzuki-shunsuke/go-graylog/mockserver/logic"
)
// HandleGetAlert is the handler of Get an Alert API.
func HandleGetAlert(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// GET /streams/alerts/{id} Get an alarm
id := ps.PathParam("alertID")
// TODO authorization
// if sc, err := lgc.Authorize(user, "inputs:read", id); err != nil {
// return nil, sc, err
// }
return lgc.GetAlert(id)
}
// HandleGetAlerts is the handler of GET Alerts API.
func HandleGetAlerts(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// GET /streams/alerts Get the most recent alarms of all streams
since := 0
limit := 0
query := r.URL.Query()
s, ok := query["since"]
var err error
if ok && len(s) > 0 {
since, err = strconv.Atoi(s[0])
if err != nil {
lgc.Logger().WithFields(log.Fields{
"error": err, "param_name": "since", "value": s[0],
}).Warn("failed to convert string to integer")
// Unfortunately, graylog 2.4.3 returns 404
return nil, 404, fmt.Errorf("HTTP 404 Not Found")
}
if since < 0 {
lgc.Logger().WithFields(log.Fields{
"error": err, "param_name": "since", "value": since,
}).Warn("must be greater than or equal to 0")
return nil, 400, fmt.Errorf("must be greater than or equal to 0")
}
}
l, ok := query["limit"]
if ok && len(l) > 0 {
limit, err = strconv.Atoi(l[0])
if err != nil {
lgc.Logger().WithFields(log.Fields{
"error": err, "param_name": "limit", "value": l[0],
}).Warn("failed to convert string to integer")
// Unfortunately, graylog 2.4.3 returns 404
return nil, 404, fmt.Errorf("HTTP 404 Not Found")
}
if limit < 1 {
lgc.Logger().WithFields(log.Fields{
"error": err, "param_name": "limit", "value": limit,
}).Warn("must be greater than or equal to 1")
return nil, 400, fmt.Errorf("must be greater than or equal to 1")
}
}
arr, total, sc, err := lgc.GetAlerts(since, limit)
if err != nil {
return arr, sc, err
}
return &graylog.AlertsBody{Alerts: arr, Total: total}, sc, nil
}