From 837ca712288ceffea5b54ceaeb349d6577f38360 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Tue, 17 Nov 2015 15:17:55 -0500 Subject: [PATCH] Allow arbitrary client-specific options There are occasionally some client-specific things that would be handy to be able to configure about behaviors. For instance, the only config file that ansible's openstack inventory has is clouds.yaml. Rather than teaching os-client-config about such things, allow a pass-through config section. Apply key normalization to _'s like other configs, and merge the clouds and secure files so that the sections behave like other OCC config sections. Change-Id: If307e95006abf6e1efbbd77cfc99e5fdfed6c80a --- os_client_config/config.py | 13 +++++++++++++ os_client_config/tests/base.py | 4 ++++ os_client_config/tests/test_config.py | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/os_client_config/config.py b/os_client_config/config.py index 70989bf..ab3a003 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -253,6 +253,19 @@ class OpenStackConfig(object): # Flag location to hold the peeked value of an argparse timeout value self._argv_timeout = False + def get_extra_config(self, key, defaults=None): + """Fetch an arbitrary extra chunk of config, laying in defaults. + + :param string key: name of the config section to fetch + :param dict defaults: (optional) default values to merge under the + found config + """ + if not defaults: + defaults = {} + return _merge_clouds( + self._normalize_keys(defaults), + self._normalize_keys(self.cloud_config.get(key, {}))) + def _load_config_file(self): return self._load_yaml_json_file(self._config_files) diff --git a/os_client_config/tests/base.py b/os_client_config/tests/base.py index 6d9e093..fdc50cd 100644 --- a/os_client_config/tests/base.py +++ b/os_client_config/tests/base.py @@ -121,6 +121,10 @@ USER_CONF = { 'region_name': 'test-region', } }, + 'ansible': { + 'expand-hostvars': False, + 'use_hostnames': True, + }, } SECURE_CONF = { 'clouds': { diff --git a/os_client_config/tests/test_config.py b/os_client_config/tests/test_config.py index a6a35ad..98aaf79 100644 --- a/os_client_config/tests/test_config.py +++ b/os_client_config/tests/test_config.py @@ -372,6 +372,27 @@ class TestConfigArgparse(base.TestCase): self.assertDictEqual({'compute_api_version': 1}, fixed_args) + def test_extra_config(self): + c = config.OpenStackConfig(config_files=[self.cloud_yaml], + vendor_files=[self.vendor_yaml]) + + defaults = {'use_hostnames': False, 'other-value': 'something'} + ansible_options = c.get_extra_config('ansible', defaults) + + # This should show that the default for use_hostnames above is + # overridden by the value in the config file defined in base.py + # It should also show that other-value key is normalized and passed + # through even though there is no corresponding value in the config + # file, and that expand-hostvars key is normalized and the value + # from the config comes through even though there is no default. + self.assertDictEqual( + { + 'expand_hostvars': False, + 'use_hostnames': True, + 'other_value': 'something', + }, + ansible_options) + def test_register_argparse_cloud(self): c = config.OpenStackConfig(config_files=[self.cloud_yaml], vendor_files=[self.vendor_yaml])