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 4c7f3cb30..abd5581e2 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)