This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
input.go
98 lines (88 loc) · 2.37 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
package logic
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-graylog"
"github.com/suzuki-shunsuke/go-graylog/validator"
)
// HasInput returns whether the input exists.
func (lgc *Logic) HasInput(id string) (bool, error) {
return lgc.store.HasInput(id)
}
// GetInput returns an input.
// If an input is not found, returns an error.
func (lgc *Logic) GetInput(id string) (*graylog.Input, int, error) {
if id == "" {
return nil, 400, fmt.Errorf("input id is empty")
}
if err := ValidateObjectID(id); err != nil {
// unfortunately graylog returns not 400 but 404.
return nil, 404, err
}
input, err := lgc.store.GetInput(id)
if err != nil {
return input, 500, err
}
if input == nil {
return nil, 404, fmt.Errorf("no input with id <%s> is found", id)
}
return input, 200, nil
}
// AddInput adds an input to the mock server.
func (lgc *Logic) AddInput(input *graylog.Input) (int, error) {
if err := validator.CreateValidator.Struct(input); err != nil {
return 400, err
}
if err := lgc.store.AddInput(input); err != nil {
return 500, err
}
return 200, nil
}
// UpdateInput updates an input at the Server.
// Required: Title, Type, Attrs
// Allowed: Global, Node
func (lgc *Logic) UpdateInput(prms *graylog.InputUpdateParams) (*graylog.Input, int, error) {
if prms == nil {
return nil, 400, fmt.Errorf("input is nil")
}
if err := validator.UpdateValidator.Struct(prms); err != nil {
return nil, 400, err
}
ok, err := lgc.HasInput(prms.ID)
if err != nil {
return nil, 500, err
}
if !ok {
return nil, 404, fmt.Errorf("the input <%s> is not found", prms.ID)
}
input, err := lgc.store.UpdateInput(prms)
if err != nil {
return nil, 500, err
}
return input, 200, nil
}
// DeleteInput deletes a input from the mock server.
func (lgc *Logic) DeleteInput(id string) (int, error) {
ok, err := lgc.HasInput(id)
if err != nil {
lgc.Logger().WithFields(log.Fields{
"error": err, "id": id,
}).Error("lgc.HasInput() is failure")
return 500, err
}
if !ok {
return 404, fmt.Errorf("the input <%s> is not found", id)
}
if err := lgc.store.DeleteInput(id); err != nil {
return 500, err
}
return 200, nil
}
// GetInputs returns a list of inputs.
func (lgc *Logic) GetInputs() ([]graylog.Input, int, int, error) {
inputs, total, err := lgc.store.GetInputs()
if err != nil {
return nil, 0, 500, err
}
return inputs, total, 200, nil
}