Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/release-cross-repo-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ jobs:
with:
python-version: '3.10'

- name: Install linode_api4
run: make install

- name: checkout repo
uses: actions/checkout@v3
with:
Expand All @@ -48,12 +45,15 @@ jobs:
cd .ansible/collections/ansible_collections/linode/cloud
make install

- name: Install linode_api4 # Need to install from source after all ansible dependencies have been installed
run: make install

- name: replace existing keys
run: |
cd .ansible/collections/ansible_collections/linode/cloud
rm -rf ~/.ansible/test && mkdir -p ~/.ansible/test && ssh-keygen -m PEM -q -t rsa -N '' -f ~/.ansible/test/id_rsa

- name: run tests
- name: Run Ansible Tests
run: |
cd .ansible/collections/ansible_collections/linode/cloud
make testall
Expand Down
31 changes: 27 additions & 4 deletions linode_api4/objects/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from datetime import datetime, timedelta
from typing import Any, Dict, Optional

from linode_api4.objects.serializable import JSONObject

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

@staticmethod
def _flatten_base_subclass(obj: "Base") -> Optional[Dict[str, Any]]:
if obj is None:
return None

# If the object hasn't already been lazy-loaded,
# manually refresh it
if not getattr(obj, "_populated", False):
obj._api_get()

return obj._raw_json

@property
def dict(self):
result = vars(self).copy()
Expand All @@ -111,13 +124,20 @@ def dict(self):
result[k] = [
(
item.dict
if isinstance(item, cls)
else item._raw_json if isinstance(item, Base) else item
if isinstance(item, (cls, JSONObject))
else (
self._flatten_base_subclass(item)
if isinstance(item, Base)
else item
)
)
for item in v
]
elif isinstance(v, Base):
result[k] = v._raw_json
result[k] = self._flatten_base_subclass(v)
elif isinstance(v, JSONObject):
result[k] = v.dict

return result


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

for k in type(self).properties:
for k, v in type(self).properties.items():
if v.identifier:
continue

self._set(k, None)

self._set("id", id)
Expand Down
4 changes: 2 additions & 2 deletions linode_api4/objects/dbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class DerivedBase(Base):
parent_id_name = "parent_id" # override in child classes

def __init__(self, client, id, parent_id, json={}):
Base.__init__(self, client, id, json=json)

self._set(type(self).parent_id_name, parent_id)

Base.__init__(self, client, id, json=json)

@classmethod
def _api_get_derived(cls, parent, client):
base_url = "{}/{}".format(
Expand Down
4 changes: 2 additions & 2 deletions linode_api4/objects/object_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class ObjectStorageBucket(DerivedBase):
id_attribute = "label"

properties = {
"cluster": Property(),
"cluster": Property(identifier=True),
"created": Property(is_datetime=True),
"hostname": Property(),
"label": Property(),
"label": Property(identifier=True),
"objects": Property(),
"size": Property(),
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/objects/linode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@ def test_interface_ipv4(self):
self.assertEqual(ipv4.vpc, "10.0.0.1")
self.assertEqual(ipv4.nat_1_1, "any")

def test_config_devices_unwrap(self):
"""
Tests that config devices can be successfully converted to a dict.
"""

inst = Instance(self.client, 123)
assert inst.configs[0].devices.dict.get("sda").get("id") == 12345


class StackScriptTest(ClientBaseCase):
"""
Expand Down
24 changes: 22 additions & 2 deletions test/unit/objects/mapped_object_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from test.unit.base import ClientBaseCase

from linode_api4.objects import Base, MappedObject, Property
from linode_api4.objects import Base, JSONObject, MappedObject, Property


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

def test_mapped_object_dict(self):
def test_serialize_base_objects(self):
test_property_name = "bar"
test_property_value = "bar"

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

mapped_obj = MappedObject(foo=foo)
self.assertEqual(mapped_obj.dict, expected_dict)

def test_serialize_json_objects(self):
test_property_name = "bar"
test_property_value = "bar"

@dataclass
class Foo(JSONObject):
bar: str = ""

foo = Foo.from_json({test_property_name: test_property_value})

expected_dict = {
"foo": {
test_property_name: test_property_value,
}
}

mapped_obj = MappedObject(foo=foo)
self.assertEqual(mapped_obj.dict, expected_dict)