Skip to content

Commit e2d958f

Browse files
authored
Support ACLP alerts integration with Linode (#650)
* init * address comments
1 parent 6dc0564 commit e2d958f

File tree

6 files changed

+126
-7
lines changed

6 files changed

+126
-7
lines changed

linode_api4/groups/linode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from linode_api4.objects.filtering import Filter
1919
from linode_api4.objects.linode import (
2020
Backup,
21+
InstanceACLPAlertsOptions,
2122
InstancePlacementGroupAssignment,
2223
InterfaceGeneration,
2324
NetworkInterface,
@@ -162,6 +163,9 @@ def instance_create(
162163
interface_generation: Optional[Union[InterfaceGeneration, str]] = None,
163164
network_helper: Optional[bool] = None,
164165
maintenance_policy: Optional[str] = None,
166+
alerts: Optional[
167+
Union[Dict[str, Any], InstanceACLPAlertsOptions]
168+
] = None,
165169
**kwargs,
166170
):
167171
"""
@@ -336,6 +340,9 @@ def instance_create(
336340
:param maintenance_policy: The slug of the maintenance policy to apply during maintenance.
337341
If not provided, the default policy (linode/migrate) will be applied.
338342
:type maintenance_policy: str
343+
:param alerts: ACLP alerts available to define during instance creation.
344+
This is v4beta only and may not be available to all users.
345+
:type alerts: dict[str, Any] or InstanceACLPAlertsOptions
339346
340347
:returns: A new Instance object, or a tuple containing the new Instance and
341348
the generated password.
@@ -356,6 +363,7 @@ def instance_create(
356363
"region": region,
357364
"image": image,
358365
"authorized_keys": load_and_validate_keys(authorized_keys),
366+
"alerts": alerts,
359367
# These will automatically be flattened below
360368
"firewall_id": firewall,
361369
"backup_id": backup,

linode_api4/objects/linode.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,31 @@ class UpgradeInterfacesResult(JSONObject):
766766
)
767767

768768

769+
@dataclass
770+
class InstanceAlerts(JSONObject):
771+
"""
772+
Represents both legacy and ACLP alerts for a Linode Instance.
773+
"""
774+
775+
cpu: int = 0
776+
io: int = 0
777+
network_in: int = 0
778+
network_out: int = 0
779+
transfer_quota: int = 0
780+
system_alerts: Optional[List[int]] = None
781+
user_alerts: Optional[List[int]] = None
782+
783+
784+
@dataclass
785+
class InstanceACLPAlertsOptions(JSONObject):
786+
"""
787+
Represents the ACLP alerts available to define during instance creation and cloning.
788+
"""
789+
790+
system_alerts: Optional[List[int]] = None
791+
user_alerts: Optional[List[int]] = None
792+
793+
769794
class Instance(Base):
770795
"""
771796
A Linode Instance.
@@ -782,7 +807,7 @@ class Instance(Base):
782807
"created": Property(is_datetime=True),
783808
"updated": Property(volatile=True, is_datetime=True),
784809
"region": Property(slug_relationship=Region),
785-
"alerts": Property(mutable=True),
810+
"alerts": Property(mutable=True, json_object=InstanceAlerts),
786811
"image": Property(slug_relationship=Image),
787812
"disks": Property(derived_class=Disk),
788813
"configs": Property(derived_class=Config),
@@ -1828,6 +1853,9 @@ def clone(
18281853
Dict[str, Any],
18291854
int,
18301855
] = None,
1856+
alerts: Optional[
1857+
Union[Dict[str, Any], InstanceACLPAlertsOptions]
1858+
] = None,
18311859
):
18321860
"""
18331861
Clones this linode into a new linode or into a new linode in the given region
@@ -1866,6 +1894,10 @@ def clone(
18661894
:param placement_group: Information about the placement group to create this instance under.
18671895
:type placement_group: Union[InstancePlacementGroupAssignment, PlacementGroup, Dict[str, Any], int]
18681896
1897+
:param alerts: ACLP monitor alert definitions associated with the cloned Instance.
1898+
This is under v4beta and may not be available to all users.
1899+
:type alerts: dict[str, Any] or InstanceACLPAlertsOptions
1900+
18691901
:returns: The cloned Instance.
18701902
:rtype: Instance
18711903
"""
@@ -1893,6 +1925,7 @@ def clone(
18931925
"label": label,
18941926
"group": group,
18951927
"with_backups": with_backups,
1928+
"alerts": alerts,
18961929
"placement_group": _expand_placement_group_assignment(
18971930
placement_group
18981931
),

test/fixtures/linode_instances.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"network_out": 5,
1515
"cpu": 90,
1616
"transfer_quota": 80,
17-
"io": 5000
17+
"io": 5000,
18+
"system_alerts": [123,456],
19+
"user_alerts": [555]
1820
},
1921
"label": "linode123",
2022
"backups": {
@@ -64,7 +66,9 @@
6466
"network_out": 5,
6567
"cpu": 90,
6668
"transfer_quota": 80,
67-
"io": 5000
69+
"io": 5000,
70+
"system_alerts": [123,456],
71+
"user_alerts": [555]
6872
},
6973
"label": "linode456",
7074
"backups": {
@@ -104,7 +108,9 @@
104108
"network_out": 5,
105109
"cpu": 90,
106110
"transfer_quota": 80,
107-
"io": 5000
111+
"io": 5000,
112+
"system_alerts": [123,456],
113+
"user_alerts": [555]
108114
},
109115
"group": "test",
110116
"hypervisor": "kvm",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"id": 124,
3+
"status": "running",
4+
"type": "g6-standard-1",
5+
"alerts": {
6+
"network_in": 5,
7+
"network_out": 5,
8+
"cpu": 90,
9+
"transfer_quota": 80,
10+
"io": 5000,
11+
"system_alerts": [123,456],
12+
"user_alerts": [555]
13+
},
14+
"group": "test",
15+
"hypervisor": "kvm",
16+
"label": "linode124",
17+
"backups": {
18+
"enabled": true,
19+
"schedule": {
20+
"window": "W02",
21+
"day": "Scheduling"
22+
}
23+
},
24+
"specs": {
25+
"memory": 2048,
26+
"disk": 30720,
27+
"vcpus": 1,
28+
"transfer": 2000
29+
},
30+
"ipv6": "1235:abcd::1234:abcd:89ef:67cd/64",
31+
"created": "2017-01-01T00:00:00",
32+
"region": "us-east-1",
33+
"ipv4": [
34+
"124.45.67.89"
35+
],
36+
"updated": "2017-01-01T00:00:00",
37+
"image": "linode/ubuntu24.04",
38+
"tags": ["something"],
39+
"host_uuid": "3b3ddd59d9a78bb8de041391075df44de62bfec8",
40+
"watchdog_enabled": true,
41+
"disk_encryption": "disabled",
42+
"lke_cluster_id": null,
43+
"placement_group": null,
44+
"interface_generation": "linode"
45+
}

