Skip to content

Commit c727fe0

Browse files
Merge branch 'dev' into tpt-4739-mfa-enforcement
2 parents 1de6940 + c204913 commit c727fe0

9 files changed

Lines changed: 355 additions & 10 deletions

File tree

‎.github/workflows/publish-pypi.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
LINODE_SDK_VERSION: ${{ github.event.release.tag_name }}
2929

3030
- name: Publish the release artifacts to PyPI
31-
uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # pin@release/v1.14.2
31+
uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # pin@release/v1.14.3

‎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: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
from linode_api4.objects import Capability, Region
3737

3838
ENV_TOKEN_NAME = "LINODE_TOKEN"
39-
ENV_API_URL_NAME = "LINODE_API_URL"
39+
ENV_API_URL_NAME = "LINODE_URL"
40+
ENV_API_VERSION_NAME = "LINODE_API_VERSION"
4041
ENV_REGION_OVERRIDE = "LINODE_TEST_REGION_OVERRIDE"
4142
ENV_API_CA_NAME = "LINODE_API_CA"
4243
RUN_LONG_TESTS = "RUN_LONG_TESTS"
@@ -57,7 +58,9 @@ def get_token():
5758

5859

5960
def get_api_url():
60-
return os.environ.get(ENV_API_URL_NAME, "https://api.linode.com/v4beta")
61+
api_url = os.environ.get(ENV_API_URL_NAME, "https://api.linode.com")
62+
api_version = os.environ.get(ENV_API_VERSION_NAME, "v4beta")
63+
return f"{api_url.rstrip('/')}/{api_version}"
6164

6265

6366
def get_regions(
@@ -847,6 +850,72 @@ def test_monitor_client(get_monitor_token_for_db_entities):
847850
return client, entity_ids
848851

849852

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

‎test/integration/linode_client/test_linode_client.py‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22
import time
3-
from test.integration.conftest import get_region
3+
from test.integration.conftest import get_api_url, get_region
44
from test.integration.helpers import get_test_label, wait_for_condition
55

66
import pytest
@@ -14,6 +14,60 @@
1414
)
1515

1616

17+
@pytest.mark.parametrize(
18+
"environment, expected_url",
19+
[
20+
({}, "https://api.linode.com/v4beta"),
21+
(
22+
{"LINODE_URL": "https://api.example.test"},
23+
"https://api.example.test/v4beta",
24+
),
25+
(
26+
{"LINODE_URL": "https://api.example.test/"},
27+
"https://api.example.test/v4beta",
28+
),
29+
(
30+
{"LINODE_API_VERSION": "v4"},
31+
"https://api.linode.com/v4",
32+
),
33+
(
34+
{
35+
"LINODE_URL": "https://api.example.test/",
36+
"LINODE_API_VERSION": "v4",
37+
},
38+
"https://api.example.test/v4",
39+
),
40+
(
41+
{
42+
"LINODE_URL": "http://localhost:8080/proxy///",
43+
"LINODE_API_VERSION": "v4beta",
44+
},
45+
"http://localhost:8080/proxy/v4beta",
46+
),
47+
(
48+
{"LINODE_API_URL": "https://legacy.example.test/v4"},
49+
"https://api.linode.com/v4beta",
50+
),
51+
(
52+
{
53+
"LINODE_API_URL": "https://legacy.example.test/v4",
54+
"LINODE_URL": "https://api.example.test",
55+
"LINODE_API_VERSION": "v4",
56+
},
57+
"https://api.example.test/v4",
58+
),
59+
],
60+
)
61+
def test_api_url_environment(monkeypatch, environment, expected_url):
62+
for name in ("LINODE_URL", "LINODE_API_VERSION", "LINODE_API_URL"):
63+
monkeypatch.delenv(name, raising=False)
64+
65+
for name, value in environment.items():
66+
monkeypatch.setenv(name, value)
67+
68+
assert get_api_url() == expected_url
69+
70+
1771
def is_tag_created(client, tag_label):
1872
tags = client.tags()
1973
tag_label_list = [i.label for i in tags]

0 commit comments

Comments
 (0)