From 5a3ef94c5b7528fd2062b5803d51e341497c1d0c Mon Sep 17 00:00:00 2001 From: Valeriy Ponomaryov Date: Mon, 16 May 2016 19:28:46 +0300 Subject: [PATCH] Add possibility to provide auth token via CLI Python part of Manilaclient does support auth by token, but CLI part does not. Therefore, add two new optional args with related env vars: --os-token [OS_TOKEN] --bypass-url [MANILACLIENT_BYPASS_URL] Also, disable checks for other auth data if these two are provided. Change-Id: I567294dfb61e702410c3f0fadc15a04667075434 Closes-Bug: #1572095 --- manilaclient/shell.py | 45 ++++++++++++------- manilaclient/tests/unit/test_shell.py | 63 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 15 deletions(-) diff --git a/manilaclient/shell.py b/manilaclient/shell.py index d7ce4db7b..9450e4db4 100644 --- a/manilaclient/shell.py +++ b/manilaclient/shell.py @@ -248,6 +248,22 @@ class OpenStackManilaShell(object): parser.add_argument('--os_region_name', help=argparse.SUPPRESS) + parser.add_argument('--os-token', + metavar='', + default=cliutils.env('OS_TOKEN'), + help='Defaults to env[OS_TOKEN].') + parser.add_argument('--os_token', + help=argparse.SUPPRESS) + + parser.add_argument('--bypass-url', + metavar='', + default=cliutils.env('MANILACLIENT_BYPASS_URL'), + help=("Use this API endpoint instead of the " + "Service Catalog. Defaults to " + "env[MANILACLIENT_BYPASS_URL].")) + parser.add_argument('--bypass_url', + help=argparse.SUPPRESS) + parser.add_argument('--service-type', metavar='', help='Defaults to compute for most actions.') @@ -489,17 +505,19 @@ class OpenStackManilaShell(object): project_domain_id=args.os_project_domain_id, project_domain_name=args.os_project_domain_name, cert=args.os_cert, + input_auth_token=args.os_token, + service_catalog_url=args.bypass_url, ) # Handle deprecated parameters if args.share_service_name: client_args['share_service_name'] = args.share_service_name - self._validate_required_options(args.os_tenant_name, - args.os_tenant_id, - args.os_project_name, - args.os_project_id, - client_args['auth_url']) + self._validate_required_options( + args.os_tenant_name, args.os_tenant_id, + args.os_project_name, args.os_project_id, + args.os_token, args.bypass_url, + client_args['auth_url']) # This client is needed to discover the server api version. temp_client = client.Client(manilaclient.API_MAX_VERSION, @@ -564,15 +582,12 @@ class OpenStackManilaShell(object): options.os_share_api_version) return api_version - def _validate_required_options(self, - os_tenant_name, - os_tenant_id, - os_project_name, - os_project_id, - os_auth_url): - - if not (os_tenant_name or os_tenant_id or os_project_name or - os_project_id): + def _validate_required_options(self, tenant_name, tenant_id, + project_name, project_id, + token, service_catalog_url, auth_url): + if token and service_catalog_url: + return + if not (tenant_name or tenant_id or project_name or project_id): raise exc.CommandError( "You must provide a tenant_name, tenant_id, " "project_id or project_name (with " @@ -585,7 +600,7 @@ class OpenStackManilaShell(object): "--os-project-domain-name (env[OS_PROJECT_DOMAIN_NAME])." ) - if not os_auth_url: + if not auth_url: raise exc.CommandError( "You must provide an auth url " "via either --os-auth-url or env[OS_AUTH_URL]") diff --git a/manilaclient/tests/unit/test_shell.py b/manilaclient/tests/unit/test_shell.py index 3137c813a..b03876363 100644 --- a/manilaclient/tests/unit/test_shell.py +++ b/manilaclient/tests/unit/test_shell.py @@ -127,6 +127,69 @@ class OpenstackManilaShellTest(utils.TestCase): project_domain_id=env_vars['OS_PROJECT_DOMAIN_ID'], project_domain_name=env_vars['OS_PROJECT_DOMAIN_NAME'], cert=env_vars['OS_CERT'], + input_auth_token='', + service_catalog_url='', + ) + + @ddt.data( + {"env_vars": {"MANILACLIENT_BYPASS_URL": "http://foo.url", + "OS_TOKEN": "foo_token"}, + "kwargs": {"--os-token": "bar_token", + "--bypass-url": "http://bar.url"}, + "expected": {"input_auth_token": "bar_token", + "service_catalog_url": "http://bar.url"}}, + {"env_vars": {}, + "kwargs": {"--os-token": "bar_token", + "--bypass-url": "http://bar.url"}, + "expected": {"input_auth_token": "bar_token", + "service_catalog_url": "http://bar.url"}}, + {"env_vars": {"MANILACLIENT_BYPASS_URL": "http://foo.url", + "OS_TOKEN": "foo_token"}, + "kwargs": {}, + "expected": {"input_auth_token": "foo_token", + "service_catalog_url": "http://foo.url"}}, + {"env_vars": {"OS_TOKEN": "foo_token"}, + "kwargs": {"--bypass-url": "http://bar.url"}, + "expected": {"input_auth_token": "foo_token", + "service_catalog_url": "http://bar.url"}}, + ) + @ddt.unpack + def test_main_success_with_token(self, env_vars, kwargs, expected): + self.set_env_vars(env_vars) + with mock.patch.object(shell, "client") as mock_client: + cmd = "" + for k, v in kwargs.items(): + cmd += "%s=%s " % (k, v) + cmd += "list" + + self.shell(cmd) + + mock_client.Client.assert_called_with( + manilaclient.API_MAX_VERSION, + username="", + password="", + project_name="", + auth_url="", + insecure=False, + region_name="", + tenant_id="", + endpoint_type="publicURL", + extensions=mock.ANY, + service_type=constants.V1_SERVICE_TYPE, + service_name="", + retries=0, + http_log_debug=False, + cacert=None, + use_keyring=False, + force_new_token=False, + user_id="", + user_domain_id="", + user_domain_name="", + project_domain_id="", + project_domain_name="", + cert="", + input_auth_token=expected["input_auth_token"], + service_catalog_url=expected["service_catalog_url"], ) def test_help_unknown_command(self):