From a19d64808f3c4ab1cc3306ac3678701d0fff3e87 Mon Sep 17 00:00:00 2001 From: Ken'ichi Ohmichi Date: Tue, 20 Nov 2012 01:24:48 +0000 Subject: Return an error response if the specified flavor does not exists. (v4) 'nova list --flavor ' command outputs empty list, if there is not any instance or flavor. A user cannot know the reason of empty list easily. This patch changes a response to error if the specified flavor-id does not exist instead of empty list. Example: $ nova flavor-list +----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | extra_specs | +----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ | 1 | m1.tiny | 512 | 0 | 0 | | 1 | 1.0 | True | {} | | 2 | m1.small | 2048 | 20 | 0 | | 1 | 1.0 | True | {} | | 3 | m1.medium | 4096 | 40 | 0 | | 2 | 1.0 | True | {} | | 4 | m1.large | 8192 | 80 | 0 | | 4 | 1.0 | True | {} | | 5 | m1.xlarge | 16384 | 160 | 0 | | 8 | 1.0 | True | {} | +----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ Before applying this patch: $ nova list --flavor 10 $ After applying this patch: $ nova list --flavor 10 ERROR: Flavor could not be found (HTTP 422) (Request-ID: req-003d4bbc-f338-4361-9eeb-1cf18073a5a8) $ Changelog of v4: * Change a response code of NotFoundFlavor from 404 to 422. Changelog of v3: * Merge patchset2(change a response of test_get_all_by_flavor) into one patch. * Change a response code of NotFoundFlavor from 400 to 404. Fixes bug 1076863 Change-Id: I45f35ae6a4bf1de734c258ef819fb9b4d7f9681e --- nova/api/openstack/compute/servers.py | 3 +++ nova/compute/api.py | 7 ++----- nova/tests/compute/test_compute.py | 7 ++++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 2c22ccc48..8c59b5092 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -536,6 +536,9 @@ class Controller(wsgi.Controller): except exception.MarkerNotFound as e: msg = _('marker [%s] not found') % marker raise webob.exc.HTTPBadRequest(explanation=msg) + except exception.FlavorNotFound as e: + msg = _("Flavor could not be found") + raise webob.exc.HTTPUnprocessableEntity(explanation=msg) if is_detail: self._add_instance_faults(context, instance_list) diff --git a/nova/compute/api.py b/nova/compute/api.py index 6da04c97b..948cf3fea 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1122,11 +1122,8 @@ class API(base.Base): filters = {} def _remap_flavor_filter(flavor_id): - try: - instance_type = instance_types.get_instance_type_by_flavor_id( - flavor_id) - except exception.FlavorNotFound: - raise ValueError() + instance_type = instance_types.get_instance_type_by_flavor_id( + flavor_id) filters['instance_type_id'] = instance_type['id'] diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index 662a90be4..b7ef65c8e 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -4222,9 +4222,10 @@ class ComputeAPITestCase(BaseTestCase): search_opts={'flavor': 5}) self.assertEqual(len(instances), 0) - # ensure unknown filter maps to an empty list, not an exception - instances = self.compute_api.get_all(c, search_opts={'flavor': 99}) - self.assertEqual(instances, []) + # ensure unknown filter maps to an exception + self.assertRaises(exception.FlavorNotFound, + self.compute_api.get_all, c, + search_opts={'flavor': 99}) instances = self.compute_api.get_all(c, search_opts={'flavor': 3}) self.assertEqual(len(instances), 1) -- cgit