diff options
author | Dean Troyer <dtroyer@gmail.com> | 2014-10-11 14:55:30 -0500 |
---|---|---|
committer | Dean Troyer <dtroyer@gmail.com> | 2014-10-11 15:55:49 -0500 |
commit | 470f6ea9358ac6cd6a05a96805d06c3cf8501d97 (patch) | |
tree | 3a875c92b8cd8a99bb522b5b00a7a345dc1a2576 | |
parent | b03d6e59b93cea29df30f286db02701e2e77effa (diff) | |
download | os-client-config-470f6ea9358ac6cd6a05a96805d06c3cf8501d97.tar.gz |
Add support for argparse Namespace objects
The Namespace objects returned by argparse contain all defined options
even if they are unspecified and default to None or ''. Also it
is not iterable. Change all that to add only the options presented
to argparse to the cloud_config.
Change-Id: Ia22fad60c81ab0b2878b404c0c8608d903ca964b
-rw-r--r-- | os_client_config/config.py | 68 |
1 files changed, 49 insertions, 19 deletions
diff --git a/os_client_config/config.py b/os_client_config/config.py index 34de513..753c80a 100644 --- a/os_client_config/config.py +++ b/os_client_config/config.py @@ -161,8 +161,24 @@ class OpenStackConfig(object): clouds.append(self.get_one_cloud(cloud, region)) return clouds - def _fix_args(self, args): - '''Replace - with _ and strip os_ prefixes.''' + def _fix_args(self, args, argparse=None): + """Massage the passed-in options + + Replace - with _ and strip os_ prefixes. + + Convert an argparse Namespace object to a dict, removing values + that are either None or ''. + """ + + if argparse: + # Convert the passed-in Namespace + o_dict = vars(argparse) + parsed_args = dict() + for k in o_dict: + if o_dict[k] is not None and o_dict[k] != '': + parsed_args[k] = o_dict[k] + args.update(parsed_args) + os_args = dict() new_args = dict() for (key, val) in args.iteritems(): @@ -174,13 +190,26 @@ class OpenStackConfig(object): new_args.update(os_args) return new_args - def get_one_cloud(self, **kwargs): - - args = self._fix_args(kwargs) - - if 'cloud' in args: - name = args['cloud'] - del args['cloud'] + def get_one_cloud(self, cloud=None, validate=True, + argparse=None, **kwargs): + """Retrieve a single cloud configuration and merge additional options + + :param string cloud: + The name of the configuration to load from clouds.yaml + :param boolean validate: + Validate that required arguments are present and certain + argument combinations are valid + :param Namespace argparse: + An argparse Namespace object; allows direct passing in of + argparse options to be added to the cloud config. Values + of None and '' will be removed. + :param kwargs: Additional configuration options + """ + + args = self._fix_args(kwargs, argparse=argparse) + + if cloud: + name = cloud else: name = 'openstack' @@ -199,19 +228,20 @@ class OpenStackConfig(object): 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]: + if validate: + for key in REQUIRED_VALUES: + if key not in config or not config[key]: + raise exceptions.OpenStackConfigException( + 'Unable to find full auth information for cloud' + ' {name} in config files {files}' + ' or environment variables.'.format( + name=name, files=','.join(self._config_files))) + if 'project_name' not in config and 'project_id' not in config: raise exceptions.OpenStackConfigException( - 'Unable to find full auth information for cloud {name} in' - ' config files {files}' + 'Neither project_name or project_id information found' + ' for cloud {name} in config files {files}' ' or environment variables.'.format( name=name, files=','.join(self._config_files))) - if 'project_name' not in config and 'project_id' not in config: - raise exceptions.OpenStackConfigException( - 'Neither project_name or project_id information found' - ' for cloud {name} in config files {files}' - ' or environment variables.'.format( - name=name, files=','.join(self._config_files))) # If any of the defaults reference other values, we need to expand for (key, value) in config.items(): |