Skip to content

Commit e18c8f1

Browse files
Project: Host Maintenance Policy (#582)
* Added support for Host/VM Maintenance (#532) * Added support for changes in Account Events, Maintenance, and Settings * Added support for changes in Instance and added Maintenance group * Add docstring and fix imports * Updated maintenance_policy_id to maintenance_policy (#564) * fix * Fix failing unit tests * fix * integration test * address_PR_comments * Added v4beta notices (#578) * Added missing doc link * Added maintenance to LinodeClient * Addressed PR comments --------- Co-authored-by: vshanthe <vshanthe@akamai.com> Co-authored-by: Vinay <143587840+vshanthe@users.noreply.github.com>
1 parent 74e272a commit e18c8f1

19 files changed

+397
-56
lines changed

linode_api4/groups/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .lke import *
1111
from .lke_tier import *
1212
from .longview import *
13+
from .maintenance import *
1314
from .monitor import *
1415
from .monitor_api import *
1516
from .networking import *

linode_api4/groups/account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def maintenance(self):
201201
"""
202202
Returns a collection of Maintenance objects for any entity a user has permissions to view. Cancelled Maintenance objects are not returned.
203203
204-
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-account-logins
204+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-maintenance
205205
206206
:returns: A list of Maintenance objects on this account.
207207
:rtype: List of Maintenance objects as MappedObjects

linode_api4/groups/linode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def instance_create(
153153
int,
154154
]
155155
] = None,
156+
maintenance_policy: Optional[str] = None,
156157
**kwargs,
157158
):
158159
"""
@@ -296,6 +297,11 @@ def instance_create(
296297
:type interfaces: list[ConfigInterface] or list[dict[str, Any]]
297298
:param placement_group: A Placement Group to create this Linode under.
298299
:type placement_group: Union[InstancePlacementGroupAssignment, PlacementGroup, Dict[str, Any], int]
300+
:param maintenance_policy: The slug of the maintenance policy to apply during maintenance.
301+
If not provided, the default policy (linode/migrate) will be applied.
302+
NOTE: This field is in beta and may only
303+
function if base_url is set to `https://api.linode.com/v4beta`.
304+
:type maintenance_policy: str
299305
300306
:returns: A new Instance object, or a tuple containing the new Instance and
301307
the generated password.
@@ -327,6 +333,7 @@ def instance_create(
327333
"firewall_id": firewall,
328334
"backup_id": backup,
329335
"stackscript_id": stackscript,
336+
"maintenance_policy": maintenance_policy,
330337
# Special cases
331338
"disk_encryption": (
332339
str(disk_encryption) if disk_encryption else None

linode_api4/groups/maintenance.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from linode_api4.groups import Group
2+
from linode_api4.objects import MappedObject
3+
4+
5+
class MaintenanceGroup(Group):
6+
"""
7+
Collections related to Maintenance.
8+
"""
9+
10+
def maintenance_policies(self):
11+
"""
12+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
13+
14+
Returns a collection of MaintenancePolicy objects representing
15+
available maintenance policies that can be applied to Linodes
16+
17+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-policies
18+
19+
:returns: A list of Maintenance Policies that can be applied to Linodes
20+
:rtype: List of MaintenancePolicy objects as MappedObjects
21+
"""
22+
23+
result = self.client.get("/maintenance/policies", model=self)
24+
25+
return [MappedObject(**r) for r in result["data"]]

linode_api4/linode_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
LinodeGroup,
2020
LKEGroup,
2121
LongviewGroup,
22+
MaintenanceGroup,
2223
MetricsGroup,
2324
MonitorGroup,
2425
NetworkingGroup,
@@ -399,6 +400,10 @@ def __init__(
399400
#: :any:`NetworkingGroup` for more information
400401
self.networking = NetworkingGroup(self)
401402

403+
#: Access methods related to maintenance on your account - see
404+
#: :any:`MaintenanceGroup` for more information
405+
self.maintenance = MaintenanceGroup(self)
406+
402407
#: Access methods related to support - see :any:`SupportGroup` for more
403408
#: information
404409
self.support = SupportGroup(self)

linode_api4/objects/account.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ class AccountSettings(Base):
198198
),
199199
"object_storage": Property(),
200200
"backups_enabled": Property(mutable=True),
201+
"maintenance_policy": Property(
202+
mutable=True
203+
), # Note: This field is only available when using v4beta.
201204
}
202205

203206

@@ -220,12 +223,18 @@ class Event(Base):
220223
"user_id": Property(),
221224
"username": Property(),
222225
"entity": Property(),
223-
"time_remaining": Property(),
226+
"time_remaining": Property(), # Deprecated
224227
"rate": Property(),
225228
"status": Property(),
226229
"duration": Property(),
227230
"secondary_entity": Property(),
228231
"message": Property(),
232+
"maintenance_policy_set": Property(), # Note: This field is only available when using v4beta.
233+
"description": Property(),
234+
"source": Property(),
235+
"not_before": Property(is_datetime=True),
236+
"start_time": Property(is_datetime=True),
237+
"complete_time": Property(is_datetime=True),
229238
}
230239

231240
@property

linode_api4/objects/linode.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,9 @@ class Instance(Base):
686686
"disk_encryption": Property(),
687687
"lke_cluster_id": Property(),
688688
"capabilities": Property(unordered=True),
689+
"maintenance_policy": Property(
690+
mutable=True
691+
), # Note: This field is only available when using v4beta.
689692
}
690693

691694
@property

test/fixtures/account_events_123.json

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
{
2-
"action": "ticket_create",
3-
"created": "2018-01-01T00:01:01",
4-
"duration": 300.56,
5-
"entity": {
6-
"id": 11111,
7-
"label": "Problem booting my Linode",
8-
"type": "ticket",
9-
"url": "/v4/support/tickets/11111"
10-
},
11-
"id": 123,
12-
"message": "None",
13-
"percent_complete": null,
14-
"rate": null,
15-
"read": true,
16-
"secondary_entity": {
17-
"id": "linode/debian9",
18-
"label": "linode1234",
19-
"type": "linode",
20-
"url": "/v4/linode/instances/1234"
21-
},
22-
"seen": true,
23-
"status": null,
24-
"time_remaining": null,
25-
"username": "exampleUser"
26-
}
27-
2+
"action": "ticket_create",
3+
"created": "2025-03-25T12:00:00",
4+
"duration": 300.56,
5+
"entity": {
6+
"id": 11111,
7+
"label": "Problem booting my Linode",
8+
"type": "ticket",
9+
"url": "/v4/support/tickets/11111"
10+
},
11+
"id": 123,
12+
"message": "Ticket created for user issue.",
13+
"percent_complete": null,
14+
"rate": null,
15+
"read": true,
16+
"secondary_entity": {
17+
"id": "linode/debian9",
18+
"label": "linode1234",
19+
"type": "linode",
20+
"url": "/v4/linode/instances/1234"
21+
},
22+
"seen": true,
23+
"status": "completed",
24+
"username": "exampleUser",
25+
"maintenance_policy_set": "Tentative",
26+
"description": "Scheduled maintenance",
27+
"source": "user",
28+
"not_before": "2025-03-25T12:00:00",
29+
"start_time": "2025-03-25T12:30:00",
30+
"complete_time": "2025-03-25T13:00:00"
31+
}
Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
{
2-
"data": [
3-
{
4-
"entity": {
5-
"id": 123,
6-
"label": "demo-linode",
7-
"type": "Linode",
8-
"url": "https://api.linode.com/v4/linode/instances/{linodeId}"
9-
},
10-
"reason": "This maintenance will allow us to update the BIOS on the host's motherboard.",
11-
"status": "started",
12-
"type": "reboot",
13-
"when": "2020-07-09T00:01:01"
14-
}
15-
],
16-
"page": 1,
17-
"pages": 1,
18-
"results": 1
19-
}
2+
"pages": 1,
3+
"page": 1,
4+
"results": 2,
5+
"data": [
6+
{
7+
"entity": {
8+
"id": 1234,
9+
"label": "Linode #1234",
10+
"type": "linode",
11+
"url": "/linodes/1234"
12+
},
13+
"reason": "Scheduled upgrade to faster NVMe hardware.",
14+
"type": "linode_migrate",
15+
"maintenance_policy_set": "linode/power_off_on",
16+
"description": "Scheduled Maintenance",
17+
"source": "platform",
18+
"not_before": "2025-03-25T10:00:00Z",
19+
"start_time": "2025-03-25T12:00:00Z",
20+
"complete_time": "2025-03-25T14:00:00Z",
21+
"status": "scheduled"
22+
},
23+
{
24+
"entity": {
25+
"id": 1234,
26+
"label": "Linode #1234",
27+
"type": "linode",
28+
"url": "/linodes/1234"
29+
},
30+
"reason": "Pending migration of Linode #1234 to a new host.",
31+
"type": "linode_migrate",
32+
"maintenance_policy_set": "linode/migrate",
33+
"description": "Emergency Maintenance",
34+
"source": "user",
35+
"not_before": "2025-03-26T15:00:00Z",
36+
"start_time": "2025-03-26T15:00:00Z",
37+
"complete_time": "2025-03-26T17:00:00Z",
38+
"status": "in-progress"
39+
}
40+
]
41+
}

test/fixtures/account_settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"managed": false,
44
"network_helper": false,
55
"object_storage": "active",
6-
"backups_enabled": true
6+
"backups_enabled": true,
7+
"maintenance_policy": "linode/migrate"
78
}

0 commit comments

Comments
 (0)