From 448dd2ccb088da1ca2ca8db1a6bf51aa2d0be903 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Fri, 11 Jan 2013 11:08:44 +0000 Subject: [PATCH] Allow environment variables Allow use environment variables for auth settings in place of options. These are compatible with python-novaclient's environment variables. Fixes bug #1098259 Also add set the service_type and add an option to change it Fixes bug #1089274 Change-Id: I20b5714e19a34d1a29eb33002e32e9dafefe5e9d --- doc/command.rst | 20 +++++++++++++++----- libraclient/client.py | 4 +--- libraclient/clientoptions.py | 14 ++++++++++++++ libraclient/libraapi.py | 21 ++++++++++----------- tests/test_lbaas_client.py | 15 ++++++++++++++- 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/doc/command.rst b/doc/command.rst index 802b76e..3429398 100644 --- a/doc/command.rst +++ b/doc/command.rst @@ -36,25 +36,35 @@ Global Options URL to use as an endpoint instead of the one specified by the Service Catalog +.. option:: --service_type + + Alternative service type to use for your cloud provider (default is + 'hpext:lbaas') + .. option:: --os_auth_url - The OpenStack authentication URL + The OpenStack authentication URL. Default is ``OS_AUTH_URL`` or + ``LIBRA_URL`` environment variables .. option:: --os_username - The user name to use for authentication + The user name to use for authentication. Default is ``OS_USERNAME`` or + ``LIBRA_USERNAME`` environment variables .. option:: --os_password - The password to use for authentication + The password to use for authentication. Default is ``OS_PASSWORD`` or + ``LIBRA_PASSWORD`` environment variables .. option:: --os_tenant_name - The tenant to authenticate to + The tenant to authenticate to. Default is ``OS_TENANT_NAME`` or + ``LIBRA_PROJECT_ID`` environment variables .. option:: --os_region_name - The region the load balancer is located + The region the load balancer is located. Default is ``OS_REGION_NAME`` or + ``LIBRA_REGION_NAME`` environment variables .. _libra_client-commands: diff --git a/libraclient/client.py b/libraclient/client.py index 737435e..bda5a90 100644 --- a/libraclient/client.py +++ b/libraclient/client.py @@ -21,9 +21,7 @@ def main(): options = ClientOptions() args = options.run() - api = LibraAPI(args.os_username, args.os_password, args.os_tenant_name, - args.os_auth_url, args.os_region_name, args.insecure, - args.debug, args.bypass_url) + api = LibraAPI(args) cmd = args.command.replace('-', '_') method = getattr(api, '{cmd}_lb'.format(cmd=cmd)) diff --git a/libraclient/clientoptions.py b/libraclient/clientoptions.py index ac5f16e..c8856a0 100644 --- a/libraclient/clientoptions.py +++ b/libraclient/clientoptions.py @@ -13,6 +13,9 @@ # under the License. import argparse +from novaclient import utils + +LIBRA_DEFAULT_SERVICE_TYPE = 'hpext:lbaas' class ClientOptions(object): @@ -24,30 +27,35 @@ class ClientOptions(object): '--os_auth_url', metavar='', required=True, + default=utils.env('OS_AUTH_URL', 'LIBRA_URL'), help='Authentication URL' ) self.options.add_argument( '--os_username', metavar='', required=True, + default=utils.env('OS_USERNAME', 'LIBRA_USERNAME'), help='Authentication username' ) self.options.add_argument( '--os_password', metavar='', required=True, + default=utils.env('OS_PASSWORD', 'LIBRA_PASSWORD'), help='Authentication password' ) self.options.add_argument( '--os_tenant_name', metavar='', required=True, + default=utils.env('OS_TENANT_NAME', 'LIBRA_PROJECT_ID'), help='Authentication tenant' ) self.options.add_argument( '--os_region_name', metavar='', required=True, + default=utils.env('OS_REGION_NAME', 'LIBRAL_REGION_NAME'), help='Authentication region' ) self.options.add_argument( @@ -64,6 +72,12 @@ class ClientOptions(object): '--bypass_url', help='Use this API endpoint instead of the Service Catalog' ) + self.options.add_argument( + '--service_type', + default=LIBRA_DEFAULT_SERVICE_TYPE, + help='Default ' + LIBRA_DEFAULT_SERVICE_TYPE + ) + subparsers = self.options.add_subparsers( metavar='', dest='command' ) diff --git a/libraclient/libraapi.py b/libraclient/libraapi.py index 28dddeb..dd6cfa5 100644 --- a/libraclient/libraapi.py +++ b/libraclient/libraapi.py @@ -51,18 +51,17 @@ novaclient.exceptions.from_response = from_response class LibraAPI(object): - def __init__(self, username, password, tenant, auth_url, region, - insecure, debug, bypass_url): + def __init__(self, args): self.nova = client.HTTPClient( - username, - password, - tenant, - auth_url, - region_name=region, - service_type='compute', - http_log_debug=debug, - insecure=insecure, - bypass_url=bypass_url + args.os_username, + args.os_password, + args.os_tenant_name, + args.os_auth_url, + region_name=args.os_region_name, + service_type=args.service_type, + http_log_debug=args.debug, + insecure=args.insecure, + bypass_url=args.bypass_url ) def limits_lb(self, args): diff --git a/tests/test_lbaas_client.py b/tests/test_lbaas_client.py index 79bb240..61da6da 100644 --- a/tests/test_lbaas_client.py +++ b/tests/test_lbaas_client.py @@ -30,12 +30,25 @@ class DummyModifyArgs(object): self.name = 'a-modified-loadbalancer' self.algorithm = 'LEAST_CONNECTIONS' +class MockLibraArgs(object): + def __init__(self, username, password, tenant, auth_url, region): + self.os_username=username + self.os_password=password + self.os_tenant_name=tenant + self.os_auth_url=auth_url + self.os_region_name=region + self.service_type='compute' + self.debug=False + self.insecure=False + self.bypass_url=None + class MockLibraAPI(LibraAPI): """ Used to capture data that would be sent to the API server """ def __init__(self, username, password, tenant, auth_url, region): self.postdata = None self.putdata = None - return super(MockLibraAPI, self).__init__(username, password, tenant, auth_url, region, False, False, None) + args=MockLibraArgs(username, password, tenant, auth_url, region) + return super(MockLibraAPI, self).__init__(args) def _post(self, url, **kwargs): """ Store the post data and execute as normal """ self.postdata = kwargs['body']