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:
parent
7e682d3bf0
commit
ffafb52fa7
@ -18,7 +18,8 @@ provide backwards compatibility to service-specific variables such as
|
|||||||
|
|
||||||
If you have OpenStack environment variables seet and no config files,
|
If you have OpenStack environment variables seet and no config files,
|
||||||
os-client-config will produce a cloud config object named "envvars" containing
|
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
|
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
|
default service type as a prefix. For instance, to set a special service_type
|
||||||
|
@ -90,13 +90,24 @@ class OpenStackConfig(object):
|
|||||||
self.cloud_config = dict(
|
self.cloud_config = dict(
|
||||||
clouds=dict(openstack=dict(self.defaults)))
|
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()
|
envvars = _get_os_environ()
|
||||||
if envvars:
|
if envvars:
|
||||||
if 'envvars' in self.cloud_config['clouds']:
|
if self.envvar_key in self.cloud_config['clouds']:
|
||||||
raise exceptions.OpenStackConfigException(
|
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')
|
' 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_max_age = None
|
||||||
self._cache_path = CACHE_PATH
|
self._cache_path = CACHE_PATH
|
||||||
@ -333,8 +344,8 @@ class OpenStackConfig(object):
|
|||||||
:param kwargs: Additional configuration options
|
:param kwargs: Additional configuration options
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if cloud is None and 'envvars' in self._get_cloud_sections():
|
if cloud is None and self.envvar_key in self._get_cloud_sections():
|
||||||
cloud = 'envvars'
|
cloud = self.envvar_key
|
||||||
|
|
||||||
args = self._fix_args(kwargs, argparse=argparse)
|
args = self._fix_args(kwargs, argparse=argparse)
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
from os_client_config import cloud_config
|
from os_client_config import cloud_config
|
||||||
from os_client_config import config
|
from os_client_config import config
|
||||||
|
from os_client_config import exceptions
|
||||||
from os_client_config.tests import base
|
from os_client_config.tests import base
|
||||||
|
|
||||||
|
|
||||||
@ -36,3 +37,9 @@ class TestConfig(base.TestCase):
|
|||||||
self._assert_cloud_details(cc)
|
self._assert_cloud_details(cc)
|
||||||
cc = c.get_one_cloud('_test_cloud_no_vendor')
|
cc = c.get_one_cloud('_test_cloud_no_vendor')
|
||||||
self._assert_cloud_details(cc)
|
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')
|
||||||
|
@ -15,32 +15,36 @@
|
|||||||
|
|
||||||
from os_client_config import cloud_config
|
from os_client_config import cloud_config
|
||||||
from os_client_config import config
|
from os_client_config import config
|
||||||
from os_client_config import exceptions
|
|
||||||
from os_client_config.tests import base
|
from os_client_config.tests import base
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
|
|
||||||
|
|
||||||
class TestConfig(base.TestCase):
|
class TestEnviron(base.TestCase):
|
||||||
|
|
||||||
def test_get_one_cloud(self):
|
def setUp(self):
|
||||||
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
|
super(TestEnviron, self).setUp()
|
||||||
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):
|
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.EnvironmentVariable('OS_AUTH_URL', 'https://example.com'))
|
fixtures.EnvironmentVariable('OS_AUTH_URL', 'https://example.com'))
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.EnvironmentVariable('OS_USERNAME', 'testuser'))
|
fixtures.EnvironmentVariable('OS_USERNAME', 'testuser'))
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.EnvironmentVariable('OS_PROJECT_NAME', 'testproject'))
|
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],
|
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
|
||||||
vendor_files=[self.vendor_yaml])
|
vendor_files=[self.vendor_yaml])
|
||||||
cc = c.get_one_cloud('envvars')
|
cc = c.get_one_cloud('envvars')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user