From 7dfecfd1f312a519da9b8cebd6979f7be9fa93b3 Mon Sep 17 00:00:00 2001 From: Vikas Choudhary Date: Wed, 14 Oct 2015 11:12:04 +0530 Subject: [PATCH] Check subnetpool extension support This fix is to add support for checking subnetpool extension support at Kuryr server startup time. If required extension is found missing, startup script will exit and kuryr driver server will not start. Change-Id: I264be36c1b3b23517f4f2ff741f51a5a86b2ae9f Closes-Bug: #1498393 --- kuryr/common/exceptions.py | 12 ++++++++++-- kuryr/controllers.py | 15 +++++++++++++++ kuryr/tests/test_config.py | 18 +++++++++++++++++- scripts/run_server.py | 2 ++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/kuryr/common/exceptions.py b/kuryr/common/exceptions.py index 4ec31955..2d885c99 100644 --- a/kuryr/common/exceptions.py +++ b/kuryr/common/exceptions.py @@ -26,8 +26,16 @@ class BindingFailure(KuryrException): class DuplicatedResourceException(KuryrException): """Exception represents there're multiple resources for the ID. - This exception is thrown when you query the Neutron resouce associated with - the ID and you get multiple resources. + For example, this exception is thrown when you query the Neutron resource + associated with the ID and you get multiple resources. + """ + + +class MandatoryApiMissing(KuryrException): + """Exception represents that mandatory api is not found. + + For example, this exception is thrown when expected neutron + extension(subnetpools) APIs are not found. """ diff --git a/kuryr/controllers.py b/kuryr/controllers.py index 2b5b716f..27c70e6b 100644 --- a/kuryr/controllers.py +++ b/kuryr/controllers.py @@ -24,6 +24,8 @@ from kuryr.common import exceptions from kuryr import schemata from kuryr import utils +MANDATORY_NEUTRON_EXTENSION = "subnet_allocation" + cfg.CONF.import_group('neutron_client', 'kuryr.common.config') cfg.CONF.import_group('keystone_client', 'kuryr.common.config') @@ -45,6 +47,19 @@ else: app.neutron = utils.get_neutron_client_simple( url=neutron_uri, token=auth_token) + +def check_for_neutron_ext_support(): + """Validates for mandatory extension support availability in neutron. + """ + try: + app.neutron.show_extension(MANDATORY_NEUTRON_EXTENSION) + except n_exceptions.NeutronClientException as e: + if e.status_code == n_exceptions.NotFound.status_code: + raise exceptions.MandatoryApiMissing( + "Neutron extension with alias '{0}' not found" + .format(MANDATORY_NEUTRON_EXTENSION)) + + # TODO(tfukushima): Retrieve the following subnet names from the config file. SUBNET_POOLS_V4 = [ p.strip() for p in os.environ.get('SUBNET_POOLS_V4', 'kuryr').split(',')] diff --git a/kuryr/tests/test_config.py b/kuryr/tests/test_config.py index 7d998cfb..02e40661 100644 --- a/kuryr/tests/test_config.py +++ b/kuryr/tests/test_config.py @@ -12,11 +12,15 @@ import os +from neutronclient.common import exceptions as n_exceptions + from kuryr.common import config +from kuryr.common import exceptions +from kuryr import controllers from kuryr.tests import base -class ConfigurationTest(base.TestCase): +class ConfigurationTest(base.TestKuryrBase): def test_defaults(self): basepath = os.path.abspath(os.path.join(os.path.dirname(__file__), @@ -33,3 +37,15 @@ class ConfigurationTest(base.TestCase): self.assertEqual('http://127.0.0.1:35357', config.CONF.keystone_client.auth_uri) + + def test_check_for_neutron_ext_support_with_ex(self): + ext_alias = "subnet_allocation" + self.mox.StubOutWithMock(controllers.app.neutron, "show_extension") + err = n_exceptions.NotFound.status_code + ext_not_found_ex = n_exceptions.NeutronClientException(status_code=err, + message="") + neutron = controllers.app.neutron + neutron.show_extension(ext_alias).AndRaise(ext_not_found_ex) + self.mox.ReplayAll() + ex = exceptions.MandatoryApiMissing + self.assertRaises(ex, controllers.check_for_neutron_ext_support) diff --git a/scripts/run_server.py b/scripts/run_server.py index f0b2e5af..16715043 100755 --- a/scripts/run_server.py +++ b/scripts/run_server.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +from kuryr import controllers from kuryr import server +controllers.check_for_neutron_ext_support() server.start()