diff options
author | Monty Taylor <mordred@inaugust.com> | 2017-09-19 14:40:53 -0500 |
---|---|---|
committer | Monty Taylor <mordred@inaugust.com> | 2017-09-19 14:40:53 -0500 |
commit | 45fd7b4a949e6cd8482b3025b96251c5832ddf23 (patch) | |
tree | d4f225a4d6a4642ca243116152eea4422a19a9bb /os_client_config | |
parent | eafc8bed564d41f2a0c6a050e7a301241309fa59 (diff) | |
download | os-client-config-45fd7b4a949e6cd8482b3025b96251c5832ddf23.tar.gz |
Treat clouds.yaml with one cloud like envvars
If there is only one cloud and that cloud is envvars, things work as
expected. If there is only one cloud in clouds.yaml and no envvars
cloud, we throw an error, even though it should be obvious which cloud
was intended.
Change-Id: Ia49d0fb2cc7dca36476d0e5ae3fe2b2aa1209e59
Diffstat (limited to 'os_client_config')
-rw-r--r-- | os_client_config/config.py | 11 | ||||
-rw-r--r-- | os_client_config/tests/test_config.py | 19 |
2 files changed, 30 insertions, 0 deletions
diff --git a/os_client_config/config.py b/os_client_config/config.py index eb415f7..d3f786a 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -263,6 +263,17 @@ class OpenStackConfig(object): if not self.default_cloud: self.default_cloud = self.envvar_key + if not self.default_cloud and self.cloud_config['clouds']: + if len(self.cloud_config['clouds'].keys()) == 1: + # If there is only one cloud just use it. This matches envvars + # behavior and allows for much less typing. + # TODO(mordred) allow someone to mark a cloud as "default" in + # clouds.yaml. + # The next/iter thing is for python3 compat where dict.keys + # returns an iterator but in python2 it's a list. + self.default_cloud = next(iter( + self.cloud_config['clouds'].keys())) + # Finally, fall through and make a cloud that starts with defaults # because we need somewhere to put arguments, and there are neither # config files or env vars diff --git a/os_client_config/tests/test_config.py b/os_client_config/tests/test_config.py index 09a11d2..4f2bf96 100644 --- a/os_client_config/tests/test_config.py +++ b/os_client_config/tests/test_config.py @@ -55,6 +55,25 @@ class TestConfig(base.TestCase): self.assertIsInstance(cloud, cloud_config.CloudConfig) self.assertEqual(cloud.name, '') + def test_get_one_cloud_default_cloud_from_file(self): + single_conf = base._write_yaml({ + 'clouds': { + 'single': { + 'auth': { + 'auth_url': 'http://example.com/v2', + 'username': 'testuser', + 'password': 'testpass', + 'project_name': 'testproject', + }, + 'region_name': 'test-region', + } + } + }) + c = config.OpenStackConfig(config_files=[single_conf], + vendor_files=[self.vendor_yaml]) + cc = c.get_one_cloud() + self.assertEqual(cc.name, 'single') + def test_get_one_cloud_auth_defaults(self): c = config.OpenStackConfig(config_files=[self.cloud_yaml]) cc = c.get_one_cloud(cloud='_test-cloud_', auth={'username': 'user'}) |