Rajaram/Santhosh|quantum manager loads plugin only once, even though both extension middleware and APIRouter calls it

This commit is contained in:
Rajaram Mallya 2011-07-25 18:45:21 +05:30
parent a444cd3bd9
commit ce1dbf0d2b
8 changed files with 53 additions and 77 deletions

View File

@ -23,7 +23,7 @@ use = egg:Paste#urlmap
pipeline = extensions quantumapiapp
[filter:extensions]
paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory
paste.filter_factory = quantum.common.extensions:plugin_aware_extension_middleware_factory
[app:quantumversions]
paste.app_factory = quantum.api.versions:Versions.factory

View File

@ -23,7 +23,7 @@ use = egg:Paste#urlmap
pipeline = extensions quantumapiapp
[filter:extensions]
paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory
paste.filter_factory = quantum.common.extensions:plugin_aware_extension_middleware_factory
[app:quantumversions]
paste.app_factory = quantum.api.versions:Versions.factory

View File

@ -18,24 +18,7 @@ api_extensions_path = unit/extensions
pipeline = extensions extensions_test_app
[filter:extensions]
paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory
paste.filter_factory = quantum.common.extensions:plugin_aware_extension_middleware_factory
[app:extensions_test_app]
paste.app_factory = tests.unit.test_extensions:app_factory
[composite:quantum]
use = egg:Paste#urlmap
/: quantumversions
/v0.1: quantumapi
[pipeline:quantumapi]
pipeline = extensions quantumapiapp
[filter:extensions]
paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory
[app:quantumversions]
paste.app_factory = quantum.api.versions:Versions.factory
[app:quantumapiapp]
paste.app_factory = quantum.api:APIRouterV01.factory

View File

