Add context to IronicObject._from_db_object()

There is code in oslo.versionedobjects that expects an object
class to have this method:
    _from_db_object(context, item_cls(), db_item, **extra_args)

To conform to this, this patch adds 'context' as the first
parameter of base.IronicObject._from_db_object().

As a bonus, some docstrings were cleaned up.

Change-Id: I09b02f47d47cec22d03460b693f9e5c488cf04c7
Closes-Bug: #1670778
This commit is contained in:
Ruby Loo 2017-04-28 02:56:44 +00:00
parent 8fa488ccfc
commit 737cb1d3ef
9 changed files with 94 additions and 59 deletions

View File

@ -81,17 +81,18 @@ class IronicObject(object_base.VersionedObject):
self[field] = loaded_object[field]
@staticmethod
def _from_db_object(obj, db_object):
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._context = context
obj.obj_reset_changes()
return obj
@ -102,11 +103,12 @@ class IronicObject(object_base.VersionedObject):
Returns a list of formal objects of this class that correspond to
the list of database entities.
:param cls: the VersionedObject class of the desired object
:param context: security context
:param db_objects: A list of DB models of the object
:returns: A list of objects corresponding to the database entities
"""
return [cls._from_db_object(cls(context), db_obj)
return [cls._from_db_object(context, cls(), db_obj)
for db_obj in db_objects]
def _get_target_version(self):

View File

@ -66,13 +66,15 @@ class Chassis(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_id(cls, context, chassis_id):
"""Find a chassis based on its integer id and return a Chassis object.
"""Find a chassis based on its integer ID and return a Chassis object.
:param chassis_id: the id of a chassis.
:param cls: the :class:`Chassis`
:param context: Security context
:param chassis_id: the ID of a chassis.
:returns: a :class:`Chassis` object.
"""
db_chassis = cls.dbapi.get_chassis_by_id(chassis_id)
chassis = cls._from_db_object(cls(context), db_chassis)
chassis = cls._from_db_object(context, cls(), db_chassis)
return chassis
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -81,14 +83,15 @@ class Chassis(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_uuid(cls, context, uuid):
"""Find a chassis based on uuid and return a :class:`Chassis` object.
"""Find a chassis based on UUID and return a :class:`Chassis` object.
:param uuid: the uuid of a chassis.
:param cls: the :class:`Chassis`
:param context: Security context
:param uuid: the UUID of a chassis.
:returns: a :class:`Chassis` object.
"""
db_chassis = cls.dbapi.get_chassis_by_uuid(uuid)
chassis = cls._from_db_object(cls(context), db_chassis)
chassis = cls._from_db_object(context, cls(), db_chassis)
return chassis
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -100,6 +103,7 @@ class Chassis(base.IronicObject, object_base.VersionedObjectDictCompat):
sort_key=None, sort_dir=None):
"""Return a list of Chassis objects.
:param cls: the :class:`Chassis`
:param context: Security context.
:param limit: maximum number of resources to return in a single result.
:param marker: pagination marker for large data sets.
@ -136,7 +140,7 @@ class Chassis(base.IronicObject, object_base.VersionedObjectDictCompat):
"""
values = self.obj_get_changes()
db_chassis = self.dbapi.create_chassis(values)
self._from_db_object(self, db_chassis)
self._from_db_object(self._context, self, db_chassis)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -174,7 +178,7 @@ class Chassis(base.IronicObject, object_base.VersionedObjectDictCompat):
"""
updates = self.obj_get_changes()
updated_chassis = self.dbapi.update_chassis(self.uuid, updates)
self._from_db_object(self, updated_chassis)
self._from_db_object(self._context, self, updated_chassis)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.

View File

@ -47,11 +47,13 @@ class Conductor(base.IronicObject, object_base.VersionedObjectDictCompat):
def get_by_hostname(cls, context, hostname):
"""Get a Conductor record by its hostname.
:param cls: the :class:`Conductor`
:param context: Security context
:param hostname: the hostname on which a Conductor is running
:returns: a :class:`Conductor` object.
"""
db_obj = cls.dbapi.get_conductor(hostname)
conductor = cls._from_db_object(cls(context), db_obj)
conductor = cls._from_db_object(context, cls(), db_obj)
return conductor
def save(self, context):
@ -96,6 +98,8 @@ class Conductor(base.IronicObject, object_base.VersionedObjectDictCompat):
def register(cls, context, hostname, drivers, update_existing=False):
"""Register an active conductor with the cluster.
:param cls: the :class:`Conductor`
:param context: Security context
:param hostname: the hostname on which the conductor will run
:param drivers: the list of drivers enabled in the conductor
:param update_existing: When false, registration will raise an
@ -109,7 +113,7 @@ class Conductor(base.IronicObject, object_base.VersionedObjectDictCompat):
db_cond = cls.dbapi.register_conductor({'hostname': hostname,
'drivers': drivers},
update_existing=update_existing)
return cls._from_db_object(cls(context), db_cond)
return cls._from_db_object(context, cls(), db_cond)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.

View File

@ -178,13 +178,15 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_id(cls, context, node_id):
"""Find a node based on its integer id and return a Node object.
"""Find a node based on its integer ID and return a Node object.
:param node_id: the id of a node.
:param cls: the :class:`Node`
:param context: Security context
:param node_id: the ID of a node.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.get_node_by_id(node_id)
node = cls._from_db_object(cls(context), db_node)
node = cls._from_db_object(context, cls(), db_node)
return node
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -193,13 +195,15 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_uuid(cls, context, uuid):
"""Find a node based on uuid and return a Node object.
"""Find a node based on UUID and return a Node object.
:param uuid: the uuid of a node.
:param cls: the :class:`Node`
:param context: Security context
:param uuid: the UUID of a node.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.get_node_by_uuid(uuid)
node = cls._from_db_object(cls(context), db_node)
node = cls._from_db_object(context, cls(), db_node)
return node
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -210,11 +214,13 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
def get_by_name(cls, context, name):
"""Find a node based on name and return a Node object.
:param cls: the :class:`Node`
:param context: Security context
:param name: the logical name of a node.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.get_node_by_name(name)
node = cls._from_db_object(cls(context), db_node)
node = cls._from_db_object(context, cls(), db_node)
return node
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -223,13 +229,15 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_instance_uuid(cls, context, instance_uuid):
"""Find a node based on the instance uuid and return a Node object.
"""Find a node based on the instance UUID and return a Node object.
:param uuid: the uuid of the instance.
:param cls: the :class:`Node`
:param context: Security context
:param uuid: the UUID of the instance.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.get_node_by_instance(instance_uuid)
node = cls._from_db_object(cls(context), db_node)
node = cls._from_db_object(context, cls(), db_node)
return node
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -241,6 +249,7 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
sort_dir=None, filters=None):
"""Return a list of Node objects.
:param cls: the :class:`Node`
:param context: Security context.
:param limit: maximum number of resources to return in a single result.
:param marker: pagination marker for large data sets.
@ -266,15 +275,16 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
To prevent other ManagerServices from manipulating the given
Node while a Task is performed, mark it reserved by this host.
:param cls: the :class:`Node`
:param context: Security context.
:param tag: A string uniquely identifying the reservation holder.
:param node_id: A node id or uuid.
:param node_id: A node ID or UUID.
:raises: NodeNotFound if the node is not found.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.reserve_node(tag, node_id)
node = cls._from_db_object(cls(context), db_node)
node = cls._from_db_object(context, cls(), db_node)
return node
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -316,7 +326,7 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
values = self.obj_get_changes()
self._validate_property_values(values.get('properties'))
db_node = self.dbapi.create_node(values)
self._from_db_object(self, db_node)
self._from_db_object(self._context, self, db_node)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -362,7 +372,7 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
self.driver_internal_info = {}
updates = self.obj_get_changes()
db_node = self.dbapi.update_node(self.uuid, updates)
self._from_db_object(self, db_node)
self._from_db_object(self._context, self, db_node)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -394,13 +404,14 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat):
def get_by_port_addresses(cls, context, addresses):
"""Get a node by associated port addresses.
:param cls: the :class:`Node`
:param context: Security context.
:param addresses: A list of port addresses.
:raises: NodeNotFound if the node is not found.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.get_node_by_port_addresses(addresses)
node = cls._from_db_object(cls(context), db_node)
node = cls._from_db_object(context, cls(), db_node)
return node

