Skip to content

Commit 49d0801

Browse files
Make backups and watchdog_enabled mutable; implement nested MappedObject dict Conversion (#282)
## 📝 Description Since `backups` and `watchdog_enabled` fields of the Linode `Instance` the are mutable by the API, we may also want it be mutable in Python SDK. Original `Base.dict` property doesn't support converting nested MappedObject to dict, so I also implemented it in this PR. closes #281
1 parent d511f44 commit 49d0801

5 files changed

Lines changed: 42 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ docs/_build/*
1010
.pytest_cache/*
1111
.tox/*
1212
venv
13-
baked_version
13+
baked_version
14+
.vscode

linode_api4/objects/base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ def __repr__(self):
9090

9191
@property
9292
def dict(self):
93-
return dict(self.__dict__)
93+
result = vars(self).copy()
94+
cls = type(self)
95+
96+
for k, v in result.items():
97+
if isinstance(v, cls):
98+
result[k] = v.dict
99+
elif isinstance(v, list):
100+
result[k] = [
101+
item.dict if isinstance(item, cls) else item for item in v
102+
]
103+
104+
return result
94105

95106

96107
class Base(object, metaclass=FilterableMetaclass):

linode_api4/objects/linode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,14 @@ class Instance(Base):
403403
"disks": Property(derived_class=Disk),
404404
"configs": Property(derived_class=Config),
405405
"type": Property(slug_relationship=Type),
406-
"backups": Property(),
406+
"backups": Property(mutable=True),
407407
"ipv4": Property(),
408408
"ipv6": Property(),
409409
"hypervisor": Property(),
410410
"specs": Property(),
411411
"tags": Property(mutable=True),
412412
"host_uuid": Property(),
413-
"watchdog_enabled": Property(),
413+
"watchdog_enabled": Property(mutable=True),
414414
}
415415

416416
@property

test/objects/linode_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,14 @@ def test_update_linode(self):
147147
"network_out": 5,
148148
"transfer_quota": 80,
149149
},
150+
"backups": {
151+
"enabled": True,
152+
"schedule": {"day": "Scheduling", "window": "W02"},
153+
},
150154
"label": "NewLinodeLabel",
151155
"group": "new_group",
152156
"tags": ["something"],
157+
"watchdog_enabled": True,
153158
},
154159
)
155160

test/objects/mapped_object_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from unittest import TestCase
2+
3+
from linode_api4 import MappedObject
4+
5+
6+
class MappedObjectCase(TestCase):
7+
def test_mapped_object_dict(self):
8+
test_dict = {
9+
"key1": 1,
10+
"key2": "2",
11+
"key3": 3.3,
12+
"key4": [41, "42", {"key4-3": "43"}],
13+
"key5": {
14+
"key5-1": 1,
15+
"key5-2": {"key5-2-1": {"key5-2-1-1": 1}},
16+
"key5-3": [{"key5-3-1": 531}, {"key5-3-2": 532}],
17+
},
18+
}
19+
20+
mapped_obj = MappedObject(**test_dict)
21+
self.assertEqual(mapped_obj.dict, test_dict)

0 commit comments

Comments
 (0)