diff --git a/openstack/config/loader.py b/openstack/config/loader.py index 9049ebf74..58017ecb8 100644 --- a/openstack/config/loader.py +++ b/openstack/config/loader.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/openstack/tests/unit/config/test_config.py b/openstack/tests/unit/config/test_config.py index f87b18e25..60ef0cc8d 100644 --- a/openstack/tests/unit/config/test_config.py +++ b/openstack/tests/unit/config/test_config.py @@ -56,6 +56,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 000000000..49aba3c9c --- /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.