Пример #1
0
    def obj_class_from_name(cls, objname, objver):
        """Returns a class from the registry based on a name and version."""
        if objname not in VersionedObjectRegistry.obj_classes():
            LOG.error(
                _LE('Unable to instantiate unregistered object type '
                    '%(objtype)s'), dict(objtype=objname))
            raise exception.UnsupportedObjectError(objtype=objname)

        # NOTE(comstud): If there's not an exact match, return the highest
        # compatible version. The objects stored in the class are sorted
        # such that highest version is first, so only set compatible_match
        # once below.
        compatible_match = None

        for objclass in VersionedObjectRegistry.obj_classes()[objname]:
            if objclass.VERSION == objver:
                return objclass
            if (not compatible_match
                    and vutils.is_compatible(objver, objclass.VERSION)):
                compatible_match = objclass

        if compatible_match:
            return compatible_match

        # As mentioned above, latest version is always first in the list.
        latest_ver = VersionedObjectRegistry.obj_classes()[objname][0].VERSION
        raise exception.IncompatibleObjectVersion(objname=objname,
                                                  objver=objver,
                                                  supported=latest_ver)
Пример #2
0
 def obj_from_primitive(cls, primitive, context=None):
     """Object field-by-field hydration."""
     objns = cls._obj_primitive_field(primitive, 'namespace')
     objname = cls._obj_primitive_field(primitive, 'name')
     objver = cls._obj_primitive_field(primitive, 'version')
     if objns != cls.OBJ_PROJECT_NAMESPACE:
         # NOTE(danms): We don't do anything with this now, but it's
         # there for "the future"
         raise exception.UnsupportedObjectError(objtype='%s.%s' %
                                                (objns, objname))
     objclass = cls.obj_class_from_name(objname, objver)
     return objclass._obj_from_primitive(context, objver, primitive)
Пример #3
0
    def get_schema(self):
        from oslo_versionedobjects import base as obj_base
        obj_classes = obj_base.VersionedObjectRegistry.obj_classes()
        if self._obj_name in obj_classes:
            cls = obj_classes[self._obj_name][0]
            namespace_key = cls._obj_primitive_key('namespace')
            name_key = cls._obj_primitive_key('name')
            version_key = cls._obj_primitive_key('version')
            data_key = cls._obj_primitive_key('data')
            changes_key = cls._obj_primitive_key('changes')
            field_schemas = {
                key: field.get_schema()
                for key, field in cls.fields.items()
            }
            required_fields = [
                key for key, field in sorted(cls.fields.items())
                if not field.nullable
            ]
            schema = {
                'type': ['object'],
                'properties': {
                    namespace_key: {
                        'type': 'string'
                    },
                    name_key: {
                        'type': 'string'
                    },
                    version_key: {
                        'type': 'string'
                    },
                    changes_key: {
                        'type': 'array',
                        'items': {
                            'type': 'string'
                        }
                    },
                    data_key: {
                        'type': 'object',
                        'description': 'fields of %s' % self._obj_name,
                        'properties': field_schemas,
                    },
                },
                'required': [namespace_key, name_key, version_key, data_key]
            }

            if required_fields:
                schema['properties'][data_key]['required'] = required_fields

            return schema
        else:
            raise exception.UnsupportedObjectError(objtype=self._obj_name)
Пример #4
0
    def obj_class_from_name(cls, objname, objver):
        """Returns a class from the registry based on a name and version."""
        # NOTE(slaweq): it is override method
        # oslo_versionedobjects.base.VersionedObject.obj_class_from_name
        # We need to override it to use Neutron's objects registry class
        # (NeutronObjectRegistry) instead of original VersionedObjectRegistry
        # class from oslo_versionedobjects
        # This is necessary to avoid clash in naming objects between Neutron
        # and e.g. os-vif (for example Route or Subnet objects are used in
        # both)
        if objname not in NeutronObjectRegistry.obj_classes():
            LOG.error('Unable to instantiate unregistered object type '
                      '%(objtype)s', dict(objtype=objname))
            raise obj_exception.UnsupportedObjectError(objtype=objname)

        # NOTE(comstud): If there's not an exact match, return the highest
        # compatible version. The objects stored in the class are sorted
        # such that highest version is first, so only set compatible_match
        # once below.
        compatible_match = None

        for objclass in NeutronObjectRegistry.obj_classes()[objname]:
            if objclass.VERSION == objver:
                return objclass
            if (not compatible_match and
                    versionutils.is_compatible(objver, objclass.VERSION)):
                compatible_match = objclass

        if compatible_match:
            return compatible_match

        # As mentioned above, latest version is always first in the list.
        latest_ver = (
            NeutronObjectRegistry.obj_classes()[objname][0].VERSION)
        raise obj_exception.IncompatibleObjectVersion(objname=objname,
                                                      objver=objver,
                                                      supported=latest_ver)