]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add parent_id to _item calling from _handle_action
authorShang Yong <shangyong@letv.com>
Thu, 7 May 2015 01:44:03 +0000 (09:44 +0800)
committerShang Yong <shangyong@letv.com>
Mon, 29 Jun 2015 02:40:50 +0000 (10:40 +0800)
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

neutron/api/v2/base.py
neutron/tests/unit/api/v2/test_base.py

index 8237905d26bfc101b768a552c50f00d91b81c57e..396cf9bbdd59495c34f82bd4aa4d8e7b06e6cae1 100644 (file)
@@ -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)
index 41cb83ec9eb2b8531952893094e6ec6053ad160d..5ba3343e6eead0afc136e4bd8ab42734e715c48b 100644 (file)
@@ -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):