Skip to content

Commit 69d9e55

Browse files
authored
Merge pull request #402 from linode/dev
Release v5.15.1
2 parents 29160a2 + b0920fd commit 69d9e55

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

.github/workflows/release-cross-repo-test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ jobs:
2626
with:
2727
python-version: '3.10'
2828

29-
- name: Install linode_api4
30-
run: make install
31-
3229
- name: checkout repo
3330
uses: actions/checkout@v3
3431
with:
@@ -48,12 +45,15 @@ jobs:
4845
cd .ansible/collections/ansible_collections/linode/cloud
4946
make install
5047
48+
- name: Install linode_api4 # Need to install from source after all ansible dependencies have been installed
49+
run: make install
50+
5151
- name: replace existing keys
5252
run: |
5353
cd .ansible/collections/ansible_collections/linode/cloud
5454
rm -rf ~/.ansible/test && mkdir -p ~/.ansible/test && ssh-keygen -m PEM -q -t rsa -N '' -f ~/.ansible/test/id_rsa
5555
56-
- name: run tests
56+
- name: Run Ansible Tests
5757
run: |
5858
cd .ansible/collections/ansible_collections/linode/cloud
5959
make testall

linode_api4/objects/base.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
from datetime import datetime, timedelta
3+
from typing import Any, Dict, Optional
34

45
from linode_api4.objects.serializable import JSONObject
56

@@ -99,6 +100,18 @@ def _expand_vals(self, target, **vals):
99100
def __repr__(self):
100101
return "Mapping containing {}".format(vars(self).keys())
101102

103+
@staticmethod
104+
def _flatten_base_subclass(obj: "Base") -> Optional[Dict[str, Any]]:
105+
if obj is None:
106+
return None
107+
108+
# If the object hasn't already been lazy-loaded,
109+
# manually refresh it
110+
if not getattr(obj, "_populated", False):
111+
obj._api_get()
112+
113+
return obj._raw_json
114+
102115
@property
103116
def dict(self):
104117
result = vars(self).copy()
@@ -111,13 +124,20 @@ def dict(self):
111124
result[k] = [
112125
(
113126
item.dict
114-
if isinstance(item, cls)
115-
else item._raw_json if isinstance(item, Base) else item
127+
if isinstance(item, (cls, JSONObject))
128+
else (
129+
self._flatten_base_subclass(item)
130+
if isinstance(item, Base)
131+
else item
132+
)
116133
)
117134
for item in v
118135
]
119136
elif isinstance(v, Base):
120-
result[k] = v._raw_json
137+
result[k] = self._flatten_base_subclass(v)
138+
elif isinstance(v, JSONObject):
139+
result[k] = v.dict
140+
121141
return result
122142

123143

@@ -140,7 +160,10 @@ def __init__(self, client: object, id: object, json: object = {}) -> object:
140160
#: be updated on access.
141161
self._set("_raw_json", None)
142162

143-
for k in type(self).properties:
163+
for k, v in type(self).properties.items():
164+
if v.identifier:
165+
continue
166+
144167
self._set(k, None)
145168

146169
self._set("id", id)

linode_api4/objects/dbase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class DerivedBase(Base):
1212
parent_id_name = "parent_id" # override in child classes
1313

1414
def __init__(self, client, id, parent_id, json={}):
15-
Base.__init__(self, client, id, json=json)
16-
1715
self._set(type(self).parent_id_name, parent_id)
1816

17+
Base.__init__(self, client, id, json=json)
18+
1919
@classmethod
2020
def _api_get_derived(cls, parent, client):
2121
base_url = "{}/{}".format(

linode_api4/objects/object_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class ObjectStorageBucket(DerivedBase):
3131
id_attribute = "label"
3232

3333
properties = {
34-
"cluster": Property(),
34+
"cluster": Property(identifier=True),
3535
"created": Property(is_datetime=True),
3636
"hostname": Property(),
37-
"label": Property(),
37+
"label": Property(identifier=True),
3838
"objects": Property(),
3939
"size": Property(),
4040
}

test/unit/objects/linode_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,14 @@ def test_interface_ipv4(self):
554554
self.assertEqual(ipv4.vpc, "10.0.0.1")
555555
self.assertEqual(ipv4.nat_1_1, "any")
556556

557+
def test_config_devices_unwrap(self):
558+
"""
559+
Tests that config devices can be successfully converted to a dict.
560+
"""
561+
562+
inst = Instance(self.client, 123)
563+
assert inst.configs[0].devices.dict.get("sda").get("id") == 12345
564+
557565

558566
class StackScriptTest(ClientBaseCase):
559567
"""

test/unit/objects/mapped_object_test.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from dataclasses import dataclass
12
from test.unit.base import ClientBaseCase
23

3-
from linode_api4.objects import Base, MappedObject, Property
4+
from linode_api4.objects import Base, JSONObject, MappedObject, Property
45

56

67
class MappedObjectCase(ClientBaseCase):
@@ -20,7 +21,7 @@ def test_mapped_object_dict(self):
2021
mapped_obj = MappedObject(**test_dict)
2122
self.assertEqual(mapped_obj.dict, test_dict)
2223

23-
def test_mapped_object_dict(self):
24+
def test_serialize_base_objects(self):
2425
test_property_name = "bar"
2526
test_property_value = "bar"
2627

@@ -42,3 +43,22 @@ class Foo(Base):
4243

4344
mapped_obj = MappedObject(foo=foo)
4445
self.assertEqual(mapped_obj.dict, expected_dict)
46+
47+
def test_serialize_json_objects(self):
48+
test_property_name = "bar"
49+
test_property_value = "bar"
50+
51+
@dataclass
52+
class Foo(JSONObject):
53+
bar: str = ""
54+
55+
foo = Foo.from_json({test_property_name: test_property_value})
56+
57+
expected_dict = {
58+
"foo": {
59+
test_property_name: test_property_value,
60+
}
61+
}
62+
63+
mapped_obj = MappedObject(foo=foo)
64+
self.assertEqual(mapped_obj.dict, expected_dict)

0 commit comments

Comments
 (0)