From: Deepak N Date: Mon, 18 Jul 2011 11:54:26 +0000 (+0530) Subject: Santhosh/Deepak | Made supports_extension method optional for plugin, plugin will... X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=61e481146837265f245680303293af123622c33f;p=openstack-build%2Fneutron-build.git Santhosh/Deepak | Made supports_extension method optional for plugin, plugin will be loaded only once --- diff --git a/quantum/api/__init__.py b/quantum/api/__init__.py index fc3476733..87df7673f 100644 --- a/quantum/api/__init__.py +++ b/quantum/api/__init__.py @@ -48,7 +48,7 @@ class APIRouterV01(wsgi.Router): def _setup_routes(self, mapper): # Loads the quantum plugin - plugin = manager.QuantumManager().get_plugin() + plugin = manager.QuantumManager.get_plugin() uri_prefix = '/tenants/{tenant_id}/' mapper.resource('network', 'networks', controller=networks.Controller(plugin), diff --git a/quantum/common/extensions.py b/quantum/common/extensions.py index 2682aa929..44d52a8b8 100644 --- a/quantum/common/extensions.py +++ b/quantum/common/extensions.py @@ -234,7 +234,7 @@ class ExtensionMiddleware(wsgi.Middleware): self.ext_mgr = (ext_mgr or ExtensionManager( config_params.get('api_extensions_path', - ''), QuantumManager().plugin)) + ''), QuantumManager.get_plugin())) mapper = routes.Mapper() @@ -352,11 +352,15 @@ class ExtensionManager(object): LOG.debug(_('Ext description: %s'), extension.get_description()) LOG.debug(_('Ext namespace: %s'), extension.get_namespace()) LOG.debug(_('Ext updated: %s'), extension.get_updated()) - return self.plugin.supports_extension(extension) + return self._plugin_supports(extension) except AttributeError as ex: LOG.exception(_("Exception loading extension: %s"), unicode(ex)) return False + def _plugin_supports(self, extension): + return (hasattr(self.plugin, "supports_extension") and + self.plugin.supports_extension(extension)) + def _load_all_extensions(self): """Load extensions from the configured path. diff --git a/quantum/manager.py b/quantum/manager.py index d639ec1d3..33eb77e59 100644 --- a/quantum/manager.py +++ b/quantum/manager.py @@ -44,6 +44,9 @@ def find_config(basepath): class QuantumManager(object): + + _instance = None + def __init__(self, config=None): if config == None: self.configuration_file = find_config( @@ -60,5 +63,8 @@ class QuantumManager(object): "All compatibility tests passed\n") self.plugin = plugin_klass() - def get_plugin(self): - return self.plugin + @classmethod + def get_plugin(cls): + if(cls._instance is None): + cls._instance = QuantumManager() + return cls._instance.plugin diff --git a/quantum/quantum_plugin_base.py b/quantum/quantum_plugin_base.py index 52947aa7e..a43b8c0cc 100644 --- a/quantum/quantum_plugin_base.py +++ b/quantum/quantum_plugin_base.py @@ -230,10 +230,10 @@ class QuantumPluginBase(object): """ pass - @abstractmethod def supports_extension(self, extension): """ - Returns if the extension is suppoorted + Returns if the extension is supported. + If this method is not implemented, extensions will not be loaded. :returns: True or False """ diff --git a/tests/__init__.py b/tests/__init__.py index e69de29bb..9578283b2 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,19 @@ +# 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 logging +logging.basicConfig() diff --git a/tests/unit/test_extensions.py b/tests/unit/test_extensions.py index 7a02cdeb4..0fe557f0d 100644 --- a/tests/unit/test_extensions.py +++ b/tests/unit/test_extensions.py @@ -128,6 +128,21 @@ class ExtensionManagerTest(unittest.TestCase): self.assertFalse("e2" in ext_mgr.extensions) self.assertTrue("e3" in ext_mgr.extensions) + def test_extensions_are_not_loaded_for_extensions_unaware_plugins(self): + class ExtensionUnawarePlugin(object): + """ + This plugin does not implement supports_extension method. + Extensions will not be loaded when this plugin is used. + """ + pass + + ext_mgr = setup_extensions_middleware().ext_mgr + ext_mgr.plugin = ExtensionUnawarePlugin() + + ext_mgr.add_extension(StubExtension("e1")) + + self.assertFalse("e1" in ext_mgr.extensions) + class ActionExtensionTest(unittest.TestCase):