This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
stream_alert_condition.go
141 lines (128 loc) · 4.76 KB
/
stream_alert_condition.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
package client
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/suzuki-shunsuke/go-graylog"
)
// GetStreamAlertConditions gets all alert conditions of this stream.
func (client *Client) GetStreamAlertConditions(streamID string) (
conds []graylog.AlertCondition, total int, ei *ErrorInfo, err error,
) {
return client.GetStreamAlertConditionsContext(context.Background(), streamID)
}
// GetStreamAlertConditionsContext gets all alert conditions of this stream with a context.
func (client *Client) GetStreamAlertConditionsContext(
ctx context.Context, streamID string,
) (conds []graylog.AlertCondition, total int, ei *ErrorInfo, err error) {
errMsg := "failed to get stream's alert conditions"
conditions := &graylog.AlertConditionsBody{}
u, err := client.Endpoints().StreamAlertConditions(streamID)
if err != nil {
return nil, 0, nil, errors.Wrap(err, errMsg)
}
ei, err = client.callGet(
ctx, u.String(), nil, conditions)
return conditions.AlertConditions, conditions.Total, ei, err
}
// GetStreamAlertCondition gets an alert condition.
func (client *Client) GetStreamAlertCondition(streamID, id string) (graylog.AlertCondition, *ErrorInfo, error) {
return client.GetStreamAlertConditionContext(context.Background(), streamID, id)
}
// GetStreamAlertConditionContext gets an alert condition with a context.
func (client *Client) GetStreamAlertConditionContext(
ctx context.Context, streamID, id string,
) (graylog.AlertCondition, *ErrorInfo, error) {
cond := graylog.AlertCondition{}
if streamID == "" {
return cond, nil, errors.New("stream id is empty")
}
if id == "" {
return cond, nil, errors.New("id is empty")
}
u, err := client.Endpoints().StreamAlertCondition(streamID, id)
if err != nil {
return cond, nil, err
}
ei, err := client.callGet(ctx, u.String(), nil, &cond)
return cond, ei, err
}
// CreateStreamAlertCondition creates an alert condition.
func (client *Client) CreateStreamAlertCondition(streamID string, cond *graylog.AlertCondition) (*ErrorInfo, error) {
return client.CreateStreamAlertConditionContext(context.Background(), streamID, cond)
}
// CreateStreamAlertConditionContext creates an alert condition with a context.
func (client *Client) CreateStreamAlertConditionContext(
ctx context.Context, streamID string, cond *graylog.AlertCondition,
) (*ErrorInfo, error) {
errMsg := "failed to create an alert condition"
if cond == nil {
return nil, fmt.Errorf("%s: alert condition is nil", errMsg)
}
u, err := client.Endpoints().StreamAlertConditions(streamID)
if err != nil {
return nil, errors.Wrap(err, errMsg)
}
ret := map[string]string{}
ei, err := client.callPost(ctx, u.String(), cond, &ret)
if err != nil {
return ei, errors.Wrap(err, errMsg)
}
if id, ok := ret["alert_condition_id"]; ok {
cond.ID = id
return ei, nil
}
return ei, fmt.Errorf(`%s: response doesn't have the field "alert_condition_id"`, errMsg)
}
// UpdateStreamAlertCondition modifies an alert condition.
func (client *Client) UpdateStreamAlertCondition(streamID string, cond *graylog.AlertCondition) (*ErrorInfo, error) {
return client.UpdateStreamAlertConditionContext(context.Background(), streamID, cond)
}
// UpdateStreamAlertConditionContext modifies an alert condition with a context.
func (client *Client) UpdateStreamAlertConditionContext(
ctx context.Context, streamID string, cond *graylog.AlertCondition,
) (*ErrorInfo, error) {
errMsg := "failed to update an alert condition"
if streamID == "" {
return nil, fmt.Errorf("%s: stream id is empty", errMsg)
}
if cond == nil {
return nil, fmt.Errorf("%s: alert condition is nil", errMsg)
}
condID := cond.ID
if condID == "" {
return nil, fmt.Errorf("%s: alert condition id is empty", errMsg)
}
u, err := client.Endpoints().StreamAlertCondition(streamID, condID)
if err != nil {
return nil, errors.Wrap(err, errMsg)
}
cond.ID = ""
ei, err := client.callPut(ctx, u.String(), cond, nil)
cond.ID = condID
if err != nil {
return ei, errors.Wrap(err, errMsg)
}
return ei, nil
}
// DeleteStreamAlertCondition deletes an alert condition.
func (client *Client) DeleteStreamAlertCondition(streamID, id string) (*ErrorInfo, error) {
return client.DeleteStreamAlertConditionContext(context.Background(), streamID, id)
}
// DeleteStreamAlertConditionContext deletes an alert condition with a context.
func (client *Client) DeleteStreamAlertConditionContext(
ctx context.Context, streamID, id string,
) (*ErrorInfo, error) {
errMsg := "failed to delete an alert condition"
if streamID == "" {
return nil, fmt.Errorf("%s: stream id is empty", errMsg)
}
if id == "" {
return nil, fmt.Errorf("%s: alert condition id is empty", errMsg)
}
u, err := client.Endpoints().StreamAlertCondition(streamID, id)
if err != nil {
return nil, errors.Wrap(err, errMsg)
}
return client.callDelete(ctx, u.String(), nil, nil)
}