Merge "Add support for keypair-add command reading public key from stdin"

This commit is contained in:
Jenkins 2015-02-10 17:25:59 +00:00 committed by Gerrit Code Review
commit 78fd2ed0de
2 changed files with 19 additions and 7 deletions

View File

@ -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')

View File

@ -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)