Merge "Use session when not specified token or endpoint"

This commit is contained in:
Jenkins
2016-02-01 01:48:26 +00:00
committed by Gerrit Code Review
2 changed files with 94 additions and 116 deletions

View File

@@ -349,14 +349,7 @@ class OpenStackImagesShell(object):
ks_session.auth = auth ks_session.auth = auth
return ks_session return ks_session
def _get_endpoint_and_token(self, args): def _get_kwargs_for_create_session(self, args):
endpoint = self._get_image_url(args)
auth_token = args.os_auth_token
auth_req = (hasattr(args, 'func') and
utils.is_authentication_required(args.func))
if auth_req and not (endpoint and auth_token):
if not args.os_username: if not args.os_username:
raise exc.CommandError( raise exc.CommandError(
_("You must provide a username via" _("You must provide a username via"
@@ -428,23 +421,17 @@ class OpenStackImagesShell(object):
'cert': args.os_cert, 'cert': args.os_cert,
'key': args.os_key 'key': args.os_key
} }
ks_session = self._get_keystone_session(**kwargs) return kwargs
auth_token = args.os_auth_token or ks_session.get_token()
endpoint_type = args.os_endpoint_type or 'public'
service_type = args.os_service_type or 'image'
endpoint = args.os_image_url or ks_session.get_endpoint(
service_type=service_type,
interface=endpoint_type,
region_name=args.os_region_name)
return endpoint, auth_token
def _get_versioned_client(self, api_version, args): def _get_versioned_client(self, api_version, args):
endpoint, token = self._get_endpoint_and_token(args) endpoint = self._get_image_url(args)
auth_token = args.os_auth_token
auth_req = (hasattr(args, 'func') and
utils.is_authentication_required(args.func))
if not auth_req or (endpoint and auth_token):
kwargs = { kwargs = {
'token': token, 'token': auth_token,
'insecure': args.insecure, 'insecure': args.insecure,
'timeout': args.timeout, 'timeout': args.timeout,
'cacert': args.os_cacert, 'cacert': args.os_cacert,
@@ -452,8 +439,11 @@ class OpenStackImagesShell(object):
'key': args.os_key, 'key': args.os_key,
'ssl_compression': args.ssl_compression 'ssl_compression': args.ssl_compression
} }
client = glanceclient.Client(api_version, endpoint, **kwargs) else:
return client kwargs = self._get_kwargs_for_create_session(args)
kwargs = {'session': self._get_keystone_session(**kwargs)}
return glanceclient.Client(api_version, endpoint, **kwargs)
def _cache_schemas(self, options, client, home_dir='~/.glanceclient'): def _cache_schemas(self, options, client, home_dir='~/.glanceclient'):
homedir = os.path.expanduser(home_dir) homedir = os.path.expanduser(home_dir)

View File

@@ -153,14 +153,14 @@ class ShellTest(testutils.TestCase):
def test_help(self): def test_help(self):
shell = openstack_shell.OpenStackImagesShell() shell = openstack_shell.OpenStackImagesShell()
argstr = '--os-image-api-version 2 help' argstr = '--os-image-api-version 2 help'
with mock.patch.object(shell, '_get_endpoint_and_token') as et_mock: with mock.patch.object(shell, '_get_keystone_session') as et_mock:
actual = shell.main(argstr.split()) actual = shell.main(argstr.split())
self.assertEqual(0, actual) self.assertEqual(0, actual)
self.assertFalse(et_mock.called) self.assertFalse(et_mock.called)
def test_blank_call(self): def test_blank_call(self):
shell = openstack_shell.OpenStackImagesShell() shell = openstack_shell.OpenStackImagesShell()
with mock.patch.object(shell, '_get_endpoint_and_token') as et_mock: with mock.patch.object(shell, '_get_keystone_session') as et_mock:
actual = shell.main('') actual = shell.main('')
self.assertEqual(0, actual) self.assertEqual(0, actual)
self.assertFalse(et_mock.called) self.assertFalse(et_mock.called)
@@ -172,7 +172,7 @@ class ShellTest(testutils.TestCase):
def test_help_v2_no_schema(self): def test_help_v2_no_schema(self):
shell = openstack_shell.OpenStackImagesShell() shell = openstack_shell.OpenStackImagesShell()
argstr = '--os-image-api-version 2 help image-create' argstr = '--os-image-api-version 2 help image-create'
with mock.patch.object(shell, '_get_endpoint_and_token') as et_mock: with mock.patch.object(shell, '_get_keystone_session') as et_mock:
actual = shell.main(argstr.split()) actual = shell.main(argstr.split())
self.assertEqual(0, actual) self.assertEqual(0, actual)
self.assertNotIn('<unavailable>', actual) self.assertNotIn('<unavailable>', actual)
@@ -275,15 +275,13 @@ class ShellTest(testutils.TestCase):
# authenticate to get the verison list *and* to excuted the command. # authenticate to get the verison list *and* to excuted the command.
# This is not the ideal behavior and it should be fixed in a follow # This is not the ideal behavior and it should be fixed in a follow
# up patch. # up patch.
self._assert_auth_plugin_args()
@mock.patch('glanceclient.v1.client.Client') @mock.patch('glanceclient.v1.client.Client')
def test_auth_plugin_invocation_with_v1(self, v1_client): def test_auth_plugin_invocation_with_v1(self, v1_client):
args = '--os-image-api-version 1 image-list' args = '--os-image-api-version 1 image-list'
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
glance_shell.main(args.split()) glance_shell.main(args.split())
self.assertEqual(1, self.v2_auth.call_count) self.assertEqual(0, self.v2_auth.call_count)
self._assert_auth_plugin_args()
@mock.patch('glanceclient.v2.client.Client') @mock.patch('glanceclient.v2.client.Client')
def test_auth_plugin_invocation_with_v2(self, def test_auth_plugin_invocation_with_v2(self,
@@ -291,8 +289,7 @@ class ShellTest(testutils.TestCase):
args = '--os-image-api-version 2 image-list' args = '--os-image-api-version 2 image-list'
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
glance_shell.main(args.split()) glance_shell.main(args.split())
self.assertEqual(1, self.v2_auth.call_count) self.assertEqual(0, self.v2_auth.call_count)
self._assert_auth_plugin_args()
@mock.patch('glanceclient.v1.client.Client') @mock.patch('glanceclient.v1.client.Client')
def test_auth_plugin_invocation_with_unversioned_auth_url_with_v1( def test_auth_plugin_invocation_with_unversioned_auth_url_with_v1(
@@ -301,7 +298,6 @@ class ShellTest(testutils.TestCase):
DEFAULT_UNVERSIONED_AUTH_URL) DEFAULT_UNVERSIONED_AUTH_URL)
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
glance_shell.main(args.split()) glance_shell.main(args.split())
self._assert_auth_plugin_args()
@mock.patch('glanceclient.v2.client.Client') @mock.patch('glanceclient.v2.client.Client')
@mock.patch.object(openstack_shell.OpenStackImagesShell, '_cache_schemas', @mock.patch.object(openstack_shell.OpenStackImagesShell, '_cache_schemas',
@@ -312,7 +308,6 @@ class ShellTest(testutils.TestCase):
'image-list') % DEFAULT_UNVERSIONED_AUTH_URL 'image-list') % DEFAULT_UNVERSIONED_AUTH_URL
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
glance_shell.main(args.split()) glance_shell.main(args.split())
self._assert_auth_plugin_args()
@mock.patch('glanceclient.Client') @mock.patch('glanceclient.Client')
def test_endpoint_token_no_auth_req(self, mock_client): def test_endpoint_token_no_auth_req(self, mock_client):
@@ -333,22 +328,17 @@ class ShellTest(testutils.TestCase):
glance_shell.main(args) glance_shell.main(args)
self.assertEqual(1, mock_client.call_count) self.assertEqual(1, mock_client.call_count)
@mock.patch('sys.stdin', side_effect=mock.MagicMock)
@mock.patch('getpass.getpass', return_value='password')
@mock.patch('glanceclient.v2.client.Client') @mock.patch('glanceclient.v2.client.Client')
def test_password_prompted_with_v2(self, v2_client, def test_password_prompted_with_v2(self, v2_client):
mock_getpass, mock_stdin):
self.requests.post(self.token_url, exc=requests.ConnectionError) self.requests.post(self.token_url, exc=requests.ConnectionError)
cli2 = mock.MagicMock() cli2 = mock.MagicMock()
v2_client.return_value = cli2 v2_client.return_value = cli2
cli2.http_client.get.return_value = (None, {'versions': []}) cli2.http_client.get.return_value = (None, {'versions': []})
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
self.make_env(exclude='OS_PASSWORD') os.environ['OS_PASSWORD'] = 'password'
self.assertRaises(ks_exc.ConnectionRefused, self.assertRaises(exc.CommunicationError,
glance_shell.main, ['image-list']) glance_shell.main, ['image-list'])
# Make sure we are actually prompted.
mock_getpass.assert_called_once_with('OS Password: ')
@mock.patch('sys.stdin', side_effect=mock.MagicMock) @mock.patch('sys.stdin', side_effect=mock.MagicMock)
@mock.patch('getpass.getpass', side_effect=EOFError) @mock.patch('getpass.getpass', side_effect=EOFError)
@@ -561,16 +551,14 @@ class ShellTestWithKeystoneV3Auth(ShellTest):
args = '--os-image-api-version 1 image-list' args = '--os-image-api-version 1 image-list'
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
glance_shell.main(args.split()) glance_shell.main(args.split())
self.assertEqual(1, self.v3_auth.call_count) self.assertEqual(0, self.v3_auth.call_count)
self._assert_auth_plugin_args()
@mock.patch('glanceclient.v2.client.Client') @mock.patch('glanceclient.v2.client.Client')
def test_auth_plugin_invocation_with_v2(self, v2_client): def test_auth_plugin_invocation_with_v2(self, v2_client):
args = '--os-image-api-version 2 image-list' args = '--os-image-api-version 2 image-list'
glance_shell = openstack_shell.OpenStackImagesShell() glance_shell = openstack_shell.OpenStackImagesShell()
glance_shell.main(args.split()) glance_shell.main(args.split())
self.assertEqual(1, self.v3_auth.call_count) self.assertEqual(0, self.v3_auth.call_count)
self._assert_auth_plugin_args()
@mock.patch('keystoneclient.discover.Discover', @mock.patch('keystoneclient.discover.Discover',
side_effect=ks_exc.ClientException()) side_effect=ks_exc.ClientException())