Remove the ambiguous context argument on remotable methods

Early on, we decided to make context always a parameter to remotable
methods. If the caller passed a context, we'd use that, and if not,
we'd use the one stored on the object if possible. That has been
not that useful, and also a source of infinite confusion for people
learning the code. One of the work items here is to remove that
ambiguity and require that the object have an embedded context in
order to make any remotable calls.

Change-Id: I6ee4404f3b16c5cb8db93db13eb75e1aa1a3c146
This commit is contained in:
Dan Smith
2015-02-11 12:52:25 -08:00
parent 4b76fbde46
commit 5e79ffca1d
2 changed files with 8 additions and 25 deletions

View File

@@ -23,7 +23,6 @@ import logging
import traceback
import netaddr
from oslo_context import context
import oslo_messaging as messaging
from oslo_utils import timeutils
import six
@@ -186,17 +185,9 @@ def remotable(fn):
@functools.wraps(fn)
def wrapper(self, *args, **kwargs):
ctxt = self._context
try:
if isinstance(args[0], (context.RequestContext)):
ctxt = args[0]
args = args[1:]
except IndexError:
pass
if ctxt is None:
raise exception.OrphanedObjectError(method=fn.__name__,
objtype=self.obj_name())
# Force this to be set if it wasn't before.
self._context = ctxt
if NovaObject.indirection_api:
updates, result = NovaObject.indirection_api.object_action(
ctxt, self, fn.__name__, args, kwargs)
@@ -214,7 +205,7 @@ def remotable(fn):
self._changed_fields = set(updates.get('obj_what_changed', []))
return result
else:
return fn(self, ctxt, *args, **kwargs)
return fn(self, *args, **kwargs)
wrapper.remotable = True
wrapper.original_fn = fn

View File

@@ -86,11 +86,11 @@ class MyObj(base.NovaPersistentObject, base.NovaObject,
return obj
@base.remotable
def marco(self, context):
def marco(self):
return 'polo'
@base.remotable
def _update_test(self, context):
def _update_test(self):
project_id = getattr(context, 'tenant', None)
if project_id is None:
project_id = getattr(context, 'project_id', None)
@@ -100,17 +100,17 @@ class MyObj(base.NovaPersistentObject, base.NovaObject,
self.bar = 'updated'
@base.remotable
def save(self, context):
def save(self):
self.obj_reset_changes()
@base.remotable
def refresh(self, context):
def refresh(self):
self.foo = 321
self.bar = 'refreshed'
self.obj_reset_changes()
@base.remotable
def modify_save_modify(self, context):
def modify_save_modify(self):
self.bar = 'meow'
self.save()
self.foo = 42
@@ -589,14 +589,6 @@ class _TestObject(object):
self.assertIsNotNone(error)
self.assertEqual('1.6', error.kwargs['supported'])
def test_with_alternate_context(self):
ctxt1 = context.RequestContext(None, 'foo', 'foo')
ctxt2 = context.RequestContext(None, 'bar', 'alternate')
obj = MyObj.query(ctxt1)
obj._update_test(ctxt2)
self.assertEqual(obj.bar, 'alternate-context')
self.assertRemotes()
def test_orphaned_object(self):
obj = MyObj.query(self.context)
obj._context = None
@@ -608,7 +600,7 @@ class _TestObject(object):
obj = MyObj.query(self.context)
obj.foo = 123
self.assertEqual(obj.obj_what_changed(), set(['foo']))
obj._update_test(self.context)
obj._update_test()
self.assertEqual(obj.obj_what_changed(), set(['foo', 'bar']))
self.assertEqual(obj.foo, 123)
self.assertRemotes()
@@ -636,7 +628,7 @@ class _TestObject(object):
obj = MyObj.query(self.context)
obj.bar = 'something'
self.assertEqual(obj.obj_what_changed(), set(['bar']))
obj.modify_save_modify(self.context)
obj.modify_save_modify()
self.assertEqual(obj.obj_what_changed(), set(['foo', 'rel_object']))
self.assertEqual(obj.foo, 42)
self.assertEqual(obj.bar, 'meow')