Slightly refactor vendor profile loading

In prep for the next patch which will add support for url-based vendor
profiles, rearrange a few bits of the vendor profile loading code. This
should make the next patch easier to read.

Change-Id: I3b18678895006249c0e958944c88f09a365daee0
This commit is contained in:
Monty Taylor 2018-11-07 09:26:31 -06:00
parent ca636b92e3
commit 1da367b537
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
2 changed files with 49 additions and 42 deletions

View File

@ -448,35 +448,37 @@ class OpenStackConfig(object):
# Expand a profile if it exists. 'cloud' is an old confusing name
# for this.
profile_name = our_cloud.get('profile', our_cloud.get('cloud', None))
if profile_name and profile_name != self.envvar_key:
if 'cloud' in our_cloud:
warnings.warn(
"{0} use the keyword 'cloud' to reference a known "
"vendor profile. This has been deprecated in favor of the "
"'profile' keyword.".format(self.config_filename))
vendor_filename, vendor_file = self._load_vendor_file()
if vendor_file and profile_name in vendor_file['public-clouds']:
_auth_update(cloud, vendor_file['public-clouds'][profile_name])
if not profile_name or profile_name == self.envvar_key:
return
if 'cloud' in our_cloud:
warnings.warn(
"{0} uses the keyword 'cloud' to reference a known "
"vendor profile. This has been deprecated in favor of the "
"'profile' keyword.".format(self.config_filename))
vendor_filename, vendor_file = self._load_vendor_file()
if vendor_file and profile_name in vendor_file['public-clouds']:
_auth_update(cloud, vendor_file['public-clouds'][profile_name])
else:
profile_data = vendors.get_profile(profile_name)
if profile_data:
status = profile_data.pop('status', 'active')
message = profile_data.pop('message', '')
if status == 'deprecated':
warnings.warn(
"{profile_name} is deprecated: {message}".format(
profile_name=profile_name, message=message))
elif status == 'shutdown':
raise exceptions.ConfigException(
"{profile_name} references a cloud that no longer"
" exists: {message}".format(
profile_name=profile_name, message=message))
_auth_update(cloud, profile_data)
else:
profile_data = vendors.get_profile(profile_name)
if profile_data:
status = profile_data.pop('status', 'active')
message = profile_data.pop('message', '')
if status == 'deprecated':
warnings.warn(
"{profile_name} is deprecated: {message}".format(
profile_name=profile_name, message=message))
elif status == 'shutdown':
raise exceptions.ConfigException(
"{profile_name} references a cloud that no longer"
" exists: {message}".format(
profile_name=profile_name, message=message))
_auth_update(cloud, profile_data)
else:
# Can't find the requested vendor config, go about business
warnings.warn("Couldn't find the vendor profile '{0}', for"
" the cloud '{1}'".format(profile_name,
name))
# Can't find the requested vendor config, go about business
warnings.warn("Couldn't find the vendor profile '{0}', for"
" the cloud '{1}'".format(profile_name,
name))
def _project_scoped(self, cloud):
return ('project_id' in cloud or 'project_name' in cloud

View File

@ -18,20 +18,25 @@ import os
import yaml
_vendors_path = os.path.dirname(os.path.realpath(__file__))
_vendor_defaults = None
_VENDORS_PATH = os.path.dirname(os.path.realpath(__file__))
_VENDOR_DEFAULTS = {}
def _get_vendor_defaults():
global _VENDOR_DEFAULTS
if not _VENDOR_DEFAULTS:
for vendor in glob.glob(os.path.join(_VENDORS_PATH, '*.yaml')):
with open(vendor, 'r') as f:
vendor_data = yaml.safe_load(f)
_VENDOR_DEFAULTS[vendor_data['name']] = vendor_data['profile']
for vendor in glob.glob(os.path.join(_VENDORS_PATH, '*.json')):
with open(vendor, 'r') as f:
vendor_data = json.load(f)
_VENDOR_DEFAULTS[vendor_data['name']] = vendor_data['profile']
return _VENDOR_DEFAULTS
def get_profile(profile_name):
global _vendor_defaults
if _vendor_defaults is None:
_vendor_defaults = {}
for vendor in glob.glob(os.path.join(_vendors_path, '*.yaml')):
with open(vendor, 'r') as f:
vendor_data = yaml.safe_load(f)
_vendor_defaults[vendor_data['name']] = vendor_data['profile']
for vendor in glob.glob(os.path.join(_vendors_path, '*.json')):
with open(vendor, 'r') as f:
vendor_data = json.load(f)
_vendor_defaults[vendor_data['name']] = vendor_data['profile']
return _vendor_defaults.get(profile_name)
vendor_defaults = _get_vendor_defaults()
if profile_name in vendor_defaults:
return vendor_defaults[profile_name].copy()