Get default credentials using new keystone package.

Change-Id: Ib12ce763b70f5c7336dbefed9cbb2f1e0201e777
This commit is contained in:
Federico Ressi 2019-03-19 09:25:12 +01:00
parent 086ad8fc15
commit 8ee18350d4
3 changed files with 9 additions and 102 deletions

View File

@ -13,114 +13,19 @@
# under the License.
from __future__ import absolute_import
from tobiko import config
def get_default_credentials(api_version=None, username=None, password=None,
project_name=None, auth_url=None,
user_domain_name=None, project_domain_name=None):
if api_version is None:
api_version = config.get_any_option(
'environ.OS_IDENTITY_API_VERSION',
'tempest.identity_feature_enabled.api_v3')
if api_version is True:
api_version = 3
elif api_version is False:
api_version = 2
elif api_version is not None:
api_version = int(api_version)
username = (username or
config.get_any_option(
'environ.OS_USERNAME',
'tobiko.keystone.username',
'tempest.auth.username',
'tempest.auth.admin_username') or
'admin')
password = (password or
config.get_any_option(
'environ.OS_PASSWORD',
'tobiko.keystone.password',
'tempest.auth.password',
'tempest.auth.admin_password',
'tempest.identity.admin_password'))
project_name = (project_name or
config.get_any_option(
'environ.OS_PROJECT_NAME',
'environ.OS_TENANT_NAME',
'tobiko.keystone.project_name',
'tempest.auth.project_name',
'tempest.auth.admin_project_name') or
'admin')
if auth_url is None and api_version in [None, 2]:
auth_url = config.get_any_option('environ.OS_AUTH_URL',
'tobiko.keystone.auth_url',
'tempest.identity.uri')
if auth_url and api_version is None:
api_version = get_version_from_url(auth_url)
if auth_url is None:
auth_url = config.get_any_option('environ.OS_AUTH_URL',
'tobiko.keystone.auth_url',
'tempest.identity.uri_v3')
if auth_url and api_version is None:
api_version = 3
if auth_url is None:
auth_url = 'http://127.0.0.1:5000/v2.0'
api_version = 2
credentials = dict(username=username,
password=password,
project_name=project_name,
auth_url=auth_url)
if api_version and api_version > 2:
credentials.update(
user_domain_name=(
user_domain_name or
config.get_any_option(
'environ.OS_USER_DOMAIN_NAME',
'tobiko.keystone.user_domain_name',
'tempest.auth.user_domain_name',
'tempest.auth.admin_domain_name') or
'admin'),
project_domain_name=(
project_domain_name or
config.get_any_option(
'environ.OS_PROJECT_DOMAIN_NAME',
'tobiko.keystone.project_domain_name'
'tempest.identity.project_domain_name',
'tempest.auth.admin_domain_name',
'tempest.identity.admin_domain_name',
'tempest.identity.admin_tenant_name') or
'admin'),)
# remove every field that is still None from credentials dictionary
return {k: v for k, v in credentials.items() if v is not None}
def get_version_from_url(auth_url):
if auth_url.endswith('/v2.0'):
return 2
elif auth_url.endswith('/v3'):
return 3
else:
return None
from tobiko.openstack import keystone
class ClientManager(object):
"""Manages OpenStack official Python clients."""
credentials = get_default_credentials()
_session = None
_heat_client = None
_neutron_client = None
_nova_client = None
def __init__(self, credentials=None):
if credentials:
self.credentials = credentials
self.credentials = credentials
@property
def session(self):
@ -129,7 +34,10 @@ class ClientManager(object):
from keystoneauth1 import loading
from keystoneauth1 import session
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(**self.credentials)
credentials = (self.credentials or
keystone.default_keystone_credentials()).to_dict()
del credentials['api_version'] # this parameter is not accepted
auth = loader.load_from_options(**credentials)
self._session = session.Session(auth=auth, verify=False)
return self._session

View File

@ -25,8 +25,7 @@ LOG = log.getLogger(__name__)
def default_keystone_credentials():
return tobiko.setup_fixture(
DefaultKeystoneCredentialsFixture).credentials
return tobiko.setup_fixture(DefaultKeystoneCredentialsFixture).credentials
class KeystoneCredentials(collections.namedtuple(

View File

@ -37,8 +37,8 @@ class TobikoCMDTest(TobikoUnitTest):
def setUp(self):
TobikoUnitTest.setUp(self)
self.patch('tobiko.openstack.keystone.default_keystone_credentials',
return_value=self.default_credentials)
self.patch('tobiko.config.CONF.tobiko.keystone',
self.default_credentials)
def test_init(self, argv=None):
self.patch_argv(argv=argv)