From 45000677ec6c6c3e9d22aa2f1650a3458a67883a Mon Sep 17 00:00:00 2001 From: Natsuki Maruyama Date: Sun, 30 Nov 2014 20:21:03 +0900 Subject: [PATCH] Add support for keypair-add command reading public key from stdin If given filename is `-` for `--pub-key` option, reads public key from stdin. $ cat id_rsa.pub | nova keypair-add --pub-key - keyname Change-Id: Ib6dfe5a7b08d588868a923defb9ddd15fc28e01f Closes-Bug: #1333476 --- novaclient/tests/unit/v2/test_shell.py | 8 ++++++++ novaclient/v2/shell.py | 18 +++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/novaclient/tests/unit/v2/test_shell.py b/novaclient/tests/unit/v2/test_shell.py index ffa3587d7..624f41d69 100644 --- a/novaclient/tests/unit/v2/test_shell.py +++ b/novaclient/tests/unit/v2/test_shell.py @@ -2304,6 +2304,14 @@ class ShellTest(utils.TestCase): 'POST', '/os-keypairs', { 'keypair': {'public_key': 'FAKE_PUBLIC_KEY', 'name': 'test'}}) + def test_keypair_stdin(self): + with mock.patch('sys.stdin', six.StringIO('FAKE_PUBLIC_KEY')): + self.run_command('keypair-add --pub-key - test') + self.assert_called( + 'POST', '/os-keypairs', { + 'keypair': + {'public_key': 'FAKE_PUBLIC_KEY', 'name': 'test'}}) + def test_keypair_list(self): self.run_command('keypair-list') self.assert_called('GET', '/os-keypairs') diff --git a/novaclient/v2/shell.py b/novaclient/v2/shell.py index 190d07fe3..736f1f36e 100644 --- a/novaclient/v2/shell.py +++ b/novaclient/v2/shell.py @@ -2865,13 +2865,17 @@ def do_keypair_add(cs, args): pub_key = args.pub_key if pub_key: - try: - with open(os.path.expanduser(pub_key)) as f: - pub_key = f.read() - except IOError as e: - raise exceptions.CommandError(_("Can't open or read '%(key)s': " - "%(exc)s") % {'key': pub_key, - 'exc': e}) + if pub_key == '-': + pub_key = sys.stdin.read() + else: + try: + with open(os.path.expanduser(pub_key)) as f: + pub_key = f.read() + except IOError as e: + raise exceptions.CommandError( + _("Can't open or read '%(key)s': %(exc)s") + % {'key': pub_key, 'exc': e} + ) keypair = cs.keypairs.create(name, pub_key)