monitor_alert_definitions.go raw
1 package linodego
2
3 import (
4 "context"
5 "encoding/json"
6 "time"
7
8 "github.com/linode/linodego/internal/parseabletime"
9 )
10
11 // AlertDefinition represents an ACLP Alert Definition object
12 type AlertDefinition struct {
13 ID int `json:"id"`
14 Label string `json:"label"`
15 Severity int `json:"severity"`
16 Type string `json:"type"`
17 ServiceType string `json:"service_type"`
18 Status string `json:"status"`
19 HasMoreResources bool `json:"has_more_resources"`
20 RuleCriteria RuleCriteria `json:"rule_criteria"`
21 TriggerConditions TriggerConditions `json:"trigger_conditions"`
22 AlertChannels []AlertChannelEnvelope `json:"alert_channels"`
23 Created *time.Time `json:"-"`
24 Updated *time.Time `json:"-"`
25 UpdatedBy string `json:"updated_by"`
26 CreatedBy string `json:"created_by"`
27 EntityIDs []any `json:"entity_ids"`
28 Description string `json:"description"`
29 Class string `json:"class"`
30 }
31
32 // Backwards-compatible alias
33
34 // MonitorAlertDefinition represents an ACLP Alert Definition object
35 //
36 // Deprecated: AlertDefinition should be used in all new implementations.
37 type MonitorAlertDefinition = AlertDefinition
38
39 // TriggerConditions represents the trigger conditions for an alert.
40 type TriggerConditions struct {
41 CriteriaCondition string `json:"criteria_condition,omitempty"`
42 EvaluationPeriodSeconds int `json:"evaluation_period_seconds,omitempty"`
43 PollingIntervalSeconds int `json:"polling_interval_seconds,omitempty"`
44 TriggerOccurrences int `json:"trigger_occurrences,omitempty"`
45 }
46
47 // RuleCriteria represents the rule criteria for an alert.
48 type RuleCriteria struct {
49 Rules []Rule `json:"rules,omitempty"`
50 }
51
52 // Rule represents a single rule for an alert.
53 type Rule struct {
54 AggregateFunction string `json:"aggregate_function"`
55 DimensionFilters []DimensionFilter `json:"dimension_filters"`
56 Label string `json:"label"`
57 Metric string `json:"metric"`
58 Operator string `json:"operator"`
59 Threshold float64 `json:"threshold"`
60 Unit string `json:"unit"`
61 }
62
63 // DimensionFilter represents a single dimension filter used inside a Rule.
64 type DimensionFilter struct {
65 DimensionLabel string `json:"dimension_label"`
66 Label string `json:"label"`
67 Operator string `json:"operator"`
68 Value string `json:"value"`
69 }
70
71 // RuleCriteriaOptions represents the rule criteria options for an alert.
72 type RuleCriteriaOptions struct {
73 Rules []RuleOptions `json:"rules,omitempty"`
74 }
75
76 // RuleOptions represents a single rule option for an alert.
77 type RuleOptions struct {
78 AggregateFunction string `json:"aggregate_function,omitempty"`
79 DimensionFilters []DimensionFilterOptions `json:"dimension_filters,omitempty"`
80 Metric string `json:"metric,omitempty"`
81 Operator string `json:"operator,omitempty"`
82 Threshold float64 `json:"threshold,omitempty"`
83 }
84
85 // DimensionFilterOptions represents a single dimension filter option used inside a Rule.
86 type DimensionFilterOptions struct {
87 DimensionLabel string `json:"dimension_label,omitempty"`
88 Operator string `json:"operator,omitempty"`
89 Value string `json:"value,omitempty"`
90 }
91
92 // AlertType represents the type of alert: "user" or "system"
93 type AlertType string
94
95 const (
96 AlertTypeUser AlertType = "user"
97 AlertTypeSystem AlertType = "system"
98 )
99
100 // Severity represents the severity level of an alert.
101 // 0 = Severe, 1 = Medium, 2 = Low, 3 = Info
102 type Severity int
103
104 const (
105 SeveritySevere Severity = 0
106 SeverityMedium Severity = 1
107 SeverityLow Severity = 2
108 SeverityInfo Severity = 3
109 )
110
111 // CriteriaCondition represents supported criteria conditions
112 type CriteriaCondition string
113
114 const (
115 CriteriaConditionAll CriteriaCondition = "ALL"
116 )
117
118 // AlertDefinitionCreateOptions are the options used to create a new alert definition.
119 type AlertDefinitionCreateOptions struct {
120 Label string `json:"label"`
121 Severity int `json:"severity"`
122 ChannelIDs []int `json:"channel_ids"`
123 RuleCriteria *RuleCriteriaOptions `json:"rule_criteria,omitempty"`
124 TriggerConditions *TriggerConditions `json:"trigger_conditions,omitempty"`
125 EntityIDs []string `json:"entity_ids,omitempty"`
126 Description *string `json:"description,omitempty"`
127 }
128
129 // AlertDefinitionUpdateOptions are the options used to update an alert definition.
130 type AlertDefinitionUpdateOptions struct {
131 Label string `json:"label"`
132 Severity int `json:"severity"`
133 ChannelIDs []int `json:"channel_ids"`
134 RuleCriteria *RuleCriteriaOptions `json:"rule_criteria,omitempty"`
135 TriggerConditions *TriggerConditions `json:"trigger_conditions,omitempty"`
136 EntityIDs []string `json:"entity_ids,omitempty"`
137 Description *string `json:"description,omitempty"`
138 }
139
140 // UnmarshalJSON implements the json.Unmarshaler interface
141 func (i *AlertDefinition) UnmarshalJSON(b []byte) error {
142 type Mask AlertDefinition
143
144 p := struct {
145 *Mask
146
147 Created *parseabletime.ParseableTime `json:"created"`
148 Updated *parseabletime.ParseableTime `json:"updated"`
149 }{
150 Mask: (*Mask)(i),
151 }
152
153 if err := json.Unmarshal(b, &p); err != nil {
154 return err
155 }
156
157 i.Created = (*time.Time)(p.Created)
158 i.Updated = (*time.Time)(p.Updated)
159
160 return nil
161 }
162
163 // ListMonitorAlertDefinitions returns a paginated list of ACLP Monitor Alert Definitions by service type.
164 func (c *Client) ListMonitorAlertDefinitions(
165 ctx context.Context,
166 serviceType string,
167 opts *ListOptions,
168 ) ([]AlertDefinition, error) {
169 endpoint := formatAPIPath("monitor/services/%s/alert-definitions", serviceType)
170 return getPaginatedResults[AlertDefinition](ctx, c, endpoint, opts)
171 }
172
173 // ListAllMonitorAlertDefinitions returns a paginated list of all ACLP Monitor Alert Definitions under this account.
174 func (c *Client) ListAllMonitorAlertDefinitions(
175 ctx context.Context,
176 opts *ListOptions,
177 ) ([]AlertDefinition, error) {
178 endpoint := formatAPIPath("monitor/alert-definitions")
179 return getPaginatedResults[AlertDefinition](ctx, c, endpoint, opts)
180 }
181
182 // GetMonitorAlertDefinition gets an ACLP Monitor Alert Definition.
183 func (c *Client) GetMonitorAlertDefinition(
184 ctx context.Context,
185 serviceType string,
186 alertID int,
187 ) (*MonitorAlertDefinition, error) {
188 e := formatAPIPath("monitor/services/%s/alert-definitions/%d", serviceType, alertID)
189 return doGETRequest[AlertDefinition](ctx, c, e)
190 }
191
192 // CreateMonitorAlertDefinition creates an ACLP Monitor Alert Definition.
193 func (c *Client) CreateMonitorAlertDefinition(
194 ctx context.Context,
195 serviceType string,
196 opts AlertDefinitionCreateOptions,
197 ) (*MonitorAlertDefinition, error) {
198 e := formatAPIPath("monitor/services/%s/alert-definitions", serviceType)
199 return doPOSTRequest[AlertDefinition](ctx, c, e, opts)
200 }
201
202 // CreateMonitorAlertDefinitionWithIdempotency creates an ACLP Monitor Alert Definition
203 // and optionally sends an Idempotency-Key header to make the request idempotent.
204 func (c *Client) CreateMonitorAlertDefinitionWithIdempotency(
205 ctx context.Context,
206 serviceType string,
207 opts AlertDefinitionCreateOptions,
208 idempotencyKey string,
209 ) (*MonitorAlertDefinition, error) {
210 e := formatAPIPath("monitor/services/%s/alert-definitions", serviceType)
211
212 var result AlertDefinition
213
214 req := c.R(ctx).SetResult(&result)
215
216 if idempotencyKey != "" {
217 req.SetHeader("Idempotency-Key", idempotencyKey)
218 }
219
220 body, err := json.Marshal(opts)
221 if err != nil {
222 return nil, err
223 }
224
225 req.SetBody(string(body))
226
227 r, err := coupleAPIErrors(req.Post(e))
228 if err != nil {
229 return nil, err
230 }
231
232 return r.Result().(*AlertDefinition), nil
233 }
234
235 // UpdateMonitorAlertDefinition updates an ACLP Monitor Alert Definition.
236 func (c *Client) UpdateMonitorAlertDefinition(
237 ctx context.Context,
238 serviceType string,
239 alertID int,
240 opts AlertDefinitionUpdateOptions,
241 ) (*AlertDefinition, error) {
242 e := formatAPIPath("monitor/services/%s/alert-definitions/%d", serviceType, alertID)
243 return doPUTRequest[AlertDefinition](ctx, c, e, opts)
244 }
245
246 // DeleteMonitorAlertDefinition deletes an ACLP Monitor Alert Definition.
247 func (c *Client) DeleteMonitorAlertDefinition(ctx context.Context, serviceType string, alertID int) error {
248 e := formatAPIPath("monitor/services/%s/alert-definitions/%d", serviceType, alertID)
249 return doDELETERequest(ctx, c, e)
250 }
251