From 999db21e1a5a0944535e0fb0fd2478ab1647d85b Mon Sep 17 00:00:00 2001 From: Nirmal Ranganathan Date: Thu, 2 Feb 2012 14:19:57 -0600 Subject: [PATCH] Throw an user error on creating duplicate keypairs Fixes bug 902162 Change-Id: I1b73943aab338bde90b4d47bc015964e9981af5d --- .mailmap | 3 ++- Authors | 2 +- nova/api/openstack/compute/contrib/keypairs.py | 3 ++- .../api/openstack/compute/contrib/test_keypairs.py | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.mailmap b/.mailmap index e5ced35ebbaf..e5a909861682 100644 --- a/.mailmap +++ b/.mailmap @@ -41,7 +41,8 @@ - + + diff --git a/Authors b/Authors index 7930898b9500..574a34f30853 100644 --- a/Authors +++ b/Authors @@ -123,7 +123,7 @@ Muneyuki Noguchi Nachi Ueno Naveed Massjouni Nikolay Sokolov -Nirmal Ranganathan +Nirmal Ranganathan Ollie Leahy Pádraig Brady Paul Voccio diff --git a/nova/api/openstack/compute/contrib/keypairs.py b/nova/api/openstack/compute/contrib/keypairs.py index 57ac48dbc912..0e8a4bb060af 100644 --- a/nova/api/openstack/compute/contrib/keypairs.py +++ b/nova/api/openstack/compute/contrib/keypairs.py @@ -87,7 +87,8 @@ class KeypairController(object): # NOTE(ja): generation is slow, so shortcut invalid name exception try: db.key_pair_get(context, context.user_id, name) - raise exception.KeyPairExists(key_name=name) + msg = _("Key pair '%s' already exists.") % name + raise webob.exc.HTTPConflict(explanation=msg) except exception.NotFound: pass diff --git a/nova/tests/api/openstack/compute/contrib/test_keypairs.py b/nova/tests/api/openstack/compute/contrib/test_keypairs.py index ff59e174b9c3..fa04e5efbf1e 100644 --- a/nova/tests/api/openstack/compute/contrib/test_keypairs.py +++ b/nova/tests/api/openstack/compute/contrib/test_keypairs.py @@ -46,6 +46,10 @@ def db_key_pair_destroy(context, user_id, name): raise Exception() +def db_key_pair_get(context, user_id, name): + pass + + class KeypairsTest(test.TestCase): def setUp(self): @@ -130,6 +134,16 @@ class KeypairsTest(test.TestCase): self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0) self.assertFalse('private_key' in res_dict['keypair']) + def test_keypair_create_duplicate(self): + self.stubs.Set(db, "key_pair_get", db_key_pair_get) + body = {'keypair': {'name': 'create_duplicate'}} + req = webob.Request.blank('/v2/fake/os-keypairs') + req.method = 'POST' + req.body = json.dumps(body) + req.headers['Content-Type'] = 'application/json' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 409) + def test_keypair_import_bad_key(self): body = { 'keypair': {