Allow overriding envvars as the name of the cloud

For environment variables created cloud objects, it's possible someone
may not want it to be called envvars. I mean, let's be honest, I cannot
imagine why this would be important ... but people get emotional about
things. Let them name their cloud "bunnyrabbit" because that makes
people happy.

Change-Id: I0c232de7d93080ea632fb66a82b9e6d3e925c901
This commit is contained in:
Monty Taylor 2015-04-11 11:55:52 -04:00
parent 7e682d3bf0
commit ffafb52fa7
4 changed files with 43 additions and 20 deletions

View File

@ -18,7 +18,8 @@ provide backwards compatibility to service-specific variables such as
If you have OpenStack environment variables seet and no config files,
os-client-config will produce a cloud config object named "envvars" containing
your values from the environment.
your values from the environment. If you don't like the name "envvars", that's
ok, you can override it by setting `OS_CLOUD_NAME`.
Service specific settings, like the nova service type, are set with the
default service type as a prefix. For instance, to set a special service_type

View File

@ -90,13 +90,24 @@ class OpenStackConfig(object):
self.cloud_config = dict(
clouds=dict(openstack=dict(self.defaults)))
self.envvar_key = os.environ.pop('OS_CLOUD_NAME', None)
if self.envvar_key:
if self.envvar_key in self.cloud_config['clouds']:
raise exceptions.OpenStackConfigException(
'clouds.yaml defines a cloud named "{0}", but'
' OS_CLOUD_NAME is also set to "{0}". Please rename'
' either your environment based cloud, or one of your'
' file-based clouds.'.format(self.envvar_key))
else:
self.envvar_key = 'envvars'
envvars = _get_os_environ()
if envvars:
if 'envvars' in self.cloud_config['clouds']:
if self.envvar_key in self.cloud_config['clouds']:
raise exceptions.OpenStackConfigException(
'clouds.yaml defines a cloud named envvars, and OS_'
'clouds.yaml defines a cloud named {0}, and OS_*'
' env vars are set')
self.cloud_config['clouds']['envvars'] = envvars
self.cloud_config['clouds'][self.envvar_key] = envvars
self._cache_max_age = None
self._cache_path = CACHE_PATH
@ -333,8 +344,8 @@ class OpenStackConfig(object):
:param kwargs: Additional configuration options
"""
if cloud is None and 'envvars' in self._get_cloud_sections():
cloud = 'envvars'
if cloud is None and self.envvar_key in self._get_cloud_sections():
cloud = self.envvar_key
args = self._fix_args(kwargs, argparse=argparse)

View File

@ -14,6 +14,7 @@
from os_client_config import cloud_config
from os_client_config import config
from os_client_config import exceptions
from os_client_config.tests import base
@ -36,3 +37,9 @@ class TestConfig(base.TestCase):
self._assert_cloud_details(cc)
cc = c.get_one_cloud('_test_cloud_no_vendor')
self._assert_cloud_details(cc)
def test_no_environ(self):
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
self.assertRaises(
exceptions.OpenStackConfigException, c.get_one_cloud, 'envvars')

View File

@ -15,32 +15,36 @@
from os_client_config import cloud_config
from os_client_config import config
from os_client_config import exceptions
from os_client_config.tests import base
import fixtures
class TestConfig(base.TestCase):
class TestEnviron(base.TestCase):
def test_get_one_cloud(self):
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
self.assertIsInstance(c.get_one_cloud(), cloud_config.CloudConfig)
def test_no_environ(self):
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
self.assertRaises(
exceptions.OpenStackConfigException, c.get_one_cloud, 'envvars')
def test_environ_exists(self):
def setUp(self):
super(TestEnviron, self).setUp()
self.useFixture(
fixtures.EnvironmentVariable('OS_AUTH_URL', 'https://example.com'))
self.useFixture(
fixtures.EnvironmentVariable('OS_USERNAME', 'testuser'))
self.useFixture(
fixtures.EnvironmentVariable('OS_PROJECT_NAME', 'testproject'))
def test_get_one_cloud(self):
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
self.assertIsInstance(c.get_one_cloud(), cloud_config.CloudConfig)
def test_envvar_name_override(self):
self.useFixture(
fixtures.EnvironmentVariable('OS_CLOUD_NAME', 'openstack'))
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
cc = c.get_one_cloud('openstack')
self._assert_cloud_details(cc)
def test_environ_exists(self):
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
cc = c.get_one_cloud('envvars')