From 21dca44202a3f0899a997ee8b1678017052c2731 Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Fri, 1 Mar 2013 23:07:18 +0000 Subject: [PATCH] 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 --- novaclient/utils.py | 5 ++++- novaclient/v1_1/shell.py | 7 +++++-- tests/test_shell.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/novaclient/utils.py b/novaclient/utils.py index 8c63452c7..019ddd116 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -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: diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index e473da8ce..4c85e48c6 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -2608,12 +2608,15 @@ def do_endpoints(cs, _args): utils.print_dict(e['endpoints'][0], e['name']) +@utils.arg('--wrap', dest='wrap', metavar='', 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='', help='Name or ID of server.') diff --git a/tests/test_shell.py b/tests/test_shell.py index 0ab76b6ac..4a8c3391a 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -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))