Skip to content

Commit c823d20

Browse files
committed
fix alert channel type
1 parent d0ad712 commit c823d20

File tree

3 files changed

+62
-24
lines changed

3 files changed

+62
-24
lines changed

alert_channels.go

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ package linodego
22

33
import (
44
"context"
5+
"encoding/json"
6+
"github.com/linode/linodego/internal/parseabletime"
7+
"time"
8+
)
9+
10+
type AlertNotificationType string
11+
12+
const (
13+
EmailAlertNotification AlertNotificationType = "email"
14+
)
15+
16+
type AlertChannelType string
17+
18+
const (
19+
SystemAlertChannel AlertChannelType = "system"
20+
UserAlertChannel AlertChannelType = "user"
521
)
622

723
// AlertChannelEnvelope represents a single alert channel entry returned inside alert definition
@@ -14,19 +30,16 @@ type AlertChannelEnvelope struct {
1430

1531
// AlertChannel represents a Monitor Channel object.
1632
type AlertChannel struct {
17-
ID int `json:"id"`
18-
Label string `json:"label"`
19-
ChannelType string `json:"channel_type"`
20-
Content ChannelContent `json:"content"`
21-
Created string `json:"created"`
22-
CreatedBy string `json:"created_by"`
23-
Updated string `json:"updated"`
24-
UpdatedBy string `json:"updated_by"`
25-
}
26-
27-
// AlertChannelDetailOptions are the options used to create the details of a new Monitor Channel.
28-
type AlertChannelDetailOptions struct {
29-
To string `json:"to,omitempty"`
33+
Alerts []AlertChannelEnvelope `json:"alerts"`
34+
ChannelType AlertNotificationType `json:"channel_type"`
35+
Content ChannelContent `json:"content"`
36+
Created *time.Time `json:"-"`
37+
CreatedBy string `json:"created_by"`
38+
Updated *time.Time `json:"-"`
39+
UpdatedBy string `json:"updated_by"`
40+
ID int `json:"id"`
41+
Label string `json:"label"`
42+
Type AlertChannelType `json:"type"`
3043
}
3144

3245
type EmailChannelContent struct {
@@ -35,10 +48,33 @@ type EmailChannelContent struct {
3548

3649
// ChannelContent represents the content block for an AlertChannel, which varies by channel type.
3750
type ChannelContent struct {
38-
Email *EmailChannelContent `json:"email,omitempty"`
51+
Email *EmailChannelContent `json:"email"`
3952
// Other channel types like 'webhook', 'slack' could be added here as optional fields.
4053
}
4154

55+
// UnmarshalJSON implements the json.Unmarshaler interface
56+
func (a *AlertChannel) UnmarshalJSON(b []byte) error {
57+
type Mask AlertChannel
58+
59+
p := struct {
60+
*Mask
61+
62+
Updated *parseabletime.ParseableTime `json:"updated"`
63+
Created *parseabletime.ParseableTime `json:"created"`
64+
}{
65+
Mask: (*Mask)(a),
66+
}
67+
68+
if err := json.Unmarshal(b, &p); err != nil {
69+
return err
70+
}
71+
72+
a.Updated = (*time.Time)(p.Updated)
73+
a.Created = (*time.Time)(p.Created)
74+
75+
return nil
76+
}
77+
4278
// ListAlertChannels gets a paginated list of Alert Channels.
4379
func (c *Client) ListAlertChannels(ctx context.Context, opts *ListOptions) ([]AlertChannel, error) {
4480
endpoint := formatAPIPath("monitor/alert-channels")

test/integration/fixtures/TestListMonitorAlertChannels.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ interactions:
6767
"url": "/monitor/alerts-definitions/10583", "type": "alerts-definitions"}, {"id":
6868
10584, "label": "ansible-test-32062765-updated", "url": "/monitor/alerts-definitions/10584",
6969
"type": "alerts-definitions"}, {"id": 10620, "label": "go-test-alert-definition-create-updated",
70-
"url": "/monitor/alerts-definitions/10620", "type": "alerts-definitions"}],
70+
"url": "/monitor/alerts-definitions/10620", "type": "alerts-definitions"}, {"id":
71+
10621, "label": "go-test-alert-definition-idempotency-1766162641225442000",
72+
"url": "/monitor/alerts-definitions/10621", "type": "alerts-definitions"}],
7173
"created": "2018-01-02T03:04:05", "updated": "2018-01-02T03:04:05", "created_by":
7274
"system", "updated_by": "system"}]}'
7375
headers:
@@ -92,7 +94,7 @@ interactions:
9294
Content-Type:
9395
- application/json
9496
Expires:
95-
- Fri, 19 Dec 2025 16:42:26 GMT
97+
- Mon, 22 Dec 2025 15:52:06 GMT
9698
Pragma:
9799
- no-cache
98100
Strict-Transport-Security:

