/
ruleset.go
300 lines (248 loc) · 9.35 KB
/
ruleset.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package pagerduty
import (
"fmt"
)
// RulesetService handles the communication with rulesets
// related methods of the PagerDuty API.
type RulesetService service
// Ruleset represents a ruleset.
type Ruleset struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
RoutingKeys []string `json:"routing_keys,omitempty"`
Team *RulesetObject `json:"team,omitempty"`
Updater *RulesetObject `json:"updater,omitempty"`
Creator *RulesetObject `json:"creator,omitempty"`
}
// RulesetObject represents a generic object that is common within a ruleset object
type RulesetObject struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
}
// RulesetPayload represents payload with a ruleset object
type RulesetPayload struct {
Ruleset *Ruleset `json:"ruleset,omitempty"`
}
// ListRulesetsResponse represents a list response of rulesets.
type ListRulesetsResponse struct {
Total int `json:"total,omitempty"`
Rulesets []*Ruleset `json:"rulesets,omitempty"`
Offset int `json:"offset,omitempty"`
More bool `json:"more,omitempty"`
Limit int `json:"limit,omitempty"`
}
// RulesetRule represents a Ruleset rule
type RulesetRule struct {
ID string `json:"id,omitempty"`
Position *int `json:"position,omitempty"`
Disabled bool `json:"disabled"`
Conditions *RuleConditions `json:"conditions,omitempty"`
Actions *RuleActions `json:"actions,omitempty"`
Ruleset *RulesetReference `json:"ruleset,omitempty"`
Self string `json:"self,omitempty"`
CatchAll bool `json:"catch_all,omitempty"`
TimeFrame *RuleTimeFrame `json:"time_frame,omitempty"`
Variables []*RuleVariable `json:"variables,omitempty"`
}
// RulesetRulePayload represents a payload for ruleset rules
type RulesetRulePayload struct {
Rule *RulesetRule `json:"rule,omitempty"`
}
// RuleConditions represents the conditions field for a Ruleset
type RuleConditions struct {
Operator string `json:"operator,omitempty"`
RuleSubconditions []*RuleSubcondition `json:"subconditions,omitempty"`
}
// RuleSubcondition represents a subcondition of a ruleset condition
type RuleSubcondition struct {
Operator string `json:"operator,omitempty"`
Parameters *ConditionParameter `json:"parameters,omitempty"`
}
// ConditionParameter represents parameters in a rule condition
type ConditionParameter struct {
Path string `json:"path,omitempty"`
Value string `json:"value,omitempty"`
}
// RuleTimeFrame represents a time_frame object on the rule object
type RuleTimeFrame struct {
ScheduledWeekly *ScheduledWeekly `json:"scheduled_weekly,omitempty"`
ActiveBetween *ActiveBetween `json:"active_between,omitempty"`
}
// RuleVariable represents a rule variable
type RuleVariable struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Parameters *RuleVariableParameter `json:"parameters,omitempty"`
}
// RuleVariableParameter represents a rule variable parameter
type RuleVariableParameter struct {
Value string `json:"value"`
Path string `json:"path"`
}
// ScheduledWeekly represents a time_frame object for scheduling rules weekly
type ScheduledWeekly struct {
Weekdays []int `json:"weekdays,omitempty"`
Timezone string `json:"timezone,omitempty"`
StartTime int `json:"start_time,omitempty"`
Duration int `json:"duration,omitempty"`
}
// ActiveBetween represents an active_between object for setting a timeline for rules
type ActiveBetween struct {
StartTime int `json:"start_time,omitempty"`
EndTime int `json:"end_time,omitempty"`
}
// ListRulesetRulesResponse represents a list of rules in a ruleset
type ListRulesetRulesResponse struct {
Total int `json:"total,omitempty"`
Rules []*RulesetRule `json:"rules,omitempty"`
Offset int `json:"offset,omitempty"`
More bool `json:"more,omitempty"`
Limit int `json:"limit,omitempty"`
}
// RuleActions represents a rule action
type RuleActions struct {
Suppress *RuleActionSuppress `json:"suppress"`
Annotate *RuleActionParameter `json:"annotate"`
Severity *RuleActionParameter `json:"severity"`
Priority *RuleActionParameter `json:"priority"`
Route *RuleActionParameter `json:"route"`
EventAction *RuleActionParameter `json:"event_action"`
Extractions []*RuleActionExtraction `json:"extractions,omitempty"`
Suspend *RuleActionIntParameter `json:"suspend"`
}
// RuleActionParameter represents a string parameter object on a rule action
type RuleActionParameter struct {
Value string `json:"value,omitempty"`
}
// RuleActionIntParameter represents an integer parameter object on a rule action
type RuleActionIntParameter struct {
Value int `json:"value"`
}
// RuleActionSuppress represents a rule suppress action object
type RuleActionSuppress struct {
Value bool `json:"value"`
ThresholdValue int `json:"threshold_value,omitempty"`
ThresholdTimeUnit string `json:"threshold_time_unit,omitempty"`
ThresholdTimeAmount int `json:"threshold_time_amount,omitempty"`
}
// RuleActionExtraction represents a rule extraction action object
type RuleActionExtraction struct {
Target string `json:"target,omitempty"`
Source string `json:"source,omitempty"`
Regex string `json:"regex,omitempty"`
Template string `json:"template,omitempty"`
}
// List lists existing rulesets.
func (s *RulesetService) List() (*ListRulesetsResponse, *Response, error) {
u := "/rulesets"
v := new(ListRulesetsResponse)
rulesets := make([]*Ruleset, 0)
// Create a handler closure capable of parsing data from the rulesets endpoint
// and appending resultant rulesets to the return slice.
responseHandler := func(response *Response) (ListResp, *Response, error) {
var result ListRulesetsResponse
if err := s.client.DecodeJSON(response, &result); err != nil {
return ListResp{}, response, err
}
rulesets = append(rulesets, result.Rulesets...)
// Return stats on the current page. Caller can use this information to
// adjust for requesting additional pages.
return ListResp{
More: result.More,
Offset: result.Offset,
Limit: result.Limit,
}, response, nil
}
err := s.client.newRequestPagedGetDo(u, responseHandler)
if err != nil {
return nil, nil, err
}
v.Rulesets = rulesets
return v, nil, nil
}
// Create creates a new ruleset.
func (s *RulesetService) Create(ruleset *Ruleset) (*Ruleset, *Response, error) {
u := "/rulesets"
v := new(RulesetPayload)
p := &RulesetPayload{Ruleset: ruleset}
resp, err := s.client.newRequestDo("POST", u, nil, p, v)
if err != nil {
return nil, nil, err
}
return v.Ruleset, resp, nil
}
// Get gets a new ruleset.
func (s *RulesetService) Get(ID string) (*Ruleset, *Response, error) {
u := fmt.Sprintf("/rulesets/%s", ID)
v := new(RulesetPayload)
p := &RulesetPayload{}
resp, err := s.client.newRequestDo("GET", u, nil, p, v)
if err != nil {
return nil, nil, err
}
return v.Ruleset, resp, nil
}
// Delete deletes an existing ruleset.
func (s *RulesetService) Delete(ID string) (*Response, error) {
u := fmt.Sprintf("/rulesets/%s", ID)
return s.client.newRequestDo("DELETE", u, nil, nil, nil)
}
// Update updates an existing ruleset.
func (s *RulesetService) Update(ID string, ruleset *Ruleset) (*Ruleset, *Response, error) {
u := fmt.Sprintf("/rulesets/%s", ID)
v := new(RulesetPayload)
p := RulesetPayload{Ruleset: ruleset}
resp, err := s.client.newRequestDo("PUT", u, nil, p, v)
if err != nil {
return nil, nil, err
}
return v.Ruleset, resp, nil
}
// ListRules Lists Event Rules for Ruleset
func (s *RulesetService) ListRules(rulesetID string) (*ListRulesetRulesResponse, *Response, error) {
u := fmt.Sprintf("/rulesets/%s/rules", rulesetID)
v := new(ListRulesetRulesResponse)
resp, err := s.client.newRequestDo("GET", u, nil, nil, &v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
// CreateRule for Ruleset
func (s *RulesetService) CreateRule(rulesetID string, rule *RulesetRule) (*RulesetRule, *Response, error) {
u := fmt.Sprintf("/rulesets/%s/rules", rulesetID)
v := new(RulesetRulePayload)
p := RulesetRulePayload{Rule: rule}
resp, err := s.client.newRequestDo("POST", u, nil, p, &v)
if err != nil {
return nil, nil, err
}
return v.Rule, resp, nil
}
// GetRule for Ruleset
func (s *RulesetService) GetRule(rulesetID, ruleID string) (*RulesetRule, *Response, error) {
u := fmt.Sprintf("/rulesets/%s/rules/%s", rulesetID, ruleID)
v := new(RulesetRulePayload)
resp, err := s.client.newRequestDo("GET", u, nil, nil, &v)
if err != nil {
return nil, nil, err
}
return v.Rule, resp, nil
}
// UpdateRule for Ruleset
func (s *RulesetService) UpdateRule(rulesetID, ruleID string, rule *RulesetRule) (*RulesetRule, *Response, error) {
u := fmt.Sprintf("/rulesets/%s/rules/%s", rulesetID, ruleID)
v := new(RulesetRulePayload)
p := RulesetRulePayload{Rule: rule}
resp, err := s.client.newRequestDo("PUT", u, nil, p, &v)
if err != nil {
return nil, nil, err
}
return v.Rule, resp, nil
}
// DeleteRule deletes an existing rule from the ruleset.
func (s *RulesetService) DeleteRule(rulesetID, ruleID string) (*Response, error) {
u := fmt.Sprintf("/rulesets/%s/rules/%s", rulesetID, ruleID)
return s.client.newRequestDo("DELETE", u, nil, nil, nil)
}