This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
input.go
147 lines (137 loc) · 4.19 KB
/
input.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
package handler
import (
"net/http"
log "github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-set"
"github.com/suzuki-shunsuke/go-graylog"
"github.com/suzuki-shunsuke/go-graylog/mockserver/logic"
"github.com/suzuki-shunsuke/go-graylog/util"
)
// HandleGetInput is the handler of Get an Input API.
func HandleGetInput(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// GET /system/inputs/{inputID} Get information of a single input on this node
id := ps.PathParam("inputID")
if sc, err := lgc.Authorize(user, "inputs:read", id); err != nil {
return nil, sc, err
}
return lgc.GetInput(id)
}
// HandleGetInputs is the handler of Get Inputs API.
func HandleGetInputs(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// GET /system/inputs Get all inputs
arr, total, sc, err := lgc.GetInputs()
if err != nil {
return arr, sc, err
}
inputs := &graylog.InputsBody{Inputs: arr, Total: total}
return inputs, sc, nil
}
// HandleCreateInput is the handler of Create an Input API.
func HandleCreateInput(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// POST /system/inputs Launch input on this node
if sc, err := lgc.Authorize(user, "inputs:create"); err != nil {
return nil, sc, err
}
body, sc, err := validateRequestBody(
r.Body, &validateReqBodyPrms{
Required: set.NewStrSet("title", "type", "configuration"),
Optional: set.NewStrSet("global", "node"),
ExtForbidden: true,
})
if err != nil {
return nil, sc, err
}
// change configuration to attributes
// https://github.com/Graylog2/graylog2-server/issues/3480
body["attributes"] = body["configuration"]
delete(body, "configuration")
d := &graylog.InputData{}
if err := util.MSDecode(body, d); err != nil {
lgc.Logger().WithFields(log.Fields{
"body": body, "error": err,
}).Info("Failed to parse request body as InputData")
return nil, 400, err
}
input := &graylog.Input{}
if err := d.ToInput(input); err != nil {
return nil, 400, err
}
sc, err = lgc.AddInput(input)
if err != nil {
return nil, sc, err
}
if err := lgc.Save(); err != nil {
return nil, 500, err
}
return &map[string]string{"id": input.ID}, sc, nil
}
// HandleUpdateInput is the handler of Update an Input API.
func HandleUpdateInput(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// PUT /system/inputs/{inputID} Update input on this node
id := ps.PathParam("inputID")
if sc, err := lgc.Authorize(user, "inputs:edit", id); err != nil {
return nil, sc, err
}
body, sc, err := validateRequestBody(
r.Body, &validateReqBodyPrms{
Required: set.NewStrSet("title", "type", "configuration"),
Optional: set.NewStrSet("global", "node"),
ExtForbidden: true,
})
if err != nil {
return nil, sc, err
}
// change configuration to attributes
// https://github.com/Graylog2/graylog2-server/issues/3480
body["attributes"] = body["configuration"]
delete(body, "configuration")
d := &graylog.InputUpdateParamsData{}
if err := util.MSDecode(body, d); err != nil {
lgc.Logger().WithFields(log.Fields{
"body": body, "error": err,
}).Info("Failed to parse request body as InputUpdateParamsData")
return nil, 400, err
}
prms := &graylog.InputUpdateParams{}
if err := d.ToInputUpdateParams(prms); err != nil {
return nil, 400, err
}
lgc.Logger().WithFields(log.Fields{
"body": body, "input": prms, "id": id,
}).Debug("request body")
prms.ID = id
input, sc, err := lgc.UpdateInput(prms)
if err != nil {
return nil, sc, err
}
if err := lgc.Save(); err != nil {
return nil, 500, err
}
return input, sc, nil
}
// HandleDeleteInput is the handler of Delete an Input API.
func HandleDeleteInput(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// DELETE /system/inputs/{inputID} Terminate input on this node
id := ps.PathParam("inputID")
if sc, err := lgc.Authorize(user, "inputs:terminate", id); err != nil {
return nil, sc, err
}
sc, err := lgc.DeleteInput(id)
if err != nil {
return nil, sc, err
}
if err := lgc.Save(); err != nil {
return nil, 500, err
}
return nil, sc, nil
}