This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
input.go
115 lines (102 loc) · 3.18 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
package graylog
import (
"encoding/json"
"github.com/suzuki-shunsuke/go-graylog/v9/util"
"github.com/suzuki-shunsuke/go-ptr"
)
type (
// Input represents Graylog Input.
Input struct {
// Select a name of your new input that describes it.
Title string `json:"title,omitempty" v-create:"required"`
// https://github.com/Graylog2/graylog2-server/issues/3480
// update input overwrite attributes
Attrs InputAttrs `json:"attributes,omitempty" v-create:"required"`
ID string `json:"id,omitempty" v-create:"isdefault"`
// Should this input start on all nodes
Global bool `json:"global"`
// On which node should this input start
// ex. "2ad6b340-3e5f-4a96-ae81-040cfb8b6024"
Node string `json:"node,omitempty"`
// ex. 2018-02-24T03:02:26.001Z
CreatedAt string `json:"created_at,omitempty" v-create:"isdefault"`
// ex. "admin"
CreatorUserID string `json:"creator_user_id,omitempty" v-create:"isdefault"`
StaticFields map[string]string `json:"static_fields,omitempty"`
// ContextPack `json:"context_pack,omitempty"`
}
// InputUpdateParams represents Graylog Input update API's parameter.
InputUpdateParams struct {
ID string `json:"id,omitempty" v-update:"required,objectid"`
Title string `json:"title,omitempty" v-update:"required"`
Attrs InputAttrs `json:"attributes,omitempty" v-update:"required"`
Global *bool `json:"global,omitempty"`
Node string `json:"node,omitempty"`
}
// InputsBody represents Get Inputs API's response body.
// Basically users don't use this struct, but this struct is public because some sub packages use this struct.
InputsBody struct {
Inputs []Input `json:"inputs"`
Total int `json:"total"`
}
)
// Type returns the input's type.
func (input Input) Type() string {
if input.Attrs == nil {
return ""
}
return input.Attrs.InputType()
}
// NewUpdateParams converts Input to InputUpdateParams.
func (input *Input) NewUpdateParams() *InputUpdateParams {
return &InputUpdateParams{
ID: input.ID,
Title: input.Title,
Attrs: input.Attrs,
Node: input.Node,
Global: ptr.PBool(input.Global),
}
}
// ToData assigns input's data to InputData.
func (input *Input) ToData() (*InputData, error) {
d := &InputData{
Title: input.Title,
Type: input.Type(),
ID: input.ID,
Global: input.Global,
Node: input.Node,
CreatedAt: input.CreatedAt,
CreatorUserID: input.CreatorUserID,
Attrs: map[string]interface{}{},
}
if input.Attrs == nil {
return d, nil
}
return d, util.MSDecode(input.Attrs, &d.Attrs)
}
// UnmarshalJSON is the implementation of the json.Unmarshaler interface.
func (input *Input) UnmarshalJSON(b []byte) error {
d, err := input.ToData()
if err != nil {
return err
}
if err := json.Unmarshal(b, d); err != nil {
return err
}
return d.ToInput(input)
}
// MarshalJSON is the implementation of the json.Marshaler interface.
func (input *Input) MarshalJSON() ([]byte, error) {
d, err := input.ToData()
if err != nil {
return nil, err
}
return json.Marshal(d)
}
// Type returns the input's type.
func (input InputUpdateParams) Type() string {
if input.Attrs == nil {
return ""
}
return input.Attrs.InputType()
}