Skip to content

Commit

Permalink
[linode] Recursively expand MappedObjects before serializing.
Browse files Browse the repository at this point in the history
  • Loading branch information
blacklight committed Oct 21, 2024
1 parent bd7644b commit fe3d3d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions platypush/plugins/linode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,29 @@ def _get_instance(
assert instances, f'No such Linode instance: {instance}'
return instances[0]

def _linode_instance_to_dict(self, instance: Instance) -> dict:
@classmethod
def _expand_mapped_objects(cls, data: dict) -> dict:
"""
Expand the mapped objects in a :class:`linode_api4.Instance` to
dictionaries.
"""
for key, value in data.items():
if isinstance(value, objects.MappedObject):
value = data[key] = value.dict
if isinstance(value, dict):
data[key] = cls._expand_mapped_objects(value)

return data

@classmethod
def _linode_instance_to_dict(cls, instance: Instance) -> dict:
"""
Convert an internal :class:`linode_api4.Instance` to a
dictionary representation that can be used to create a
:class:`platypush.entities.cloud.CloudInstance` object.
"""
return {
key: (value.dict if isinstance(value, objects.MappedObject) else value)
key: cls._expand_mapped_objects(value)
for key, value in instance.__dict__.items()
if not key.startswith('_')
}
Expand Down
2 changes: 1 addition & 1 deletion platypush/schemas/linode/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class LinodeInstanceBackups:

available: bool
enabled: bool
schedule: LinodeInstanceBackupSchedule
schedule: Optional[LinodeInstanceBackupSchedule] = None
last_successful: Optional[datetime] = None


Expand Down

0 comments on commit fe3d3d6

Please sign in to comment.