Skip to content

Commit 3322091

Browse files
TPT-3596: Project CloudPulse Metrics Integration for Core Compute (#750)
* Support ACLP alerts integration with Linode (#650) * init * address comments * Integration tests for support Linode integration with alerts (#669) * Create tests for Linode integration with alerts * Code review fixes * Remove test test_try_to_update_linode_alerts_legacy_and_aclp_at_the_same_time and add test test_update_linode_aclp_alerts * Apply code review suggestions * Submit code review suggestions * address copilot comments --------- Co-authored-by: Pawel <100145168+psnoch-akamai@users.noreply.github.com>
1 parent 5698b3c commit 3322091

7 files changed

Lines changed: 294 additions & 6 deletions

File tree

‎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,
@@ -167,6 +168,9 @@ def instance_create(
167168
boot_size: Optional[int] = None,
168169
authorized_users: Optional[List[str]] = None,
169170
ipv4: Optional[List[str]] = None,
171+
alerts: Optional[
172+
Union[Dict[str, Any], InstanceACLPAlertsOptions]
173+
] = None,
170174
**kwargs,
171175
):
172176
"""
@@ -359,6 +363,9 @@ def instance_create(
359363
:param ipv4: A list of reserved IPv4 addresses to assign to this Instance.
360364
NOTE: Reserved IP feature may not currently be available to all users.
361365
:type ipv4: list[str]
366+
:param alerts: ACLP alerts available to define during instance creation.
367+
This is v4beta only and may not be available to all users.
368+
:type alerts: dict[str, Any] or InstanceACLPAlertsOptions
362369
363370
:returns: A new Instance object
364371
:rtype: Instance
@@ -385,6 +392,7 @@ def instance_create(
385392
"image": image,
386393
"root_pass": root_pass,
387394
"authorized_keys": load_and_validate_keys(authorized_keys),
395+
"alerts": alerts,
388396
"authorized_users": authorized_users,
389397
# These will automatically be flattened below
390398
"firewall_id": firewall,

‎linode_api4/objects/linode.py‎

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

792792

793+
@dataclass
794+
class InstanceAlerts(JSONObject):
795+
"""
796+
Represents both legacy and ACLP alerts for a Linode Instance.
797+
"""
798+
799+
cpu: int = 0
800+
io: int = 0
801+
network_in: int = 0
802+
network_out: int = 0
803+
transfer_quota: int = 0
804+
system_alerts: Optional[List[int]] = None
805+
user_alerts: Optional[List[int]] = None
806+
807+
808+
@dataclass
809+
class InstanceACLPAlertsOptions(JSONObject):
810+
"""
811+
Represents the ACLP alerts available to define during instance creation and cloning.
812+
"""
813+
814+
system_alerts: Optional[List[int]] = None
815+
user_alerts: Optional[List[int]] = None
816+
817+
793818
@dataclass
794819
class InstanceBackupSchedule(JSONObject):
795820
"""
@@ -842,7 +867,7 @@ class Instance(Base):
842867
"created": Property(is_datetime=True),
843868
"updated": Property(volatile=True, is_datetime=True),
844869
"region": Property(slug_relationship=Region),
845-
"alerts": Property(mutable=True),
870+
"alerts": Property(mutable=True, json_object=InstanceAlerts),
846871
"image": Property(slug_relationship=Image),
847872
"disks": Property(derived_class=Disk),
848873
"configs": Property(derived_class=Config),
@@ -1949,6 +1974,9 @@ def clone(
19491974
Dict[str, Any],
19501975
int,
19511976
] = None,
1977+
alerts: Optional[
1978+
Union[Dict[str, Any], InstanceACLPAlertsOptions]
1979+
] = None,
19521980
):
19531981
"""
19541982
Clones this linode into a new linode or into a new linode in the given region
@@ -1987,6 +2015,10 @@ def clone(
19872015
:param placement_group: Information about the placement group to create this instance under.
19882016
:type placement_group: Union[InstancePlacementGroupAssignment, PlacementGroup, Dict[str, Any], int]
19892017
2018+
:param alerts: ACLP monitor alert definitions associated with the cloned Instance.
2019+
This is under v4beta and may not be available to all users.
2020+
:type alerts: dict[str, Any] or InstanceACLPAlertsOptions
2021+
19902022
:returns: The cloned Instance.
19912023
:rtype: Instance
19922024
"""
@@ -2017,6 +2049,7 @@ def clone(
20172049
"label": label,
20182050
"group": group,
20192051
"with_backups": with_backups,
2052+
"alerts": alerts,
20202053
"placement_group": _expand_placement_group_assignment(
20212054
placement_group
20222055
),

‎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": {
@@ -66,7 +68,9 @@
6668
"network_out": 5,
6769
"cpu": 90,
6870
"transfer_quota": 80,
69-
"io": 5000
71+
"io": 5000,
72+
"system_alerts": [123,456],
73+
"user_alerts": [555]
7074
},
7175
"label": "linode456",
7276
"backups": {
@@ -106,7 +110,9 @@
106110
"network_out": 5,
107111
"cpu": 90,
108112
"transfer_quota": 80,
109-
"io": 5000
113+
"io": 5000,
114+
"system_alerts": [123,456],
115+
"user_alerts": [555]
110116
},
111117
"group": "test",
112118
"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/conftest.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,72 @@ def test_monitor_client(get_monitor_token_for_db_entities):
847847
return client, entity_ids
848848

849849

850+
def get_alert_channels_list(test_linode_client):
851+
channels = list(test_linode_client.monitor.alert_channels())
852+
if len(channels) == 0:
853+
pytest.fail(
854+
"No alert channels available for testing. Please create an alert channel and try again."
855+
)
856+
857+
return channels
858+
859+
860+
@pytest.fixture(scope="session")
861+
def create_alert_service_definition(test_linode_client):
862+
rule_criteria = {
863+
"rules": [
864+
{
865+
"aggregate_function": "min",
866+
"dimension_filters": [
867+
{
868+
"dimension_label": "node_type",
869+
"label": "Node Type",
870+
"operator": "eq",
871+
"value": "primary",
872+
}
873+
],
874+
"label": "Memory Usage",
875+
"metric": "memory_usage",
876+
"operator": "eq",
877+
"threshold": 95,
878+
"unit": "percent",
879+
}
880+
]
881+
}
882+
trigger_conditions = {
883+
"criteria_condition": "ALL",
884+
"evaluation_period_seconds": 300,
885+
"polling_interval_seconds": 900,
886+
"trigger_occurrences": 3,
887+
}
888+
alert = test_linode_client.monitor.create_alert_definition(
889+
service_type="dbaas",
890+
label=get_test_label() + "-service-definition",
891+
severity=1,
892+
description="description",
893+
channel_ids=[get_alert_channels_list(test_linode_client)[0].id],
894+
rule_criteria=rule_criteria,
895+
trigger_conditions=trigger_conditions,
896+
)
897+
898+
yield alert
899+
900+
alert.delete()
901+
902+
903+
def get_system_alerts(client: LinodeClient):
904+
alerts = client.monitor.alert_definitions()
905+
system_alerts = []
906+
for alert in alerts.lists[0]:
907+
if alert.type == "system":
908+
system_alerts.append(alert)
909+
if len(system_alerts) == 0:
910+
raise Exception(
911+
"No system alert definitions found. Cannot run tests dependent on system alert definitions."
912+
)
913+
return system_alerts
914+
915+
850916
@pytest.fixture
851917
def create_reserved_ip(test_linode_client):
852918
client = test_linode_client

0 commit comments

Comments
 (0)