[stable-only] Make ironicclient initialization future-proof

At some point ironicclient breaks the way it's initialized in stable/stein
and older. While these versions are excluded in upper-constraints, users
still may get broken. The new way to initialize the client works in
both versions, just use it.

This is a partial backport of 40450dc4b5.

Change-Id: I6e47f2178ab6a25711e05205a322895e09786578
This commit is contained in:
Dmitry Tantsur 2020-04-09 17:18:07 +02:00
parent c7a4ec8a3b
commit 72510ea935
3 changed files with 21 additions and 11 deletions

View File

@ -123,6 +123,7 @@ def get_client(token=None,
IRONIC_SESSION = keystone.get_session('ironic')
args = {
'session': IRONIC_SESSION,
'os_ironic_api_version': api_version,
'max_retries': CONF.ironic.max_retries,
'retry_interval': CONF.ironic.retry_interval}
@ -130,11 +131,8 @@ def get_client(token=None,
adapter_opts = dict()
# TODO(pas-ha) use service auth with incoming token
if CONF.ironic.auth_type != 'none':
if token is None:
args['session'] = IRONIC_SESSION
else:
args['token'] = token
if CONF.ironic.auth_type != 'none' and token is not None:
args['token'] = token
# TODO(pas-ha): remove handling of deprecated options in Rocky
if CONF.ironic.os_region and not CONF.ironic.region_name:
@ -147,7 +145,11 @@ def get_client(token=None,
adapter = keystone.get_adapter('ironic', session=IRONIC_SESSION,
**adapter_opts)
endpoint = adapter.get_endpoint()
return client.Client(1, endpoint, **args)
if not endpoint:
raise utils.Error(
_('Cannot find the bare metal endpoint either in Keystone or '
'in the configuration'), code=500)
return client.get_client(1, endpoint=endpoint, **args)
def check_provision_state(node):

View File

@ -40,11 +40,13 @@ class TestGetClientBase(object):
mock_adapter.assert_called_once_with(
'ironic', region_name='somewhere', session=mock_sess)
mock_adapter.return_value.get_endpoint.assert_called_once_with()
args = {'token': fake_token,
args = {'session': mock_sess,
'token': fake_token,
'os_ironic_api_version': ir_utils.DEFAULT_IRONIC_API_VERSION,
'max_retries': CONF.ironic.max_retries,
'retry_interval': CONF.ironic.retry_interval}
mock_client.assert_called_once_with(1, fake_ironic_url, **args)
mock_client.assert_called_once_with(1, endpoint=fake_ironic_url,
**args)
def test_get_client_without_auth_token(self, mock_client, mock_load,
mock_opts, mock_adapter):
@ -57,13 +59,14 @@ class TestGetClientBase(object):
'os_ironic_api_version': ir_utils.DEFAULT_IRONIC_API_VERSION,
'max_retries': CONF.ironic.max_retries,
'retry_interval': CONF.ironic.retry_interval}
mock_client.assert_called_once_with(1, fake_ironic_url, **args)
mock_client.assert_called_once_with(1, endpoint=fake_ironic_url,
**args)
@mock.patch.object(keystone, 'get_adapter')
@mock.patch.object(keystone, 'register_auth_opts')
@mock.patch.object(keystone, 'get_session')
@mock.patch.object(client, 'Client')
@mock.patch.object(client, 'get_client', autospec=True)
class TestGetClientAuth(TestGetClientBase, base.BaseTest):
def setUp(self):
super(TestGetClientAuth, self).setUp()
@ -76,7 +79,7 @@ class TestGetClientAuth(TestGetClientBase, base.BaseTest):
@mock.patch.object(keystone, 'get_adapter')
@mock.patch.object(keystone, 'register_auth_opts')
@mock.patch.object(keystone, 'get_session')
@mock.patch.object(client, 'Client')
@mock.patch.object(client, 'get_client', autospec=True)
class TestGetClientNoAuth(TestGetClientBase, base.BaseTest):
def setUp(self):
super(TestGetClientNoAuth, self).setUp()

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Makes the way ironic-inspector creates an ironic client compatible with
future (post-Stein) versions of ironicclient.