From b7f7848ec01103361e9238bcf264b4a68043c1c1 Mon Sep 17 00:00:00 2001 From: Angie Wang Date: Mon, 21 Oct 2019 15:10:06 -0400 Subject: [PATCH] Update subcloud add command arguments to accept a URL This commit updates the argument --bootstrap-values and --deploy-playbooks for the command subcloud add to accept a URL in addition to a local file path. Tested: - arguments with URL and local path - exceptions Change-Id: I5f2128ce35284499f1b3cecd9f1535111a08d775 Closes-Bug: 1848539 Signed-off-by: Angie Wang --- .../commands/v1/subcloud_manager.py | 29 ++++++------------- dcmanagerclient/utils.py | 14 +++++---- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/dcmanagerclient/commands/v1/subcloud_manager.py b/dcmanagerclient/commands/v1/subcloud_manager.py index 411e3839..d9e1a2c3 100644 --- a/dcmanagerclient/commands/v1/subcloud_manager.py +++ b/dcmanagerclient/commands/v1/subcloud_manager.py @@ -27,6 +27,7 @@ from osc_lib.command import command from dcmanagerclient.commands.v1 import base from dcmanagerclient import exceptions +from dcmanagerclient import utils def format(subcloud=None): @@ -124,7 +125,8 @@ class AddSubcloud(base.DCManagerShowOne): parser.add_argument( '--bootstrap-values', required=True, - help='YAML file containing subcloud configuration settings.' + help='YAML file containing subcloud configuration settings. ' + 'Can be either a local file path or a URL.' ) parser.add_argument( @@ -133,7 +135,8 @@ class AddSubcloud(base.DCManagerShowOne): help='An optional ansible playbook to be run after the subcloud ' 'has been successfully bootstrapped. It will be run with the ' 'subcloud as the target and authentication is ' - 'handled automatically.' + 'handled automatically. ' + 'Can be either a local file path or a URL.' ) parser.add_argument( @@ -159,15 +162,8 @@ class AddSubcloud(base.DCManagerShowOne): # Load the configuration from the bootstrap yaml file filename = parsed_args.bootstrap_values - if os.path.isdir(filename): - error_msg = "Error: %s is a directory." % filename - raise exceptions.DCManagerClientException(error_msg) - try: - with open(filename, 'rb') as stream: - kwargs.update(yaml.safe_load(stream)) - except Exception: - error_msg = "Error: Could not open file %s." % filename - raise exceptions.DCManagerClientException(error_msg) + stream = utils.get_contents_if_file(filename) + kwargs.update(yaml.safe_load(stream)) # Load the the deploy playbook yaml file if parsed_args.deploy_playbook is not None: @@ -177,15 +173,8 @@ class AddSubcloud(base.DCManagerShowOne): "specified." raise exceptions.DCManagerClientException(error_msg) filename = parsed_args.deploy_playbook - if os.path.isdir(filename): - error_msg = "Error: %s is a directory." % filename - raise exceptions.DCManagerClientException(error_msg) - try: - with open(filename, 'rb') as stream: - kwargs['deploy_playbook'] = yaml.safe_load(stream) - except Exception: - error_msg = "Error: Could not open file %s." % filename - raise exceptions.DCManagerClientException(error_msg) + stream = utils.get_contents_if_file(filename) + kwargs['deploy_playbook'] = yaml.safe_load(stream) # Load the configuration from the deploy values yaml file if parsed_args.deploy_values is not None: diff --git a/dcmanagerclient/utils.py b/dcmanagerclient/utils.py index 72d761cd..5267065b 100644 --- a/dcmanagerclient/utils.py +++ b/dcmanagerclient/utils.py @@ -69,12 +69,13 @@ def get_contents_if_file(contents_or_file_name): If the value passed in is a file name or file URI, return the contents. If not, or there is an error reading the file contents, - return the value passed in as the contents. + raise an exception. - For example, a workflow definition will be returned if either the - workflow definition file name, or file URI are passed in, or the - actual workflow definition itself is passed in. """ + if os.path.isdir(contents_or_file_name): + error_msg = "Error: %s is a directory." % contents_or_file_name + raise exceptions.DCManagerClientException(error_msg) + try: if parse.urlparse(contents_or_file_name).scheme: definition_url = contents_or_file_name @@ -85,5 +86,6 @@ def get_contents_if_file(contents_or_file_name): request.pathname2url(path) ) return request.urlopen(definition_url).read().decode('utf8') - except Exception: - return contents_or_file_name + except Exception as e: + raise exceptions.DCManagerClientException( + "Error: Could not open file %s: %s" % (contents_or_file_name, e))