From fa1857184db370ff78b9b7344be5f3a1030b862f Mon Sep 17 00:00:00 2001 From: Aaron Rosen Date: Mon, 28 Oct 2013 15:25:42 -0700 Subject: [PATCH] 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 --- nova/api/openstack/compute/plugins/v3/servers.py | 6 +++--- nova/api/openstack/compute/servers.py | 1 + .../openstack/compute/plugins/v3/test_servers.py | 14 +++++++++++++- nova/tests/api/openstack/compute/test_servers.py | 13 +++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/nova/api/openstack/compute/plugins/v3/servers.py b/nova/api/openstack/compute/plugins/v3/servers.py index 66ebb28f957c..1475e483f015 100644 --- a/nova/api/openstack/compute/plugins/v3/servers.py +++ b/nova/api/openstack/compute/plugins/v3/servers.py @@ -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()) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index fdf71252ee54..e9e9430aca8b 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -970,6 +970,7 @@ class Controller(wsgi.Controller): exception.InvalidMetadata, exception.InvalidRequest, exception.MultiplePortsNotApplicable, + exception.NetworkNotFound, exception.PortNotFound, exception.SecurityGroupNotFound, exception.InvalidBDM) as error: diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_servers.py b/nova/tests/api/openstack/compute/plugins/v3/test_servers.py index 2459b127a009..94f0bc83208d 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_servers.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_servers.py @@ -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) diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py index 24dbb263c168..86f046e726c9 100644 --- a/nova/tests/api/openstack/compute/test_servers.py +++ b/nova/tests/api/openstack/compute/test_servers.py @@ -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'