Ai
1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hook.go 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

搜索帮助