diff --git a/openstack-common.conf b/openstack-common.conf index 8024763722..31b82b840d 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=cfg,iniparser,setup +modules=cfg,importutils,iniparser,setup # The base module to hold the copy of openstack.common base=quantum diff --git a/quantum/common/utils.py b/quantum/common/utils.py index a44a2e4bb5..1c2fc971d7 100644 --- a/quantum/common/utils.py +++ b/quantum/common/utils.py @@ -36,27 +36,6 @@ from quantum.common.exceptions import ProcessExecutionError from quantum.common import flags -def import_class(import_str): - """Returns a class from a string including module and class.""" - mod_str, _sep, class_str = import_str.rpartition('.') - try: - __import__(mod_str) - return getattr(sys.modules[mod_str], class_str) - except (ImportError, ValueError, AttributeError), exc: - print(('Inner Exception: %s'), exc) - raise exception.ClassNotFound(class_name=class_str) - - -def import_object(import_str): - """Returns an object including a module or module and class.""" - try: - __import__(import_str) - return sys.modules[import_str] - except ImportError: - cls = import_class(import_str) - return cls() - - # NOTE(jkoelker) Since to_primitive isn't used anywhere can we just drop it def to_primitive(value): if isinstance(value, (list, tuple)): diff --git a/quantum/manager.py b/quantum/manager.py index b632486682..4db5ffdf53 100644 --- a/quantum/manager.py +++ b/quantum/manager.py @@ -29,6 +29,7 @@ import os from quantum.common import utils from quantum.common.config import find_config_file from quantum.common.exceptions import ClassNotFound +from quantum.openstack.common import importutils from quantum.quantum_plugin_base import QuantumPluginBase @@ -66,7 +67,7 @@ class QuantumManager(object): # If the plugin can't be found let them know gracefully try: - plugin_klass = utils.import_class(options['plugin_provider']) + plugin_klass = importutils.import_class(options['plugin_provider']) except ClassNotFound: raise Exception("Plugin not found. You can install a " "plugin with: pip install \n" diff --git a/quantum/openstack/common/importutils.py b/quantum/openstack/common/importutils.py new file mode 100644 index 0000000000..7654af5b95 --- /dev/null +++ b/quantum/openstack/common/importutils.py @@ -0,0 +1,44 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# 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. + +""" +Import related utilities and helper functions. +""" + +import sys + + +def import_class(import_str): + """Returns a class from a string including module and class""" + mod_str, _sep, class_str = import_str.rpartition('.') + try: + __import__(mod_str) + return getattr(sys.modules[mod_str], class_str) + except (ImportError, ValueError, AttributeError), exc: + raise ImportError('Class %s cannot be found (%s)' % + (class_str, str(exc))) + + +def import_object(import_str, *args, **kwargs): + """Import a class and return an instance of it.""" + return import_class(import_str)(*args, **kwargs) + + +def import_module(import_str): + """Import a module.""" + __import__(import_str) + return sys.modules[import_str] diff --git a/quantum/plugins/cisco/l2network_plugin.py b/quantum/plugins/cisco/l2network_plugin.py index 6d6810a50a..deb58ee0fe 100644 --- a/quantum/plugins/cisco/l2network_plugin.py +++ b/quantum/plugins/cisco/l2network_plugin.py @@ -21,7 +21,7 @@ import logging import re from quantum.common import exceptions as exc -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.quantum_plugin_base import QuantumPluginBase from quantum.plugins.cisco import l2network_plugin_configuration as conf from quantum.plugins.cisco.common import cisco_constants as const @@ -44,8 +44,8 @@ class L2Network(QuantumPluginBase): def __init__(self): cdb.initialize() cred.Store.initialize() - self._model = utils.import_object(conf.MODEL_CLASS) - self._vlan_mgr = utils.import_object(conf.MANAGER_CLASS) + self._model = importutils.import_object(conf.MODEL_CLASS) + self._vlan_mgr = importutils.import_object(conf.MANAGER_CLASS) LOG.debug("L2Network plugin initialization done successfully\n") """ diff --git a/quantum/plugins/cisco/models/l2network_multi_blade.py b/quantum/plugins/cisco/models/l2network_multi_blade.py index 219d75c9ab..f55888fcca 100644 --- a/quantum/plugins/cisco/models/l2network_multi_blade.py +++ b/quantum/plugins/cisco/models/l2network_multi_blade.py @@ -22,7 +22,7 @@ import logging import platform from quantum.common import exceptions as exc -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.plugins.cisco.l2network_model_base import L2NetworkModelBase from quantum.plugins.cisco import l2network_plugin_configuration as conf from quantum.plugins.cisco.common import cisco_constants as const @@ -45,13 +45,13 @@ class L2NetworkMultiBlade(L2NetworkModelBase): def __init__(self): for key in conf.PLUGINS[const.PLUGINS].keys(): - self._plugins[key] = utils.import_object( - conf.PLUGINS[const.PLUGINS][key]) + plugin_obj = conf.PLUGINS[const.PLUGINS][key] + self._plugins[key] = importutils.import_object(plugin_obj) LOG.debug("Loaded device plugin %s\n" % conf.PLUGINS[const.PLUGINS][key]) if key in conf.PLUGINS[const.INVENTORY].keys(): - self._inventory[key] = utils.import_object( - conf.PLUGINS[const.INVENTORY][key]) + inventory_obj = conf.PLUGINS[const.INVENTORY][key] + self._inventory[key] = importutils.import_object(inventory_obj) LOG.debug("Loaded device inventory %s\n" % conf.PLUGINS[const.INVENTORY][key]) diff --git a/quantum/plugins/cisco/models/l2network_single_blade.py b/quantum/plugins/cisco/models/l2network_single_blade.py index d6567d3b49..61395d889b 100644 --- a/quantum/plugins/cisco/models/l2network_single_blade.py +++ b/quantum/plugins/cisco/models/l2network_single_blade.py @@ -22,7 +22,7 @@ import logging import platform from quantum.common import exceptions as exc -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.plugins.cisco.l2network_model_base import L2NetworkModelBase from quantum.plugins.cisco import l2network_plugin_configuration as conf from quantum.plugins.cisco.common import cisco_constants as const @@ -42,13 +42,13 @@ class L2NetworkSingleBlade(L2NetworkModelBase): def __init__(self): for key in conf.PLUGINS[const.PLUGINS].keys(): - self._plugins[key] = utils.import_object( - conf.PLUGINS[const.PLUGINS][key]) + plugin_obj = conf.PLUGINS[const.PLUGINS][key] + self._plugins[key] = importutils.import_object(plugin_obj) LOG.debug("Loaded device plugin %s\n" % conf.PLUGINS[const.PLUGINS][key]) if key in conf.PLUGINS[const.INVENTORY].keys(): - self._inventory[key] = utils.import_object( - conf.PLUGINS[const.INVENTORY][key]) + inventory_obj = conf.PLUGINS[const.INVENTORY][key] + self._inventory[key] = importutils.import_object(inventory_obj) LOG.debug("Loaded device inventory %s\n" % conf.PLUGINS[const.INVENTORY][key]) diff --git a/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py b/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py index 36e1394a86..30511a67b5 100644 --- a/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py +++ b/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py @@ -23,7 +23,7 @@ PlugIn for Nexus OS driver import logging from quantum.common import exceptions as exc -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.plugins.cisco.common import cisco_constants as const from quantum.plugins.cisco.common import cisco_credentials as cred from quantum.plugins.cisco.db import api as db @@ -46,7 +46,7 @@ class NexusPlugin(L2DevicePluginBase): """ Extracts the configuration parameters from the configuration file """ - self._client = utils.import_object(conf.NEXUS_DRIVER) + self._client = importutils.import_object(conf.NEXUS_DRIVER) LOG.debug("Loaded driver %s\n" % conf.NEXUS_DRIVER) self._nexus_ip = conf.NEXUS_IP_ADDRESS self._nexus_username = cred.Store.getUsername(conf.NEXUS_IP_ADDRESS) diff --git a/quantum/plugins/cisco/services/services_logistics.py b/quantum/plugins/cisco/services/services_logistics.py index a83976b9b2..bf486dc95e 100644 --- a/quantum/plugins/cisco/services/services_logistics.py +++ b/quantum/plugins/cisco/services/services_logistics.py @@ -24,7 +24,7 @@ import subprocess import re import time -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.plugins.cisco import l2network_plugin_configuration as conf from quantum.plugins.cisco.common import cisco_constants as const from quantum.plugins.cisco.db import services_db as sdb @@ -106,8 +106,8 @@ class ServicesLogistics(): """ _plugins = {} for key in conf.PLUGINS[const.PLUGINS].keys(): - _plugins[key] = ( - utils.import_object(conf.PLUGINS[const.PLUGINS][key])) + plugin_obj = conf.PLUGINS[const.PLUGINS][key] + _plugins[key] = importutils.import_object(plugin_obj) if not plugin_key in _plugins.keys(): LOG.debug("No %s Plugin loaded" % plugin_key) return False diff --git a/quantum/plugins/cisco/tests/unit/test_l2network_multi_blade.py b/quantum/plugins/cisco/tests/unit/test_l2network_multi_blade.py index e69d4bf3e9..1b0d1a2477 100644 --- a/quantum/plugins/cisco/tests/unit/test_l2network_multi_blade.py +++ b/quantum/plugins/cisco/tests/unit/test_l2network_multi_blade.py @@ -23,7 +23,7 @@ import logging import unittest from quantum.common import exceptions as exc -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.plugins.cisco.common import cisco_constants as const from quantum.plugins.cisco.common import cisco_credentials as creds from quantum.plugins.cisco.db import api as db @@ -78,8 +78,8 @@ class TestMultiBlade(unittest.TestCase): # Get UCS inventory to make sure all UCSs are affected by tests for key in conf.PLUGINS[const.PLUGINS].keys(): if key in conf.PLUGINS[const.INVENTORY].keys(): - self._inventory[key] = utils.import_object( - conf.PLUGINS[const.INVENTORY][key]) + plugin_obj = conf.PLUGINS[const.INVENTORY][key] + self._inventory[key] = importutils.import_object(plugin_obj) self.ucs_count = self._inventory['ucs_plugin']._inventory.__len__() diff --git a/quantum/plugins/cisco/ucs/cisco_ucs_plugin.py b/quantum/plugins/cisco/ucs/cisco_ucs_plugin.py index 5259467933..e2dcd67156 100644 --- a/quantum/plugins/cisco/ucs/cisco_ucs_plugin.py +++ b/quantum/plugins/cisco/ucs/cisco_ucs_plugin.py @@ -20,7 +20,7 @@ import logging from quantum.common import exceptions as exc -from quantum.common import utils +from quantum.openstack.common import importutils from quantum.plugins.cisco.common import cisco_constants as const from quantum.plugins.cisco.common import cisco_credentials as cred from quantum.plugins.cisco.common import cisco_exceptions as cexc @@ -39,7 +39,7 @@ class UCSVICPlugin(L2DevicePluginBase): """UCS Device Plugin""" def __init__(self): - self._driver = utils.import_object(conf.UCSM_DRIVER) + self._driver = importutils.import_object(conf.UCSM_DRIVER) LOG.debug("Loaded driver %s\n" % conf.UCSM_DRIVER) # TODO (Sumit) Make the counter per UCSM self._port_profile_counter = 0 diff --git a/quantum/tests/unit/_test_api.py b/quantum/tests/unit/_test_api.py index 5d27fb8662..d30c4bf7bf 100644 --- a/quantum/tests/unit/_test_api.py +++ b/quantum/tests/unit/_test_api.py @@ -25,8 +25,8 @@ import mock from quantum.api.api_common import APIFaultWrapper from quantum.api.networks import Controller from quantum.common.test_lib import test_config -from quantum.common import utils from quantum.db import api as db +from quantum.openstack.common import importutils import quantum.tests.unit.testlib_api as testlib from quantum.wsgi import XMLDeserializer, JSONDeserializer @@ -107,7 +107,7 @@ class AbstractAPITest(unittest.TestCase): def setUp(self, api_router_klass, xml_metadata_dict): options = {} options['plugin_provider'] = test_config['plugin_name'] - api_router_cls = utils.import_class(api_router_klass) + api_router_cls = importutils.import_class(api_router_klass) self.api = api_router_cls(options) self.tenant_id = "test_tenant" self.network_name = "test_network"