diff options
author | Monty Taylor <mordred@inaugust.com> | 2015-10-14 12:40:50 -0400 |
---|---|---|
committer | Monty Taylor <mordred@inaugust.com> | 2015-10-14 12:40:50 -0400 |
commit | 512ca01715683f7c46985677e7b9be8e3d7a191c (patch) | |
tree | 19d9670484e8d9d32721a828814a737de6c219bd | |
parent | f6681a83192386fcf27479c820998691a43e8b79 (diff) | |
download | os-client-config-512ca01715683f7c46985677e7b9be8e3d7a191c.tar.gz |
Validate requested region against region list
We have lists of valid regions for clouds, but specifying a region
incorrectly is a common mistake (specifying dfw instead of DFW,
for instance) Throw an error early with a helpful error message.
Change-Id: I55edf20c6dddde4a4d71dad33a41d4e10448ddac
-rw-r--r-- | os_client_config/config.py | 10 | ||||
-rw-r--r-- | os_client_config/tests/test_config.py | 17 |
2 files changed, 26 insertions, 1 deletions
diff --git a/os_client_config/config.py b/os_client_config/config.py index bff2f5c..b2c1649 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -573,7 +573,15 @@ class OpenStackConfig(object): # Regions is a list that we can use to create a list of cloud/region # objects. It does not belong in the single-cloud dict - config.pop('regions', None) + regions = config.pop('regions', None) + if regions and args['region_name'] not in regions: + raise exceptions.OpenStackConfigException( + 'Region {region_name} is not a valid region name for cloud' + ' {cloud}. Valid choices are {region_list}. Please note that' + ' region names are case sensitive.'.format( + region_name=args['region_name'], + region_list=','.join(regions), + cloud=cloud)) # Can't just do update, because None values take over for (key, val) in iter(args.items()): diff --git a/os_client_config/tests/test_config.py b/os_client_config/tests/test_config.py index 271c40b..0130748 100644 --- a/os_client_config/tests/test_config.py +++ b/os_client_config/tests/test_config.py @@ -298,6 +298,23 @@ class TestConfigArgparse(base.TestCase): self.assertEqual(cc.region_name, 'region1') self.assertIsNone(cc.snack_type) + def test_get_one_cloud_bad_region(self): + c = config.OpenStackConfig(config_files=[self.cloud_yaml], + vendor_files=[self.vendor_yaml]) + + self.assertRaises( + exceptions.OpenStackConfigException, + c.get_one_cloud, + cloud='_test_cloud_regions', region_name='bad') + + def test_get_one_cloud_bad_region_no_regions(self): + c = config.OpenStackConfig(config_files=[self.cloud_yaml], + vendor_files=[self.vendor_yaml]) + + cc = c.get_one_cloud(cloud='_test-cloud_', region_name='bad_region') + self._assert_cloud_details(cc) + self.assertEqual(cc.region_name, 'bad_region') + def test_get_one_cloud_no_argparse_region2(self): c = config.OpenStackConfig(config_files=[self.cloud_yaml], vendor_files=[self.vendor_yaml]) |