84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
# Copyright 2017 Huawei Technologies Co.,LTD.
|
|
# All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
"""Cyborg common internal object model"""
|
|
|
|
from oslo_utils import versionutils
|
|
from oslo_versionedobjects import base as object_base
|
|
|
|
from cyborg import objects
|
|
from cyborg.objects import fields as object_fields
|
|
|
|
|
|
class CyborgObjectRegistry(object_base.VersionedObjectRegistry):
|
|
def registration_hook(self, cls, index):
|
|
# NOTE(jroll): blatantly stolen from nova
|
|
# NOTE(danms): This is called when an object is registered,
|
|
# and is responsible for maintaining cyborg.objects.$OBJECT
|
|
# as the highest-versioned implementation of a given object.
|
|
version = versionutils.convert_version_to_tuple(cls.VERSION)
|
|
if not hasattr(objects, cls.obj_name()):
|
|
setattr(objects, cls.obj_name(), cls)
|
|
else:
|
|
cur_version = versionutils.convert_version_to_tuple(
|
|
getattr(objects, cls.obj_name()).VERSION)
|
|
if version >= cur_version:
|
|
setattr(objects, cls.obj_name(), cls)
|
|
|
|
|
|
class CyborgObject(object_base.VersionedObject):
|
|
"""Base class and object factory.
|
|
|
|
This forms the base of all objects that can be remoted or instantiated
|
|
via RPC. Simply defining a class that inherits from this base class
|
|
will make it remotely instantiatable. Objects should implement the
|
|
necessary "get" classmethod routines as well as "save" object methods
|
|
as appropriate.
|
|
"""
|
|
|
|
OBJ_SERIAL_NAMESPACE = 'cyborg_object'
|
|
OBJ_PROJECT_NAMESPACE = 'cyborg'
|
|
|
|
fields = {
|
|
'created_at': object_fields.DateTimeField(nullable=True),
|
|
'updated_at': object_fields.DateTimeField(nullable=True),
|
|
}
|
|
|
|
def as_dict(self):
|
|
return dict((k, getattr(self, k))
|
|
for k in self.fields
|
|
if hasattr(self, k))
|
|
|
|
@staticmethod
|
|
def _from_db_object(context, obj, db_object):
|
|
"""Converts a database entity to a formal object.
|
|
|
|
:param context: security context
|
|
:param obj: An object of the class.
|
|
:param db_object: A DB model of the object
|
|
:return: The object of the class with the database entity added
|
|
"""
|
|
|
|
for field in obj.fields:
|
|
obj[field] = db_object[field]
|
|
|
|
obj.obj_reset_changes()
|
|
return obj
|
|
|
|
|
|
class CyborgObjectSerializer(object_base.VersionedObjectSerializer):
|
|
# Base class to use for object hydration
|
|
OBJ_BASE_CLASS = CyborgObject
|