Skip to content

Commit fd90d67

Browse files
Added support for changes in Instance and added Maintenance group
1 parent f8fa49b commit fd90d67

File tree

10 files changed

+115
-2
lines changed

10 files changed

+115
-2
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 .networking import *
1415
from .nodebalancer import *
1516
from .object_storage import *

linode_api4/groups/linode.py

Lines changed: 5 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_id: Optional[int] = None,
156157
**kwargs,
157158
):
158159
"""
@@ -296,6 +297,9 @@ 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_id: The ID of the maintenance policy to apply during maintenance.
301+
If not provided, the default policy (migrate) will be applied.
302+
:type maintenance_policy_id: int
299303
300304
:returns: A new Instance object, or a tuple containing the new Instance and
301305
the generated password.
@@ -327,6 +331,7 @@ def instance_create(
327331
"firewall_id": firewall,
328332
"backup_id": backup,
329333
"stackscript_id": stackscript,
334+
"maintenance_policy_id": maintenance_policy_id,
330335
# Special cases
331336
"disk_encryption": (
332337
str(disk_encryption) if disk_encryption else None

linode_api4/groups/maintenance.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from linode_api4 import MappedObject
2+
from linode_api4.groups import Group
3+
4+
5+
class MaintenanceGroup(Group):
6+
def maintenance_policies(self):
7+
"""
8+
Returns a collection of MaintenancePolicy objects representing
9+
available maintenance policies that can be applied to Linodes
10+
11+
API Documentation: TODO
12+
13+
:returns: A list of Maintenance Policies that can be applied to Linodes
14+
:rtype: List of MaintenancePolicy objects as MappedObjects
15+
"""
16+
17+
result = self.client.get("/maintenance/policies", model=self)
18+
19+
return [MappedObject(**r) for r in result]

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
NetworkingGroup,
2324
NodeBalancerGroup,
2425
ObjectStorageGroup,
@@ -149,6 +150,10 @@ def __init__(
149150
#: more information
150151
self.account = AccountGroup(self)
151152

153+
#: Access methods related to Maintenance Policies - see :any:`MaintenanceGroup` for
154+
#: more information
155+
self.maintenance = MaintenanceGroup(self)
156+
152157
#: Access methods related to networking on your account - see
153158
#: :any:`NetworkingGroup` for more information
154159
self.networking = NetworkingGroup(self)

linode_api4/objects/linode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ class Instance(Base):
686686
"disk_encryption": Property(),
687687
"lke_cluster_id": Property(),
688688
"capabilities": Property(unordered=True),
689+
"maintenance_policy_id": Property(mutable=True),
689690
}
690691

691692
@property

test/fixtures/linode_instances.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"label": "test",
4949
"placement_group_type": "anti_affinity:local",
5050
"placement_group_policy": "strict"
51-
}
51+
},
52+
"maintenance_policy_id" : 1
5253
},
5354
{
5455
"group": "test",
@@ -90,7 +91,8 @@
9091
"watchdog_enabled": false,
9192
"disk_encryption": "enabled",
9293
"lke_cluster_id": 18881,
93-
"placement_group": null
94+
"placement_group": null,
95+
"maintenance_policy_id" : 2
9496
}
9597
]
9698
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"id": "1",
4+
"name": "Default Migrate",
5+
"description": "predefined maintenance policy default for all linodes",
6+
"type": "migrate",
7+
"notification_period_sec": 3600,
8+
"is_default": true
9+
},
10+
{
11+
"id": "2",
12+
"name": "Default Power On/Off",
13+
"description": "predefined maintenance policy for general use cases",
14+
"type": "power on/off",
15+
"notification_period_sec": 1800,
16+
"is_default": false
17+
}
18+
]

test/unit/groups/linode_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ def test_create_with_placement_group(self):
9696
m.call_data["placement_group"], {"id": 123, "compliant_only": True}
9797
)
9898

99+
def test_create_with_mainteance_policy_id(self):
100+
"""
101+
Tests that you can create a Linode with a maintenance policy
102+
"""
103+
104+
with self.mock_post("linode/instances/123") as m:
105+
self.client.linode.instance_create(
106+
"g6-nanode-1",
107+
"eu-west",
108+
maintenance_policy_id=1,
109+
)
110+
111+
self.assertEqual(m.call_data["maintenance_policy_id"], 1)
112+
99113

100114
class TypeTest(ClientBaseCase):
101115
def test_get_types(self):

test/unit/linode_client_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,51 @@ def get_mock(*params, verify=True, **kwargs):
307307
assert called
308308

309309

310+
class MaintenanceGroupTest(ClientBaseCase):
311+
"""
312+
Tests methods of the MaintenanceGroup
313+
"""
314+
315+
def test_maintenance(self):
316+
"""
317+
Tests that maintenance can be retrieved
318+
"""
319+
with self.mock_get("/maintenance/policies") as m:
320+
result = self.client.maintenance.maintenance_policies()
321+
322+
self.assertEqual(m.call_url, "/maintenance/policies")
323+
self.assertEqual(len(result), 2)
324+
325+
policy_default_migrate = result[0]
326+
policy_default_power_on_off = result[1]
327+
328+
self.assertEqual(policy_default_migrate.id, "1")
329+
self.assertEqual(policy_default_migrate.name, "Default Migrate")
330+
self.assertEqual(
331+
policy_default_migrate.description,
332+
"predefined maintenance policy default for all linodes",
333+
)
334+
self.assertEqual(policy_default_migrate.type, "migrate")
335+
self.assertEqual(
336+
policy_default_migrate.notification_period_sec, 3600
337+
)
338+
self.assertEqual(policy_default_migrate.is_default, True)
339+
340+
self.assertEqual(policy_default_power_on_off.id, "2")
341+
self.assertEqual(
342+
policy_default_power_on_off.name, "Default Power On/Off"
343+
)
344+
self.assertEqual(
345+
policy_default_power_on_off.description,
346+
"predefined maintenance policy for general use cases",
347+
)
348+
self.assertEqual(policy_default_power_on_off.type, "power on/off")
349+
self.assertEqual(
350+
policy_default_power_on_off.notification_period_sec, 1800
351+
)
352+
self.assertEqual(policy_default_power_on_off.is_default, False)
353+
354+
310355
class AccountGroupTest(ClientBaseCase):
311356
"""
312357
Tests methods of the AccountGroup

test/unit/objects/linode_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_get_linode(self):
4040
linode.disk_encryption, InstanceDiskEncryptionType.disabled
4141
)
4242
self.assertEqual(linode.lke_cluster_id, None)
43+
self.assertEqual(linode.maintenance_policy_id, 1)
4344

4445
json = linode._raw_json
4546
self.assertIsNotNone(json)
@@ -153,6 +154,7 @@ def test_update_linode(self):
153154

154155
linode.label = "NewLinodeLabel"
155156
linode.group = "new_group"
157+
linode.maintenance_policy_id = 2
156158
linode.save()
157159

158160
self.assertEqual(m.call_url, "/linode/instances/123")
@@ -174,6 +176,7 @@ def test_update_linode(self):
174176
"group": "new_group",
175177
"tags": ["something"],
176178
"watchdog_enabled": True,
179+
"maintenance_policy_id": 2,
177180
},
178181
)
179182

0 commit comments

Comments
 (0)