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.
This commit is contained in:
Jiri Stransky 2013-07-02 13:34:31 +02:00
parent b6d75089f6
commit 06326e5232
4 changed files with 135 additions and 3 deletions

View File

@ -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()

26
tuskarclient/utils.py Normal file
View File

@ -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', '')

View File

View File

@ -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