Skip to content

Commit b52d1bf

Browse files
Fix backups attempted update on non-editable last_successful
1 parent e35b61d commit b52d1bf

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎linode_api4/objects/linode.py‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,43 @@ class UpgradeInterfacesResult(JSONObject):
790790
)
791791

792792

793+
@dataclass
794+
class InstanceBackupSchedule(JSONObject):
795+
"""
796+
InstanceBackupSchedule represents when automatic backups of a Linode Instance are taken.
797+
"""
798+
799+
# The API rejects an empty schedule, so unset values are sent as null
800+
include_none_values = True
801+
802+
day: Optional[str] = None
803+
window: Optional[str] = None
804+
805+
806+
@dataclass
807+
class InstanceBackupsOptions(JSONObject):
808+
"""
809+
InstanceBackupsOptions is used to update the backup settings of a Linode Instance.
810+
"""
811+
812+
enabled: bool = False
813+
schedule: Optional[InstanceBackupSchedule] = None
814+
815+
816+
@dataclass
817+
class InstanceBackups(JSONObject):
818+
"""
819+
InstanceBackups represents the backup settings and status of a Linode Instance.
820+
"""
821+
822+
put_class = InstanceBackupsOptions
823+
824+
enabled: bool = False
825+
available: bool = False
826+
schedule: Optional[InstanceBackupSchedule] = None
827+
last_successful: Optional[str] = None
828+
829+
793830
class Instance(Base):
794831
"""
795832
A Linode Instance.
@@ -811,7 +848,7 @@ class Instance(Base):
811848
"disks": Property(derived_class=Disk),
812849
"configs": Property(derived_class=Config),
813850
"type": Property(slug_relationship=Type),
814-
"backups": Property(mutable=True),
851+
"backups": Property(mutable=True, json_object=InstanceBackups),
815852
"ipv4": Property(unordered=True),
816853
"ipv6": Property(),
817854
"hypervisor": Property(),

‎test/fixtures/linode_instances.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"label": "linode123",
2020
"backups": {
2121
"enabled": true,
22+
"available": true,
23+
"last_successful": "2018-01-01T00:01:01",
2224
"schedule": {
2325
"window": "W02",
2426
"day": "Scheduling"

‎test/unit/objects/linode_test.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
Disk,
2727
Image,
2828
Instance,
29+
InstanceBackups,
30+
InstanceBackupSchedule,
2931
StackScript,
3032
Type,
3133
VPCSubnet,
@@ -59,6 +61,14 @@ def test_get_linode(self):
5961
self.assertEqual(linode.lke_cluster_id, None)
6062
self.assertEqual(linode.maintenance_policy, "linode/migrate")
6163

64+
self.assertIsInstance(linode.backups, InstanceBackups)
65+
self.assertTrue(linode.backups.enabled)
66+
self.assertTrue(linode.backups.available)
67+
self.assertEqual(linode.backups.last_successful, "2018-01-01T00:01:01")
68+
self.assertIsInstance(linode.backups.schedule, InstanceBackupSchedule)
69+
self.assertEqual(linode.backups.schedule.day, "Scheduling")
70+
self.assertEqual(linode.backups.schedule.window, "W02")
71+
6272
json = linode._raw_json
6373
self.assertIsNotNone(json)
6474
self.assertEqual(json["id"], 123)
@@ -229,6 +239,22 @@ def test_update_linode(self):
229239
},
230240
)
231241

242+
def test_update_linode_backups_disabled(self):
243+
"""
244+
Tests that an unset backup schedule is sent as explicit nulls
245+
"""
246+
with self.mock_put("linode/instances/456") as m:
247+
linode = self.client.load(Instance, 456)
248+
linode.save()
249+
250+
self.assertEqual(
251+
m.call_data["backups"],
252+
{
253+
"enabled": False,
254+
"schedule": {"day": None, "window": None},
255+
},
256+
)
257+
232258
def test_delete_linode(self):
233259
"""
234260
Tests that deleting a Linode creates the correct api request

0 commit comments

Comments
 (0)