]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Migrate to oslo.context
authorIhar Hrachyshka <ihrachys@redhat.com>
Thu, 11 Dec 2014 19:36:48 +0000 (20:36 +0100)
committerIhar Hrachyshka <ihrachys@redhat.com>
Thu, 11 Dec 2014 19:49:11 +0000 (20:49 +0100)
That's just a matter of adding requirements.txt entry, fixing imports,
and dropping the corresponding oslo-incubator module from the tree.

While at it, made all imports to import the module into 'oslo_context' and
not just 'context', so that we don't override the module locally in
multiple methods that receive their context arguments with the same
name, making the library inaccessible from inside those methods.

Change-Id: Ie62af970b3b7f225de453e56c01abc4b12af8f5e
Closes-Bug: #1401054

neutron/context.py
neutron/openstack/common/context.py [deleted file]
neutron/plugins/brocade/NeutronPlugin.py
neutron/tests/unit/brocade/test_brocade_vlan.py
neutron/tests/unit/hyperv/test_hyperv_rpcapi.py
neutron/tests/unit/ml2/drivers/brocade/test_brocade_l3_plugin.py
neutron/tests/unit/ml2/test_rpcapi.py
neutron/tests/unit/mlnx/test_rpcapi.py
neutron/tests/unit/test_agent_rpc.py
openstack-common.conf
requirements.txt

index 7d294427ab7fa92e8c72f924f9b6fd9ad1654b7d..038ebfa4005ed13b540e17c4d57b388a7fb20cd8 100644 (file)
 """Context: context for security/db session."""
 
 import copy
-
 import datetime
 
+from oslo_context import context as oslo_context
+
 from neutron.db import api as db_api
-from neutron.openstack.common import context as common_context
 from neutron.openstack.common import local
 from neutron.openstack.common import log as logging
 from neutron import policy
@@ -29,7 +29,7 @@ from neutron import policy
 LOG = logging.getLogger(__name__)
 
 
-class ContextBase(common_context.RequestContext):
+class ContextBase(oslo_context.RequestContext):
     """Security context and request information.
 
     Represents the user taking a given action within the system.
diff --git a/neutron/openstack/common/context.py b/neutron/openstack/common/context.py
deleted file mode 100644 (file)
index fa62fc1..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-
-# Copyright 2011 OpenStack Foundation.
-# All Rights Reserved.
-#
-#    Licensed under the Apache License, Version 2.0 (the "License"); you may
-#    not use this file except in compliance with the License. You may obtain
-#    a copy of the License at
-#
-#         http://www.apache.org/licenses/LICENSE-2.0
-#
-#    Unless required by applicable law or agreed to in writing, software
-#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-#    License for the specific language governing permissions and limitations
-#    under the License.
-
-"""
-Simple class that stores security context information in the web request.
-
-Projects should subclass this class if they wish to enhance the request
-context or provide additional information in their specific WSGI pipeline.
-"""
-
-import itertools
-
-from neutron.openstack.common import uuidutils
-
-
-def generate_request_id():
-    return 'req-%s' % uuidutils.generate_uuid()
-
-
-class RequestContext(object):
-
-    """Helper class to represent useful information about a request context.
-
-    Stores information about the security context under which the user
-    accesses the system, as well as additional request information.
-    """
-
-    def __init__(self, auth_token=None, user=None, tenant=None, is_admin=False,
-                 read_only=False, show_deleted=False, request_id=None):
-        self.auth_token = auth_token
-        self.user = user
-        self.tenant = tenant
-        self.is_admin = is_admin
-        self.read_only = read_only
-        self.show_deleted = show_deleted
-        if not request_id:
-            request_id = generate_request_id()
-        self.request_id = request_id
-
-    def to_dict(self):
-        return {'user': self.user,
-                'tenant': self.tenant,
-                'is_admin': self.is_admin,
-                'read_only': self.read_only,
-                'show_deleted': self.show_deleted,
-                'auth_token': self.auth_token,
-                'request_id': self.request_id}
-
-
-def get_admin_context(show_deleted="no"):
-    context = RequestContext(None,
-                             tenant=None,
-                             is_admin=True,
-                             show_deleted=show_deleted)
-    return context
-
-
-def get_context_from_function_and_args(function, args, kwargs):
-    """Find an arg of type RequestContext and return it.
-
-       This is useful in a couple of decorators where we don't
-       know much about the function we're wrapping.
-    """
-
-    for arg in itertools.chain(kwargs.values(), args):
-        if isinstance(arg, RequestContext):
-            return arg
-
-    return None
index 9a81fdf5935b24ef315737d8e9e4efdad2425fde..de19083f247d76a0112130a0b2a51cc7585f8a0e 100644 (file)
@@ -22,6 +22,7 @@
 from oslo.config import cfg
 from oslo import messaging
 from oslo.utils import importutils
+from oslo_context import context as oslo_context
 
 from neutron.agent import securitygroups_rpc as sg_rpc
 from neutron.api.rpc.agentnotifiers import dhcp_rpc_agent_api
@@ -45,7 +46,6 @@ from neutron.db import portbindings_base
 from neutron.db import securitygroups_rpc_base as sg_db_rpc
 from neutron.extensions import portbindings
 from neutron.extensions import securitygroup as ext_sg
-from neutron.openstack.common import context
 from neutron.i18n import _LE, _LI
 from neutron.openstack.common import log as logging
 from neutron.plugins.brocade.db import models as brocade_db
@@ -228,7 +228,7 @@ class BrocadePluginV2(db_base_plugin_v2.NeutronDbPluginV2,
                                    physical_interface)
         self.base_binding_dict = self._get_base_binding_dict()
         portbindings_base.register_port_dict_function()
-        self.ctxt = context.get_admin_context()
+        self.ctxt = oslo_context.get_admin_context()
         self.ctxt.session = db.get_session()
         self._vlan_bitmap = vbm.VlanBitmap(self.ctxt)
         self._setup_rpc()
@@ -253,7 +253,7 @@ class BrocadePluginV2(db_base_plugin_v2.NeutronDbPluginV2,
         # RPC support
         self.service_topics = {svc_constants.CORE: topics.PLUGIN,
                                svc_constants.L3_ROUTER_NAT: topics.L3PLUGIN}
-        self.rpc_context = context.RequestContext('neutron', 'neutron',
+        self.rpc_context = oslo_context.RequestContext('neutron', 'neutron',
                                                   is_admin=False)
         self.conn = n_rpc.create_connection(new=True)
         self.endpoints = [BridgeRpcCallbacks(),
index 09d030767a6f2730bd5464577febfca20c075f18..024e26f8ffe31c8b3e315aa5514f3a288074fcb6 100644 (file)
@@ -18,8 +18,9 @@
 Test vlans alloc/dealloc.
 """
 
