diff options
author | Gregory Haynes <greg@greghaynes.net> | 2015-05-14 20:01:38 +0000 |
---|---|---|
committer | David Shrewsbury <shrewsbury.dave@gmail.com> | 2015-05-28 10:27:41 -0400 |
commit | daab3615e2884100f88a797570c1326c8139983c (patch) | |
tree | ff8a3bce9a0c161964bc3bb8545e8c00d3d74d53 | |
parent | 979827150da569f3a8e588f7f1220f3f6cf5c2fa (diff) | |
download | os-client-config-daab3615e2884100f88a797570c1326c8139983c.tar.gz |
Add set_one_cloud method1.2.0
It is useful for clients to be able to update configuration as well.
By doing this in shade we can perform things like merging and do some
testing rather than require clients to do it.
Change-Id: Ia185847c29c10f2cc6838adf962defd80894d0db
-rw-r--r-- | os_client_config/config.py | 31 | ||||
-rw-r--r-- | os_client_config/tests/test_config.py | 37 |
2 files changed, 68 insertions, 0 deletions
diff --git a/os_client_config/config.py b/os_client_config/config.py index 7b52912..b323e70 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -424,6 +424,37 @@ class OpenStackConfig(object): name=cloud_name, region=config['region_name'], config=self._normalize_keys(config)) + @staticmethod + def set_one_cloud(config_file, cloud, set_config=None): + """Set a single cloud configuration. + + :param string config_file: + The path to the config file to edit. If this file does not exist + it will be created. + :param string cloud: + The name of the configuration to save to clouds.yaml + :param dict set_config: Configuration options to be set + """ + + set_config = set_config or {} + cur_config = {} + try: + with open(config_file) as fh: + cur_config = yaml.safe_load(fh) + except IOError as e: + # Not no such file + if e.errno != 2: + raise + pass + + clouds_config = cur_config.get('clouds', {}) + cloud_config = _auth_update(clouds_config.get(cloud, {}), set_config) + clouds_config[cloud] = cloud_config + cur_config['clouds'] = clouds_config + + with open(config_file, 'w') as fh: + yaml.safe_dump(cur_config, fh, default_flow_style=False) + if __name__ == '__main__': config = OpenStackConfig().get_all_clouds() for cloud in config: diff --git a/os_client_config/tests/test_config.py b/os_client_config/tests/test_config.py index dddaf8b..25e1cc1 100644 --- a/os_client_config/tests/test_config.py +++ b/os_client_config/tests/test_config.py @@ -13,9 +13,11 @@ # under the License. import argparse +import copy import os import fixtures +import yaml from os_client_config import cloud_config from os_client_config import config @@ -103,6 +105,41 @@ class TestConfig(base.TestCase): c.get_one_cloud(cloud='defaults') self.assertEqual(['defaults'], sorted(c.get_cloud_names())) + def test_set_one_cloud_creates_file(self): + config_dir = fixtures.TempDir() + self.useFixture(config_dir) + config_path = os.path.join(config_dir.path, 'clouds.yaml') + config.OpenStackConfig.set_one_cloud(config_path, '_test_cloud_') + self.assertTrue(os.path.isfile(config_path)) + with open(config_path) as fh: + self.assertEqual({'clouds': {'_test_cloud_': {}}}, + yaml.safe_load(fh)) + + def test_set_one_cloud_updates_cloud(self): + new_config = { + 'cloud': 'new_cloud', + 'auth': { + 'password': 'newpass' + } + } + + resulting_cloud_config = { + 'auth': { + 'password': 'newpass', + 'username': 'testuser' + }, + 'cloud': 'new_cloud', + 'profile': '_test_cloud_in_our_cloud', + 'region_name': 'test-region' + } + resulting_config = copy.deepcopy(base.USER_CONF) + resulting_config['clouds']['_test-cloud_'] = resulting_cloud_config + config.OpenStackConfig.set_one_cloud(self.cloud_yaml, '_test-cloud_', + new_config) + with open(self.cloud_yaml) as fh: + written_config = yaml.safe_load(fh) + self.assertEqual(written_config, resulting_config) + class TestConfigArgparse(base.TestCase): |