This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
collector_configuration.go
122 lines (112 loc) · 3.89 KB
/
collector_configuration.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
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"
)
// HandleGetCollectorConfiguration is the handler of Get an CollectorConfiguration API.
func HandleGetCollectorConfiguration(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// GET /api/plugins/org.graylog.plugins.collector/configurations/:id
id := ps.PathParam("collectorConfigurationID")
// TODO authorize
return lgc.GetCollectorConfiguration(id)
}
// HandleGetCollectorConfigurations is the handler of Get Collector Configurations API.
func HandleGetCollectorConfigurations(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// GET /plugins/org.graylog.plugins.collector/configurations List all collector configurations
arr, total, sc, err := lgc.GetCollectorConfigurations()
if err != nil {
return arr, sc, err
}
cfgs := &graylog.CollectorConfigurationsBody{Configurations: arr, Total: total}
return cfgs, sc, nil
}
// HandleCreateCollectorConfiguration is the handler of Create an CollectorConfiguration API.
func HandleCreateCollectorConfiguration(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// POST /system/inputs Launch input on this node
// TODO authorize
body, sc, err := validateRequestBody(
r.Body, &validateReqBodyPrms{
Required: set.NewStrSet("tags", "inputs", "outputs", "snippets"),
Optional: set.NewStrSet("name", "id"),
ExtForbidden: true,
})
if err != nil {
return nil, sc, err
}
cfg := &graylog.CollectorConfiguration{}
if err := util.MSDecode(body, cfg); err != nil {
lgc.Logger().WithFields(log.Fields{
"body": body, "error": err,
}).Info("Failed to parse request body as CollectorConfiguration")
return nil, 400, err
}
sc, err = lgc.AddCollectorConfiguration(cfg)
if err != nil {
return nil, sc, err
}
if err := lgc.Save(); err != nil {
return nil, 500, err
}
return cfg, sc, nil
}
// HandleRenameCollectorConfiguration is the handler of Rename a CollectorConfiguration API.
func HandleRenameCollectorConfiguration(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// PUT /plugins/org.graylog.plugins.collector/configurations/{id}/name Updates a collector configuration name
id := ps.PathParam("collectorConfigurationID")
// TODO authorize
body, sc, err := validateRequestBody(
r.Body, &validateReqBodyPrms{
Required: set.NewStrSet("tags", "inputs", "outputs", "snippets"),
Optional: set.NewStrSet("name", "id"),
ExtForbidden: true,
})
if err != nil {
return nil, sc, err
}
prms := &graylog.CollectorConfiguration{}
if err := util.MSDecode(body, prms); err != nil {
lgc.Logger().WithFields(log.Fields{
"body": body, "error": err,
}).Info("Failed to parse request body as CollectorConfiguration")
return nil, 400, err
}
lgc.Logger().WithFields(log.Fields{
"body": body, "input": prms, "id": id,
}).Debug("request body")
cfg, sc, err := lgc.RenameCollectorConfiguration(id, prms.Name)
if err != nil {
return nil, sc, err
}
if err := lgc.Save(); err != nil {
return nil, 500, err
}
return cfg, sc, nil
}
// HandleDeleteCollectorConfiguration is the handler of Delete an CollectorConfiguration API.
func HandleDeleteCollectorConfiguration(
user *graylog.User, lgc *logic.Logic, r *http.Request, ps Params,
) (interface{}, int, error) {
// DELETE /plugins/org.graylog.plugins.collector/configurations/{id} Delete a collector configuration
id := ps.PathParam("collectorConfigurationID")
// TODO authorize
sc, err := lgc.DeleteCollectorConfiguration(id)
if err != nil {
return nil, sc, err
}
if err := lgc.Save(); err != nil {
return nil, 500, err
}
return nil, sc, nil
}