Merge "Add --parameters and --create-vars-file arguments to the list subcommand"

This commit is contained in:
Zuul 2019-07-31 05:21:56 +00:00 committed by Gerrit Code Review
commit c3fb3cb662
5 changed files with 185 additions and 12 deletions

View File

@ -0,0 +1,6 @@
---
features:
- |
The 'openstack tripleo validator list' subcommand can only display all the
available parameters for the validations using the new --parameters
argument and extract them to a file using the new --create-vars-file argument.

View File

@ -1780,6 +1780,51 @@ class TestAnsibleSymlink(TestCase):
mock_cmd.assert_not_called()
class TestGetParamFieldName(TestCase):
def test_with_empty_val_data(self):
input_parameter = {}
expected = "parameters"
result = utils.get_param_field_name(input_parameter)
self.assertEqual(result, expected)
def test_with_val_data_and_returns_parameters(self):
input_parameter = {'validations': [
{'description': 'validation number one',
'groups': ['prep', 'pre-deployment'],
'id': 'Validation_Number_One',
'name': 'Validation Number One',
'parameters': {}},
{'description': 'validation number two',
'groups': ['post-deployment'],
'id': 'Validation_Number_Two',
'name': 'Validation Number Two',
'parameters': {'config_file': "/etc/config.conf"}},
]}
expected = "parameters"
result = utils.get_param_field_name(input_parameter)
self.assertEqual(result, expected)
def test_with_val_data_and_returns_metadata(self):
input_parameter = {'validations': [
{'description': 'validation number one',
'groups': ['prep', 'pre-deployment'],
'id': 'Validation_Number_One',
'name': 'Validation Number One',
'metadata': {}},
{'description': 'validation number two',
'groups': ['post-deployment'],
'id': 'Validation_Number_Two',
'name': 'Validation Number Two',
'metadata': {'config_file': "/etc/config.conf"}},
]}
expected = "metadata"
result = utils.get_param_field_name(input_parameter)
self.assertEqual(result, expected)
class TestParseExtraVars(TestCase):
def test_simple_case_text_format(self):
input_parameter = ['key1=val1', 'key2=val2 key3=val3']

View File

@ -1878,14 +1878,55 @@ def _get_from_cfg(cfg, accessor, param, section):
return val
def get_param_field_name(validations_data=None):
"""Get the current parameters field name in a Dict
Returns either 'parameters' or 'metadata'.
By Default, it returns 'parameters'.
"""
# TODO(gchamoul): Added for backwards compatibility and will be
# removed for Train release.
if validations_data is None:
validations_data = {}
if 'metadata' in validations_data.get('validations', [[]])[0]:
return 'metadata'
return 'parameters'
def get_validations_parameters(validations_data,
validation_name=None,
groups=None):
if validation_name is None:
validation_name = []
if groups is None:
groups = []
params = {}
param_field_name = get_param_field_name(validations_data)
for val in validations_data['validations']:
wanted_validation = False
wanted_group = False
if val.get('id') in validation_name:
wanted_validation = True
for grp in groups:
if grp in val.get('groups'):
wanted_group = True
if wanted_validation or wanted_group:
params[val.get('id')] = {
'parameters': val.get(param_field_name)
}
return params
def get_validations_table(validations_data):
"""Return the validations information as a pretty printed table """
# TODO(gchamoul): Added for backwards compatibility and will be removed for
# Train Release.
if 'parameters' in validations_data['validations'][0]:
param_field_name = 'parameters'
else:
param_field_name = 'metadata'
param_field_name = get_param_field_name(validations_data)
t = PrettyTable(border=True, header=True, padding_width=1)
t.title = "TripleO validations"

View File

@ -68,6 +68,39 @@ class TripleOValidatorList(command.Command):
)
parser.add_argument(
'--parameters',
action='store_true',
default=False,
help=_("List available validations parameters")
)
parser.add_argument(
'--create-vars-file',
metavar=('[json|yaml]', '/tmp/myvars'),
action='store',
default=[],
nargs=2,
help=_("Create a json or a yaml file "
"containing all the variables "
"available for the validations: "
"[yaml|json] /tmp/myvars")
)
ex_group = parser.add_mutually_exclusive_group(required=False)
ex_group.add_argument(
'--validation-name',
metavar='<validation_id>[,<validation_id>,...]',
action=_CommaListAction,
default=[],
help=_("List specific validations, "
"if more than one validation is required "
"separate the names with commas: "
"--validation-name check-ftype,512e | "
"--validation-name 512e")
)
ex_group.add_argument(
'--group',
metavar='<group>[,<group>,...]',
action=_CommaListGroupAction,
@ -81,6 +114,38 @@ class TripleOValidatorList(command.Command):
return parser
def _create_variables_file(self, data, varsfile):
msg = (_("The file %s already exists on the filesystem, "
"do you still want to continue [y/N] "))
if varsfile[0] not in ['json', 'yaml']:
raise RuntimeError(_('Wrong file type: %s') % varsfile[0])
else:
LOG.debug(_('Launch variables file creation'))
try:
if os.path.exists(varsfile[-1]):
confirm = oooutils.prompt_user_for_confirmation(
message=msg % varsfile[-1], logger=LOG)
if not confirm:
raise RuntimeError(_("Action not confirmed, exiting"))
with open(varsfile[-1], 'w') as f:
params = {}
for val_name in data.keys():
for k, v in data[val_name].get('parameters').items():
params[k] = v
if varsfile[0] == 'json':
f.write(oooutils.get_validations_json(params))
elif varsfile[0] == 'yaml':
f.write(oooutils.get_validations_yaml(params))
print(
_('The file %s has been created successfully') %
varsfile[-1])
except Exception as e:
print(_("Creating variables file finished with errors"))
print('Output: {}'.format(e))
def _run_validator_list(self, parsed_args):
clients = self.app.client_manager
@ -91,13 +156,29 @@ class TripleOValidatorList(command.Command):
LOG.debug(_('Launch listing the validations'))
try:
output = validations.list_validations(clients, workflow_input)
if parsed_args.output == 'json':
out = oooutils.get_validations_json({'validations': output})
elif parsed_args.output == 'yaml':
out = oooutils.get_validations_yaml({'validations': output})
if parsed_args.parameters:
out = oooutils.get_validations_parameters(
{'validations': output},
parsed_args.validation_name,
parsed_args.group
)
if parsed_args.create_vars_file:
self._create_variables_file(out,
parsed_args.create_vars_file)
else:
print(oooutils.get_validations_json(out))
else:
out = oooutils.get_validations_table({'validations': output})
print(out)
if parsed_args.output == 'json':
out = oooutils.get_validations_json(
{'validations': output})
elif parsed_args.output == 'yaml':
out = oooutils.get_validations_yaml(
{'validations': output})
else:
out = oooutils.get_validations_table(
{'validations': output})
print(out)
except Exception as e:
print(_("Validations listing finished with errors"))
print('Output: {}'.format(e))