Skip to content

Commit

Permalink
Throw an user error on creating duplicate keypairs
Browse files Browse the repository at this point in the history
Fixes bug 902162

Change-Id: I1b73943aab338bde90b4d47bc015964e9981af5d
  • Loading branch information
rnirmal committed Feb 2, 2012
1 parent ed51688 commit 999db21
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .mailmap
Expand Up @@ -41,7 +41,8 @@
<matt.dietz@rackspace.com> <mdietz@openstack>
<mordred@inaugust.com> <mordred@hudson>
<naveedm9@gmail.com> <naveed.massjouni@rackspace.com>
<nirmal.ranganathan@rackspace.com> <nirmal.ranganathan@rackspace.coom>
<rnirmal@gmail.com> <nirmal.ranganathan@rackspace.com>
<rnirmal@gmail.com> <nirmal.ranganathan@rackspace.coom>
<paul@openstack.org> <paul.voccio@rackspace.com>
<paul@openstack.org> <pvoccio@castor.local>
<paul@openstack.org> <paul@substation9.com>
Expand Down
2 changes: 1 addition & 1 deletion Authors
Expand Up @@ -123,7 +123,7 @@ Muneyuki Noguchi <noguchimn@nttdata.co.jp>
Nachi Ueno <ueno.nachi@lab.ntt.co.jp>
Naveed Massjouni <naveedm9@gmail.com>
Nikolay Sokolov <nsokolov@griddynamics.com>
Nirmal Ranganathan <nirmal.ranganathan@rackspace.com>
Nirmal Ranganathan <rnirmal@gmail.com>
Ollie Leahy <oliver.leahy@hp.com>
Pádraig Brady <pbrady@redhat.com>
Paul Voccio <paul@openstack.org>
Expand Down
3 changes: 2 additions & 1 deletion nova/api/openstack/compute/contrib/keypairs.py
Expand Up @@ -87,7 +87,8 @@ def create(self, req, body):
# 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

Expand Down
14 changes: 14 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_keypairs.py
Expand Up @@ -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):
Expand Down Expand Up @@ -130,6 +134,16 @@ def test_keypair_import(self):
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': {
Expand Down

0 comments on commit 999db21

Please sign in to comment.