Merge "Add possibility to provide auth token via CLI"

This commit is contained in:
Jenkins 2016-05-23 23:33:41 +00:00 committed by Gerrit Code Review
commit 03054717d3
2 changed files with 93 additions and 15 deletions

View File

@ -248,6 +248,22 @@ class OpenStackManilaShell(object):
parser.add_argument('--os_region_name',
help=argparse.SUPPRESS)
parser.add_argument('--os-token',
metavar='<token>',
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='<bypass-url>',
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='<service-type>',
help='Defaults to compute for most actions.')
@ -489,16 +505,18 @@ 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,
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.
@ -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]")

View File

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