summaryrefslogtreecommitdiff
path: root/openstackclient/compute
diff options
context:
space:
mode:
authorRui Chen <chenrui.momo@gmail.com>2017-02-27 14:35:05 +0800
committerRui Chen <chenrui.momo@gmail.com>2017-03-06 08:34:28 +0000
commitdee22d8faa0c8a0da1d6ff62c0997c2cc770b759 (patch)
treee6463efc79b4abd27170c96233085eddc5b91419 /openstackclient/compute
parent69b7b9b0592d3699a02bb8d17539c20749281b8d (diff)
downloadpython-openstackclient-dee22d8faa0c8a0da1d6ff62c0997c2cc770b759.tar.gz
Add "--private-key" option for "keypair create"
Aim to specify the private key file to save when keypair is created. That is a convenient way to save private key in OSC interactive mode, avoid to copy CLI output, then paste it into file. Change-Id: I119d2f2a3323d17ecbe3de4e27f35e1ceef6e0a5 Closes-Bug: #1549410
Diffstat (limited to 'openstackclient/compute')
-rw-r--r--openstackclient/compute/v2/keypair.py31
1 files changed, 28 insertions, 3 deletions
diff --git a/openstackclient/compute/v2/keypair.py b/openstackclient/compute/v2/keypair.py
index 2a8524d6..851cced0 100644
--- a/openstackclient/compute/v2/keypair.py
+++ b/openstackclient/compute/v2/keypair.py
@@ -41,12 +41,19 @@ class CreateKeypair(command.ShowOne):
metavar='<name>',
help=_("New public or private key name")
)
- parser.add_argument(
+ key_group = parser.add_mutually_exclusive_group()
+ key_group.add_argument(
'--public-key',
metavar='<file>',
help=_("Filename for public key to add. If not used, "
"creates a private key.")
)
+ key_group.add_argument(
+ '--private-key',
+ metavar='<file>',
+ help=_("Filename for private key to save. If not used, "
+ "print private key in console.")
+ )
return parser
def take_action(self, parsed_args):
@@ -69,13 +76,31 @@ class CreateKeypair(command.ShowOne):
public_key=public_key,
)
+ private_key = parsed_args.private_key
+ # Save private key into specified file
+ if private_key:
+ try:
+ with io.open(
+ os.path.expanduser(parsed_args.private_key), 'w+'
+ ) as p:
+ p.write(keypair.private_key)
+ except IOError as e:
+ msg = _("Key file %(private_key)s can not be saved: "
+ "%(exception)s")
+ raise exceptions.CommandError(
+ msg % {"private_key": parsed_args.private_key,
+ "exception": e}
+ )
# NOTE(dtroyer): how do we want to handle the display of the private
# key when it needs to be communicated back to the user
# For now, duplicate nova keypair-add command output
info = {}
- if public_key:
+ if public_key or private_key:
info.update(keypair._info)
- del info['public_key']
+ if 'public_key' in info:
+ del info['public_key']
+ if 'private_key' in info:
+ del info['private_key']
return zip(*sorted(six.iteritems(info)))
else:
sys.stdout.write(keypair.private_key)