From 89c2f286a6246606335f31abf1a6289e89b427f2 Mon Sep 17 00:00:00 2001 From: Elvin Tubillara Date: Thu, 17 Sep 2015 02:30:20 -0500 Subject: [PATCH] enable barbican help without authentication This fix enables 'barbican help' and 'barbican complete' to not need authentication. This commit also enables deferred_help so that the following command can be ran: 'barbican secret list --help' (the help for the particular command will be shown). The code changes are similar to the python-openstackclient usage of the cliff library. The current implementation of the barbican client authenticates before checking the type of command. This early authentication causes the client to fail with 'barbican help'. This code change will check if the command requires authentication and creates the client. Change-Id: If335ef0373c75b145dc696d8501153da59fff1b7 Closes-Bug: #1488934 --- barbicanclient/barbican.py | 25 +++++++++++++++++++------ barbicanclient/tests/test_barbican.py | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/barbicanclient/barbican.py b/barbicanclient/barbican.py index 725e6b25..e8e49dfd 100644 --- a/barbicanclient/barbican.py +++ b/barbicanclient/barbican.py @@ -20,7 +20,10 @@ Command-line interface to the Barbican API. import sys from cliff import app +from cliff import command from cliff import commandmanager +from cliff import complete +from cliff import help from keystoneclient.auth.identity import v3 from keystoneclient.auth.identity import v2 from keystoneclient import session @@ -37,10 +40,20 @@ class Barbican(app.App): """Barbican command line interface.""" def __init__(self, **kwargs): + self.client = None + + # Patch command.Command to add a default auth_required = True + command.Command.auth_required = True + + # Some commands do not need authentication + help.HelpCommand.auth_required = False + complete.CompleteCommand.auth_required = False + super(Barbican, self).__init__( description=__doc__.strip(), version=version.__version__, command_manager=commandmanager.CommandManager('barbican.client'), + deferred_help=True, **kwargs ) @@ -290,14 +303,14 @@ class Barbican(app.App): session.Session.register_cli_options(parser) return parser - def initialize_app(self, argv): - """Initializes the application. - Checks if the minimal parameters are provided and creates the client - interface. + def prepare_to_run_command(self, cmd): + """Prepares to run the command + Checks if the minimal parameters are provided and creates the + client interface. This is inherited from the framework. """ - args = self.options - self.client = self.create_client(args) + if cmd.auth_required: + self.client = self.create_client(self.options) def run(self, argv): # If no arguments are provided, usage is displayed diff --git a/barbicanclient/tests/test_barbican.py b/barbicanclient/tests/test_barbican.py index b6aa4030..6b3d92cd 100644 --- a/barbicanclient/tests/test_barbican.py +++ b/barbicanclient/tests/test_barbican.py @@ -48,7 +48,7 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): return client def test_should_show_usage_with_help_flag(self): - e = self.assertRaises(SystemExit, self.parser.parse_known_args, ['-h']) + e = self.assertRaises(SystemExit, self.barbican.run, ['-h']) self.assertEqual(0, e.code) self.assertIn('usage', self.captured_stdout.getvalue())