Merge "Refactoring of the cli show module"

This commit is contained in:
Zuul 2021-06-02 13:31:52 +00:00 committed by Gerrit Code Review
commit eaf304f62e
2 changed files with 30 additions and 14 deletions

View File

@ -14,9 +14,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import sys
from cliff.show import ShowOne from cliff.show import ShowOne
from validations_libs.validation_actions import ValidationActions from validations_libs.validation_actions import ValidationActions
@ -33,7 +30,7 @@ class Show(ShowOne):
parser.add_argument('--validation-dir', dest='validation_dir', parser.add_argument('--validation-dir', dest='validation_dir',
default=constants.ANSIBLE_VALIDATION_DIR, default=constants.ANSIBLE_VALIDATION_DIR,
help=("Path where the validation playbooks " help=("Path where the validation playbooks "
"is located.")) "are located."))
parser.add_argument('validation_name', parser.add_argument('validation_name',
metavar="<validation>", metavar="<validation>",
type=str, type=str,
@ -62,7 +59,7 @@ class ShowGroup(ShowOne):
parser.add_argument('--validation-dir', dest='validation_dir', parser.add_argument('--validation-dir', dest='validation_dir',
default=constants.ANSIBLE_VALIDATION_DIR, default=constants.ANSIBLE_VALIDATION_DIR,
help=("Path where the validation playbooks " help=("Path where the validation playbooks "
"is located.")) "are located."))
parser.add_argument('--group', '-g', parser.add_argument('--group', '-g',
metavar='<group_name>', metavar='<group_name>',
dest="group", dest="group",
@ -88,7 +85,7 @@ class ShowParameter(ShowOne):
parser.add_argument('--validation-dir', dest='validation_dir', parser.add_argument('--validation-dir', dest='validation_dir',
default=constants.ANSIBLE_VALIDATION_DIR, default=constants.ANSIBLE_VALIDATION_DIR,
help=("Path where the validation playbooks " help=("Path where the validation playbooks "
"is located.")) "are located."))
ex_group = parser.add_mutually_exclusive_group(required=False) ex_group = parser.add_mutually_exclusive_group(required=False)
ex_group.add_argument( ex_group.add_argument(
@ -145,7 +142,9 @@ class ShowParameter(ShowOne):
parsed_args.group, parsed_args.group,
parsed_args.format_output, parsed_args.format_output,
parsed_args.download) parsed_args.download)
if parsed_args.download: if parsed_args.download:
print("The file {} has been created successfully".format( self.app.LOG.info(
"The file {} has been created successfully".format(
parsed_args.download)) parsed_args.download))
return params.keys(), params.values() return params.keys(), params.values()

View File

@ -504,19 +504,36 @@ class ValidationActions(object):
group) group)
if download_file: if download_file:
params_only = {} params_only = {}
with open(download_file, 'w') as f: try:
for val_name in params.keys(): with open(download_file, 'w') as parameters_file:
params_only.update(params[val_name].get('parameters')) for val_name in params.keys():
params_only.update(params[val_name].get('parameters'))
if output_format == 'json': if output_format == 'json':
f.write(json.dumps(params_only, parameters_file.write(
json.dumps(params_only,
indent=4, indent=4,
sort_keys=True)) sort_keys=True))
else: else:
f.write(yaml.safe_dump(params_only, parameters_file.write(
yaml.safe_dump(params_only,
allow_unicode=True, allow_unicode=True,
default_flow_style=False, default_flow_style=False,
indent=2)) indent=2))
self.log.debug(
"Validations parameters file {} saved as {} ".format(
download_file,
output_format))
except (PermissionError, OSError) as error:
self.log.exception(
(
"Exception {} encountered while tring to write "
"a validations parameters file {}"
).format(
error,
download_file))
return params return params
def show_history(self, validation_ids=None, extension='json', def show_history(self, validation_ids=None, extension='json',