From 98317302088287ef227426abe33f5d55f976bb73 Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Sun, 19 Jan 2020 20:50:29 +0800 Subject: [PATCH] Add obj_make_compatible() This adds a method to CyborgObject that allows it to convert itself to and older version, within a compatibility window. So, if an object had a revision that added or changed the formatting of an attribute, the obj_make_compatible() method can fix up a primitive representation before it is sent to a client expecting the older version. Partial-Implements: blueprint add-description-field-to-device-profiles Change-Id: I196629059bc32165f161fe9c071a339d63d71c10 --- cyborg/objects/base.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cyborg/objects/base.py b/cyborg/objects/base.py index 4b3c2be1..3f164f53 100644 --- a/cyborg/objects/base.py +++ b/cyborg/objects/base.py @@ -16,6 +16,7 @@ """Cyborg common internal object model""" import netaddr +from oslo_log import log as logging from oslo_utils import versionutils from oslo_versionedobjects import base as object_base @@ -23,6 +24,9 @@ from cyborg import objects from cyborg.objects import fields as object_fields +LOG = logging.getLogger(__name__) + + class CyborgObjectRegistry(object_base.VersionedObjectRegistry): def registration_hook(self, cls, index): # NOTE(jroll): blatantly stolen from nova @@ -95,6 +99,22 @@ class CyborgObject(object_base.VersionedObject): objs.append(cls._from_db_object(cls(context), db_obj)) return objs + def obj_make_compatible(self, primitive, target_version): + """Make an object representation compatible with a target version. + + This is responsible for taking the primitive representation of + an object and making it suitable for the given target_version. + This may mean converting the format of object attributes, removing + attributes that have been added since the target version, etc. + + :param:primitive: The result of self.obj_to_primitive() + :param:target_version: The version string requested by the recipient + of the object. + """ + _log_backport(self, target_version) + super(CyborgObject, self).obj_make_compatible(primitive, + target_version) + class CyborgObjectSerializer(object_base.VersionedObjectSerializer): # Base class to use for object hydration @@ -199,3 +219,13 @@ class DriverObjectBase(CyborgObject): obj.obj_reset_changes() return obj + + +def _log_backport(ovo, target_version): + """Log backported versioned objects.""" + if target_version and target_version != ovo.VERSION: + LOG.debug('Backporting %(obj_name)s from version %(src_vers)s ' + 'to version %(dst_vers)s', + {'obj_name': ovo.obj_name(), + 'src_vers': ovo.VERSION, + 'dst_vers': target_version})