From 1767e0ae9595003c22facc86e590b7c9b1ed6a75 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Tue, 21 Feb 2012 21:10:57 -0800 Subject: Return 40x for flavor.create duplicate. * Fixes bug 938194 * Return error 409 instead of 400 Change-Id: Ia3c597dc996d88e7026f76d1104058259c96301c --- nova/api/openstack/compute/contrib/flavormanage.py | 9 +++++--- .../compute/contrib/test_flavor_manage.py | 27 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/nova/api/openstack/compute/contrib/flavormanage.py b/nova/api/openstack/compute/contrib/flavormanage.py index 3fe9bc669..547760e94 100644 --- a/nova/api/openstack/compute/contrib/flavormanage.py +++ b/nova/api/openstack/compute/contrib/flavormanage.py @@ -66,9 +66,12 @@ class FlavorManageController(wsgi.Controller): swap = vals.get('swap') rxtx_factor = vals.get('rxtx_factor') - flavor = instance_types.create(name, memory_mb, vcpus, - root_gb, ephemeral_gb, flavorid, - swap, rxtx_factor) + try: + flavor = instance_types.create(name, memory_mb, vcpus, + root_gb, ephemeral_gb, flavorid, + swap, rxtx_factor) + except exception.InstanceTypeExists as err: + raise webob.exc.HTTPConflict(explanation=str(err)) return self._view_builder.show(req, flavor) diff --git a/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py b/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py index e825aa4bd..8a18c1ab2 100644 --- a/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py +++ b/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py @@ -115,3 +115,30 @@ class FlavorManageTest(test.TestCase): body = json.loads(res.body) for key in expected["flavor"]: self.assertEquals(body["flavor"][key], expected["flavor"][key]) + + def test_instance_type_exists_exception_returns_409(self): + expected = { + "flavor": { + "name": "test", + "ram": 512, + "vcpus": 2, + "disk": 1, + "OS-FLV-EXT-DATA:ephemeral": 1, + "id": 1235, + "swap": 512, + "rxtx_factor": 1, + } + } + + def fake_create(name, memory_mb, vcpus, root_gb, ephemeral_gb, + flavorid, swap, rxtx_factor): + raise exception.InstanceTypeExists() + + self.stubs.Set(instance_types, "create", fake_create) + url = '/v2/fake/flavors' + req = webob.Request.blank(url) + req.headers['Content-Type'] = 'application/json' + req.method = 'POST' + req.body = json.dumps(expected) + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 409) -- cgit