From: Ihar Hrachyshka Date: Thu, 11 Dec 2014 19:36:48 +0000 (+0100) Subject: Migrate to oslo.context X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=089e60a68b313c1f7077f010964ac27141425517;p=openstack-build%2Fneutron-build.git Migrate to oslo.context 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 --- diff --git a/neutron/context.py b/neutron/context.py index 7d294427a..038ebfa40 100644 --- a/neutron/context.py +++ b/neutron/context.py @@ -16,11 +16,11 @@ """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 index fa62fc1f1..000000000 --- a/neutron/openstack/common/context.py +++ /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 diff --git a/neutron/plugins/brocade/NeutronPlugin.py b/neutron/plugins/brocade/NeutronPlugin.py index 9a81fdf59..de19083f2 100644 --- a/neutron/plugins/brocade/NeutronPlugin.py +++ b/neutron/plugins/brocade/NeutronPlugin.py @@ -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(), diff --git a/neutron/tests/unit/brocade/test_brocade_vlan.py b/neutron/tests/unit/brocade/test_brocade_vlan.py index 09d030767..024e26f8f 100644 --- a/neutron/tests/unit/brocade/test_brocade_vlan.py +++ b/neutron/tests/unit/brocade/test_brocade_vlan.py @@ -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): diff --git a/neutron/tests/unit/hyperv/test_hyperv_rpcapi.py b/neutron/tests/unit/hyperv/test_hyperv_rpcapi.py index f3d8d2bc0..7f2c8672f 100644 --- a/neutron/tests/unit/hyperv/test_hyperv_rpcapi.py +++ b/neutron/tests/unit/hyperv/test_hyperv_rpcapi.py @@ -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) diff --git a/neutron/tests/unit/ml2/drivers/brocade/test_brocade_l3_plugin.py b/neutron/tests/unit/ml2/drivers/brocade/test_brocade_l3_plugin.py index 2dd1abfef..8ef5987e4 100644 --- a/neutron/tests/unit/ml2/drivers/brocade/test_brocade_l3_plugin.py +++ b/neutron/tests/unit/ml2/drivers/brocade/test_brocade_l3_plugin.py @@ -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, diff --git a/neutron/tests/unit/ml2/test_rpcapi.py b/neutron/tests/unit/ml2/test_rpcapi.py index f4a5fb17a..2acf8dd1c 100644 --- a/neutron/tests/unit/ml2/test_rpcapi.py +++ b/neutron/tests/unit/ml2/test_rpcapi.py @@ -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) diff --git a/neutron/tests/unit/mlnx/test_rpcapi.py b/neutron/tests/unit/mlnx/test_rpcapi.py index 7f1880d8b..329e7b5e8 100644 --- a/neutron/tests/unit/mlnx/test_rpcapi.py +++ b/neutron/tests/unit/mlnx/test_rpcapi.py @@ -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) diff --git a/neutron/tests/unit/test_agent_rpc.py b/neutron/tests/unit/test_agent_rpc.py index ccabce161..35047ee0a 100644 --- a/neutron/tests/unit/test_agent_rpc.py +++ b/neutron/tests/unit/test_agent_rpc.py @@ -14,18 +14,19 @@ # 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') diff --git a/openstack-common.conf b/openstack-common.conf index 7bba50dfa..d786b629e 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -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 diff --git a/requirements.txt b/requirements.txt index c07d219bc..771de3405 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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