Merge "Move to using ovo's remotable decorators"
This commit is contained in:
commit
a1e6abc125
|
@ -27,7 +27,6 @@ from oslo_versionedobjects import base as ovoo_base
|
|||
from oslo_versionedobjects import exception as ovoo_exc
|
||||
import six
|
||||
|
||||
from nova import context
|
||||
from nova import exception
|
||||
from nova import objects
|
||||
from nova.objects import fields as obj_fields
|
||||
|
@ -51,71 +50,8 @@ class NovaObjectRegistry(ovoo_base.VersionedObjectRegistry):
|
|||
setattr(objects, cls.obj_name(), newest)
|
||||
|
||||
|
||||
# These are decorators that mark an object's method as remotable.
|
||||
# If the metaclass is configured to forward object methods to an
|
||||
# indirection service, these will result in making an RPC call
|
||||
# instead of directly calling the implementation in the object. Instead,
|
||||
# the object implementation on the remote end will perform the
|
||||
# requested action and the result will be returned here.
|
||||
def remotable_classmethod(fn):
|
||||
"""Decorator for remotable classmethods."""
|
||||
@functools.wraps(fn)
|
||||
def wrapper(cls, context, *args, **kwargs):
|
||||
if NovaObject.indirection_api:
|
||||
result = NovaObject.indirection_api.object_class_action(
|
||||
context, cls.obj_name(), fn.__name__, cls.VERSION,
|
||||
args, kwargs)
|
||||
else:
|
||||
result = fn(cls, context, *args, **kwargs)
|
||||
if isinstance(result, NovaObject):
|
||||
result._context = context
|
||||
return result
|
||||
|
||||
# NOTE(danms): Make this discoverable
|
||||
wrapper.remotable = True
|
||||
wrapper.original_fn = fn
|
||||
return classmethod(wrapper)
|
||||
|
||||
|
||||
# See comment above for remotable_classmethod()
|
||||
#
|
||||
# Note that this will use either the provided context, or the one
|
||||
# stashed in the object. If neither are present, the object is
|
||||
# "orphaned" and remotable methods cannot be called.
|
||||
def remotable(fn):
|
||||
"""Decorator for remotable object methods."""
|
||||
@functools.wraps(fn)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
if args and isinstance(args[0], context.RequestContext):
|
||||
raise exception.ObjectActionError(
|
||||
action=fn.__name__,
|
||||
reason='Calling remotables with context is deprecated')
|
||||
if self._context is None:
|
||||
raise exception.OrphanedObjectError(method=fn.__name__,
|
||||
objtype=self.obj_name())
|
||||
if NovaObject.indirection_api:
|
||||
updates, result = NovaObject.indirection_api.object_action(
|
||||
self._context, self, fn.__name__, args, kwargs)
|
||||
for key, value in six.iteritems(updates):
|
||||
if key in self.fields:
|
||||
field = self.fields[key]
|
||||
# NOTE(ndipanov): Since NovaObjectSerializer will have
|
||||
# deserialized any object fields into objects already,
|
||||
# we do not try to deserialize them again here.
|
||||
if isinstance(value, NovaObject):
|
||||
setattr(self, key, value)
|
||||
else:
|
||||
setattr(self, key,
|
||||
field.from_primitive(self, key, value))
|
||||
self.obj_reset_changes()
|
||||
self._changed_fields = set(updates.get('obj_what_changed', []))
|
||||
return result
|
||||
else:
|
||||
return fn(self, *args, **kwargs)
|
||||
|
||||
wrapper.remotable = True
|
||||
wrapper.original_fn = fn
|
||||
return wrapper
|
||||
remotable_classmethod = ovoo_base.remotable_classmethod
|
||||
remotable = ovoo_base.remotable
|
||||
|
||||
|
||||
class NovaObject(ovoo_base.VersionedObject):
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo_versionedobjects import base as ovo
|
||||
|
||||
from nova.db.sqlalchemy import api as db_api
|
||||
from nova.db.sqlalchemy import api_models
|
||||
from nova import exception
|
||||
|
@ -17,8 +19,10 @@ from nova.objects import base
|
|||
from nova.objects import fields
|
||||
|
||||
|
||||
# NOTE(danms): Maintain Dict compatibility because of ovo bug 1474952
|
||||
@base.NovaObjectRegistry.register
|
||||
class CellMapping(base.NovaTimestampObject, base.NovaObject):
|
||||
class CellMapping(base.NovaTimestampObject, base.NovaObject,
|
||||
ovo.VersionedObjectDictCompat):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo_versionedobjects import base as ovo
|
||||
|
||||
from nova.db.sqlalchemy import api as db_api
|
||||
from nova.db.sqlalchemy import api_models
|
||||
from nova import exception
|
||||
|
@ -24,8 +26,10 @@ def _cell_id_in_updates(updates):
|
|||
updates["cell_id"] = cell_mapping_obj.id
|
||||
|
||||
|
||||
# NOTE(danms): Maintain Dict compatibility because of ovo bug 1474952
|
||||
@base.NovaObjectRegistry.register
|
||||
class HostMapping(base.NovaTimestampObject, base.NovaObject):
|
||||
class HostMapping(base.NovaTimestampObject, base.NovaObject,
|
||||
ovo.VersionedObjectDictCompat):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo_versionedobjects import base as ovo
|
||||
|
||||
from nova.db.sqlalchemy import api as db_api
|
||||
from nova.db.sqlalchemy import api_models
|
||||
from nova import exception
|
||||
|
@ -18,8 +20,10 @@ from nova.objects import base
|
|||
from nova.objects import fields
|
||||
|
||||
|
||||
# NOTE(danms): Maintain Dict compatibility because of ovo bug 1474952
|
||||
@base.NovaObjectRegistry.register
|
||||
class InstanceMapping(base.NovaTimestampObject, base.NovaObject):
|
||||
class InstanceMapping(base.NovaTimestampObject, base.NovaObject,
|
||||
ovo.VersionedObjectDictCompat):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
|
|
|
@ -65,8 +65,7 @@ class _TestAggregateObject(object):
|
|||
agg.name = 'foo'
|
||||
agg.metadata = {'one': 'two'}
|
||||
agg.create()
|
||||
self.assertRaises(exception.ObjectActionError, agg.create,
|
||||
self.context)
|
||||
self.assertRaises(exception.ObjectActionError, agg.create)
|
||||
|
||||
def test_save(self):
|
||||
self.mox.StubOutWithMock(db, 'aggregate_update')
|
||||
|
@ -80,13 +79,13 @@ class _TestAggregateObject(object):
|
|||
self.compare_obj(agg, fake_aggregate, subs=SUBS)
|
||||
|
||||
def test_save_and_create_no_hosts(self):
|
||||
agg = aggregate.Aggregate()
|
||||
agg = aggregate.Aggregate(context=self.context)
|
||||
agg.id = 123
|
||||
agg.hosts = ['foo', 'bar']
|
||||
self.assertRaises(exception.ObjectActionError,
|
||||
agg.create, self.context)
|
||||
agg.create)
|
||||
self.assertRaises(exception.ObjectActionError,
|
||||
agg.save, self.context)
|
||||
agg.save)
|
||||
|
||||
def test_update_metadata(self):
|
||||
self.mox.StubOutWithMock(db, 'aggregate_metadata_delete')
|
||||
|
|
|
@ -84,10 +84,10 @@ class _TestBlockDeviceMappingObject(object):
|
|||
self._test_save(cell_type='compute')
|
||||
|
||||
def test_save_instance_changed(self):
|
||||
bdm_object = objects.BlockDeviceMapping()
|
||||
bdm_object = objects.BlockDeviceMapping(context=self.context)
|
||||
bdm_object.instance = objects.Instance()
|
||||
self.assertRaises(exception.ObjectActionError,
|
||||
bdm_object.save, self.context)
|
||||
bdm_object.save)
|
||||
|
||||
@mock.patch.object(db, 'block_device_mapping_update', return_value=None)
|
||||
def test_save_not_found(self, bdm_update):
|
||||
|
@ -226,16 +226,16 @@ class _TestBlockDeviceMappingObject(object):
|
|||
bdm.create()
|
||||
|
||||
self.assertRaises(exception.ObjectActionError,
|
||||
bdm.create, self.context)
|
||||
bdm.create)
|
||||
|
||||
def test_create_fails_instance(self):
|
||||
values = {'source_type': 'volume', 'volume_id': 'fake-vol-id',
|
||||
'destination_type': 'volume',
|
||||
'instance_uuid': 'fake-instance',
|
||||
'instance': objects.Instance()}
|
||||
bdm = objects.BlockDeviceMapping(**values)
|
||||
bdm = objects.BlockDeviceMapping(context=self.context, **values)
|
||||
self.assertRaises(exception.ObjectActionError,
|
||||
bdm.create, self.context)
|
||||
bdm.create)
|
||||
|
||||
def _test_destroy_mocked(self, cell_type=None):
|
||||
values = {'source_type': 'volume', 'volume_id': 'fake-vol-id',
|
||||
|
|
|
@ -316,8 +316,7 @@ class _TestComputeNodeObject(object):
|
|||
compute = compute_node.ComputeNode(context=self.context)
|
||||
compute.service_id = 456
|
||||
compute.create()
|
||||
self.assertRaises(exception.ObjectActionError, compute.create,
|
||||
self.context)
|
||||
self.assertRaises(exception.ObjectActionError, compute.create)
|
||||
|
||||
def test_save(self):
|
||||
self.mox.StubOutWithMock(db, 'compute_node_update')
|
||||
|
|
|
@ -129,9 +129,8 @@ class _TestFlavor(object):
|
|||
self.assertEqual(['project-1', 'project-2'], flavor.projects)
|
||||
|
||||
def test_create_with_id(self):
|
||||
flavor = flavor_obj.Flavor(id=123)
|
||||
self.assertRaises(exception.ObjectActionError, flavor.create,
|
||||
self.context)
|
||||
flavor = flavor_obj.Flavor(context=self.context, id=123)
|
||||
self.assertRaises(exception.ObjectActionError, flavor.create)
|
||||
|
||||
@mock.patch('nova.db.flavor_access_add')
|
||||
@mock.patch('nova.db.flavor_access_remove')
|
||||
|
|
|
@ -894,8 +894,7 @@ class _TestInstanceObject(object):
|
|||
project_id=self.context.project_id,
|
||||
host='foo-host')
|
||||
inst.create()
|
||||
self.assertRaises(exception.ObjectActionError, inst.create,
|
||||
self.context)
|
||||
self.assertRaises(exception.ObjectActionError, inst.create)
|
||||
|
||||
def test_create_with_special_things(self):
|
||||
self.mox.StubOutWithMock(db, 'instance_create')
|
||||
|
|
|
@ -110,10 +110,10 @@ class _TestInstanceFault(object):
|
|||
self._test_create(True)
|
||||
|
||||
def test_create_already_created(self):
|
||||
fault = instance_fault.InstanceFault()
|
||||
fault = instance_fault.InstanceFault(context=self.context)
|
||||
fault.id = 1
|
||||
self.assertRaises(exception.ObjectActionError,
|
||||
fault.create, self.context)
|
||||
fault.create)
|
||||
|
||||
|
||||
class TestInstanceFault(test_objects._LocalTest,
|
||||
|
|
|
@ -159,8 +159,7 @@ class _TestInstanceGroupObjects(object):
|
|||
def test_recreate_fails(self):
|
||||
group = instance_group.InstanceGroup(context=self.context)
|
||||
group.create()
|
||||
self.assertRaises(exception.ObjectActionError, group.create,
|
||||
self.context)
|
||||
self.assertRaises(exception.ObjectActionError, group.create)
|
||||
|
||||
def test_destroy(self):
|
||||
values = self._get_default_values()
|
||||
|
|
|
@ -66,8 +66,7 @@ class _TestKeyPairObject(object):
|
|||
keypair_obj.name = 'foo-keypair'
|
||||
keypair_obj.public_key = 'keydata'
|
||||
keypair_obj.create()
|
||||
self.assertRaises(exception.ObjectActionError, keypair_obj.create,
|
||||
self.context)
|
||||
self.assertRaises(exception.ObjectActionError, keypair_obj.create)
|
||||
|
||||
def test_destroy(self):
|
||||
self.mox.StubOutWithMock(db, 'key_pair_destroy')
|
||||
|
|
|
@ -97,8 +97,7 @@ class _TestMigrationObject(object):
|
|||
mig = migration.Migration(context=ctxt)
|
||||
mig.source_compute = 'foo'
|
||||
mig.create()
|
||||
self.assertRaises(exception.ObjectActionError, mig.create,
|
||||
self.context)
|
||||
self.assertRaises(exception.ObjectActionError, mig.create)
|
||||
|
||||
def test_save(self):
|
||||
ctxt = context.get_admin_context()
|
||||
|
|
|
@ -122,7 +122,7 @@ class MyObjDiffVers(MyObj):
|
|||
return 'MyObj'
|
||||
|
||||
|
||||
class MyObj2(object):
|
||||
class MyObj2(base.NovaObject):
|
||||
fields = {
|
||||
'bar': fields.StringField(),
|
||||
}
|
||||
|
@ -507,7 +507,7 @@ class _TestObject(object):
|
|||
def test_orphaned_object(self):
|
||||
obj = MyObj.query(self.context)
|
||||
obj._context = None
|
||||
self.assertRaises(exception.OrphanedObjectError,
|
||||
self.assertRaises(ovo_exc.OrphanedObjectError,
|
||||
obj._update_test)
|
||||
|
||||
def test_changed_1(self):
|
||||
|
|
Loading…
Reference in New Issue