forked from sensu/sensu-go
/
hook.go
182 lines (148 loc) · 4.28 KB
/
hook.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package v2
import (
"errors"
fmt "fmt"
"net/url"
"path"
"regexp"
"time"
jsoniter "github.com/json-iterator/go"
)
const (
// HooksResource is the name of this resource type
HooksResource = "hooks"
// HookRequestType is the message type string for hook request.
HookRequestType = "hook_request"
)
var (
// CheckHookRegexStr used to validate type of check hook
CheckHookRegexStr = `([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])`
// CheckHookRegex used to validate type of check hook
CheckHookRegex = regexp.MustCompile("^" + CheckHookRegexStr + "$")
// Severities used to validate type of check hook
Severities = []string{"ok", "warning", "critical", "unknown", "non-zero"}
)
// Validate returns an error if the hook does not pass validation tests.
func (h *Hook) Validate() error {
if err := h.HookConfig.Validate(); err != nil {
return err
}
if h.Status < 0 {
return errors.New("hook status must be greater than or equal to 0")
}
return nil
}
// StorePrefix returns the path prefix to this resource in the store
func (c *HookConfig) StorePrefix() string {
return HooksResource
}
// URIPath returns the path component of a hook URI.
func (c *HookConfig) URIPath() string {
return path.Join(URLPrefix, "namespaces", url.PathEscape(c.Namespace), HooksResource, url.PathEscape(c.Name))
}
// Validate returns an error if the hook does not pass validation tests.
func (c *HookConfig) Validate() error {
if err := ValidateName(c.Name); err != nil {
return errors.New("hook name " + err.Error())
}
if c.Command == "" {
return errors.New("command cannot be empty")
}
if c.Timeout <= 0 {
return errors.New("hook timeout must be greater than 0")
}
if c.Namespace == "" {
return errors.New("namespace must be set")
}
return nil
}
// Validate returns an error if the check hook does not pass validation tests.
func (h *HookList) Validate() error {
if h.Type == "" {
return errors.New("type cannot be empty")
}
if h.Hooks == nil || len(h.Hooks) == 0 {
return errors.New("hooks cannot be empty")
}
if !(CheckHookRegex.MatchString(h.Type) || isSeverity(h.Type)) {
return errors.New(
"valid check hook types are \"0\"-\"255\", \"ok\", \"warning\", \"critical\", \"unknown\", and \"non-zero\"",
)
}
return nil
}
func isSeverity(name string) bool {
for _, sev := range Severities {
if sev == name {
return true
}
}
return false
}
// MarshalJSON implements the json.Marshaler interface.
func (h *HookList) MarshalJSON() ([]byte, error) {
result := map[string][]string{h.Type: h.Hooks}
return jsoniter.Marshal(result)
}
// UnmarshalJSON implements the json.Marshaler interface.
func (h *HookList) UnmarshalJSON(b []byte) error {
result := map[string][]string{}
if err := jsoniter.Unmarshal(b, &result); err != nil {
return err
}
for k, v := range result {
h.Type = k
h.Hooks = v
}
return nil
}
// FixtureHookConfig returns a fixture for a HookConfig object.
func FixtureHookConfig(name string) *HookConfig {
timeout := uint32(10)
return &HookConfig{
Command: "true",
Timeout: timeout,
Stdin: false,
ObjectMeta: NewObjectMeta(name, "default"),
}
}
// FixtureHook returns a fixture for a Hook object.
func FixtureHook(id string) *Hook {
t := time.Now().Unix()
config := FixtureHookConfig(id)
return &Hook{
Status: 0,
Output: "",
Issued: t,
Executed: t + 1,
Duration: 1.0,
HookConfig: *config,
}
}
// FixtureHookList returns a fixture for a HookList object.
func FixtureHookList(hookName string) *HookList {
return &HookList{
Hooks: []string{hookName},
Type: "non-zero",
}
}
// URIPath returns the path component of a Hook URI.
func (h *Hook) URIPath() string {
return fmt.Sprintf("/api/core/v2/namespaces/%s/hooks/%s", url.PathEscape(h.Namespace), url.PathEscape(h.Name))
}
// HookConfigFields returns a set of fields that represent that resource
func HookConfigFields(r Resource) map[string]string {
resource := r.(*HookConfig)
return map[string]string{
"hook.name": resource.ObjectMeta.Name,
"hook.namespace": resource.ObjectMeta.Namespace,
}
}
// SetNamespace sets the namespace of the resource.
func (c *HookConfig) SetNamespace(namespace string) {
c.Namespace = namespace
}
// SetNamespace sets the namespace of the resource.
func (h *Hook) SetNamespace(namespace string) {
h.Namespace = namespace
}