diff options
author | Monty Taylor <mordred@inaugust.com> | 2014-10-11 09:41:48 -0700 |
---|---|---|
committer | Monty Taylor <mordred@inaugust.com> | 2014-10-11 13:01:24 -0700 |
commit | b03d6e59b93cea29df30f286db02701e2e77effa (patch) | |
tree | 3954bb15c78d9dd2b4bd8a45e1e051cc4b492a6f | |
parent | 91afaeb4aeb811b28d27b9eaf3887235a841f680 (diff) | |
download | os-client-config-b03d6e59b93cea29df30f286db02701e2e77effa.tar.gz |
Add support for command line argument processing
Now takes the ability to pass in a dict of key/value pairs, probably
from a command line processing thing like argparse, and to overlay them
on the config that came from the files or env vars.
Change-Id: I830699476e2340389979b34704c0dfbfe97a1e08
-rw-r--r-- | os_client_config/config.py | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/os_client_config/config.py b/os_client_config/config.py index 781dc10..34de513 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -161,17 +161,43 @@ class OpenStackConfig(object): clouds.append(self.get_one_cloud(cloud, region)) return clouds - def get_one_cloud(self, name='openstack', region=None): + def _fix_args(self, args): + '''Replace - with _ and strip os_ prefixes.''' + os_args = dict() + new_args = dict() + for (key, val) in args.iteritems(): + key = key.replace('-', '_') + if key.startswith('os'): + os_args[key[3:]] = val + else: + new_args[key] = val + new_args.update(os_args) + return new_args + + def get_one_cloud(self, **kwargs): + + args = self._fix_args(kwargs) - if not region: - region = self._get_region(name) + if 'cloud' in args: + name = args['cloud'] + del args['cloud'] + else: + name = 'openstack' + + if 'region_name' not in args: + args['region_name'] = self._get_region(name) config = self._get_base_cloud_config(name) - config['region_name'] = region + + # Can't just do update, because None values take over + for (key, val) in args.iteritems(): + if val is not None: + config[key] = val for key in BOOL_KEYS: if key in config: - config[key] = get_boolean(config[key]) + if type(config[key]) is not bool: + config[key] = get_boolean(config[key]) for key in REQUIRED_VALUES: if key not in config or not config[key]: @@ -193,7 +219,7 @@ class OpenStackConfig(object): config[key] = value.format(**config) return cloud_config.CloudConfig( - name=name, region=region, config=config) + name=name, region=config['region_name'], config=config) if __name__ == '__main__': config = OpenStackConfig().get_all_clouds() |