-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
84 lines (65 loc) · 1.87 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
package client
import (
"encoding/json"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
)
var hooksPath = createNSBasePath(coreAPIGroup, coreAPIVersion, "hooks")
// CreateHook creates new hook on configured Sensu instance
func (client *RestClient) CreateHook(hook *types.HookConfig) (err error) {
bytes, err := json.Marshal(hook)
if err != nil {
return err
}
path := hooksPath(hook.Namespace)
res, err := client.R().SetBody(bytes).Post(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// UpdateHook updates given hook on configured Sensu instance
func (client *RestClient) UpdateHook(hook *types.HookConfig) (err error) {
bytes, err := json.Marshal(hook)
if err != nil {
return err
}
path := hooksPath(hook.Namespace, hook.Name)
res, err := client.R().SetBody(bytes).Put(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// DeleteHook deletes hook from configured Sensu instance
func (client *RestClient) DeleteHook(namespace, name string) error {
return client.Delete(hooksPath(namespace, name))
}
// FetchHook fetches a specific hook
func (client *RestClient) FetchHook(name string) (*types.HookConfig, error) {
var hook *types.HookConfig
path := hooksPath(client.config.Namespace(), name)
res, err := client.R().Get(path)
if err != nil {
return nil, err
}
if res.StatusCode() >= 400 {
return nil, UnmarshalError(res)
}
err = json.Unmarshal(res.Body(), &hook)
return hook, err
}
// ListHooks fetches all hooks from configured Sensu instance
func (client *RestClient) ListHooks(namespace string, options *ListOptions) ([]corev2.HookConfig, error) {
var hooks []corev2.HookConfig
if err := client.List(hooksPath(namespace), &hooks, options); err != nil {
return hooks, err
}
return hooks, nil
}