From: Shang Yong Date: Thu, 7 May 2015 01:44:03 +0000 (+0800) Subject: Add parent_id to _item calling from _handle_action X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=87c3ea7e739b1d8af61e5fae39e1d86aa53bc1b2;p=openstack-build%2Fneutron-build.git Add parent_id to _item calling from _handle_action When we add member_actions to sub-resources, this will cause error (500 Internal Server Error) for no parent_id parameter. Closes-Bug: 1452518 Change-Id: If2f9e0924d21406aa766261cd11797a8452e1b1f --- diff --git a/neutron/api/v2/base.py b/neutron/api/v2/base.py index 8237905d2..396cf9bbd 100644 --- a/neutron/api/v2/base.py +++ b/neutron/api/v2/base.py @@ -191,7 +191,12 @@ class Controller(object): policy.init() # Fetch the resource and verify if the user can access it try: - resource = self._item(request, id, True) + parent_id = kwargs.get(self._parent_id_name) + resource = self._item(request, + id, + do_authz=True, + field_list=None, + parent_id=parent_id) except oslo_policy.PolicyNotAuthorized: msg = _('The resource could not be found.') raise webob.exc.HTTPNotFound(msg) diff --git a/neutron/tests/unit/api/v2/test_base.py b/neutron/tests/unit/api/v2/test_base.py index 41cb83ec9..5ba3343e6 100644 --- a/neutron/tests/unit/api/v2/test_base.py +++ b/neutron/tests/unit/api/v2/test_base.py @@ -1127,12 +1127,16 @@ class SubresourceTest(base.BaseTestCase): self._plugin_patcher = mock.patch(plugin, autospec=True) self.plugin = self._plugin_patcher.start() - router.SUB_RESOURCES['dummy'] = { + api = router.APIRouter() + + SUB_RESOURCES = {} + RESOURCE_ATTRIBUTE_MAP = {} + SUB_RESOURCES['dummy'] = { 'collection_name': 'dummies', 'parent': {'collection_name': 'networks', 'member_name': 'network'} } - attributes.RESOURCE_ATTRIBUTE_MAP['dummies'] = { + RESOURCE_ATTRIBUTE_MAP['dummies'] = { 'foo': {'allow_post': True, 'allow_put': True, 'validate': {'type:string': None}, 'default': '', 'is_visible': True}, @@ -1141,11 +1145,33 @@ class SubresourceTest(base.BaseTestCase): 'required_by_policy': True, 'is_visible': True} } - api = router.APIRouter() + collection_name = SUB_RESOURCES['dummy'].get('collection_name') + resource_name = 'dummy' + parent = SUB_RESOURCES['dummy'].get('parent') + params = RESOURCE_ATTRIBUTE_MAP['dummies'] + member_actions = {'mactions': 'GET'} + _plugin = manager.NeutronManager.get_plugin() + controller = v2_base.create_resource(collection_name, resource_name, + _plugin, params, + member_actions=member_actions, + parent=parent, + allow_bulk=True, + allow_pagination=True, + allow_sorting=True) + + path_prefix = "/%s/{%s_id}/%s" % (parent['collection_name'], + parent['member_name'], + collection_name) + mapper_kwargs = dict(controller=controller, + path_prefix=path_prefix) + api.map.collection(collection_name, resource_name, **mapper_kwargs) + api.map.resource(collection_name, collection_name, + controller=controller, + parent_resource=parent, + member=member_actions) self.api = webtest.TestApp(api) def tearDown(self): - router.SUB_RESOURCES = {} super(SubresourceTest, self).tearDown() def test_index_sub_resource(self): @@ -1209,6 +1235,16 @@ class SubresourceTest(base.BaseTestCase): dummy_id, network_id='id1') + def test_sub_resource_member_actions(self): + instance = self.plugin.return_value + + dummy_id = _uuid() + self.api.get('/networks/id1' + _get_path('dummies', id=dummy_id, + action='mactions')) + instance.mactions.assert_called_once_with(mock.ANY, + dummy_id, + network_id='id1') + # Note: since all resources use the same controller and validation # logic, we actually get really good coverage from testing just networks. @@ -1465,6 +1501,9 @@ class TestSubresourcePlugin(object): def delete_network_dummy(self, context, id, network_id): return + def mactions(self, context, id, network_id): + return + class ListArgsTestCase(base.BaseTestCase): def test_list_args(self):