Pecan: replace dashes with underscores on controller lookup

This is needed for extensions like security-groups as resource
names do not use dashes but underscores.

Change-Id: I952abd261a3cf8ac964463dd50377905f08830c8
Closes-Bug: #1537929
This commit is contained in:
Salvatore Orlando 2016-01-25 14:33:21 -08:00 committed by Kevin Benton
parent 171351d5fb
commit a47f4d1726
5 changed files with 15 additions and 6 deletions

View File

@ -258,7 +258,12 @@ class NeutronManager(object):
@classmethod
def get_controller_for_resource(cls, resource):
return cls.get_instance().resource_controller_mappings.get(resource)
res_ctrl_mappings = cls.get_instance().resource_controller_mappings
# If no controller is found for resource, try replacing dashes with
# underscores
return res_ctrl_mappings.get(
resource,
res_ctrl_mappings.get(resource.replace('-', '_')))
@classmethod
def get_service_plugin_by_path_prefix(cls, path_prefix):

View File

@ -97,9 +97,11 @@ class V2Controller(object):
"code 404"), collection)
pecan.abort(404)
# Store resource and collection names in pecan request context so that
# hooks can leverage them if necessary
# hooks can leverage them if necessary. The following code uses
# attributes from the controller instance to ensure names have been
# properly sanitized (eg: replacing dashes with underscores)
request.context['resource'] = controller.resource
request.context['collection'] = collection
request.context['collection'] = controller.collection
return controller, remainder

View File

@ -37,8 +37,9 @@ def when(index, *args, **kwargs):
class NeutronPecanController(object):
def __init__(self, collection, resource):
self.collection = collection
self.resource = resource
# Ensure dashes are always replaced with underscores
self.collection = collection and collection.replace('-', '_')
self.resource = resource and resource.replace('-', '_')
self._plugin = None
@property

View File

@ -109,7 +109,7 @@ def initialize_all():
resource)
manager.NeutronManager.set_controller_for_resource(
collection, controller)
controller.collection, controller)
LOG.info(_LI("Added controller for resource %(resource)s "
"via URI path segment:%(collection)s"),
{'resource': resource,

View File

@ -30,6 +30,7 @@ _SERVICE_PLUGIN_INDEX_BODY = {_SERVICE_PLUGIN_COLLECTION: []}
class FakeServicePluginController(object):
resource = _SERVICE_PLUGIN_RESOURCE
collection = _SERVICE_PLUGIN_COLLECTION
@pecan.expose(generic=True,
content_type='application/json',