Add support for dealing with 501 errors (notimplemented)

This allows us to return NotImplemented() from an extension if the plugin
doesn't support a given function.

Change-Id: I73c6dec959aea7b2bde1378222b62e6fc82a5d43
This commit is contained in:
Brad Hall 2012-01-24 11:12:55 -08:00
parent a645692d25
commit 975d5a996b
4 changed files with 37 additions and 1 deletions

View File

@ -26,6 +26,7 @@ _PORTNOTFOUND_EXPL = 'Unable to find a port with the specified identifier.'
_STATEINVALID_EXPL = 'Unable to update port state with specified value.'
_PORTINUSE_EXPL = 'A resource is currently attached to the logical port'
_ALREADYATTACHED_EXPL = 'The resource is already attached to another port'
_NOTIMPLEMENTED_EXPL = 'Not implemented'
class QuantumHTTPError(webob.exc.HTTPClientError):
@ -60,6 +61,11 @@ class QuantumHTTPError(webob.exc.HTTPClientError):
'code': 440,
'title': 'alreadyAttached',
'explanation': _ALREADYATTACHED_EXPL
},
exceptions.NotImplementedError: {
'code': 501,
'title': 'notImplemented',
'explanation': _NOTIMPLEMENTED_EXPL
}
}

View File

@ -33,7 +33,8 @@ EXCEPTIONS = {
430: exceptions.PortNotFound,
431: exceptions.StateInvalid,
432: exceptions.PortInUseClient,
440: exceptions.AlreadyAttachedClient}
440: exceptions.AlreadyAttachedClient,
501: exceptions.NotImplementedError}
AUTH_TOKEN_HEADER = "X-Auth-Token"

View File

@ -159,6 +159,10 @@ class MissingArgumentError(Error):
pass
class NotImplementedError(Error):
pass
def wrap_exception(f):
def _wrap(*args, **kw):
try:

View File

@ -20,10 +20,13 @@ import routes
import unittest
from quantum.tests.unit import BaseTest
from webtest import TestApp
from webtest import AppError
from quantum import wsgi
from quantum.api import faults
from quantum.common import config
from quantum.common import exceptions
from quantum.common import extensions
import sys
print sys.path
@ -63,12 +66,34 @@ class ResourceExtensionTest(unittest.TestCase):
def show(self, request, id):
return {'data': {'id': id}}
def notimplemented_function(self, request, id):
return faults.QuantumHTTPError(
exceptions.NotImplementedError("notimplemented_function"))
def custom_member_action(self, request, id):
return {'member_action': 'value'}
def custom_collection_action(self, request, **kwargs):
return {'collection': 'value'}
def test_exceptions_notimplemented(self):
controller = self.ResourceExtensionController()
member = {'notimplemented_function': "GET"}
res_ext = extensions.ResourceExtension('tweedles', controller,
member_actions=member)
test_app = setup_extensions_test_app(SimpleExtensionManager(res_ext))
# Ideally we would check for a 501 code here but webtest doesn't take
# anything that is below 200 or above 400 so we can't actually check
# it. It thows AppError instead.
try:
response = \
test_app.get("/tweedles/some_id/notimplemented_function")
# Shouldn't be reached
self.assertTrue(False)
except AppError:
pass
def test_resource_can_be_added_as_extension(self):
res_ext = extensions.ResourceExtension('tweedles',
self.ResourceExtensionController())