Refactoring of the cli show module

New logging statements for the action and related
method call of 'ValidationActions' class.

Expanded exception handling for the 'show_validations_parameters'
method of the 'ValidationActions' class.

Removal of superfluous imports.

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I2ad5a212dcd4cdb40ca04f7cce3d18c7527f63b2
This commit is contained in:
Jiri Podivin 2021-05-26 10:59:13 +02:00 committed by Gael Chamoulaud
parent 0f00fd79f9
commit b2e87ca49a
2 changed files with 30 additions and 14 deletions

View File

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

View File

@ -500,19 +500,36 @@ class ValidationActions(object):
group)
if download_file:
params_only = {}
with open(download_file, 'w') as f:
for val_name in params.keys():
params_only.update(params[val_name].get('parameters'))
try:
with open(download_file, 'w') as parameters_file:
for val_name in params.keys():
params_only.update(params[val_name].get('parameters'))
if output_format == 'json':
f.write(json.dumps(params_only,
if output_format == 'json':
parameters_file.write(
json.dumps(params_only,
indent=4,
sort_keys=True))
else:
f.write(yaml.safe_dump(params_only,
else:
parameters_file.write(
yaml.safe_dump(params_only,
allow_unicode=True,
default_flow_style=False,
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
def show_history(self, validation_ids=None, extension='json',