Assisted snapshot: use Keystone Session with Nova Client

The Nova client has trouble dealing with Keystone V3 or unversioned
Keystone endpoint, when instantiated without a Keystone Session. Thus
we can't instantiate Nova client directly as cinder.compute.nova
does currently.

Keystone V3 and unversioned service catalog is the future (see [1]
and [2]) so we should adapt.

Please read comments #5, #6 and #11 of the related bug report.

[1] https://review.openstack.org/#/c/271508/
[2] https://review.openstack.org/#/c/302480/

Change-Id: Ic790955e677c5dfa47680bf619c57fba3deeee20
Related-Bug: #1522402
This commit is contained in:
Jordan Pittier 2016-04-26 15:51:04 +02:00
parent fb732eeb8d
commit 7570482c53
3 changed files with 62 additions and 26 deletions

View File

@ -16,7 +16,8 @@
Handles all requests to Nova.
"""
import keystoneauth1.loading
import keystoneauth1.session
from novaclient import client as nova_client
from novaclient import exceptions as nova_exceptions
from novaclient import service_catalog
@ -134,11 +135,17 @@ def novaclient(context, admin_endpoint=False, privileged_user=False,
LOG.debug('Nova client connection created using URL: %s', url)
# Now that we have the correct auth_url, username, password and
# project_name, let's build a Keystone session.
loader = keystoneauth1.loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=url,
username=context.user_id,
password=context.auth_token,
project_name=context.project_name)
keystone_session = keystoneauth1.session.Session(auth=auth)
c = nova_client.Client(NOVA_API_VERSION,
context.user_id,
context.auth_token,
context.project_name,
auth_url=url,
session=keystone_session,
insecure=CONF.nova_api_insecure,
timeout=timeout,
region_name=CONF.os_region_name,

View File

@ -39,55 +39,86 @@ class NovaClientTestCase(test.TestCase):
self.override_config('os_privileged_user_password', 'strongpassword')
@mock.patch('novaclient.client.Client')
def test_nova_client_regular(self, p_client):
@mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_regular(self, p_session, p_plugin_loader, p_client):
nova.novaclient(self.ctx)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
password='token', project_name=None, username='regularuser'
)
p_client.assert_called_once_with(
nova.NOVA_API_VERSION,
'regularuser', 'token', None, region_name=None,
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
session=p_session.return_value, region_name=None,
insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client')
def test_nova_client_admin_endpoint(self, p_client):
@mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_admin_endpoint(self, p_session, p_plugin_loader,
p_client):
nova.novaclient(self.ctx, admin_endpoint=True)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://novaadmhost:4778/v2/e3f0833dc08b4cea',
password='token', project_name=None, username='regularuser'
)
p_client.assert_called_once_with(
nova.NOVA_API_VERSION,
'regularuser', 'token', None, region_name=None,
auth_url='http://novaadmhost:4778/v2/e3f0833dc08b4cea',
session=p_session.return_value, region_name=None,
insecure=False, endpoint_type='adminURL', cacert=None,
timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client')
def test_nova_client_privileged_user(self, p_client):
@mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_privileged_user(self, p_session, p_plugin_loader,
p_client):
nova.novaclient(self.ctx, privileged_user=True)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://keystonehost:5000/v2.0',
password='strongpassword', project_name=None, username='adminuser'
)
p_client.assert_called_once_with(
nova.NOVA_API_VERSION,
'adminuser', 'strongpassword', None, region_name=None,
auth_url='http://keystonehost:5000/v2.0',
session=p_session.return_value, region_name=None,
insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client')
def test_nova_client_privileged_user_custom_auth_url(self, p_client):
@mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_privileged_user_custom_auth_url(self, p_session,
p_plugin_loader,
p_client):
self.override_config('os_privileged_user_auth_url',
'http://privatekeystonehost:5000/v2.0')
nova.novaclient(self.ctx, privileged_user=True)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://privatekeystonehost:5000/v2.0',
password='strongpassword', project_name=None, username='adminuser'
)
p_client.assert_called_once_with(
nova.NOVA_API_VERSION,
'adminuser', 'strongpassword', None, region_name=None,
auth_url='http://privatekeystonehost:5000/v2.0',
session=p_session.return_value, region_name=None,
insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions)
@mock.patch('novaclient.client.Client')
def test_nova_client_custom_region(self, p_client):
@mock.patch('keystoneauth1.loading.get_plugin_loader')
@mock.patch('keystoneauth1.session.Session')
def test_nova_client_custom_region(self, p_session, p_plugin_loader,
p_client):
self.override_config('os_region_name', 'farfaraway')
nova.novaclient(self.ctx)
p_plugin_loader.return_value.load_from_options.assert_called_once_with(
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
password='token', project_name=None, username='regularuser'
)
p_client.assert_called_once_with(
nova.NOVA_API_VERSION,
'regularuser', 'token', None, region_name='farfaraway',
auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
session=p_session.return_value, region_name='farfaraway',
insecure=False, endpoint_type='publicURL', cacert=None,
timeout=None, extensions=nova.nova_extensions)

View File

@ -996,13 +996,11 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase):
filter_properties = {'context': self.context, 'size': 100}
self.assertTrue(filt_cls.host_passes(host, filter_properties))
@mock.patch('novaclient.client.discover_extensions')
@mock.patch('requests.request')
def test_nova_timeout(self, _mock_request, fake_extensions):
@mock.patch('cinder.compute.nova.novaclient')
def test_nova_timeout(self, mock_novaclient):
# Simulate a HTTP timeout
_mock_request.side_effect = request_exceptions.Timeout
fake_extensions.return_value = (
fakes.FakeNovaClient().list_extensions.show_all())
mock_show_all = mock_novaclient.return_value.list_extensions.show_all
mock_show_all.side_effect = request_exceptions.Timeout
filt_cls = self.class_map['InstanceLocalityFilter']()
host = fakes.FakeHostState('host1', {})