Fix HTTP response code for network APIs and improve error message

This patch corrects the http response codes for network resources to be
consistant with other resources by returning a 400 instead of a 404
if the desired resource is not found.

This patch makes the following api changes:

v2-api: NetworkNotFound now returns 400 instead of 404 and improved error
        message which network was not found instead of
        'The resource could not be found'.

v3-api: PortNotFound and NetworkNotFound now returns a 400 instead of a 404.

Closes-bug: #1245696
DocImpact

Change-Id: I66eb0c0ab926e0a8d1e2c9cfe1f7fd579ea3da21
This commit is contained in:
Aaron Rosen 2013-10-28 15:25:42 -07:00 committed by Aaron Rosen
parent 85915c4c3a
commit fa1857184d
4 changed files with 30 additions and 4 deletions

View File

@ -835,11 +835,11 @@ class ServersController(wsgi.Controller):
exception.InvalidMetadata,
exception.InvalidRequest,
exception.MultiplePortsNotApplicable,
exception.InstanceUserDataMalformed,
exception.PortNotFound,
exception.SecurityGroupNotFound,
exception.InstanceUserDataMalformed) as error:
exception.NetworkNotFound) as error:
raise exc.HTTPBadRequest(explanation=error.format_message())
except exception.PortNotFound as error:
raise exc.HTTPNotFound(explanation=error.format_message())
except exception.PortInUse as error:
raise exc.HTTPConflict(explanation=error.format_message())

View File

@ -970,6 +970,7 @@ class Controller(wsgi.Controller):
exception.InvalidMetadata,
exception.InvalidRequest,
exception.MultiplePortsNotApplicable,
exception.NetworkNotFound,
exception.PortNotFound,
exception.SecurityGroupNotFound,
exception.InvalidBDM) as error:

View File

@ -2334,6 +2334,18 @@ class ServersControllerCreateTest(test.TestCase):
self.assertRaises(webob.exc.HTTPBadRequest,
self._test_create_extra, params)
def test_create_instance_with_neturonv2_not_found_network(self):
network = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
requested_networks = [{'uuid': network}]
params = {'networks': requested_networks}
def fake_create(*args, **kwargs):
raise exception.NetworkNotFound(network_id=network)
self.stubs.Set(compute_api.API, 'create', fake_create)
self.assertRaises(webob.exc.HTTPBadRequest,
self._test_create_extra, params)
def test_create_instance_with_neutronv2_port_not_found(self):
network = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
port = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee'
@ -2344,7 +2356,7 @@ class ServersControllerCreateTest(test.TestCase):
raise exception.PortNotFound(port_id=port)
self.stubs.Set(compute_api.API, 'create', fake_create)
self.assertRaises(webob.exc.HTTPNotFound,
self.assertRaises(webob.exc.HTTPBadRequest,
self._test_create_extra, params)

View File

@ -2949,6 +2949,19 @@ class ServersControllerCreateTest(test.TestCase):
self.assertRaises(webob.exc.HTTPConflict,
self._test_create_extra, params)
def test_create_instance_with_neturonv2_not_found_network(self):
self.flags(network_api_class='nova.network.neutronv2.api.API')
network = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
requested_networks = [{'uuid': network}]
params = {'networks': requested_networks}
def fake_create(*args, **kwargs):
raise exception.NetworkNotFound(network_id=network)
self.stubs.Set(compute_api.API, 'create', fake_create)
self.assertRaises(webob.exc.HTTPBadRequest,
self._test_create_extra, params)
def test_create_instance_with_neutronv2_port_not_found(self):
self.flags(network_api_class='nova.network.neutronv2.api.API')
network = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'