From fdd59e142a69d02123ad5b8522ba0e75c731e02f Mon Sep 17 00:00:00 2001 From: Zhengguang Date: Fri, 18 Apr 2014 15:15:48 +0800 Subject: [PATCH] Fix the incorrect return messages in keypair show and delete When we delete or show a keypair, if the keypair doesn't exist, we'll get "The resource could not be found.(HTTP 404)", this patch will change it to "ERROR: No keypair with a name or ID of 'keypair_name' exists." Change-Id: Ifebd8d2213c327f3d3fdd672207170aebbe1bb40 Closes-Bug: #1307338 --- novaclient/tests/v1_1/fakes.py | 20 ++++++++++++++++---- novaclient/utils.py | 2 +- novaclient/v1_1/keypairs.py | 1 + novaclient/v1_1/shell.py | 9 +++++++-- novaclient/v3/shell.py | 9 +++++++-- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/novaclient/tests/v1_1/fakes.py b/novaclient/tests/v1_1/fakes.py index 983357619..cfd0f24ec 100644 --- a/novaclient/tests/v1_1/fakes.py +++ b/novaclient/tests/v1_1/fakes.py @@ -1013,12 +1013,24 @@ class FakeHTTPClient(base_client.HTTPClient): # Keypairs # def get_os_keypairs_test(self, *kw): - return (200, {}, {'keypair': self.get_os_keypairs()[2]['keypairs'][0]}) + return (200, {}, {'keypair': + self.get_os_keypairs()[2]['keypairs'][0]['keypair']}) def get_os_keypairs(self, *kw): return (200, {}, {"keypairs": [ - {'fingerprint': 'FAKE_KEYPAIR', 'name': 'test'} - ]}) + {"keypair": { + "public_key": "FAKE_SSH_RSA", + "private_key": "FAKE_PRIVATE_KEY", + "user_id": + "81e373b596d6466e99c4896826abaa46", + "name": "test", + "deleted": False, + "created_at": "2014-04-19T02:16:44.000000", + "updated_at": "2014-04-19T10:12:3.000000", + "figerprint": "FAKE_KEYPAIR", + "deleted_at": None, + "id": 4} + }]}) def delete_os_keypairs_test(self, **kw): return (202, {}, None) @@ -1027,7 +1039,7 @@ class FakeHTTPClient(base_client.HTTPClient): assert list(body) == ['keypair'] fakes.assert_has_keys(body['keypair'], required=['name']) - r = {'keypair': self.get_os_keypairs()[2]['keypairs'][0]} + r = {'keypair': self.get_os_keypairs()[2]['keypairs'][0]['keypair']} return (202, {}, r) # diff --git a/novaclient/utils.py b/novaclient/utils.py index c10d5c3b2..6d93f26ae 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -203,7 +203,7 @@ def print_dict(d, dict_property="Property", dict_value="Value", wrap=0): def find_resource(manager, name_or_id, **find_args): """Helper for the _find_* methods.""" - # for str id which is not uuid (for Flavor search currently) + # for str id which is not uuid (for Flavor and Keypair search currently) if getattr(manager, 'is_alphanum_id_allowed', False): try: return manager.get(name_or_id) diff --git a/novaclient/v1_1/keypairs.py b/novaclient/v1_1/keypairs.py index 3a102a43d..96caff618 100644 --- a/novaclient/v1_1/keypairs.py +++ b/novaclient/v1_1/keypairs.py @@ -53,6 +53,7 @@ class Keypair(base.Resource): class KeypairManager(base.ManagerWithFind): resource_class = Keypair keypair_prefix = "os-keypairs" + is_alphanum_id_allowed = True def get(self, keypair): """ diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index fd1feba86..df1e2ac62 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -2432,7 +2432,7 @@ def do_keypair_add(cs, args): @utils.arg('name', metavar='', help=_('Keypair name to delete.')) def do_keypair_delete(cs, args): """Delete keypair given by its name.""" - name = args.name + name = _find_keypair(cs, args.name) cs.keypairs.delete(name) @@ -2455,10 +2455,15 @@ def _print_keypair(keypair): help=_("Name or ID of keypair")) def do_keypair_show(cs, args): """Show details about the given keypair.""" - keypair = cs.keypairs.get(args.keypair) + keypair = _find_keypair(cs, args.keypair) _print_keypair(keypair) +def _find_keypair(cs, keypair): + """Get a keypair by name or ID.""" + return utils.find_resource(cs.keypairs, keypair) + + @utils.arg('--tenant', #nova db searches by project_id dest='tenant', diff --git a/novaclient/v3/shell.py b/novaclient/v3/shell.py index eb5cda1b3..2b8ac5acf 100644 --- a/novaclient/v3/shell.py +++ b/novaclient/v3/shell.py @@ -1973,7 +1973,7 @@ def do_keypair_add(cs, args): @utils.arg('name', metavar='', help='Keypair name to delete.') def do_keypair_delete(cs, args): """Delete keypair given by its name.""" - name = args.name + name = _find_keypair(cs, args.name) cs.keypairs.delete(name) @@ -1996,10 +1996,15 @@ def _print_keypair(keypair): help="Name or ID of keypair") def do_keypair_show(cs, args): """Show details about the given keypair.""" - keypair = cs.keypairs.get(args.keypair) + keypair = _find_keypair(cs, args.keypair) _print_keypair(keypair) +def _find_keypair(cs, keypair): + """Get a keypair by name or ID.""" + return utils.find_resource(cs.keypairs, keypair) + + @utils.arg('--start', metavar='', help='Usage range start date ex 2012-01-20 (default: 4 weeks ago)', default=None)