From 5a5a88074352f47ebb8808ae3344004e2cdeea51 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 11 Jun 2015 07:32:44 -0700 Subject: [PATCH] Add extensions listing to the controller Extensions aren't like normal resources handled by the core plugin or a service plugin so they need special code to handle them. The resource identifier hook also had to be tweaked to let the request slip through without having an entry in the attributes map. Change-Id: I5f42bdd7aa3c103b2ff3550ce1b0d9a3baf35a35 Co-Authored-By: Brandon Logan Partially-Implements: blueprint wsgi-pecan-switch --- neutron/api/extensions.py | 3 +- neutron/newapi/controllers/root.py | 34 +++++++++++++++++++ neutron/newapi/hooks/resource_identifier.py | 3 +- .../functional/newapi/test_functional.py | 8 +++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/neutron/api/extensions.py b/neutron/api/extensions.py index 8eb0f9070..27b8a0d4d 100644 --- a/neutron/api/extensions.py +++ b/neutron/api/extensions.py @@ -208,7 +208,8 @@ class ExtensionController(wsgi.Controller): def __init__(self, extension_manager): self.extension_manager = extension_manager - def _translate(self, ext): + @staticmethod + def _translate(ext): ext_data = {} ext_data['name'] = ext.get_name() ext_data['alias'] = ext.get_alias() diff --git a/neutron/newapi/controllers/root.py b/neutron/newapi/controllers/root.py index 221755f94..4185080f5 100644 --- a/neutron/newapi/controllers/root.py +++ b/neutron/newapi/controllers/root.py @@ -16,6 +16,8 @@ import pecan +from neutron.api import extensions + def expose(*args, **kwargs): """Helper function so we don't have to specify json for everything.""" @@ -48,9 +50,41 @@ class V2Controller(object): @expose() def _lookup(self, endpoint, *remainder): + if endpoint == 'extensions': + return ExtensionsController(), remainder return GeneralController(endpoint), remainder +class ExtensionsController(object): + + @expose() + def _lookup(self, alias, *remainder): + return ExtensionController(alias), remainder + + @expose() + def index(self): + ext_mgr = extensions.PluginAwareExtensionManager.get_instance() + exts = [extensions.ExtensionController._translate(ext) + for ext in ext_mgr.extensions.values()] + return {'extensions': exts} + + +class ExtensionController(object): + + def __init__(self, alias): + self.alias = alias + + @expose() + def index(self): + ext_mgr = extensions.PluginAwareExtensionManager.get_instance() + ext = ext_mgr.extensions.get(self.alias, None) + if not ext: + pecan.abort( + 404, detail=_("Extension with alias %s " + "does not exist") % self.alias) + return {'extension': extensions.ExtensionController._translate(ext)} + + class GeneralController(object): def __init__(self, token): diff --git a/neutron/newapi/hooks/resource_identifier.py b/neutron/newapi/hooks/resource_identifier.py index 5b6f1bade..91000f4f0 100644 --- a/neutron/newapi/hooks/resource_identifier.py +++ b/neutron/newapi/hooks/resource_identifier.py @@ -35,7 +35,8 @@ class ResourceIdentifierHook(hooks.PecanHook): url_type = state.request.path.split('/')[2].rsplit('.', 1)[0] except IndexError: return - + if url_type == 'extensions': + return for plural, single in attributes.PLURALS.items(): if plural == url_type: state.request.resource_type = single diff --git a/neutron/tests/functional/newapi/test_functional.py b/neutron/tests/functional/newapi/test_functional.py index bc7089c8d..e4e4506d2 100644 --- a/neutron/tests/functional/newapi/test_functional.py +++ b/neutron/tests/functional/newapi/test_functional.py @@ -91,6 +91,14 @@ class TestV2Controller(PecanFunctionalTest): def test_plugin_initialized(self): self.assertIsNotNone(manager.NeutronManager._instance) + def test_get_extensions(self): + response = self.app.get('/v2.0/extensions.json') + self.assertEqual(response.status_int, 200) + + def test_get_specific_extension(self): + response = self.app.get('/v2.0/extensions/allowed-address-pairs.json') + self.assertEqual(response.status_int, 200) + class TestErrors(PecanFunctionalTest): -- 2.45.2