Make context mandatory when instantiating a RPC object
RPC objects should have a context within it, this patch is making passing the context mandatory when instantiating the object and is a plumbing work to remove passing the context later for things like create(), refresh(), destroy() and save(). Partial-Bug: #1314732 Change-Id: If9b175fa874bcb96c77cf22d176f1111f450f796
This commit is contained in:
parent
f3f32b44b5
commit
654ea01741
@ -213,7 +213,7 @@ class ChassisController(rest.RestController):
|
||||
|
||||
:param chassis: a chassis within the request body.
|
||||
"""
|
||||
new_chassis = objects.Chassis(context=pecan.request.context,
|
||||
new_chassis = objects.Chassis(pecan.request.context,
|
||||
**chassis.as_dict())
|
||||
new_chassis.create()
|
||||
# Set the HTTP Location Header
|
||||
|
@ -748,7 +748,7 @@ class NodesController(rest.RestController):
|
||||
e.code = 400
|
||||
raise e
|
||||
|
||||
new_node = objects.Node(context=pecan.request.context,
|
||||
new_node = objects.Node(pecan.request.context,
|
||||
**node.as_dict())
|
||||
new_node.create()
|
||||
# Set the HTTP Location Header
|
||||
|
@ -289,7 +289,7 @@ class PortsController(rest.RestController):
|
||||
if self.from_nodes:
|
||||
raise exception.OperationNotPermitted
|
||||
|
||||
new_port = objects.Port(context=pecan.request.context,
|
||||
new_port = objects.Port(pecan.request.context,
|
||||
**port.as_dict())
|
||||
new_port.create()
|
||||
# Set the HTTP Location Header
|
||||
|
@ -203,7 +203,7 @@ class IronicObject(object):
|
||||
_attr_created_at_to_primitive = obj_utils.dt_serializer('created_at')
|
||||
_attr_updated_at_to_primitive = obj_utils.dt_serializer('updated_at')
|
||||
|
||||
def __init__(self, context=None, **kwargs):
|
||||
def __init__(self, context, **kwargs):
|
||||
self._changed_fields = set()
|
||||
self._context = context
|
||||
self.update(kwargs)
|
||||
@ -260,8 +260,7 @@ class IronicObject(object):
|
||||
|
||||
@classmethod
|
||||
def _obj_from_primitive(cls, context, objver, primitive):
|
||||
self = cls()
|
||||
self._context = context
|
||||
self = cls(context)
|
||||
self.VERSION = objver
|
||||
objdata = primitive['ironic_object.data']
|
||||
changes = primitive.get('ironic_object.changes', [])
|
||||
@ -298,8 +297,7 @@ class IronicObject(object):
|
||||
# some objects may be uncopyable, so we can avoid those sorts
|
||||
# of issues by copying only our field data.
|
||||
|
||||
nobj = self.__class__()
|
||||
nobj._context = self._context
|
||||
nobj = self.__class__(self._context)
|
||||
for name in self.fields:
|
||||
if self.obj_attr_is_set(name):
|
||||
nval = copy.deepcopy(getattr(self, name), memo)
|
||||
@ -487,11 +485,10 @@ class ObjectListBase(object):
|
||||
def __getitem__(self, index):
|
||||
"""List index access."""
|
||||
if isinstance(index, slice):
|
||||
new_obj = self.__class__()
|
||||
new_obj = self.__class__(self._context)
|
||||
new_obj.objects = self.objects[index]
|
||||
# NOTE(danms): We must be mixed in with an IronicObject!
|
||||
new_obj.obj_reset_changes()
|
||||
new_obj._context = self._context
|
||||
return new_obj
|
||||
return self.objects[index]
|
||||
|
||||
|
@ -73,7 +73,7 @@ class Chassis(base.IronicObject):
|
||||
:returns: a :class:`Chassis` object.
|
||||
"""
|
||||
db_chassis = cls.dbapi.get_chassis_by_id(chassis_id)
|
||||
chassis = Chassis._from_db_object(cls(), db_chassis)
|
||||
chassis = Chassis._from_db_object(cls(context), db_chassis)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
chassis._context = context
|
||||
@ -88,7 +88,7 @@ class Chassis(base.IronicObject):
|
||||
:returns: a :class:`Chassis` object.
|
||||
"""
|
||||
db_chassis = cls.dbapi.get_chassis_by_uuid(uuid)
|
||||
chassis = Chassis._from_db_object(cls(), db_chassis)
|
||||
chassis = Chassis._from_db_object(cls(context), db_chassis)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
chassis._context = context
|
||||
@ -113,7 +113,7 @@ class Chassis(base.IronicObject):
|
||||
sort_key=sort_key,
|
||||
sort_dir=sort_dir)
|
||||
for obj in db_chassis:
|
||||
chassis = Chassis._from_db_object(cls(), obj)
|
||||
chassis = Chassis._from_db_object(cls(context), obj)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
chassis._context = context
|
||||
|
@ -47,7 +47,7 @@ class Conductor(base.IronicObject):
|
||||
:returns: a :class:`Conductor` object.
|
||||
"""
|
||||
db_obj = cls.dbapi.get_conductor(hostname)
|
||||
conductor = Conductor._from_db_object(cls(), db_obj)
|
||||
conductor = Conductor._from_db_object(cls(context), db_obj)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
conductor._context = context
|
||||
|
@ -99,7 +99,7 @@ class Node(base.IronicObject):
|
||||
:returns: a :class:`Node` object.
|
||||
"""
|
||||
db_node = cls.dbapi.get_node_by_id(node_id)
|
||||
node = Node._from_db_object(cls(), db_node)
|
||||
node = Node._from_db_object(cls(context), db_node)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
node._context = context
|
||||
@ -113,7 +113,7 @@ class Node(base.IronicObject):
|
||||
:returns: a :class:`Node` object.
|
||||
"""
|
||||
db_node = cls.dbapi.get_node_by_uuid(uuid)
|
||||
node = Node._from_db_object(cls(), db_node)
|
||||
node = Node._from_db_object(cls(context), db_node)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
node._context = context
|
||||
@ -127,7 +127,7 @@ class Node(base.IronicObject):
|
||||
:returns: a :class:`Node` object.
|
||||
"""
|
||||
db_node = cls.dbapi.get_node_by_instance(instance_uuid)
|
||||
node = Node._from_db_object(cls(), db_node)
|
||||
node = Node._from_db_object(cls(context), db_node)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
node._context = context
|
||||
@ -152,7 +152,7 @@ class Node(base.IronicObject):
|
||||
marker=marker, sort_key=sort_key,
|
||||
sort_dir=sort_dir)
|
||||
for obj in db_nodes:
|
||||
node = Node._from_db_object(cls(), obj)
|
||||
node = Node._from_db_object(cls(context), obj)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
node._context = context
|
||||
@ -174,7 +174,7 @@ class Node(base.IronicObject):
|
||||
|
||||
"""
|
||||
db_node = cls.dbapi.reserve_node(tag, node_id)
|
||||
node = Node._from_db_object(cls(), db_node)
|
||||
node = Node._from_db_object(cls(context), db_node)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
node._context = context
|
||||
|
@ -53,7 +53,7 @@ class Port(base.IronicObject):
|
||||
"""Converts a list of database entities to a list of formal objects."""
|
||||
port_list = []
|
||||
for obj in db_objects:
|
||||
port = Port._from_db_object(cls(), obj)
|
||||
port = Port._from_db_object(cls(context), obj)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
port._context = context
|
||||
@ -84,7 +84,7 @@ class Port(base.IronicObject):
|
||||
:returns: a :class:`Port` object.
|
||||
"""
|
||||
db_port = cls.dbapi.get_port_by_id(port_id)
|
||||
port = Port._from_db_object(cls(), db_port)
|
||||
port = Port._from_db_object(cls(context), db_port)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
port._context = context
|
||||
@ -99,7 +99,7 @@ class Port(base.IronicObject):
|
||||
:returns: a :class:`Port` object.
|
||||
"""
|
||||
db_port = cls.dbapi.get_port_by_uuid(uuid)
|
||||
port = Port._from_db_object(cls(), db_port)
|
||||
port = Port._from_db_object(cls(context), db_port)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
port._context = context
|
||||
@ -114,7 +114,7 @@ class Port(base.IronicObject):
|
||||
:returns: a :class:`Port` object.
|
||||
"""
|
||||
db_port = cls.dbapi.get_port_by_address(address)
|
||||
port = Port._from_db_object(cls(), db_port)
|
||||
port = Port._from_db_object(cls(context), db_port)
|
||||
# FIXME(comstud): Setting of the context should be moved to
|
||||
# _from_db_object().
|
||||
port._context = context
|
||||
|
@ -54,7 +54,7 @@ class RPCAPITestCase(base.DbTestCase):
|
||||
self.dbapi = dbapi.get_instance()
|
||||
self.fake_node = dbutils.get_test_node(driver='fake-driver')
|
||||
self.fake_node_obj = objects.Node._from_db_object(
|
||||
objects.Node(),
|
||||
objects.Node(self.context),
|
||||
self.fake_node)
|
||||
|
||||
def test_serialized_instance_has_uuid(self):
|
||||
|
@ -43,7 +43,7 @@ class MyObj(base.IronicObject):
|
||||
|
||||
@base.remotable_classmethod
|
||||
def query(cls, context):
|
||||
obj = cls()
|
||||
obj = cls(context)
|
||||
obj.foo = 1
|
||||
obj.bar = 'bar'
|
||||
obj.obj_reset_changes()
|
||||
@ -124,6 +124,11 @@ class TestMetaclass(test_base.TestCase):
|
||||
|
||||
|
||||
class TestUtils(test_base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestUtils, self).setUp()
|
||||
self.context = context.get_admin_context()
|
||||
|
||||
def test_datetime_or_none(self):
|
||||
naive_dt = datetime.datetime.now()
|
||||
dt = timeutils.parse_isotime(timeutils.isotime(naive_dt))
|
||||
@ -187,12 +192,12 @@ class TestUtils(test_base.TestCase):
|
||||
def test_obj_to_primitive_list(self):
|
||||
class MyList(base.ObjectListBase, base.IronicObject):
|
||||
pass
|
||||
mylist = MyList()
|
||||
mylist = MyList(self.context)
|
||||
mylist.objects = [1, 2, 3]
|
||||
self.assertEqual([1, 2, 3], base.obj_to_primitive(mylist))
|
||||
|
||||
def test_obj_to_primitive_dict(self):
|
||||
myobj = MyObj()
|
||||
myobj = MyObj(self.context)
|
||||
myobj.foo = 1
|
||||
myobj.bar = 'foo'
|
||||
self.assertEqual({'foo': 1, 'bar': 'foo'},
|
||||
@ -202,8 +207,8 @@ class TestUtils(test_base.TestCase):
|
||||
class MyList(base.ObjectListBase, base.IronicObject):
|
||||
pass
|
||||
|
||||
mylist = MyList()
|
||||
mylist.objects = [MyObj(), MyObj()]
|
||||
mylist = MyList(self.context)
|
||||
mylist.objects = [MyObj(self.context), MyObj(self.context)]
|
||||
for i, value in enumerate(mylist):
|
||||
value.foo = i
|
||||
self.assertEqual([{'foo': 0}, {'foo': 1}],
|
||||
@ -266,13 +271,13 @@ class _TestObject(object):
|
||||
'ironic_object.namespace': 'ironic',
|
||||
'ironic_object.version': '1.5',
|
||||
'ironic_object.data': {'foo': 1}}
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
obj.foo = 1
|
||||
obj.obj_reset_changes()
|
||||
self.assertEqual(expected, obj.obj_to_primitive())
|
||||
|
||||
def test_get_updates(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
self.assertEqual({}, obj.obj_get_changes())
|
||||
obj.foo = 123
|
||||
self.assertEqual({'foo': 123}, obj.obj_get_changes())
|
||||
@ -282,18 +287,18 @@ class _TestObject(object):
|
||||
self.assertEqual({}, obj.obj_get_changes())
|
||||
|
||||
def test_object_property(self):
|
||||
obj = MyObj(foo=1)
|
||||
obj = MyObj(self.context, foo=1)
|
||||
self.assertEqual(1, obj.foo)
|
||||
|
||||
def test_object_property_type_error(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
|
||||
def fail():
|
||||
obj.foo = 'a'
|
||||
self.assertRaises(ValueError, fail)
|
||||
|
||||
def test_object_dict_syntax(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
obj.foo = 123
|
||||
obj.bar = 'bar'
|
||||
self.assertEqual(123, obj['foo'])
|
||||
@ -303,13 +308,13 @@ class _TestObject(object):
|
||||
sorted(list(obj.iteritems()), key=lambda x: x[0]))
|
||||
|
||||
def test_load(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
self.assertEqual('loaded!', obj.bar)
|
||||
|
||||
def test_load_in_base(self):
|
||||
class Foo(base.IronicObject):
|
||||
fields = {'foobar': int}
|
||||
obj = Foo()
|
||||
obj = Foo(self.context)
|
||||
# NOTE(danms): Can't use assertRaisesRegexp() because of py26
|
||||
raised = False
|
||||
try:
|
||||
@ -320,7 +325,7 @@ class _TestObject(object):
|
||||
self.assertTrue('foobar' in str(ex))
|
||||
|
||||
def test_loaded_in_primitive(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
obj.foo = 1
|
||||
obj.obj_reset_changes()
|
||||
self.assertEqual('loaded!', obj.bar)
|
||||
@ -333,7 +338,7 @@ class _TestObject(object):
|
||||
self.assertEqual(expected, obj.obj_to_primitive())
|
||||
|
||||
def test_changes_in_primitive(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
obj.foo = 123
|
||||
self.assertEqual(set(['foo']), obj.obj_what_changed())
|
||||
primitive = obj.obj_to_primitive()
|
||||
@ -416,7 +421,7 @@ class _TestObject(object):
|
||||
|
||||
def test_base_attributes(self):
|
||||
dt = datetime.datetime(1955, 11, 5)
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
obj.created_at = dt
|
||||
obj.updated_at = dt
|
||||
expected = {'ironic_object.name': 'MyObj',
|
||||
@ -437,20 +442,20 @@ class _TestObject(object):
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_contains(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
self.assertFalse('foo' in obj)
|
||||
obj.foo = 1
|
||||
self.assertTrue('foo' in obj)
|
||||
self.assertFalse('does_not_exist' in obj)
|
||||
|
||||
def test_obj_attr_is_set(self):
|
||||
obj = MyObj(foo=1)
|
||||
obj = MyObj(self.context, foo=1)
|
||||
self.assertTrue(obj.obj_attr_is_set('foo'))
|
||||
self.assertFalse(obj.obj_attr_is_set('bar'))
|
||||
self.assertRaises(AttributeError, obj.obj_attr_is_set, 'bang')
|
||||
|
||||
def test_get(self):
|
||||
obj = MyObj(foo=1)
|
||||
obj = MyObj(self.context, foo=1)
|
||||
# Foo has value, should not get the default
|
||||
self.assertEqual(obj.get('foo', 2), 1)
|
||||
# Foo has value, should return the value without error
|
||||
@ -479,7 +484,7 @@ class _TestObject(object):
|
||||
set(TestSubclassedObject.fields.keys()))
|
||||
|
||||
def test_get_changes(self):
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
self.assertEqual({}, obj.obj_get_changes())
|
||||
obj.foo = 123
|
||||
self.assertEqual({'foo': 123}, obj.obj_get_changes())
|
||||
@ -497,12 +502,12 @@ class _TestObject(object):
|
||||
def bar(self):
|
||||
return 'this is bar'
|
||||
|
||||
obj = TestObj()
|
||||
obj = TestObj(self.context)
|
||||
self.assertEqual(set(['created_at', 'updated_at', 'foo', 'bar']),
|
||||
set(obj.obj_fields))
|
||||
|
||||
def test_obj_constructor(self):
|
||||
obj = MyObj(context=self.context, foo=123, bar='abc')
|
||||
obj = MyObj(self.context, foo=123, bar='abc')
|
||||
self.assertEqual(123, obj.foo)
|
||||
self.assertEqual('abc', obj.bar)
|
||||
self.assertEqual(set(['foo', 'bar']), obj.obj_what_changed())
|
||||
@ -513,11 +518,16 @@ class TestObject(_LocalTest, _TestObject):
|
||||
|
||||
|
||||
class TestObjectListBase(test_base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestObjectListBase, self).setUp()
|
||||
self.context = context.get_admin_context()
|
||||
|
||||
def test_list_like_operations(self):
|
||||
class Foo(base.ObjectListBase, base.IronicObject):
|
||||
pass
|
||||
|
||||
objlist = Foo()
|
||||
objlist = Foo(self.context)
|
||||
objlist._context = 'foo'
|
||||
objlist.objects = [1, 2, 3]
|
||||
self.assertEqual(list(objlist), objlist.objects)
|
||||
@ -536,10 +546,10 @@ class TestObjectListBase(test_base.TestCase):
|
||||
class Bar(base.IronicObject):
|
||||
fields = {'foo': str}
|
||||
|
||||
obj = Foo()
|
||||
obj = Foo(self.context)
|
||||
obj.objects = []
|
||||
for i in 'abc':
|
||||
bar = Bar()
|
||||
bar = Bar(self.context)
|
||||
bar.foo = i
|
||||
obj.objects.append(bar)
|
||||
|
||||
@ -576,9 +586,9 @@ class TestObjectListBase(test_base.TestCase):
|
||||
class Bar(base.IronicObject):
|
||||
fields = {'foo': str}
|
||||
|
||||
obj = Foo(objects=[])
|
||||
obj = Foo(self.context, objects=[])
|
||||
self.assertEqual(set(['objects']), obj.obj_what_changed())
|
||||
obj.objects.append(Bar(foo='test'))
|
||||
obj.objects.append(Bar(self.context, foo='test'))
|
||||
self.assertEqual(set(['objects']), obj.obj_what_changed())
|
||||
obj.obj_reset_changes()
|
||||
# This should still look dirty because the child is dirty
|
||||
@ -589,6 +599,11 @@ class TestObjectListBase(test_base.TestCase):
|
||||
|
||||
|
||||
class TestObjectSerializer(test_base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestObjectSerializer, self).setUp()
|
||||
self.context = context.get_admin_context()
|
||||
|
||||
def test_serialize_entity_primitive(self):
|
||||
ser = base.IronicObjectSerializer()
|
||||
for thing in (1, 'foo', [1, 2], {'foo': 'bar'}):
|
||||
@ -601,25 +616,23 @@ class TestObjectSerializer(test_base.TestCase):
|
||||
|
||||
def test_object_serialization(self):
|
||||
ser = base.IronicObjectSerializer()
|
||||
ctxt = context.get_admin_context()
|
||||
obj = MyObj()
|
||||
primitive = ser.serialize_entity(ctxt, obj)
|
||||
obj = MyObj(self.context)
|
||||
primitive = ser.serialize_entity(self.context, obj)
|
||||
self.assertTrue('ironic_object.name' in primitive)
|
||||
obj2 = ser.deserialize_entity(ctxt, primitive)
|
||||
obj2 = ser.deserialize_entity(self.context, primitive)
|
||||
self.assertIsInstance(obj2, MyObj)
|
||||
self.assertEqual(ctxt, obj2._context)
|
||||
self.assertEqual(self.context, obj2._context)
|
||||
|
||||
def test_object_serialization_iterables(self):
|
||||
ser = base.IronicObjectSerializer()
|
||||
ctxt = context.get_admin_context()
|
||||
obj = MyObj()
|
||||
obj = MyObj(self.context)
|
||||
for iterable in (list, tuple, set):
|
||||
thing = iterable([obj])
|
||||
primitive = ser.serialize_entity(ctxt, thing)
|
||||
primitive = ser.serialize_entity(self.context, thing)
|
||||
self.assertEqual(1, len(primitive))
|
||||
for item in primitive:
|
||||
self.assertFalse(isinstance(item, base.IronicObject))
|
||||
thing2 = ser.deserialize_entity(ctxt, primitive)
|
||||
thing2 = ser.deserialize_entity(self.context, primitive)
|
||||
self.assertEqual(1, len(thing2))
|
||||
for item in thing2:
|
||||
self.assertIsInstance(item, MyObj)
|
||||
|
@ -25,7 +25,7 @@ def get_test_node(ctxt, **kw):
|
||||
that a create() could be used to commit it to the DB.
|
||||
"""
|
||||
db_node = db_utils.get_test_node(**kw)
|
||||
node = objects.Node(context=ctxt)
|
||||
node = objects.Node(ctxt)
|
||||
for key in db_node:
|
||||
setattr(node, key, db_node[key])
|
||||
return node
|
||||
@ -47,7 +47,7 @@ def get_test_port(ctxt, **kw):
|
||||
that a create() could be used to commit it to the DB.
|
||||
"""
|
||||
db_port = db_utils.get_test_port(**kw)
|
||||
port = objects.Port(context=ctxt)
|
||||
port = objects.Port(ctxt)
|
||||
for key in db_port:
|
||||
setattr(port, key, db_port[key])
|
||||
return port
|
||||
@ -69,7 +69,7 @@ def get_test_chassis(ctxt, **kw):
|
||||
that a create() could be used to commit it to the DB.
|
||||
"""
|
||||
db_chassis = db_utils.get_test_chassis(**kw)
|
||||
chassis = objects.Chassis(context=ctxt)
|
||||
chassis = objects.Chassis(ctxt)
|
||||
for key in db_chassis:
|
||||
setattr(chassis, key, db_chassis[key])
|
||||
return chassis
|
||||
|
Loading…
Reference in New Issue
Block a user