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
This commit is contained in:
parent
578390ee7e
commit
45000677ec
@ -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')
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user