Pull option processing out to service.py

...because it seems silly that we do nearly the same thing in two
different places

Change-Id: Iafafe3c553d00652adb91ceefd0de1479cbcb5da
This commit is contained in:
Tim Burke 2016-05-03 14:18:34 -07:00
parent 5b714f104d
commit c3766319b9
3 changed files with 46 additions and 35 deletions
swiftclient
tests/unit

@ -86,6 +86,12 @@ class SwiftError(Exception):
def process_options(options):
# tolerate sloppy auth_version
if options.get('auth_version') == '3.0':
options['auth_version'] = '3'
elif options.get('auth_version') == '2':
options['auth_version'] = '2.0'
if (not (options.get('auth') and options.get('user')
and options.get('key'))
and options.get('auth_version') != '3'):

@ -36,7 +36,7 @@ from swiftclient import __version__ as client_version
from swiftclient.client import logger_settings as client_logger_settings, \
parse_header_string
from swiftclient.service import SwiftService, SwiftError, \
SwiftUploadObject, get_conn
SwiftUploadObject, get_conn, process_options
from swiftclient.command_helpers import print_account_stats, \
print_container_stats, print_object_stats
@ -1190,40 +1190,8 @@ def parse_args(parser, args, enforce_requires=True):
if args and args[0] == 'tempurl':
return options, args
if options['auth_version'] == '3.0':
# tolerate sloppy auth_version
options['auth_version'] = '3'
if (not (options['auth'] and options['user'] and options['key'])
and options['auth_version'] != '3'):
# Use keystone auth if any of the old-style args are missing
options['auth_version'] = '2.0'
# Use new-style args if old ones not present
if not options['auth'] and options['os_auth_url']:
options['auth'] = options['os_auth_url']
if not options['user'] and options['os_username']:
options['user'] = options['os_username']
if not options['key'] and options['os_password']:
options['key'] = options['os_password']
# Specific OpenStack options
options['os_options'] = {
'user_id': options['os_user_id'],
'user_domain_id': options['os_user_domain_id'],
'user_domain_name': options['os_user_domain_name'],
'tenant_id': options['os_tenant_id'],
'tenant_name': options['os_tenant_name'],
'project_id': options['os_project_id'],
'project_name': options['os_project_name'],
'project_domain_id': options['os_project_domain_id'],
'project_domain_name': options['os_project_domain_name'],
'service_type': options['os_service_type'],
'endpoint_type': options['os_endpoint_type'],
'auth_token': options['os_auth_token'],
'object_storage_url': options['os_storage_url'],
'region_name': options['os_region_name'],
}
# Massage auth version; build out os_options subdict
process_options(options)
if len(args) > 1 and args[0] == "capabilities":
return options, args

@ -1522,6 +1522,43 @@ class TestParsing(TestBase):
swiftclient.shell.main(args)
self._verify_opts(result[0], opts, os_opts, os_opts_dict)
def test_sloppy_versions(self):
os_opts = {"password": "secret",
"username": "user",
"auth_url": "http://example.com:5000/v3",
"identity-api-version": "3.0"}
# check os_identity_api_version is sufficient in place of auth_version
args = _make_args("stat", {}, os_opts, '-')
result = [None, None]
fake_command = self._make_fake_command(result)
with mock.patch.dict(os.environ, {}):
with mock.patch('swiftclient.shell.st_stat', fake_command):
swiftclient.shell.main(args)
expected_opts = {'auth_version': '3'} # NB: not '3.0'
expected_os_opts = {"password": "secret",
"username": "user",
"auth_url": "http://example.com:5000/v3"}
self._verify_opts(result[0], expected_opts, expected_os_opts, {})
os_opts = {"password": "secret",
"username": "user",
"auth_url": "http://example.com:5000/v2.0",
"identity-api-version": "2"}
# check os_identity_api_version is sufficient in place of auth_version
args = _make_args("stat", {}, os_opts, '-')
result = [None, None]
fake_command = self._make_fake_command(result)
with mock.patch.dict(os.environ, {}):
with mock.patch('swiftclient.shell.st_stat', fake_command):
swiftclient.shell.main(args)
expected_opts = {'auth_version': '2.0'} # NB: not '2'
expected_os_opts = {"password": "secret",
"username": "user",
"auth_url": "http://example.com:5000/v2.0"}
self._verify_opts(result[0], expected_opts, expected_os_opts, {})
def test_os_identity_api_version(self):
os_opts = {"password": "secret",
"username": "user",