diff --git a/os_client_config/config.py b/os_client_config/config.py index aac41a4..87b8d65 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'}) diff --git a/releasenotes/notes/default-cloud-7ee0bcb9e5dd24b9.yaml b/releasenotes/notes/default-cloud-7ee0bcb9e5dd24b9.yaml new file mode 100644 index 0000000..49aba3c --- /dev/null +++ b/releasenotes/notes/default-cloud-7ee0bcb9e5dd24b9.yaml @@ -0,0 +1,7 @@ +--- +issues: + - If there was only one cloud defined in clouds.yaml + os-client-config was requiring the cloud parameter + be passed. This is inconsistent with how the envvars + cloud works which WILL work without setting the cloud + parameter if it's the only cloud.