2020-03-03 16:55:20 +01:00
|
|
|
# Copyright 2020 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
2020-03-11 17:07:56 +01:00
|
|
|
import datetime
|
2020-03-03 16:55:20 +01:00
|
|
|
import glob
|
|
|
|
import logging
|
|
|
|
import os
|
2020-03-04 09:02:17 +01:00
|
|
|
import six
|
2020-04-01 15:48:23 +02:00
|
|
|
import uuid
|
2020-03-03 16:55:20 +01:00
|
|
|
|
2020-04-01 15:48:23 +02:00
|
|
|
from os.path import join
|
2020-03-03 18:57:40 +01:00
|
|
|
from validations_libs import constants
|
2020-03-18 23:10:27 +01:00
|
|
|
from validations_libs.group import Group
|
2020-03-18 21:03:25 +01:00
|
|
|
from validations_libs.validation import Validation
|
2020-03-03 18:57:40 +01:00
|
|
|
|
2020-03-03 16:55:20 +01:00
|
|
|
LOG = logging.getLogger(__name__ + ".utils")
|
|
|
|
|
|
|
|
|
2020-03-11 17:07:56 +01:00
|
|
|
def current_time():
|
2020-03-18 21:03:25 +01:00
|
|
|
"""Return current time"""
|
2020-03-11 17:07:56 +01:00
|
|
|
return '%sZ' % datetime.datetime.utcnow().isoformat()
|
|
|
|
|
|
|
|
|
2021-05-03 11:36:29 +02:00
|
|
|
def create_log_dir(log_path=constants.VALIDATIONS_LOG_BASEDIR):
|
2021-06-07 14:17:07 +02:00
|
|
|
"""Check for presence of the selected validations log dir.
|
|
|
|
Create the directory if needed, and use fallback if that
|
|
|
|
proves too tall an order.
|
|
|
|
|
2021-05-03 11:36:29 +02:00
|
|
|
Log the failure if encountering OSError or PermissionError.
|
2021-06-07 14:17:07 +02:00
|
|
|
|
|
|
|
:param log_path: path of the selected log directory
|
|
|
|
:type log_path: `string`
|
|
|
|
:return: valid path to the log directory
|
|
|
|
:rtype: `string`
|
|
|
|
|
|
|
|
:raises: RuntimeError if even the fallback proves unavailable.
|
2021-05-03 11:36:29 +02:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if os.path.exists(log_path):
|
|
|
|
if os.access(log_path, os.W_OK):
|
2021-05-05 11:18:27 +02:00
|
|
|
return os.path.abspath(log_path)
|
2021-05-03 11:36:29 +02:00
|
|
|
else:
|
2021-06-07 14:17:07 +02:00
|
|
|
LOG.error(
|
|
|
|
(
|
|
|
|
"Selected log directory '{log_path}' is inaccessible. "
|
|
|
|
"Please check the access rights for: '{log_path}'"
|
|
|
|
).format(
|
|
|
|
log_path=log_path))
|
|
|
|
if log_path != constants.VALIDATIONS_LOG_BASEDIR:
|
|
|
|
LOG.warning(
|
|
|
|
(
|
|
|
|
"Resorting to the preset '{default_log_path}'"
|
|
|
|
).format(
|
|
|
|
default_log_path=constants.VALIDATIONS_LOG_BASEDIR))
|
|
|
|
|
|
|
|
return create_log_dir()
|
|
|
|
else:
|
|
|
|
raise RuntimeError()
|
2021-05-03 11:36:29 +02:00
|
|
|
else:
|
2021-06-07 14:17:07 +02:00
|
|
|
LOG.warning(
|
|
|
|
(
|
|
|
|
"Selected log directory '{log_path}' does not exist. "
|
|
|
|
"Attempting to create it."
|
|
|
|
).format(
|
|
|
|
log_path=log_path))
|
|
|
|
|
2021-05-03 11:36:29 +02:00
|
|
|
os.makedirs(log_path)
|
2021-05-05 11:18:27 +02:00
|
|
|
return os.path.abspath(log_path)
|
2021-06-07 14:17:07 +02:00
|
|
|
except (OSError, PermissionError) as error:
|
2021-05-03 11:36:29 +02:00
|
|
|
LOG.error(
|
|
|
|
(
|
2021-06-07 14:17:07 +02:00
|
|
|
"Encountered an {error} while creating the log directory. "
|
|
|
|
"Please check the access rights for: '{log_path}'"
|
|
|
|
).format(
|
|
|
|
error=error,
|
|
|
|
log_path=log_path))
|
|
|
|
|
2021-05-03 11:36:29 +02:00
|
|
|
# Fallback in default path if log_path != from constants path
|
|
|
|
if log_path != constants.VALIDATIONS_LOG_BASEDIR:
|
2021-06-07 14:17:07 +02:00
|
|
|
LOG.debug(
|
|
|
|
(
|
|
|
|
"Resorting to the preset '{default_log_path}'."
|
|
|
|
).format(
|
|
|
|
default_log_path=constants.VALIDATIONS_LOG_BASEDIR))
|
|
|
|
|
|
|
|
return create_log_dir()
|
2021-05-03 11:36:29 +02:00
|
|
|
raise RuntimeError()
|
|
|
|
|
2020-11-09 14:24:40 +01:00
|
|
|
|
2021-05-03 11:36:29 +02:00
|
|
|
def create_artifacts_dir(log_path=constants.VALIDATIONS_LOG_BASEDIR,
|
2021-06-07 14:17:07 +02:00
|
|
|
prefix=''):
|
|
|
|
"""Create Ansible artifacts directory for the validation run
|
2021-05-03 11:36:29 +02:00
|
|
|
:param log_path: Directory asbolute path
|
|
|
|
:type log_path: `string`
|
2020-11-09 14:24:40 +01:00
|
|
|
:param prefix: Playbook name
|
|
|
|
:type prefix: `string`
|
2021-06-07 14:17:07 +02:00
|
|
|
:return: UUID of the validation run, absolute path of the validation artifacts directory
|
2020-11-09 14:24:40 +01:00
|
|
|
:rtype: `string`, `string`
|
|
|
|
"""
|
2021-05-03 11:36:29 +02:00
|
|
|
artifact_dir = os.path.join(log_path, 'artifacts')
|
2020-04-01 15:48:23 +02:00
|
|
|
validation_uuid = str(uuid.uuid4())
|
2021-06-07 14:17:07 +02:00
|
|
|
validation_artifacts_dir = "{}/{}_{}_{}".format(
|
|
|
|
artifact_dir,
|
|
|
|
validation_uuid,
|
|
|
|
prefix,
|
|
|
|
current_time())
|
2020-03-11 17:07:56 +01:00
|
|
|
try:
|
2021-06-07 14:17:07 +02:00
|
|
|
os.makedirs(validation_artifacts_dir)
|
2021-05-05 11:18:27 +02:00
|
|
|
return validation_uuid, os.path.abspath(validation_artifacts_dir)
|
2021-05-03 11:36:29 +02:00
|
|
|
except (OSError, PermissionError):
|
2021-03-09 16:17:35 +01:00
|
|
|
LOG.exception(
|
|
|
|
(
|
2021-06-07 14:17:07 +02:00
|
|
|
"Error while creating Ansible artifacts log file. "
|
|
|
|
"Please check the access rights for '{}'"
|
|
|
|
).format(validation_artifacts_dir))
|
2020-03-11 17:07:56 +01:00
|
|
|
|
2021-05-06 12:01:29 +02:00
|
|
|
raise RuntimeError()
|
|
|
|
|
2020-03-11 17:07:56 +01:00
|
|
|
|
2021-07-08 12:31:38 +02:00
|
|
|
def parse_all_validations_on_disk(path, groups=None, categories=None):
|
|
|
|
"""Return a list of validations metadata which can be sorted by Groups or by
|
|
|
|
Categories.
|
2020-11-09 14:24:40 +01:00
|
|
|
|
|
|
|
:param path: The absolute path of the validations directory
|
|
|
|
:type path: `string`
|
2021-07-08 12:31:38 +02:00
|
|
|
|
2021-07-12 11:57:29 +02:00
|
|
|
:param groups: Groups of validations
|
|
|
|
:type groups: `list`
|
2021-07-08 12:31:38 +02:00
|
|
|
|
|
|
|
:param categories: Categories of validations
|
|
|
|
:type categories: `list`
|
|
|
|
|
|
|
|
:return: A list of validations metadata.
|
2020-11-09 14:24:40 +01:00
|
|
|
:rtype: `list`
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
|
|
|
>>> path = '/foo/bar'
|
|
|
|
>>> parse_all_validations_on_disk(path)
|
2021-07-08 12:31:38 +02:00
|
|
|
[{'categories': ['storage'],
|
|
|
|
'description': 'Detect whether the node disks use Advanced Format.',
|
2020-11-09 14:24:40 +01:00
|
|
|
'groups': ['prep', 'pre-deployment'],
|
|
|
|
'id': '512e',
|
|
|
|
'name': 'Advanced Format 512e Support'},
|
2021-07-08 12:31:38 +02:00
|
|
|
{'categories': ['system'],
|
|
|
|
'description': 'Make sure that the server has enough CPU cores.',
|
2020-11-09 14:24:40 +01:00
|
|
|
'groups': ['prep', 'pre-introspection'],
|
|
|
|
'id': 'check-cpu',
|
|
|
|
'name': 'Verify if the server fits the CPU core requirements'}]
|
2020-03-18 21:03:25 +01:00
|
|
|
"""
|
2021-07-12 11:57:29 +02:00
|
|
|
if not isinstance(path, six.string_types):
|
2021-07-08 12:31:38 +02:00
|
|
|
raise TypeError("The 'path' argument must be a String")
|
2021-05-12 10:01:53 +02:00
|
|
|
|
2020-11-03 15:29:42 +01:00
|
|
|
if not groups:
|
|
|
|
groups = []
|
2021-07-08 12:31:38 +02:00
|
|
|
elif not isinstance(groups, list):
|
|
|
|
raise TypeError("The 'groups' argument must be a List")
|
2020-11-03 15:29:42 +01:00
|
|
|
|
2021-07-08 12:31:38 +02:00
|
|
|
if not categories:
|
|
|
|
categories = []
|
|
|
|
elif not isinstance(categories, list):
|
|
|
|
raise TypeError("The 'categories' argument must be a List")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
|
|
|
results = []
|
2020-03-03 16:55:20 +01:00
|
|
|
validations_abspath = glob.glob("{path}/*.yaml".format(path=path))
|
2020-03-11 17:07:56 +01:00
|
|
|
|
2021-05-12 10:01:53 +02:00
|
|
|
LOG.debug(
|
2021-07-08 12:31:38 +02:00
|
|
|
"Attempting to parse validations by:\n"
|
|
|
|
" - groups: {}\n"
|
|
|
|
" - categories: {}\n"
|
|
|
|
"from {}".format(groups, categories, validations_abspath)
|
2021-05-12 10:01:53 +02:00
|
|
|
)
|
|
|
|
|
2021-05-27 08:36:54 +02:00
|
|
|
for playbook in validations_abspath:
|
|
|
|
val = Validation(playbook)
|
2021-01-27 13:17:57 -05:00
|
|
|
|
2021-07-08 12:31:38 +02:00
|
|
|
if not groups and not categories:
|
2020-03-18 21:03:25 +01:00
|
|
|
results.append(val.get_metadata)
|
2021-07-08 12:31:38 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if set(groups).intersection(val.groups) or \
|
|
|
|
set(categories).intersection(val.categories):
|
|
|
|
results.append(val.get_metadata)
|
|
|
|
|
2020-03-03 16:55:20 +01:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
2021-07-08 12:31:38 +02:00
|
|
|
def get_validations_playbook(path,
|
|
|
|
validation_id=None,
|
|
|
|
groups=None,
|
|
|
|
categories=None):
|
|
|
|
"""Get a list of validations playbooks paths either by their names,
|
|
|
|
their groups or by their categories.
|
2020-10-15 11:13:13 +02:00
|
|
|
|
|
|
|
:param path: Path of the validations playbooks
|
|
|
|
:type path: `string`
|
2021-07-08 12:31:38 +02:00
|
|
|
|
2020-10-15 11:13:13 +02:00
|
|
|
:param validation_id: List of validation name
|
2021-07-12 11:57:29 +02:00
|
|
|
:type validation_id: `list`
|
2021-07-08 12:31:38 +02:00
|
|
|
|
2020-10-15 11:13:13 +02:00
|
|
|
:param groups: List of validation group
|
2021-07-12 11:57:29 +02:00
|
|
|
:type groups: `list`
|
2021-07-08 12:31:38 +02:00
|
|
|
|
|
|
|
:param categories: List of validation category
|
|
|
|
:type categories: `list`
|
|
|
|
|
2020-10-15 11:13:13 +02:00
|
|
|
:return: A list of absolute validations playbooks path
|
2020-11-09 14:24:40 +01:00
|
|
|
:rtype: `list`
|
2020-10-15 11:13:13 +02:00
|
|
|
|
2020-11-09 14:24:40 +01:00
|
|
|
:Example:
|
2020-10-15 11:13:13 +02:00
|
|
|
|
|
|
|
>>> path = '/usr/share/validation-playbooks'
|
|
|
|
>>> validation_id = ['512e','check-cpu']
|
|
|
|
>>> groups = None
|
2021-07-08 12:31:38 +02:00
|
|
|
>>> categories = None
|
|
|
|
>>> get_validations_playbook(path, validation_id, groups, categories)
|
2020-10-15 11:13:13 +02:00
|
|
|
['/usr/share/ansible/validation-playbooks/512e.yaml',
|
|
|
|
'/usr/share/ansible/validation-playbooks/check-cpu.yaml',]
|
2020-03-24 00:01:22 +01:00
|
|
|
"""
|
2021-07-12 11:57:29 +02:00
|
|
|
if not isinstance(path, six.string_types):
|
2021-07-08 12:31:38 +02:00
|
|
|
raise TypeError("The 'path' argument must be a String")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
2020-11-03 15:29:42 +01:00
|
|
|
if not validation_id:
|
|
|
|
validation_id = []
|
2021-07-08 12:31:38 +02:00
|
|
|
elif not isinstance(validation_id, list):
|
|
|
|
raise TypeError("The 'validation_id' argument must be a List")
|
2020-11-03 15:29:42 +01:00
|
|
|
|
|
|
|
if not groups:
|
|
|
|
groups = []
|
2021-07-08 12:31:38 +02:00
|
|
|
elif not isinstance(groups, list):
|
|
|
|
raise TypeError("The 'groups' argument must be a List")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
2021-07-08 12:31:38 +02:00
|
|
|
if not categories:
|
|
|
|
categories = []
|
|
|
|
elif not isinstance(categories, list):
|
|
|
|
raise TypeError("The 'categories' argument must be a List")
|
2020-11-03 15:29:42 +01:00
|
|
|
|
2020-03-24 00:01:22 +01:00
|
|
|
pl = []
|
2020-04-01 15:48:23 +02:00
|
|
|
for f in os.listdir(path):
|
2020-03-24 00:01:22 +01:00
|
|
|
pl_path = join(path, f)
|
2020-04-01 15:48:23 +02:00
|
|
|
if os.path.isfile(pl_path):
|
2020-10-15 11:13:13 +02:00
|
|
|
if validation_id:
|
2020-10-27 14:43:55 +01:00
|
|
|
if os.path.splitext(f)[0] in validation_id or \
|
|
|
|
os.path.basename(f) in validation_id:
|
2020-10-15 11:13:13 +02:00
|
|
|
pl.append(pl_path)
|
2021-07-08 12:31:38 +02:00
|
|
|
|
|
|
|
val = Validation(pl_path)
|
2020-10-15 11:13:13 +02:00
|
|
|
if groups:
|
|
|
|
if set(groups).intersection(val.groups):
|
2020-03-24 00:01:22 +01:00
|
|
|
pl.append(pl_path)
|
2021-07-08 12:31:38 +02:00
|
|
|
if categories:
|
|
|
|
if set(categories).intersection(val.categories):
|
|
|
|
pl.append(pl_path)
|
2020-03-24 00:01:22 +01:00
|
|
|
return pl
|
|
|
|
|
|
|
|
|
2020-03-03 16:55:20 +01:00
|
|
|
def get_validation_parameters(validation):
|
2020-03-18 21:03:25 +01:00
|
|
|
"""Return dictionary of parameters"""
|
|
|
|
return Validation(validation).get_vars
|
2020-03-04 09:02:17 +01:00
|
|
|
|
|
|
|
|
2020-03-18 23:10:27 +01:00
|
|
|
def read_validation_groups_file(groups_path=None):
|
2020-11-09 14:24:40 +01:00
|
|
|
"""Load groups.yaml file and return a dictionary with its contents
|
|
|
|
|
|
|
|
:params groups_path: The path the groups.yaml file
|
|
|
|
:type groups_path: `string`
|
|
|
|
:return: The group list with their descriptions
|
|
|
|
:rtype: `dict`
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
|
|
|
>>> read_validation_groups_file()
|
|
|
|
{'group1': [{'description': 'Group1 description.'}],
|
|
|
|
'group2': [{'description': 'Group2 description.'}]}
|
|
|
|
"""
|
2020-03-18 23:10:27 +01:00
|
|
|
gp = Group((groups_path if groups_path else
|
|
|
|
constants.VALIDATION_GROUPS_INFO))
|
|
|
|
return gp.get_data
|
2020-03-04 09:02:17 +01:00
|
|
|
|
|
|
|
|
2020-03-18 23:10:27 +01:00
|
|
|
def get_validation_group_name_list(groups_path=None):
|
2020-11-09 14:24:40 +01:00
|
|
|
"""Get the validation group name list only
|
|
|
|
|
|
|
|
:params groups_path: The path the groups.yaml file
|
|
|
|
:type groups_path: `string`
|
|
|
|
:return: The group name list
|
|
|
|
:rtype: `list`
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
|
|
|
>>> get_validation_group_name_list()
|
|
|
|
['group1',
|
|
|
|
'group2',
|
|
|
|
'group3',
|
|
|
|
'group4']
|
|
|
|
"""
|
2020-03-18 23:10:27 +01:00
|
|
|
gp = Group((groups_path if groups_path else
|
|
|
|
constants.VALIDATION_GROUPS_INFO))
|
|
|
|
return gp.get_groups_keys_list
|
2020-03-04 09:02:17 +01:00
|
|
|
|
|
|
|
|
2020-03-16 10:45:53 +01:00
|
|
|
def get_validations_details(validation):
|
2020-11-09 14:24:40 +01:00
|
|
|
"""Return information details for a validation
|
|
|
|
|
|
|
|
:param validation: Name of the validation
|
|
|
|
:type validation: `string`
|
|
|
|
:return: The information of the validation
|
|
|
|
:rtype: `dict`
|
|
|
|
:raises: a `TypeError` exception if `validation` is not a string
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
|
|
|
>>> validation = "check-something"
|
|
|
|
>>> get_validations_details(validation)
|
|
|
|
{'description': 'Verify that the server has enough something.',
|
|
|
|
'groups': ['group1', 'group2'],
|
2021-07-08 12:31:38 +02:00
|
|
|
'categories': ['category1', 'category2'],
|
2020-11-09 14:24:40 +01:00
|
|
|
'id': 'check-something',
|
|
|
|
'name': 'Verify the server fits the something requirements'}
|
|
|
|
"""
|
|
|
|
if not isinstance(validation, six.string_types):
|
2021-07-08 12:31:38 +02:00
|
|
|
raise TypeError("The 'validation' argument must be a String")
|
2020-11-09 14:24:40 +01:00
|
|
|
|
2020-03-16 10:45:53 +01:00
|
|
|
results = parse_all_validations_on_disk(constants.ANSIBLE_VALIDATION_DIR)
|
|
|
|
for r in results:
|
|
|
|
if r['id'] == validation:
|
|
|
|
return r
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2020-03-24 14:47:16 +01:00
|
|
|
def get_validations_data(validation, path=constants.ANSIBLE_VALIDATION_DIR):
|
2020-11-09 14:24:40 +01:00
|
|
|
"""Return validation data with format:
|
|
|
|
|
|
|
|
ID, Name, Description, Groups, Parameters
|
|
|
|
|
|
|
|
:param validation: Name of the validation without the `yaml` extension.
|
|
|
|
Defaults to `constants.ANSIBLE_VALIDATION_DIR`
|
|
|
|
:type validation: `string`
|
|
|
|
:param path: The path to the validations directory
|
|
|
|
:type path: `string`
|
|
|
|
:return: The validation data with the format
|
|
|
|
(ID, Name, Description, Groups, Parameters)
|
|
|
|
:rtype: `dict`
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
|
|
|
>>> validation = 'check-something'
|
|
|
|
>>> get_validations_data(validation)
|
|
|
|
{'Description': 'Verify that the server has enough something',
|
|
|
|
'Groups': ['group1', 'group2'],
|
2021-07-08 12:31:38 +02:00
|
|
|
'Categories': ['category1', 'category2'],
|
2020-11-09 14:24:40 +01:00
|
|
|
'ID': 'check-something',
|
|
|
|
'Name': 'Verify the server fits the something requirements',
|
|
|
|
'Parameters': {'param1': 24}}
|
2020-03-17 16:10:17 +01:00
|
|
|
"""
|
2020-11-09 14:24:40 +01:00
|
|
|
if not isinstance(validation, six.string_types):
|
2021-07-08 12:31:38 +02:00
|
|
|
raise TypeError("The 'validation' argument must be a String")
|
2020-11-09 14:24:40 +01:00
|
|
|
|
2020-10-13 23:58:20 +02:00
|
|
|
data = {}
|
2020-03-24 14:47:16 +01:00
|
|
|
val_path = "{}/{}.yaml".format(path, validation)
|
2021-05-12 14:29:35 +02:00
|
|
|
|
|
|
|
LOG.debug(
|
|
|
|
"Obtaining information about validation {} from {}".format(
|
|
|
|
validation,
|
|
|
|
val_path)
|
|
|
|
)
|
|
|
|
|
2020-03-24 14:47:16 +01:00
|
|
|
if os.path.exists(val_path):
|
2020-10-13 23:58:20 +02:00
|
|
|
val = Validation(val_path)
|
|
|
|
data.update(val.get_formated_data)
|
|
|
|
data.update({'Parameters': val.get_vars})
|
|
|
|
return data
|
2020-03-16 10:45:53 +01:00
|
|
|
|
|
|
|
|
2021-07-12 11:57:29 +02:00
|
|
|
def get_validations_parameters(validations_data,
|
|
|
|
validation_name=None,
|
2021-07-08 12:31:38 +02:00
|
|
|
groups=None,
|
|
|
|
categories=None):
|
2020-11-09 14:24:40 +01:00
|
|
|
"""Return parameters for a list of validations
|
|
|
|
|
|
|
|
|
|
|
|
:param validations_data: A list of absolute validations playbooks path
|
|
|
|
:type validations_data: `list`
|
|
|
|
:param validation_name: A list of validation name
|
|
|
|
:type validation_name: `list`
|
|
|
|
:param groups: A list of validation groups
|
|
|
|
:type groups: `list`
|
2021-07-08 12:31:38 +02:00
|
|
|
:param categories: A list of validation categories
|
|
|
|
:type categories: `list`
|
2020-11-09 14:24:40 +01:00
|
|
|
:return: a dictionary containing the current parameters for
|
|
|
|
each `validation_name` or `groups`
|
|
|
|
:rtype: `dict`
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
|
|
|
>>> validations_data = ['/foo/bar/check-ram.yaml',
|
|
|
|
'/foo/bar/check-cpu.yaml']
|
|
|
|
>>> validation_name = ['check-ram', 'check-cpu']
|
|
|
|
>>> get_validations_parameters(validations_data, validation_name)
|
|
|
|
{'check-cpu': {'parameters': {'minimal_cpu_count': 8}},
|
|
|
|
'check-ram': {'parameters': {'minimal_ram_gb': 24}}}
|
2020-04-09 14:17:01 +02:00
|
|
|
"""
|
2021-07-12 11:57:29 +02:00
|
|
|
if not isinstance(validations_data, list):
|
2021-07-08 12:31:38 +02:00
|
|
|
raise TypeError("The 'validations_data' argument must be a List")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
|
|
|
if not validation_name:
|
|
|
|
validation_name = []
|
2021-07-08 12:31:38 +02:00
|
|
|
elif not isinstance(validation_name, list):
|
|
|
|
raise TypeError("The 'validation_name' argument must be a List")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
|
|
|
if not groups:
|
|
|
|
groups = []
|
2021-07-08 12:31:38 +02:00
|
|
|
elif not isinstance(groups, list):
|
|
|
|
raise TypeError("The 'groups' argument must be a List")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
2021-07-08 12:31:38 +02:00
|
|
|
if not categories:
|
|
|
|
categories = []
|
|
|
|
elif not isinstance(categories, list):
|
|
|
|
raise TypeError("The 'categories' argument must be a List")
|
2021-07-12 11:57:29 +02:00
|
|
|
|
2020-03-19 00:22:15 +01:00
|
|
|
params = {}
|
2020-04-01 15:48:23 +02:00
|
|
|
for val in validations_data:
|
2020-03-19 00:22:15 +01:00
|
|
|
v = Validation(val)
|
2021-07-08 12:31:38 +02:00
|
|
|
if v.id in validation_name or \
|
|
|
|
set(groups).intersection(v.groups) or \
|
|
|
|
set(categories).intersection(v.categories):
|
2020-03-19 00:22:15 +01:00
|
|
|
params[v.id] = {
|
2020-04-09 14:17:01 +02:00
|
|
|
'parameters': v.get_vars
|
2020-03-19 00:22:15 +01:00
|
|
|
}
|
2020-10-15 11:13:13 +02:00
|
|
|
|
|
|
|
return params
|