@ -48,7 +48,7 @@ class APIRouterV01(wsgi.Router):
def _setup_routes(self, mapper, options):
# Loads the quantum plugin
plugin = manager.QuantumManager(options).get_plugin()
plugin = manager.QuantumManager.get_plugin(options)
uri_prefix = '/tenants/{tenant_id}/'
mapper.resource('network', 'networks',

View File

@ -318,16 +318,14 @@ class ExtensionMiddleware(wsgi.Middleware):
return app
class PluginAwareExtensionMiddleware(ExtensionMiddleware):
def __init__(self, application, config_params, ext_mgr=None,
plugin_options=None):
plugin_aware_extension_mgr = PluginAwareExtensionManager(
config_params.get('api_extensions_path', ''),
plugin_options)
ext_mgr = (ext_mgr or plugin_aware_extension_mgr)
super(PluginAwareExtensionMiddleware, self).__init__(
application, config_params, ext_mgr)
def plugin_aware_extension_middleware_factory(global_config, **local_config):
"""Paste factory."""
def _factory(app):
extensions_path = global_config.get('api_extensions_path', '')
ext_mgr = PluginAwareExtensionManager(extensions_path,
QuantumManager().get_plugin())
return ExtensionMiddleware(app, global_config, ext_mgr=ext_mgr)
return _factory
class ExtensionManager(object):
@ -449,8 +447,8 @@ class ExtensionManager(object):
class PluginAwareExtensionManager(ExtensionManager):
def __init__(self, path, plugin_options=None):
self.plugin = QuantumManager(plugin_options).get_plugin()
def __init__(self, path, plugin):
self.plugin = plugin
super(PluginAwareExtensionManager, self).__init__(path)
def _check_extension(self, extension):
@ -470,7 +468,8 @@ class PluginAwareExtensionManager(ExtensionManager):
if(not hasattr(extension, "get_plugin_interface") or
extension.get_plugin_interface() is None):
return True
return isinstance(self.plugin, extension.get_plugin_interface())
return isinstance(self.plugin,
extension.get_plugin_interface())
class RequestExtension(object):

View File

@ -47,6 +47,8 @@ def find_config(basepath):
class QuantumManager(object):
_instance = None
def __init__(self, options=None, config_file=None):
if config_file == None:
self.configuration_file = find_config(
@ -69,5 +71,8 @@ class QuantumManager(object):
"All compatibility tests passed")
self.plugin = plugin_klass()
def get_plugin(self):
return self.plugin
@classmethod
def get_plugin(cls, options=None, config_file=None):
if cls._instance is None:
cls._instance = cls(options, config_file)
return cls._instance.plugin

View File

@ -1,7 +1,7 @@
[DATABASE]
name = ovs_quantum
user = root
pass = nova
pass =
host = 127.0.0.1
port = 3306

View File

@ -26,7 +26,7 @@ from quantum.common import wsgi
from quantum.common import config
from quantum.common.extensions import (ExtensionManager,
PluginAwareExtensionManager,
PluginAwareExtensionMiddleware)
ExtensionMiddleware)
test_conf_file = os.path.join(os.path.dirname(__file__), os.pardir,
os.pardir, 'etc', 'quantum.conf.test')
@ -288,19 +288,17 @@ class ExtensionManagerTest(unittest.TestCase):
class PluginAwareExtensionManagerTest(unittest.TestCase):
def setUp(self):
self.ext_mgr = PluginAwareExtensionManager('', plugin_options)
def test_unsupported_extensions_are_not_loaded(self):
self.ext_mgr.plugin = StubPlugin(supported_extensions=["e1", "e3"])
stub_plugin = StubPlugin(supported_extensions=["e1", "e3"])
ext_mgr = PluginAwareExtensionManager('', stub_plugin)
self.ext_mgr.add_extension(StubExtension("e1"))
self.ext_mgr.add_extension(StubExtension("e2"))
self.ext_mgr.add_extension(StubExtension("e3"))
ext_mgr.add_extension(StubExtension("e1"))
ext_mgr.add_extension(StubExtension("e2"))
ext_mgr.add_extension(StubExtension("e3"))
self.assertTrue("e1" in self.ext_mgr.extensions)
self.assertFalse("e2" in self.ext_mgr.extensions)
self.assertTrue("e3" in self.ext_mgr.extensions)
self.assertTrue("e1" in ext_mgr.extensions)
self.assertFalse("e2" in ext_mgr.extensions)
self.assertTrue("e3" in ext_mgr.extensions)
def test_extensions_are_not_loaded_for_plugins_unaware_of_extensions(self):
class ExtensionUnawarePlugin(object):
@ -310,10 +308,10 @@ class PluginAwareExtensionManagerTest(unittest.TestCase):
"""
pass
self.ext_mgr.plugin = ExtensionUnawarePlugin()
self.ext_mgr.add_extension(StubExtension("e1"))
ext_mgr = PluginAwareExtensionManager('', ExtensionUnawarePlugin())
ext_mgr.add_extension(StubExtension("e1"))
self.assertFalse("e1" in self.ext_mgr.extensions)
self.assertFalse("e1" in ext_mgr.extensions)
def test_extensions_not_loaded_for_plugin_without_expected_interface(self):
@ -323,11 +321,12 @@ class PluginAwareExtensionManagerTest(unittest.TestCase):
"""
supported_extension_aliases = ["supported_extension"]
self.ext_mgr.plugin = PluginWithoutExpectedInterface()
self.ext_mgr.add_extension(
ext_mgr = PluginAwareExtensionManager('',
PluginWithoutExpectedInterface())
ext_mgr.add_extension(
ExtensionExpectingPluginInterface("supported_extension"))
self.assertFalse("e1" in self.ext_mgr.extensions)
self.assertFalse("e1" in ext_mgr.extensions)
def test_extensions_are_loaded_for_plugin_with_expected_interface(self):
@ -339,12 +338,12 @@ class PluginAwareExtensionManagerTest(unittest.TestCase):
def get_foo(self, bar=None):
pass
self.ext_mgr.plugin = PluginWithExpectedInterface()
self.ext_mgr.add_extension(
ext_mgr = PluginAwareExtensionManager('',
PluginWithExpectedInterface())
ext_mgr.add_extension(
ExtensionExpectingPluginInterface("supported_extension"))
self.assertTrue("supported_extension" in self.ext_mgr.extensions)
self.assertTrue("supported_extension" in ext_mgr.extensions)
def test_extensions_expecting_quantum_plugin_interface_are_loaded(self):
class ExtensionForQuamtumPluginInterface(StubExtension):
@ -353,11 +352,11 @@ class PluginAwareExtensionManagerTest(unittest.TestCase):
This will work with any plugin implementing QuantumPluginBase
"""
pass
stub_plugin = StubPlugin(supported_extensions=["e1"])
ext_mgr = PluginAwareExtensionManager('', stub_plugin)
ext_mgr.add_extension(ExtensionForQuamtumPluginInterface("e1"))
self.ext_mgr.plugin = StubPlugin(supported_extensions=["e1"])
self.ext_mgr.add_extension(ExtensionForQuamtumPluginInterface("e1"))
self.assertTrue("e1" in self.ext_mgr.extensions)
self.assertTrue("e1" in ext_mgr.extensions)
def test_extensions_without_need_for__plugin_interface_are_loaded(self):
class ExtensionWithNoNeedForPluginInterface(StubExtension):
@ -368,10 +367,11 @@ class PluginAwareExtensionManagerTest(unittest.TestCase):
def get_plugin_interface(self):
return None
self.ext_mgr.plugin = StubPlugin(supported_extensions=["e1"])
self.ext_mgr.add_extension(ExtensionWithNoNeedForPluginInterface("e1"))
stub_plugin = StubPlugin(supported_extensions=["e1"])
ext_mgr = PluginAwareExtensionManager('', stub_plugin)
ext_mgr.add_extension(ExtensionWithNoNeedForPluginInterface("e1"))
self.assertTrue("e1" in self.ext_mgr.extensions)
self.assertTrue("e1" in ext_mgr.extensions)
class ExtensionControllerTest(unittest.TestCase):
@ -396,16 +396,6 @@ class ExtensionControllerTest(unittest.TestCase):
"http://www.fox.in.socks/api/ext/pie/v1.0")
class TestExtensionMiddlewareFactory(unittest.TestCase):
def test_app_configured_with_extensions_as_filter(self):
conf, quantum_app = config.load_paste_app('extensions_app_with_filter',
{"config_file": test_conf_file}, None)
response = TestApp(quantum_app).get("/extensions")
self.assertEqual(response.status_int, 200)
def app_factory(global_conf, **local_conf):
conf = global_conf.copy()
conf.update(local_conf)
@ -421,8 +411,7 @@ def setup_base_app():
def setup_extensions_middleware(extension_manager=None):
options = {'config_file': test_conf_file}
conf, app = config.load_paste_app('extensions_test_app', options, None)
return PluginAwareExtensionMiddleware(app, conf, ext_mgr=extension_manager,
plugin_options=plugin_options)
return ExtensionMiddleware(app, conf, ext_mgr=extension_manager)
def setup_extensions_test_app(extension_manager=None):