From fe3d3d6c16cee40bd76e9fb55a092b4f747e70e1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 21 Oct 2024 23:33:34 +0200 Subject: [PATCH] [linode] Recursively expand MappedObjects before serializing. --- platypush/plugins/linode/__init__.py | 19 +++++++++++++++++-- platypush/schemas/linode/_model.py | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/linode/__init__.py b/platypush/plugins/linode/__init__.py index e61cad83b6..5a3796b0c8 100644 --- a/platypush/plugins/linode/__init__.py +++ b/platypush/plugins/linode/__init__.py @@ -66,14 +66,29 @@ class LinodePlugin(RunnablePlugin, CloudInstanceEntityManager, EnumSwitchEntityM 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('_') } diff --git a/platypush/schemas/linode/_model.py b/platypush/schemas/linode/_model.py index fff1aff782..72ded24378 100644 --- a/platypush/schemas/linode/_model.py +++ b/platypush/schemas/linode/_model.py @@ -108,7 +108,7 @@ class LinodeInstanceBackups: available: bool enabled: bool - schedule: LinodeInstanceBackupSchedule + schedule: Optional[LinodeInstanceBackupSchedule] = None last_successful: Optional[datetime] = None