Get the api_version decorator to work

The api_version decorator didn't work properly in before. The
controller will raise HTTPNotAcceptable on routing the request.
If there is a version mismatch for one action (i.e. detach_network),
all other actions will fail to route and response 406.

This commit fix it by raise HTTPNotAcceptable on method invocation
stage. This will avoid failing the api request routing incorrectly.

Change-Id: I44422e19b51a6fe50f4dc84867f8d437059e9f08
This commit is contained in:
Hongbin Lu 2017-08-30 02:21:10 +00:00
parent e9ac0f4074
commit 470b080fd0
2 changed files with 12 additions and 6 deletions

View File

@ -15,6 +15,7 @@
import operator
import six
import pecan
from pecan import rest
from webob import exc
from zun.api.controllers import versions
@ -96,6 +97,14 @@ class ControllerMetaclass(type):
class Controller(rest.RestController):
"""Base Rest Controller"""
@pecan.expose('json')
def _no_version_match(self, *args, **kwargs):
from pecan import request
raise exc.HTTPNotAcceptable(_(
"Version %(ver)s was requested but the requested API is not "
"supported for this version.") % {'ver': request.version})
def __getattribute__(self, key):
def version_select():
@ -114,10 +123,7 @@ class Controller(rest.RestController):
if ver.matches(func.start_version, func.end_version):
return func.func
raise exc.HTTPNotAcceptable(_(
"Version %(ver)s was requested but the requested API %(api)s "
"is not supported for this version.") % {'ver': ver,
'api': key})
return self._no_version_match
try:
version_meth_dict = object.__getattribute__(self, VER_METHOD_ATTR)

View File

@ -475,5 +475,5 @@ class TestController(test_base.TestCase):
"", "1.2")
controller.request = mock_pecan_request
self.assertRaises(exc.HTTPNotAcceptable,
controller.__getattribute__, 'testapi1')
method = controller.__getattribute__('testapi1')
self.assertRaises(exc.HTTPNotAcceptable, method)