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 @@ -167,6 +168,9 @@ def instance_create(
boot_size: Optional[int] = None,
authorized_users: Optional[List[str]] = None,
ipv4: Optional[List[str]] = None,
alerts: Optional[
Union[Dict[str, Any], InstanceACLPAlertsOptions]
] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -359,6 +363,9 @@ def instance_create(
:param ipv4: A list of reserved IPv4 addresses to assign to this Instance.
NOTE: Reserved IP feature may not currently be available to all users.
:type ipv4: list[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

:returns: A new Instance object
:rtype: Instance
Expand All @@ -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",
Comment thread
yec-akamai marked this conversation as resolved.
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]:
Comment thread
yec-akamai marked this conversation as resolved.
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
Loading
Loading