+from oslo_context import context as oslo_context
+
 from neutron.db import api as db
-from neutron.openstack.common import context
 from neutron.plugins.brocade import vlanbm as vlan_bitmap
 from neutron.tests.unit import testlib_api
 
@@ -29,7 +30,7 @@ class TestVlanBitmap(testlib_api.SqlTestCase):
 
     def setUp(self):
         super(TestVlanBitmap, self).setUp()
-        self.context = context.get_admin_context()
+        self.context = oslo_context.get_admin_context()
         self.context.session = db.get_session()
 
     def test_vlan(self):
index f3d8d2bc052482f050f02b1a89121b2c36b80558..7f2c8672ff3393e6693c45655a823a95ed94b016 100644 (file)
@@ -19,11 +19,12 @@ Unit Tests for hyperv neutron rpc
 """
 
 import contextlib
+
 import mock
+from oslo_context import context as oslo_context
 
 from neutron.agent import rpc as agent_rpc
 from neutron.common import topics
-from neutron.openstack.common import context
 from neutron.plugins.hyperv import agent_notifier_api as ana
 from neutron.plugins.hyperv.common import constants
 from neutron.tests import base
@@ -33,7 +34,7 @@ class rpcHyperVApiTestCase(base.BaseTestCase):
 
     def _test_hyperv_neutron_api(
             self, rpcapi, topic, method, rpc_method, **kwargs):
-        ctxt = context.RequestContext('fake_user', 'fake_project')
+        ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
         expected_retval = 'foo' if rpc_method == 'call' else None
         expected_version = kwargs.pop('version', None)
         fanout = kwargs.pop('fanout', False)
index 2dd1abfef725a836b4e4115922652670c0e1881e..8ef5987e4bac6fb7e4e07ec13735ebd0e031b698 100644 (file)
@@ -18,9 +18,9 @@
 import mock
 from oslo.config import cfg
 from oslo.utils import importutils
+from oslo_context import context as oslo_context
 
 from neutron.db import api as db
-from neutron.openstack.common import context
 from neutron.openstack.common import log as logging
 from neutron.tests.unit import test_l3_plugin
 
@@ -44,7 +44,7 @@ class BrocadeSVIPlugin_TestCases(test_l3_plugin.TestL3NatBasePlugin):
             LOG.info(_("rbridge id %s"), self._switch['rbridge_id'])
             self._driver = mock.MagicMock()
 
-        self.context = context.get_admin_context()
+        self.context = oslo_context.get_admin_context()
         self.context.session = db.get_session()
         self.l3_plugin = importutils.import_object(L3_SVC_PLUGIN)
         with mock.patch.object(self.l3_plugin,
index f4a5fb17ab5d23e3a7c31e294d6f01b73a7cdfad..2acf8dd1c2eebf6c15e74fd75d1eaed3f736a29f 100644 (file)
@@ -21,12 +21,12 @@ import collections
 import contextlib
 
 import mock
+from oslo_context import context as oslo_context
 
 from neutron.agent import rpc as agent_rpc
 from neutron.common import constants
 from neutron.common import exceptions
 from neutron.common import topics
-from neutron.openstack.common import context
 from neutron.plugins.ml2.drivers import type_tunnel
 from neutron.plugins.ml2 import rpc as plugin_rpc
 from neutron.tests import base
@@ -166,7 +166,7 @@ class RpcCallbacksTestCase(base.BaseTestCase):
 class RpcApiTestCase(base.BaseTestCase):
 
     def _test_rpc_api(self, rpcapi, topic, method, rpc_method, **kwargs):
-        ctxt = context.RequestContext('fake_user', 'fake_project')
+        ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
         expected_retval = 'foo' if rpc_method == 'call' else None
         expected_version = kwargs.pop('version', None)
         fanout = kwargs.pop('fanout', False)
index 7f1880d8b526b58b51531475971d6a53a09ab47c..329e7b5e87c487eca9bc04f781dc8a663d7a45f6 100644 (file)
@@ -21,10 +21,10 @@ import contextlib
 import mock
 
 from oslo.config import cfg
+from oslo_context import context as oslo_context
 
 from neutron.agent import rpc as agent_rpc
 from neutron.common import topics
-from neutron.openstack.common import context
 from neutron.plugins.mlnx import agent_notify_api
 from neutron.tests import base
 
@@ -32,7 +32,7 @@ from neutron.tests import base
 class rpcApiTestCase(base.BaseTestCase):
 
     def _test_mlnx_api(self, rpcapi, topic, method, rpc_method, **kwargs):
-        ctxt = context.RequestContext('fake_user', 'fake_project')
+        ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
         expected_retval = 'foo' if rpc_method == 'call' else None
         expected_version = kwargs.pop('version', None)
         fanout = kwargs.pop('fanout', False)
index ccabce16120c9762074cad5d0acf3f3a468adf07..35047ee0a5e49b4e993b115c3d27e84a00be87e2 100644 (file)
 #    under the License.
 
 import contextlib
+
 import mock
 from oslo import messaging
+from oslo_context import context as oslo_context
 
 from neutron.agent import rpc
-from neutron.openstack.common import context
 from neutron.tests import base
 
 
 class AgentRPCPluginApi(base.BaseTestCase):
     def _test_rpc_call(self, method):
         agent = rpc.PluginApi('fake_topic')
-        ctxt = context.RequestContext('fake_user', 'fake_project')
+        ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
         expect_val = 'foo'
         with contextlib.nested(
             mock.patch.object(agent.client, 'call'),
@@ -50,7 +51,7 @@ class AgentRPCPluginApi(base.BaseTestCase):
 
     def test_devices_details_list_unsupported(self):
         agent = rpc.PluginApi('fake_topic')
-        ctxt = context.RequestContext('fake_user', 'fake_project')
+        ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
         expect_val_get_device_details = 'foo'
         expect_val = [expect_val_get_device_details]
         with contextlib.nested(
@@ -86,7 +87,7 @@ class AgentPluginReportState(base.BaseTestCase):
             mock_call, mock_cast, mock_prepare
         ):
             mock_prepare.return_value = reportStateAPI.client
-            ctxt = context.RequestContext('fake_user', 'fake_project')
+            ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
             reportStateAPI.report_state(ctxt, expected_agent_state,
                                         use_call=True)
             self.assertEqual(mock_call.call_args[0][0], ctxt)
@@ -107,7 +108,7 @@ class AgentPluginReportState(base.BaseTestCase):
             mock_call, mock_cast, mock_prepare
         ):
             mock_prepare.return_value = reportStateAPI.client
-            ctxt = context.RequestContext('fake_user', 'fake_project')
+            ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
             reportStateAPI.report_state(ctxt, expected_agent_state)
             self.assertEqual(mock_cast.call_args[0][0], ctxt)
             self.assertEqual(mock_cast.call_args[0][1], 'report_state')
index 7bba50dfae89a0d0b9e756b37c317e7959381d69..d786b629ef56f087a75776d5948a1e40d83fcdb9 100644 (file)
@@ -1,7 +1,6 @@
 [DEFAULT]
 # The list of modules to copy from oslo-incubator.git
 module=cache
-module=context
 module=eventlet_backdoor
 module=fileutils
 module=fixture
index c07d219bc65e1972bbb8caae672349e36f14f01c..771de3405a1b8cfbaf00256569ba6cd81da548ed 100644 (file)
@@ -25,6 +25,7 @@ alembic>=0.6.4
 six>=1.7.0
 stevedore>=1.1.0  # Apache-2.0
 oslo.config>=1.4.0  # Apache-2.0
+oslo.context>=0.1.0                     # Apache-2.0
 oslo.db>=1.1.0  # Apache-2.0
 oslo.i18n>=1.0.0  # Apache-2.0
 oslo.messaging>=1.4.0,!=1.5.0