This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
collector_configuration_output.go
77 lines (69 loc) · 2.39 KB
/
collector_configuration_output.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
package logic
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-graylog"
"github.com/suzuki-shunsuke/go-graylog/validator"
)
// HasCollectorConfigurationOutput returns whether the collector configuration exists.
func (lgc *Logic) HasCollectorConfigurationOutput(cfgID, outputID string) (bool, error) {
return lgc.store.HasCollectorConfigurationOutput(cfgID, outputID)
}
// AddCollectorConfigurationOutput adds a collector configuration output to the mock server.
func (lgc *Logic) AddCollectorConfigurationOutput(id string, output *graylog.CollectorConfigurationOutput) (int, error) {
if id == "" {
return 400, fmt.Errorf("id is required")
}
if err := validator.CreateValidator.Struct(output); err != nil {
return 400, err
}
if err := lgc.store.AddCollectorConfigurationOutput(id, output); err != nil {
return 500, err
}
// 202 no content
return 202, nil
}
// UpdateCollectorConfigurationOutput updates a collector configuration output.
func (lgc *Logic) UpdateCollectorConfigurationOutput(
cfgID, outputID string, output *graylog.CollectorConfigurationOutput,
) (int, error) {
if cfgID == "" {
return 400, fmt.Errorf("collector configuration id is required")
}
if outputID == "" {
return 400, fmt.Errorf("collector configuration output id is required")
}
if err := validator.UpdateValidator.Struct(output); err != nil {
return 400, err
}
ok, err := lgc.HasCollectorConfigurationOutput(cfgID, outputID)
if err != nil {
return 500, err
}
if !ok {
return 404, fmt.Errorf("the collector configuration output is not found")
}
if err := lgc.store.UpdateCollectorConfigurationOutput(cfgID, outputID, output); err != nil {
return 500, err
}
// 202 no content
return 200, nil
}
// DeleteCollectorConfigurationOutput deletes a collector configuration output from the mock server.
func (lgc *Logic) DeleteCollectorConfigurationOutput(cfgID, outputID string) (int, error) {
ok, err := lgc.HasCollectorConfigurationOutput(cfgID, outputID)
if err != nil {
lgc.Logger().WithFields(log.Fields{
"error": err, "configuration_id": cfgID,
"output_id": outputID,
}).Error("failed to check whether the collector configuration exists")
return 500, err
}
if !ok {
return 404, fmt.Errorf("the collector configuration output is not found")
}
if err := lgc.store.DeleteCollectorConfigurationOutput(cfgID, outputID); err != nil {
return 500, err
}
return 204, nil
}