]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Remove cinder.context dependency from cinder.rpc
authorRussell Bryant <rbryant@redhat.com>
Fri, 25 May 2012 20:24:03 +0000 (16:24 -0400)
committerMark McLoughlin <markmc@redhat.com>
Thu, 19 Jul 2012 16:20:18 +0000 (17:20 +0100)
Part of blueprint common-rpc.

This patch removes the usage of cinder.context from cinder.rpc.  Everything
needed to implement RpcContext now exists within cinder.rpc.

Change-Id: I9e6ec0d22e55d3d4f38e12e0fdd2df745da496f5

cinder/rpc/amqp.py
cinder/rpc/common.py
cinder/rpc/impl_fake.py

index c3488037d51e5ba146b92f995c884841f121e781..0e6e5de6d09b0a2a3ade3710c86d4c96f89e99ce 100644 (file)
@@ -33,7 +33,6 @@ from eventlet import greenpool
 from eventlet import pools
 from eventlet import semaphore
 
-from cinder import context
 from cinder import log as logging
 from cinder.openstack.common import local
 import cinder.rpc.common as rpc_common
@@ -164,12 +163,12 @@ def msg_reply(conf, msg_id, connection_pool, reply=None, failure=None,
         conn.direct_send(msg_id, msg)
 
 
-class RpcContext(context.RequestContext):
+class RpcContext(rpc_common.CommonRpcContext):
     """Context that supports replying to a rpc.call"""
-    def __init__(self, *args, **kwargs):
+    def __init__(self, **kwargs):
         self.msg_id = kwargs.pop('msg_id', None)
         self.conf = kwargs.pop('conf')
-        super(RpcContext, self).__init__(*args, **kwargs)
+        super(RpcContext, self).__init__(**kwargs)
 
     def reply(self, reply=None, failure=None, ending=False,
               connection_pool=None):
index e5403cb1e8091f02550077fe90ab9e6a141b540b..29db0c4a621c9b2239d81143fa6f476fc96a09da 100644 (file)
@@ -25,6 +25,7 @@ from cinder import log as logging
 from cinder.openstack.common import cfg
 from cinder.openstack.common import importutils
 from cinder.openstack.common import jsonutils
+from cinder.openstack.common import local
 
 
 LOG = logging.getLogger(__name__)
@@ -248,3 +249,47 @@ def deserialize_remote_exception(conf, data):
         # first exception argument.
         failure.args = (message,) + failure.args[1:]
     return failure
+
+
+class CommonRpcContext(object):
+    def __init__(self, **kwargs):
+        self.values = kwargs
+
+    def __getattr__(self, key):
+        try:
+            return self.values[key]
+        except KeyError:
+            raise AttributeError(key)
+
+    def to_dict(self):
+        return copy.deepcopy(self.values)
+
+    @classmethod
+    def from_dict(cls, values):
+        return cls(**values)
+
+    def update_store(self):
+        local.store.context = self
+
+    def elevated(self, read_deleted=None, overwrite=False):
+        """Return a version of this context with admin flag set."""
+        # TODO(russellb) This method is a bit of a cinder-ism.  It makes
+        # some assumptions about the data in the request context sent
+        # across rpc, while the rest of this class does not.  We could get
+        # rid of this if we changed the cinder code that uses this to
+        # convert the RpcContext back to its native RequestContext doing
+        # something like
+        # cinder.context.RequestContext.from_dict(ctxt.to_dict())
+
+        context = copy.deepcopy(self)
+        context.values['is_admin'] = True
+
+        context.values.setdefault('roles', [])
+
+        if 'admin' not in context.values['roles']:
+            context.values['roles'].append('admin')
+
+        if read_deleted is not None:
+            context.values['read_deleted'] = read_deleted
+
+        return context
index d373f9c40fa32cb7e321a318f21894a7e6c0925f..a1ca192b09d168df8818f291f66c8d46c3c303ad 100644 (file)
@@ -26,15 +26,14 @@ import traceback
 
 import eventlet
 
-from cinder import context
 from cinder.rpc import common as rpc_common
 
 CONSUMERS = {}
 
 
-class RpcContext(context.RequestContext):
-    def __init__(self, *args, **kwargs):
-        super(RpcContext, self).__init__(*args, **kwargs)
+class RpcContext(rpc_common.CommonRpcContext):
+    def __init__(self, **kwargs):
+        super(RpcContext, self).__init__(**kwargs)
         self._response = []
         self._done = False