View File

@ -84,15 +84,17 @@ class Port(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_id(cls, context, port_id):
"""Find a port based on its integer id and return a Port object.
"""Find a port based on its integer ID and return a Port object.
:param port_id: the id of a port.
:param cls: the :class:`Port`
:param context: Security context
:param port_id: the ID of a port.
:returns: a :class:`Port` object.
:raises: PortNotFound
"""
db_port = cls.dbapi.get_port_by_id(port_id)
port = cls._from_db_object(cls(context), db_port)
port = cls._from_db_object(context, cls(), db_port)
return port
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -101,16 +103,17 @@ class Port(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_uuid(cls, context, uuid):
"""Find a port based on uuid and return a :class:`Port` object.
"""Find a port based on UUID and return a :class:`Port` object.
:param uuid: the uuid of a port.
:param cls: the :class:`Port`
:param context: Security context
:param uuid: the UUID of a port.
:returns: a :class:`Port` object.
:raises: PortNotFound
"""
db_port = cls.dbapi.get_port_by_uuid(uuid)
port = cls._from_db_object(cls(context), db_port)
port = cls._from_db_object(context, cls(), db_port)
return port
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -121,14 +124,15 @@ class Port(base.IronicObject, object_base.VersionedObjectDictCompat):
def get_by_address(cls, context, address):
"""Find a port based on address and return a :class:`Port` object.
:param address: the address of a port.
:param cls: the :class:`Port`
:param context: Security context
:param address: the address of a port.
:returns: a :class:`Port` object.
:raises: PortNotFound
"""
db_port = cls.dbapi.get_port_by_address(address)
port = cls._from_db_object(cls(context), db_port)
port = cls._from_db_object(context, cls(), db_port)
return port
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -223,7 +227,7 @@ class Port(base.IronicObject, object_base.VersionedObjectDictCompat):
"""
values = self.obj_get_changes()
db_port = self.dbapi.create_port(values)
self._from_db_object(self, db_port)
self._from_db_object(self._context, self, db_port)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -266,7 +270,7 @@ class Port(base.IronicObject, object_base.VersionedObjectDictCompat):
"""
updates = self.obj_get_changes()
updated_port = self.dbapi.update_port(self.uuid, updates)
self._from_db_object(self, updated_port)
self._from_db_object(self._context, self, updated_port)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.

View File

@ -80,16 +80,17 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_id(cls, context, portgroup_id):
"""Find a portgroup based on its integer id and return a Portgroup object.
"""Find a portgroup based on its integer ID and return a Portgroup object.
:param portgroup_id: The id of a portgroup.
:param cls: the :class:`Portgroup`
:param context: Security context
:param portgroup_id: The ID of a portgroup.
:returns: A :class:`Portgroup` object.
:raises: PortgroupNotFound
"""
db_portgroup = cls.dbapi.get_portgroup_by_id(portgroup_id)
portgroup = cls._from_db_object(cls(context), db_portgroup)
portgroup = cls._from_db_object(context, cls(), db_portgroup)
return portgroup
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -98,16 +99,17 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
# @object_base.remotable_classmethod
@classmethod
def get_by_uuid(cls, context, uuid):
"""Find a portgroup based on uuid and return a :class:`Portgroup` object.
"""Find a portgroup based on UUID and return a :class:`Portgroup` object.
:param uuid: The uuid of a portgroup.
:param cls: the :class:`Portgroup`
:param context: Security context
:param uuid: The UUID of a portgroup.
:returns: A :class:`Portgroup` object.
:raises: PortgroupNotFound
"""
db_portgroup = cls.dbapi.get_portgroup_by_uuid(uuid)
portgroup = cls._from_db_object(cls(context), db_portgroup)
portgroup = cls._from_db_object(context, cls(), db_portgroup)
return portgroup
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -118,14 +120,15 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
def get_by_address(cls, context, address):
"""Find a portgroup based on address and return a :class:`Portgroup` object.
:param address: The MAC address of a portgroup.
:param cls: the :class:`Portgroup`
:param context: Security context
:param address: The MAC address of a portgroup.
:returns: A :class:`Portgroup` object.
:raises: PortgroupNotFound
"""
db_portgroup = cls.dbapi.get_portgroup_by_address(address)
portgroup = cls._from_db_object(cls(context), db_portgroup)
portgroup = cls._from_db_object(context, cls(), db_portgroup)
return portgroup
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -136,14 +139,15 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
def get_by_name(cls, context, name):
"""Find a portgroup based on name and return a :class:`Portgroup` object.
:param name: The name of a portgroup.
:param cls: the :class:`Portgroup`
:param context: Security context
:param name: The name of a portgroup.
:returns: A :class:`Portgroup` object.
:raises: PortgroupNotFound
"""
db_portgroup = cls.dbapi.get_portgroup_by_name(name)
portgroup = cls._from_db_object(cls(context), db_portgroup)
portgroup = cls._from_db_object(context, cls(), db_portgroup)
return portgroup
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -155,6 +159,7 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
sort_key=None, sort_dir=None):
"""Return a list of Portgroup objects.
:param cls: the :class:`Portgroup`
:param context: Security context.
:param limit: Maximum number of resources to return in a single result.
:param marker: Pagination marker for large data sets.
@ -179,6 +184,7 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
sort_key=None, sort_dir=None):
"""Return a list of Portgroup objects associated with a given node ID.
:param cls: the :class:`Portgroup`
:param context: Security context.
:param node_id: The ID of the node.
:param limit: Maximum number of resources to return in a single result.
@ -214,7 +220,7 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
"""
values = self.obj_get_changes()
db_portgroup = self.dbapi.create_portgroup(values)
self._from_db_object(self, db_portgroup)
self._from_db_object(self._context, self, db_portgroup)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -256,7 +262,7 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat):
"""
updates = self.obj_get_changes()
updated_portgroup = self.dbapi.update_portgroup(self.uuid, updates)
self._from_db_object(self, updated_portgroup)
self._from_db_object(self._context, self, updated_portgroup)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.

