Add wrap option to nova credentials for humans

When using Keystone PKI, a token ID can be over 3200 chars long.
Add optional --wrap option to make 'keystone token-get' human readable
By default wrap=64 (to fit in 80 char terminal).  And can be turned off by
setting wrap=0

Fix bug 1131001

Change-Id: I50be7ebb4323ab1bf07af557403f5136b49080a4
This commit is contained in:
Joe Gordon 2013-03-01 23:07:18 +00:00
parent 0e569e1d9b
commit 21dca44202
3 changed files with 10 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import os
import re
import sys
import textwrap
import uuid
import prettytable
@ -167,13 +168,15 @@ def print_list(objs, fields, formatters={}, sortby_index=0):
print(pt.get_string())
def print_dict(d, dict_property="Property"):
def print_dict(d, dict_property="Property", wrap=0):
pt = prettytable.PrettyTable([dict_property, 'Value'], caching=False)
pt.align = 'l'
for k, v in d.iteritems():
# convert dict to str to check length
if isinstance(v, dict):
v = str(v)
if wrap > 0:
v = textwrap.fill(str(v), wrap)
# if value has a newline, add in multiple rows
# e.g. fault with stacktrace
if v and isinstance(v, basestring) and r'\n' in v:

View File

@ -2608,12 +2608,15 @@ def do_endpoints(cs, _args):
utils.print_dict(e['endpoints'][0], e['name'])
@utils.arg('--wrap', dest='wrap', metavar='<integer>', default=64,
help='wrap PKI tokens to a specified length, or 0 to disable')
def do_credentials(cs, _args):
"""Show user credentials returned from auth"""
ensure_service_catalog_present(cs)
catalog = cs.client.service_catalog.catalog
utils.print_dict(catalog['access']['user'], "User Credentials")
utils.print_dict(catalog['access']['token'], "Token")
utils.print_dict(catalog['access']['user'], "User Credentials",
wrap=int(_args.wrap))
utils.print_dict(catalog['access']['token'], "Token", wrap=int(_args.wrap))
@utils.arg('server', metavar='<server>', help='Name or ID of server.')

View File

@ -102,7 +102,7 @@ class ShellTest(utils.TestCase):
def test_bash_completion(self):
stdout, stderr = self.shell('bash-completion')
# just check we have some output
required = ['--matching help secgroup-delete-rule --priority']
required = ['--matching --wrap help secgroup-delete-rule --priority']
for r in required:
self.assertThat((stdout + stderr),
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))