test/integration/fixtures/TestMonitorAlertDefinition_instance.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ interactions:
200200
Content-Type:
201201
- application/json
202202
Expires:
203-
- Fri, 19 Dec 2025 16:36:22 GMT
203+
- Mon, 22 Dec 2025 15:53:01 GMT
204204
Pragma:
205205
- no-cache
206206
Strict-Transport-Security:
@@ -239,7 +239,7 @@ interactions:
239239
url: https://api.linode.com/v4beta/monitor/services/dbaas/alert-definitions
240240
method: POST
241241
response:
242-
body: '{"id": 10620, "label": "go-test-alert-definition-create", "description":
242+
body: '{"id": 10622, "label": "go-test-alert-definition-create", "description":
243243
"Test alert definition creation", "service_type": "dbaas", "type": "user", "scope":
244244
"entity", "class": null, "regions": [], "status": "enabled", "entity_ids": [],
245245
"has_more_resources": false, "severity": 2, "rule_criteria": {"rules": [{"label":
@@ -273,7 +273,7 @@ interactions:
273273
Content-Type:
274274
- application/json
275275
Expires:
276-
- Fri, 19 Dec 2025 16:36:23 GMT
276+
- Mon, 22 Dec 2025 15:53:02 GMT
277277
Pragma:
278278
- no-cache
279279
Strict-Transport-Security:
@@ -308,10 +308,10 @@ interactions:
308308
- application/json
309309
User-Agent:
310310
- linodego/dev https://github.com/linode/linodego
311-
url: https://api.linode.com/v4beta/monitor/services/dbaas/alert-definitions/10620
311+
url: https://api.linode.com/v4beta/monitor/services/dbaas/alert-definitions/10622
312312
method: PUT
313313
response:
314-
body: '{"id": 10620, "label": "go-test-alert-definition-create-updated", "description":
314+
body: '{"id": 10622, "label": "go-test-alert-definition-create-updated", "description":
315315
"Test alert definition creation", "service_type": "dbaas", "type": "user", "scope":
316316
"entity", "class": null, "regions": [], "status": "disabled", "entity_ids":
317317
[], "has_more_resources": false, "severity": 2, "rule_criteria": {"rules": [{"label":
@@ -345,7 +345,7 @@ interactions:
345345
Content-Type:
346346
- application/json
347347
Expires:
348-
- Fri, 19 Dec 2025 16:37:26 GMT
348+
- Mon, 22 Dec 2025 15:54:04 GMT
349349
Pragma:
350350
- no-cache
351351
Strict-Transport-Security:
@@ -379,7 +379,7 @@ interactions:
379379
- application/json
380380
User-Agent:
381381
- linodego/dev https://github.com/linode/linodego
382-
url: https://api.linode.com/v4beta/monitor/services/dbaas/alert-definitions/10620
382+
url: https://api.linode.com/v4beta/monitor/services/dbaas/alert-definitions/10622
383383
method: DELETE
384384
response:
385385
body: '{}'
@@ -407,7 +407,7 @@ interactions:
407407
Content-Type:
408408
- application/json
409409
Expires:
410-
- Fri, 19 Dec 2025 16:37:27 GMT
410+
- Mon, 22 Dec 2025 15:54:05 GMT
411411
Pragma:
412412
- no-cache
413413
Strict-Transport-Security:

0 commit comments

Comments
 (0)