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 <angie.wang@windriver.com>
This commit is contained in:
Angie Wang 2019-10-21 15:10:06 -04:00
parent ce49ea8a03
commit b7f7848ec0
2 changed files with 17 additions and 26 deletions

View File

@ -27,6 +27,7 @@ from osc_lib.command import command
from dcmanagerclient.commands.v1 import base from dcmanagerclient.commands.v1 import base
from dcmanagerclient import exceptions from dcmanagerclient import exceptions
from dcmanagerclient import utils
def format(subcloud=None): def format(subcloud=None):
@ -125,6 +126,7 @@ class AddSubcloud(base.DCManagerShowOne):
'--bootstrap-values', '--bootstrap-values',
required=True, 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( parser.add_argument(
@ -134,6 +136,7 @@ class AddSubcloud(base.DCManagerShowOne):
'has been successfully bootstrapped. It will be run with the ' 'has been successfully bootstrapped. It will be run with the '
'subcloud as the target and authentication is ' 'subcloud as the target and authentication is '
'handled automatically. ' 'handled automatically. '
'Can be either a local file path or a URL.'
) )
parser.add_argument( parser.add_argument(
@ -159,15 +162,8 @@ class AddSubcloud(base.DCManagerShowOne):
# Load the configuration from the bootstrap yaml file # Load the configuration from the bootstrap yaml file
filename = parsed_args.bootstrap_values filename = parsed_args.bootstrap_values
if os.path.isdir(filename): stream = utils.get_contents_if_file(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)) kwargs.update(yaml.safe_load(stream))
except Exception:
error_msg = "Error: Could not open file %s." % filename
raise exceptions.DCManagerClientException(error_msg)
# Load the the deploy playbook yaml file # Load the the deploy playbook yaml file
if parsed_args.deploy_playbook is not None: if parsed_args.deploy_playbook is not None:
@ -177,15 +173,8 @@ class AddSubcloud(base.DCManagerShowOne):
"specified." "specified."
raise exceptions.DCManagerClientException(error_msg) raise exceptions.DCManagerClientException(error_msg)
filename = parsed_args.deploy_playbook filename = parsed_args.deploy_playbook
if os.path.isdir(filename): stream = utils.get_contents_if_file(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) kwargs['deploy_playbook'] = yaml.safe_load(stream)
except Exception:
error_msg = "Error: Could not open file %s." % filename
raise exceptions.DCManagerClientException(error_msg)
# Load the configuration from the deploy values yaml file # Load the configuration from the deploy values yaml file
if parsed_args.deploy_values is not None: if parsed_args.deploy_values is not None:

View File

@ -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 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, 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: try:
if parse.urlparse(contents_or_file_name).scheme: if parse.urlparse(contents_or_file_name).scheme:
definition_url = contents_or_file_name definition_url = contents_or_file_name
@ -85,5 +86,6 @@ def get_contents_if_file(contents_or_file_name):
request.pathname2url(path) request.pathname2url(path)
) )
return request.urlopen(definition_url).read().decode('utf8') return request.urlopen(definition_url).read().decode('utf8')
except Exception: except Exception as e:
return contents_or_file_name raise exceptions.DCManagerClientException(
"Error: Could not open file %s: %s" % (contents_or_file_name, e))