From 06326e5232bf841be72f64d881c3c5e78ae9ded2 Mon Sep 17 00:00:00 2001 From: Jiri Stransky Date: Tue, 2 Jul 2013 13:34:31 +0200 Subject: [PATCH] Top level argument parser with auth info assertion Adds a top level argument parser with support for help option and options for setting authentication information. Checks that authentication information is supplied. --- tuskarclient/shell.py | 39 ++++++++++++++++-- tuskarclient/utils.py | 26 ++++++++++++ tuskarclient/v1_0/__init__.py | 0 tuskarclient/v1_0/argparsers.py | 73 +++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 tuskarclient/utils.py create mode 100644 tuskarclient/v1_0/__init__.py create mode 100644 tuskarclient/v1_0/argparsers.py diff --git a/tuskarclient/shell.py b/tuskarclient/shell.py index a7f58bb..71f542b 100755 --- a/tuskarclient/shell.py +++ b/tuskarclient/shell.py @@ -19,26 +19,59 @@ from __future__ import print_function import logging import logging.handlers import sys +import tuskarclient.v1_0.argparsers logger = logging.getLogger(__name__) class TuskarShell(object): - def __init__(self, args): - self.args = args + def __init__(self, raw_args): + self.raw_args = raw_args def run(self): - pass + parser = tuskarclient.v1_0.argparsers.create_top_parser() + args = parser.parse_args(self.raw_args) + + if args.help or not self.raw_args: + parser.print_help() + return 0 + + self._ensure_auth_info(args) + + def _ensure_auth_info(self, args): + if not args.os_username: + raise UsageError("You must provide username via either " + "--os-username or env[OS_USERNAME]") + + if not args.os_password: + raise UsageError("You must provide password via either " + "--os-password or env[OS_PASSWORD]") + + if not args.os_tenant_id and not args.os_tenant_name: + raise UsageError("You must provide tenant via either " + "--os-tenant-name or --os-tenant-id or " + "env[OS_TENANT_NAME] or env[OS_TENANT_ID]") + + if not args.os_auth_url: + raise UsageError("You must provide auth URL via either " + "--os-auth-url or env[OS_AUTH_URL]") + + +class UsageError(Exception): + pass def main(): logger.addHandler(logging.StreamHandler(sys.stderr)) try: TuskarShell(sys.argv[1:]).run() + except UsageError as e: + print(e.message, file=sys.stderr) except Exception as e: logger.exception("Exiting due to an error:") sys.exit(1) + if __name__ == '__main__': main() diff --git a/tuskarclient/utils.py b/tuskarclient/utils.py new file mode 100644 index 0000000..d0691ec --- /dev/null +++ b/tuskarclient/utils.py @@ -0,0 +1,26 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os + + +def env(*vars, **kwargs): + """Search for the first defined of possibly many env vars + + Returns the first environment variable defined in vars, or + returns the default defined in kwargs. + """ + for v in vars: + value = os.environ.get(v, None) + if value: + return value + return kwargs.get('default', '') diff --git a/tuskarclient/v1_0/__init__.py b/tuskarclient/v1_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tuskarclient/v1_0/argparsers.py b/tuskarclient/v1_0/argparsers.py new file mode 100644 index 0000000..cc4a2eb --- /dev/null +++ b/tuskarclient/v1_0/argparsers.py @@ -0,0 +1,73 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import argparse +import tuskarclient.utils as utils + + +def create_top_parser(): + parser = argparse.ArgumentParser(prog='tuskar', + description='OpenStack Management CLI', + add_help=False + ) + + parser.add_argument('-h', '--help', + action='store_true', + help="Print this help message and exit.", + ) + + parser.add_argument('--os-username', + default=utils.env('OS_USERNAME'), + help='Defaults to env[OS_USERNAME]', + ) + + parser.add_argument('--os_username', + help=argparse.SUPPRESS, + ) + + parser.add_argument('--os-password', + default=utils.env('OS_PASSWORD'), + help='Defaults to env[OS_PASSWORD]', + ) + + parser.add_argument('--os_password', + help=argparse.SUPPRESS, + ) + + parser.add_argument('--os-tenant-id', + default=utils.env('OS_TENANT_ID'), + help='Defaults to env[OS_TENANT_ID]', + ) + + parser.add_argument('--os_tenant_id', + help=argparse.SUPPRESS, + ) + + parser.add_argument('--os-tenant-name', + default=utils.env('OS_TENANT_NAME'), + help='Defaults to env[OS_TENANT_NAME]', + ) + + parser.add_argument('--os_tenant_name', + help=argparse.SUPPRESS, + ) + + parser.add_argument('--os-auth-url', + default=utils.env('OS_AUTH_URL'), + help='Defaults to env[OS_AUTH_URL]', + ) + + parser.add_argument('--os_auth_url', + help=argparse.SUPPRESS, + ) + + return parser