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,
**kwargs,
):
"""
Expand Down Expand Up @@ -336,6 +340,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

:returns: A new Instance object, or a tuple containing the new Instance and
the generated password.
Expand All @@ -356,6 +363,7 @@ def instance_create(
"region": region,
"image": image,
"authorized_keys": load_and_validate_keys(authorized_keys),
"alerts": alerts,
# These will automatically be flattened below
"firewall_id": firewall,
"backup_id": backup,
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 @@ -766,6 +766,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


class Instance(Base):
"""
A Linode Instance.
Expand All @@ -782,7 +807,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 @@ -1828,6 +1853,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 @@ -1866,6 +1894,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 @@ -1893,6 +1925,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 @@ -64,7 +66,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 @@ -104,7 +108,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"
}
4 changes: 1 addition & 3 deletions test/integration/models/sharegroups/test_sharegroups.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import datetime
from test.integration.conftest import get_region
from test.integration.helpers import (
get_test_label,
)
from test.integration.helpers import get_test_label

import pytest

Expand Down
29 changes: 29 additions & 0 deletions test/unit/objects/linode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Disk,
Image,
Instance,
InstanceACLPAlertsOptions,
StackScript,
Type,
VPCSubnet,
Expand Down Expand Up @@ -58,6 +59,14 @@ def test_get_linode(self):
self.assertEqual(linode.lke_cluster_id, None)
self.assertEqual(linode.maintenance_policy, "linode/migrate")

self.assertEqual(linode.alerts.cpu, 90)
self.assertEqual(linode.alerts.io, 5000)
self.assertEqual(linode.alerts.network_in, 5)
self.assertEqual(linode.alerts.network_out, 5)
self.assertEqual(linode.alerts.transfer_quota, 80)
self.assertEqual(linode.alerts.system_alerts, [123, 456])
self.assertEqual(linode.alerts.user_alerts, [555])

json = linode._raw_json
self.assertIsNotNone(json)
self.assertEqual(json["id"], 123)
Expand Down Expand Up @@ -183,6 +192,8 @@ def test_update_linode(self):
"network_in": 5,
"network_out": 5,
"transfer_quota": 80,
"system_alerts": [123, 456],
"user_alerts": [555],
},
"backups": {
"enabled": True,
Expand Down Expand Up @@ -641,6 +652,24 @@ def test_create_interface_vlan(self):

LinodeInterfaceTest.assert_linode_124_interface_789(result)

def test_instance_clone_with_alerts(self):
src = Instance(self.client, 123)

with self.mock_post("linode/instances/123/clone") as m:
src.clone(
region="us-east",
instance_type="g6-standard-1",
alerts=InstanceACLPAlertsOptions(
system_alerts=[123, 456],
user_alerts=[555],
),
)

assert m.call_data["alerts"] == {
"system_alerts": [123, 456],
"user_alerts": [555],
}


class DiskTest(ClientBaseCase):
"""
Expand Down