-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathmonitor_alert_definitions.go
More file actions
270 lines (227 loc) · 9.44 KB
/
monitor_alert_definitions.go
File metadata and controls
270 lines (227 loc) · 9.44 KB
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
package linodego
import (
"context"
"encoding/json"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
type AlertDefinitionStatus string
const (
AlertDefinitionStatusProvisioning AlertDefinitionStatus = "provisioning"
AlertDefinitionStatusEnabling AlertDefinitionStatus = "enabling"
AlertDefinitionStatusDisabling AlertDefinitionStatus = "disabling"
AlertDefinitionStatusEnabled AlertDefinitionStatus = "enabled"
AlertDefinitionStatusDisabled AlertDefinitionStatus = "disabled"
AlertDefinitionStatusFailed AlertDefinitionStatus = "failed"
)
// AlertDefinition represents an ACLP Alert Definition object
type AlertDefinition struct {
ID int `json:"id"`
Label string `json:"label"`
Severity int `json:"severity"`
Type string `json:"type"`
ServiceType string `json:"service_type"`
Status AlertDefinitionStatus `json:"status"`
HasMoreResources bool `json:"has_more_resources"`
RuleCriteria RuleCriteria `json:"rule_criteria"`
TriggerConditions TriggerConditions `json:"trigger_conditions"`
AlertChannels []AlertChannelEnvelope `json:"alert_channels"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
UpdatedBy string `json:"updated_by"`
CreatedBy string `json:"created_by"`
EntityIDs []string `json:"entity_ids"`
Description string `json:"description"`
Class string `json:"class"`
}
// Backwards-compatible alias
// MonitorAlertDefinition represents an ACLP Alert Definition object
//
// Deprecated: AlertDefinition should be used in all new implementations.
type MonitorAlertDefinition = AlertDefinition
// TriggerConditions represents the trigger conditions for an alert.
type TriggerConditions struct {
CriteriaCondition string `json:"criteria_condition,omitempty"`
EvaluationPeriodSeconds int `json:"evaluation_period_seconds,omitempty"`
PollingIntervalSeconds int `json:"polling_interval_seconds,omitempty"`
TriggerOccurrences int `json:"trigger_occurrences,omitempty"`
}
// RuleCriteria represents the rule criteria for an alert.
type RuleCriteria struct {
Rules []Rule `json:"rules,omitempty"`
}
// Rule represents a single rule for an alert.
type Rule struct {
AggregateFunction string `json:"aggregate_function"`
DimensionFilters []DimensionFilter `json:"dimension_filters"`
Label string `json:"label"`
Metric string `json:"metric"`
Operator string `json:"operator"`
Threshold float64 `json:"threshold"`
Unit string `json:"unit"`
}
// DimensionFilter represents a single dimension filter used inside a Rule.
type DimensionFilter struct {
DimensionLabel string `json:"dimension_label"`
Label string `json:"label"`
Operator string `json:"operator"`
Value string `json:"value"`
}
// RuleCriteriaOptions represents the rule criteria options for an alert.
type RuleCriteriaOptions struct {
Rules []RuleOptions `json:"rules,omitempty"`
}
// RuleOptions represents a single rule option for an alert.
type RuleOptions struct {
AggregateFunction string `json:"aggregate_function,omitempty"`
DimensionFilters []DimensionFilterOptions `json:"dimension_filters,omitempty"`
Metric string `json:"metric,omitempty"`
Operator string `json:"operator,omitempty"`
Threshold float64 `json:"threshold,omitempty"`
}
// DimensionFilterOptions represents a single dimension filter option used inside a Rule.
type DimensionFilterOptions struct {
DimensionLabel string `json:"dimension_label,omitempty"`
Operator string `json:"operator,omitempty"`
Value string `json:"value,omitempty"`
}
// AlertChannelEnvelope represents a single alert channel entry returned inside alert definition
type AlertChannelEnvelope struct {
ID int `json:"id"`
Label string `json:"label"`
Type string `json:"type"`
URL string `json:"url"`
}
// AlertType represents the type of alert: "user" or "system"
type AlertType string
const (
AlertTypeUser AlertType = "user"
AlertTypeSystem AlertType = "system"
)
// Severity represents the severity level of an alert.
// 0 = Severe, 1 = Medium, 2 = Low, 3 = Info
type Severity int
const (
SeveritySevere Severity = 0
SeverityMedium Severity = 1
SeverityLow Severity = 2
SeverityInfo Severity = 3
)
// CriteriaCondition represents supported criteria conditions
type CriteriaCondition string
const (
CriteriaConditionAll CriteriaCondition = "ALL"
)
// AlertDefinitionCreateOptions are the options used to create a new alert definition.
type AlertDefinitionCreateOptions struct {
Label string `json:"label"`
Severity int `json:"severity"`
ChannelIDs []int `json:"channel_ids"`
RuleCriteria *RuleCriteriaOptions `json:"rule_criteria,omitempty"`
TriggerConditions *TriggerConditions `json:"trigger_conditions,omitempty"`
EntityIDs []string `json:"entity_ids,omitempty"`
Description *string `json:"description,omitempty"`
}
// AlertDefinitionUpdateOptions are the options used to update an alert definition.
type AlertDefinitionUpdateOptions struct {
Label string `json:"label"`
Severity int `json:"severity"`
ChannelIDs []int `json:"channel_ids"`
RuleCriteria *RuleCriteriaOptions `json:"rule_criteria,omitempty"`
TriggerConditions *TriggerConditions `json:"trigger_conditions,omitempty"`
EntityIDs []string `json:"entity_ids,omitempty"`
Description *string `json:"description,omitempty"`
Status *AlertDefinitionStatus `json:"status,omitempty"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *AlertDefinition) UnmarshalJSON(b []byte) error {
type Mask AlertDefinition
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
i.Created = (*time.Time)(p.Created)
i.Updated = (*time.Time)(p.Updated)
return nil
}
// ListMonitorAlertDefinitions returns a paginated list of ACLP Monitor Alert Definitions by service type.
func (c *Client) ListMonitorAlertDefinitions(
ctx context.Context,
serviceType string,
opts *ListOptions,
) ([]AlertDefinition, error) {
endpoint := formatAPIPath("monitor/services/%s/alert-definitions", serviceType)
return getPaginatedResults[AlertDefinition](ctx, c, endpoint, opts)
}
// ListAllMonitorAlertDefinitions returns a paginated list of all ACLP Monitor Alert Definitions under this account.
func (c *Client) ListAllMonitorAlertDefinitions(
ctx context.Context,
opts *ListOptions,
) ([]AlertDefinition, error) {
endpoint := formatAPIPath("monitor/alert-definitions")
return getPaginatedResults[AlertDefinition](ctx, c, endpoint, opts)
}
// GetMonitorAlertDefinition gets an ACLP Monitor Alert Definition.
func (c *Client) GetMonitorAlertDefinition(
ctx context.Context,
serviceType string,
alertID int,
) (*MonitorAlertDefinition, error) {
e := formatAPIPath("monitor/services/%s/alert-definitions/%d", serviceType, alertID)
return doGETRequest[AlertDefinition](ctx, c, e)
}
// CreateMonitorAlertDefinition creates an ACLP Monitor Alert Definition.
func (c *Client) CreateMonitorAlertDefinition(
ctx context.Context,
serviceType string,
opts AlertDefinitionCreateOptions,
) (*MonitorAlertDefinition, error) {
e := formatAPIPath("monitor/services/%s/alert-definitions", serviceType)
return doPOSTRequest[AlertDefinition](ctx, c, e, opts)
}
// CreateMonitorAlertDefinitionWithIdempotency creates an ACLP Monitor Alert Definition
// and optionally sends an Idempotency-Key header to make the request idempotent.
func (c *Client) CreateMonitorAlertDefinitionWithIdempotency(
ctx context.Context,
serviceType string,
opts AlertDefinitionCreateOptions,
idempotencyKey string,
) (*MonitorAlertDefinition, error) {
e := formatAPIPath("monitor/services/%s/alert-definitions", serviceType)
var result AlertDefinition
req := c.R(ctx).SetResult(&result)
if idempotencyKey != "" {
req.SetHeader("Idempotency-Key", idempotencyKey)
}
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
req.SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*AlertDefinition), nil
}
// UpdateMonitorAlertDefinition updates an ACLP Monitor Alert Definition.
func (c *Client) UpdateMonitorAlertDefinition(
ctx context.Context,
serviceType string,
alertID int,
opts AlertDefinitionUpdateOptions,
) (*AlertDefinition, error) {
e := formatAPIPath("monitor/services/%s/alert-definitions/%d", serviceType, alertID)
return doPUTRequest[AlertDefinition](ctx, c, e, opts)
}
// DeleteMonitorAlertDefinition deletes an ACLP Monitor Alert Definition.
func (c *Client) DeleteMonitorAlertDefinition(ctx context.Context, serviceType string, alertID int) error {
e := formatAPIPath("monitor/services/%s/alert-definitions/%d", serviceType, alertID)
return doDELETERequest(ctx, c, e)
}