Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions linode_api4/groups/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from linode_api4.objects.filtering import Filter
from linode_api4.objects.linode import (
Backup,
InstanceACLPAlertsOptions,
InstancePlacementGroupAssignment,
InterfaceGeneration,
NetworkInterface,
Expand Down Expand Up @@ -162,6 +163,9 @@ def instance_create(
interface_generation: Optional[Union[InterfaceGeneration, str]] = None,
network_helper: Optional[bool] = None,
maintenance_policy: Optional[str] = None,
alerts: Optional[
Union[Dict[str, Any], InstanceACLPAlertsOptions]
] = None,
root_pass: Optional[str] = None,
kernel: Optional[str] = None,
boot_size: Optional[int] = None,
Expand Down Expand Up @@ -350,6 +354,9 @@ def instance_create(
:param maintenance_policy: The slug of the maintenance policy to apply during maintenance.
If not provided, the default policy (linode/migrate) will be applied.
:type maintenance_policy: str
:param alerts: ACLP alerts available to define during instance creation.
This is v4beta only and may not be available to all users.
:type alerts: dict[str, Any] or InstanceACLPAlertsOptions
:param kernel: The kernel to boot the Instance with. If provided, this will be used as the
kernel for the default configuration profile.
:type kernel: str
Expand Down Expand Up @@ -385,6 +392,7 @@ def instance_create(
"image": image,
"root_pass": root_pass,
"authorized_keys": load_and_validate_keys(authorized_keys),
"alerts": alerts,
"authorized_users": authorized_users,
# These will automatically be flattened below
"firewall_id": firewall,
Expand Down
35 changes: 34 additions & 1 deletion linode_api4/objects/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,31 @@ class UpgradeInterfacesResult(JSONObject):
)


@dataclass
class InstanceAlerts(JSONObject):
"""
Represents both legacy and ACLP alerts for a Linode Instance.
"""

cpu: int = 0
io: int = 0
network_in: int = 0
network_out: int = 0
transfer_quota: int = 0
system_alerts: Optional[List[int]] = None
user_alerts: Optional[List[int]] = None


@dataclass
class InstanceACLPAlertsOptions(JSONObject):
"""
Represents the ACLP alerts available to define during instance creation and cloning.
"""

system_alerts: Optional[List[int]] = None
user_alerts: Optional[List[int]] = None


@dataclass
class InstanceBackupSchedule(JSONObject):
"""
Expand Down Expand Up @@ -842,7 +867,7 @@ class Instance(Base):
"created": Property(is_datetime=True),
"updated": Property(volatile=True, is_datetime=True),
"region": Property(slug_relationship=Region),
"alerts": Property(mutable=True),
"alerts": Property(mutable=True, json_object=InstanceAlerts),
"image": Property(slug_relationship=Image),
"disks": Property(derived_class=Disk),
"configs": Property(derived_class=Config),
Expand Down Expand Up @@ -1949,6 +1974,9 @@ def clone(
Dict[str, Any],
int,
] = None,
alerts: Optional[
Union[Dict[str, Any], InstanceACLPAlertsOptions]
] = None,
):
"""
Clones this linode into a new linode or into a new linode in the given region
Expand Down Expand Up @@ -1987,6 +2015,10 @@ def clone(
:param placement_group: Information about the placement group to create this instance under.
:type placement_group: Union[InstancePlacementGroupAssignment, PlacementGroup, Dict[str, Any], int]

:param alerts: ACLP monitor alert definitions associated with the cloned Instance.
This is under v4beta and may not be available to all users.
:type alerts: dict[str, Any] or InstanceACLPAlertsOptions

:returns: The cloned Instance.
:rtype: Instance
"""
Expand Down Expand Up @@ -2017,6 +2049,7 @@ def clone(
"label": label,
"group": group,
"with_backups": with_backups,
"alerts": alerts,
"placement_group": _expand_placement_group_assignment(
placement_group
),
Expand Down
12 changes: 9 additions & 3 deletions test/fixtures/linode_instances.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"network_out": 5,
"cpu": 90,
"transfer_quota": 80,
"io": 5000
"io": 5000,
"system_alerts": [123,456],
"user_alerts": [555]
},
"label": "linode123",
"backups": {
Expand Down Expand Up @@ -66,7 +68,9 @@
"network_out": 5,
"cpu": 90,
"transfer_quota": 80,
"io": 5000
"io": 5000,
"system_alerts": [123,456],
"user_alerts": [555]
},
"label": "linode456",
"backups": {
Expand Down Expand Up @@ -106,7 +110,9 @@
"network_out": 5,
"cpu": 90,
"transfer_quota": 80,
"io": 5000
"io": 5000,
"system_alerts": [123,456],
"user_alerts": [555]
},
"group": "test",
"hypervisor": "kvm",
Expand Down
45 changes: 45 additions & 0 deletions test/fixtures/linode_instances_123_clone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"id": 124,
"status": "running",
"type": "g6-standard-1",
"alerts": {
"network_in": 5,
"network_out": 5,
"cpu": 90,
"transfer_quota": 80,
"io": 5000,
"system_alerts": [123,456],
"user_alerts": [555]
},
"group": "test",
"hypervisor": "kvm",
"label": "linode124",
"backups": {
"enabled": true,
"schedule": {
"window": "W02",
"day": "Scheduling"
}
},
"specs": {
"memory": 2048,
"disk": 30720,
"vcpus": 1,
"transfer": 2000
},
"ipv6": "1235:abcd::1234:abcd:89ef:67cd/64",
"created": "2017-01-01T00:00:00",
"region": "us-east-1",
"ipv4": [
"124.45.67.89"
],
"updated": "2017-01-01T00:00:00",
"image": "linode/ubuntu24.04",
"tags": ["something"],
"host_uuid": "3b3ddd59d9a78bb8de041391075df44de62bfec8",
"watchdog_enabled": true,
"disk_encryption": "disabled",
"lke_cluster_id": null,
"placement_group": null,
"interface_generation": "linode"
}
66 changes: 66 additions & 0 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,72 @@ def test_monitor_client(get_monitor_token_for_db_entities):
return client, entity_ids


def get_alert_channels_list(test_linode_client):
channels = list(test_linode_client.monitor.alert_channels())
if len(channels) == 0:
pytest.fail(
"No alert channels available for testing. Please create an alert channel and try again."
)

return channels


@pytest.fixture(scope="session")
def create_alert_service_definition(test_linode_client):
rule_criteria = {
"rules": [
{
"aggregate_function": "min",
"dimension_filters": [
{
"dimension_label": "node_type",
"label": "Node Type",
"operator": "eq",
"value": "primary",
}
],
"label": "Memory Usage",
"metric": "memory_usage",
"operator": "eq",
"threshold": 95,
"unit": "percent",
}
]
}
trigger_conditions = {
"criteria_condition": "ALL",
"evaluation_period_seconds": 300,
"polling_interval_seconds": 900,
"trigger_occurrences": 3,
}
alert = test_linode_client.monitor.create_alert_definition(
service_type="dbaas",
label=get_test_label() + "-service-definition",
severity=1,
description="description",
channel_ids=[get_alert_channels_list(test_linode_client)[0].id],
rule_criteria=rule_criteria,
trigger_conditions=trigger_conditions,
)

yield alert

alert.delete()


def get_system_alerts(client: LinodeClient):
alerts = client.monitor.alert_definitions()
system_alerts = []
for alert in alerts.lists[0]:
if alert.type == "system":
system_alerts.append(alert)
if len(system_alerts) == 0:
raise Exception(
"No system alert definitions found. Cannot run tests dependent on system alert definitions."
)
return system_alerts


@pytest.fixture
def create_reserved_ip(test_linode_client):
client = test_linode_client
Expand Down
101 changes: 99 additions & 2 deletions test/integration/models/linode/test_linode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ipaddress
import time
from test.integration.conftest import get_region
from test.integration.conftest import get_region, get_system_alerts
from test.integration.helpers import (
get_test_label,
retry_sending_request,
Expand All @@ -10,7 +10,7 @@

import pytest

from linode_api4.errors import ApiError
from linode_api4.errors import ApiError, UnexpectedResponseError
from linode_api4.objects import (
Config,
ConfigInterface,
Expand Down Expand Up @@ -280,10 +280,36 @@ def get_status(linode: Instance, status: str):
return linode.status == status


def wait_for_clone_complete_and_delete_linode(
interval: int, timeout: int, linode: Instance
) -> object:
end_time = time.time() + timeout
while time.time() < end_time:
try:
return linode.delete()
except ApiError as err:
if "[400] Linode is the target of an ongoing clone" not in str(err):
raise UnexpectedResponseError(f"Unexpected delete linode error")
time.sleep(interval)
raise TimeoutError(
f"Timeout Error: not possible to delete just cloned linode in {timeout} seconds"
)


def instance_type_condition(linode: Instance, type: str):
return type in str(linode.type)


def test_get_linodes_verify_alerts(test_linode_client, create_linode):
linodes_list = test_linode_client.linode.instances().lists[0]
assert len(linodes_list) > 0
assert linodes_list[0].alerts.cpu
assert linodes_list[0].alerts.io
assert linodes_list[0].alerts.network_in
assert linodes_list[0].alerts.network_out
assert linodes_list[0].alerts.transfer_quota


def test_get_linode(test_linode_client, linode_with_volume_firewall):
linode = test_linode_client.load(Instance, linode_with_volume_firewall.id)

Expand Down Expand Up @@ -337,6 +363,8 @@ def test_linode_rebuild(test_linode_client):

assert linode.status == "rebuilding"
assert linode.image.id == "linode/debian12"
assert linode.alerts.cpu
assert linode.alerts.io

assert linode.disk_encryption == InstanceDiskEncryptionType.disabled

Expand Down Expand Up @@ -401,6 +429,75 @@ def test_linode_reboot(create_linode):
assert linode.status == "running"


def test_linode_alerts_workflow(test_linode_client, create_linode):
linode = create_linode
parent_linode_id = create_linode.id
assert linode.alerts.cpu == 90
assert linode.alerts.io == 10000
assert linode.alerts.network_in == 10
assert linode.alerts.network_out == 10
assert linode.alerts.transfer_quota == 80
assert isinstance(linode.alerts.system_alerts, list)
assert isinstance(linode.alerts.user_alerts, list)

linode = test_linode_client.load(Instance, parent_linode_id)
assert linode.alerts.cpu == 90
assert linode.alerts.io == 10000
assert linode.alerts.network_in == 10
assert linode.alerts.network_out == 10
assert linode.alerts.transfer_quota == 80
assert isinstance(linode.alerts.system_alerts, list)
assert isinstance(linode.alerts.user_alerts, list)

linode.alerts = {
"cpu": 50,
"io": 6000,
"network_in": 20,
"network_out": 20,
"transfer_quota": 40,
}
linode_save_status = linode.save()
assert linode_save_status == True
assert linode.alerts["cpu"] == 50
assert linode.alerts["io"] == 6000
assert linode.alerts["network_in"] == 20
assert linode.alerts["network_out"] == 20
assert linode.alerts["transfer_quota"] == 40

wait_for_condition(10, 100, get_status, linode, "running")
new_linode = retry_sending_request(
5,
linode.clone,
region=linode.region.id,
instance_type=linode.type.id,
label=get_test_label(),
)
assert new_linode.alerts.cpu == 90
assert new_linode.alerts.io == 10000
assert new_linode.alerts.network_in == 10
assert new_linode.alerts.network_out == 10
assert new_linode.alerts.transfer_quota == 80
assert isinstance(new_linode.alerts.system_alerts, list)
assert isinstance(new_linode.alerts.user_alerts, list)

wait_for_clone_complete_and_delete_linode(10, 100, new_linode)


def test_update_linode_aclp_alerts(
test_linode_client, create_linode, create_alert_service_definition
):
linode = create_linode
sample_system_alert = get_system_alerts(test_linode_client)[0].id

linode.alerts = {
"user_alerts": [create_alert_service_definition.id],
"system_alerts": [sample_system_alert],
}
linode.save()
assert linode.alerts["user_alerts"] == [create_alert_service_definition.id]
assert linode.alerts["system_alerts"] == [sample_system_alert]


def test_linode_shutdown(create_linode):
linode = create_linode

Expand Down
Loading
Loading