View File

@ -70,6 +70,7 @@ class VolumeConnector(base.IronicObject,
def get_by_id(cls, context, db_id):
"""Find a volume connector based on its integer ID.
:param cls: the :class:`VolumeConnector`
:param context: Security context.
:param db_id: The integer (database primary key) ID of a
volume connector.
@ -78,7 +79,7 @@ class VolumeConnector(base.IronicObject,
the specified ID.
"""
db_connector = cls.dbapi.get_volume_connector_by_id(db_id)
connector = cls._from_db_object(cls(context), db_connector)
connector = cls._from_db_object(context, cls(), db_connector)
return connector
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -89,6 +90,7 @@ class VolumeConnector(base.IronicObject,
def get_by_uuid(cls, context, uuid):
"""Find a volume connector based on its UUID.
:param cls: the :class:`VolumeConnector`
:param context: security context
:param uuid: the UUID of a volume connector
:returns: a :class:`VolumeConnector` object
@ -96,7 +98,7 @@ class VolumeConnector(base.IronicObject,
the specified UUID
"""
db_connector = cls.dbapi.get_volume_connector_by_uuid(uuid)
connector = cls._from_db_object(cls(context), db_connector)
connector = cls._from_db_object(context, cls(), db_connector)
return connector
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -168,7 +170,7 @@ class VolumeConnector(base.IronicObject,
"""
values = self.obj_get_changes()
db_connector = self.dbapi.create_volume_connector(values)
self._from_db_object(self, db_connector)
self._from_db_object(self._context, self, db_connector)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -215,7 +217,7 @@ class VolumeConnector(base.IronicObject,
updates = self.obj_get_changes()
updated_connector = self.dbapi.update_volume_connector(self.uuid,
updates)
self._from_db_object(self, updated_connector)
self._from_db_object(self._context, self, updated_connector)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.

View File

@ -72,13 +72,14 @@ class VolumeTarget(base.IronicObject,
def get_by_id(cls, context, db_id):
"""Find a volume target based on its database ID.
:param cls: the :class:`VolumeTarget`
:param context: security context
:param db_id: the database primary key (integer) ID of a volume target
:returns: a :class:`VolumeTarget` object
:raises: VolumeTargetNotFound if no volume target with this ID exists
"""
db_target = cls.dbapi.get_volume_target_by_id(db_id)
target = cls._from_db_object(cls(context), db_target)
target = cls._from_db_object(context, cls(), db_target)
return target
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -89,13 +90,14 @@ class VolumeTarget(base.IronicObject,
def get_by_uuid(cls, context, uuid):
"""Find a volume target based on its UUID.
:param cls: the :class:`VolumeTarget`
:param context: security context
:param uuid: the UUID of a volume target
:returns: a :class:`VolumeTarget` object
:raises: VolumeTargetNotFound if no volume target with this UUID exists
"""
db_target = cls.dbapi.get_volume_target_by_uuid(uuid)
target = cls._from_db_object(cls(context), db_target)
target = cls._from_db_object(context, cls(), db_target)
return target
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
@ -167,7 +169,7 @@ class VolumeTarget(base.IronicObject,
"""
values = self.obj_get_changes()
db_target = self.dbapi.create_volume_target(values)
self._from_db_object(self, db_target)
self._from_db_object(self._context, self, db_target)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
@ -210,7 +212,7 @@ class VolumeTarget(base.IronicObject,
"""
updates = self.obj_get_changes()
updated_target = self.dbapi.update_volume_target(self.uuid, updates)
self._from_db_object(self, updated_target)
self._from_db_object(self._context, self, updated_target)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.

View File

@ -69,7 +69,7 @@ class RPCAPITestCase(base.DbTestCase):
super(RPCAPITestCase, self).setUp()
self.fake_node = dbutils.get_test_node(driver='fake-driver')
self.fake_node_obj = objects.Node._from_db_object(
objects.Node(self.context), self.fake_node)
self.context, objects.Node(), self.fake_node)
self.fake_portgroup = dbutils.get_test_portgroup()
def test_serialized_instance_has_uuid(self):