Fix wrong exception return in fixed_ips v2 extention

Currently, fixed_ips api raises HTTPNotFound if passing an invalid
ip address in url, I think this is not correct. It's better to return
HTTPBadRequest to end user when the api excepts a FixedIpInvalid
exception.

This patch fixes it and also the test cases.

Change-Id: I7b18b76745f9092456d511ad023a9ed3a24cca84
This commit is contained in:
Eli Qiao 2014-10-08 15:40:44 +08:00
parent 96b39341d5
commit 378681490b
2 changed files with 13 additions and 6 deletions

View File

@ -33,9 +33,10 @@ class FixedIPController(object):
try:
fixed_ip = objects.FixedIP.get_by_address(context, id,
expected_attrs=attrs)
except (exception.FixedIpNotFoundForAddress,
exception.FixedIpInvalid) as ex:
except exception.FixedIpNotFoundForAddress as ex:
raise webob.exc.HTTPNotFound(explanation=ex.format_message())
except exception.FixedIpInvalid as ex:
raise webob.exc.HTTPBadRequest(explanation=ex.format_message())
fixed_ip_info = {"fixed_ip": {}}
if fixed_ip is None:
@ -71,9 +72,11 @@ class FixedIPController(object):
fixed_ip = objects.FixedIP.get_by_address(context, address)
fixed_ip.reserved = reserved
fixed_ip.save()
except (exception.FixedIpNotFoundForAddress, exception.FixedIpInvalid):
except exception.FixedIpNotFoundForAddress:
msg = _("Fixed IP %s not found") % address
raise webob.exc.HTTPNotFound(explanation=msg)
except exception.FixedIpInvalid as ex:
raise webob.exc.HTTPBadRequest(explanation=ex.format_message())
return webob.Response(status_int=202)

View File

@ -18,6 +18,7 @@ from nova.api.openstack.compute.contrib import fixed_ips
from nova import context
from nova import db
from nova import exception
from nova.i18n import _
from nova import test
from nova.tests.api.openstack import fakes
from nova.tests.objects import test_network
@ -72,6 +73,9 @@ fake_fixed_ips = [{'id': 1,
def fake_fixed_ip_get_by_address(context, address, columns_to_join=None):
if address == 'inv.ali.d.ip':
msg = _("Invalid fixed IP Address %s in request") % address
raise exception.FixedIpInvalid(msg)
for fixed_ip in fake_fixed_ips:
if fixed_ip['address'] == address and not fixed_ip['deleted']:
return fixed_ip
@ -150,7 +154,7 @@ class FixedIpTest(test.NoDBTestCase):
def test_fixed_ips_get_invalid_ip_address(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-fixed-ips/inv.ali.d.ip')
self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, req,
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.show, req,
'inv.ali.d.ip')
def test_fixed_ips_get_deleted_ip_fail(self):
@ -179,7 +183,7 @@ class FixedIpTest(test.NoDBTestCase):
body = {'reserve': None}
req = fakes.HTTPRequest.blank(
'/v2/fake/os-fixed-ips/inv.ali.d.ip/action')
self.assertRaises(webob.exc.HTTPNotFound,
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.action, req, 'inv.ali.d.ip', body)
def test_fixed_ip_reserve_deleted_ip(self):
@ -210,7 +214,7 @@ class FixedIpTest(test.NoDBTestCase):
body = {'unreserve': None}
req = fakes.HTTPRequest.blank(
'/v2/fake/os-fixed-ips/inv.ali.d.ip/action')
self.assertRaises(webob.exc.HTTPNotFound,
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.action, req, 'inv.ali.d.ip', body)
def test_fixed_ip_unreserve_deleted_ip(self):