test/integration/models/sharegroups/test_sharegroups.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import datetime
22
from test.integration.conftest import get_region
3-
from test.integration.helpers import (
4-
get_test_label,
5-
)
3+
from test.integration.helpers import get_test_label
64

75
import pytest
86

test/unit/objects/linode_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Disk,
2626
Image,
2727
Instance,
28+
InstanceACLPAlertsOptions,
2829
StackScript,
2930
Type,
3031
VPCSubnet,
@@ -58,6 +59,14 @@ def test_get_linode(self):
5859
self.assertEqual(linode.lke_cluster_id, None)
5960
self.assertEqual(linode.maintenance_policy, "linode/migrate")
6061

62+
self.assertEqual(linode.alerts.cpu, 90)
63+
self.assertEqual(linode.alerts.io, 5000)
64+
self.assertEqual(linode.alerts.network_in, 5)
65+
self.assertEqual(linode.alerts.network_out, 5)
66+
self.assertEqual(linode.alerts.transfer_quota, 80)
67+
self.assertEqual(linode.alerts.system_alerts, [123, 456])
68+
self.assertEqual(linode.alerts.user_alerts, [555])
69+
6170
json = linode._raw_json
6271
self.assertIsNotNone(json)
6372
self.assertEqual(json["id"], 123)
@@ -183,6 +192,8 @@ def test_update_linode(self):
183192
"network_in": 5,
184193
"network_out": 5,
185194
"transfer_quota": 80,
195+
"system_alerts": [123, 456],
196+
"user_alerts": [555],
186197
},
187198
"backups": {
188199
"enabled": True,
@@ -641,6 +652,24 @@ def test_create_interface_vlan(self):
641652

642653
LinodeInterfaceTest.assert_linode_124_interface_789(result)
643654

655+
def test_instance_clone_with_alerts(self):
656+
src = Instance(self.client, 123)
657+
658+
with self.mock_post("linode/instances/123/clone") as m:
659+
src.clone(
660+
region="us-east",
661+
instance_type="g6-standard-1",
662+
alerts=InstanceACLPAlertsOptions(
663+
system_alerts=[123, 456],
664+
user_alerts=[555],
665+
),
666+
)
667+
668+
assert m.call_data["alerts"] == {
669+
"system_alerts": [123, 456],
670+
"user_alerts": [555],
671+
}
672+
644673

645674
class DiskTest(ClientBaseCase):
646675
"""

0 commit comments

Comments
 (0)