def is_entity_version_match_playbook_version( self, implemented_entity_list_from_playbook, main_playbook_version, entity_set_from_id_set, playbook_name, file_path): """Check if the playbook's version match playbook's entities (script or sub-playbook) Goes over the relevant entity set from id_set and check if the version of this entity match is equal or lower to the main playbook's version. For example, for given scripts list : implemented_entity_list_from_playbook = ["script1", "script2"], main playbook version = "5.0.0". This code searches for script1 version in the scripts set (in id_set) and returns True only if script1 version <= "5.0.0." (main playbook version), otherwise returns False. Does the same for "script2". Args: implemented_entity_list_from_playbook (list): List of relevant entities yo check from playbook. For example, list of implementing_scripts or list of implementing_playbooks. main_playbook_version (str): Playbook's from version. entity_set_from_id_set (dict) : Entity's data set (scripts or playbooks) from id_set file. playbook_name (str) : Playbook's name. file_path (string): Path to the file (current playbook). Returns: bool. Whether the playbook's version match playbook's entities. """ for entity_data_dict in entity_set_from_id_set: if not implemented_entity_list_from_playbook: return True entity_id = list(entity_data_dict.keys())[0] all_entity_fields = entity_data_dict[entity_id] entity_name = entity_id if entity_id in implemented_entity_list_from_playbook else all_entity_fields.get( "name") is_entity_used_in_playbook = entity_name in implemented_entity_list_from_playbook if is_entity_used_in_playbook: entity_version = all_entity_fields.get("fromversion", "") is_version_valid = not entity_version or LooseVersion( entity_version) <= LooseVersion(main_playbook_version) if is_version_valid: implemented_entity_list_from_playbook.remove(entity_name) if implemented_entity_list_from_playbook: error_message, error_code = Errors.content_entity_version_not_match_playbook_version( playbook_name, implemented_entity_list_from_playbook, main_playbook_version) if self.handle_error(error_message, error_code, file_path): return False return True
def is_entity_version_match_playbook_version( self, implemented_entity_list_from_playbook, main_playbook_version, entity_set_from_id_set, playbook_name, file_path, main_playbook_data): """Check if the playbook's version match playbook's entities (script or sub-playbook) Goes over the relevant entity set from id_set and check if the version of this entity match is equal or lower to the main playbook's version. For example, for given scripts list : implemented_entity_list_from_playbook = ["script1", "script2"], main playbook version = "5.0.0". This code searches for script1 version in the scripts set (in id_set) and returns True only if script1 version <= "5.0.0." (main playbook version), otherwise returns False. Does the same for "script2". Args: implemented_entity_list_from_playbook (list): List of relevant entities yo check from playbook. For example, list of implementing_scripts or list of implementing_playbooks. main_playbook_version (str): Playbook's from version. entity_set_from_id_set (dict) : Entity's data set (scripts or playbooks) from id_set file. playbook_name (str) : Playbook's name. file_path (string): Path to the file (current playbook). main_playbook_data (dict): Data of the main playbook. Returns: bool. Whether the playbook's version match playbook's entities. """ # the following dict holds the playbook names as keys and true/false whether the version is valid. # it handles the case where multiple playbook IDs appear in the id_set and each one of them support different versions. # for example: # main_playbook_version = '5.0.0' # id_set = [{'name': 'pb1', 'fromversion': '5.0.0', 'toversion': '5.4.9'}, {'name': 'pb1' - 'fromversion': '5.5.0'}] # entity_status will look like that: { 'pb1': True} entity_status: dict = {} implemented_entities = implemented_entity_list_from_playbook.copy() is_valid = True, None for entity_data_dict in entity_set_from_id_set: if not implemented_entities: break entity_id = list(entity_data_dict.keys())[0] all_entity_fields = entity_data_dict[entity_id] entity_name = entity_id if entity_id in implemented_entity_list_from_playbook else all_entity_fields.get( "name") is_entity_used_in_playbook = entity_name in implemented_entity_list_from_playbook if is_entity_used_in_playbook: tasks_data = get_script_or_sub_playbook_tasks_from_playbook( searched_entity_name=entity_name, main_playbook_data=main_playbook_data) entity_version = all_entity_fields.get("fromversion", "") is_version_valid = not entity_version or LooseVersion( entity_version) <= LooseVersion(main_playbook_version) skip_unavailable = all(task_data.get('skipunavailable', False) for task_data in tasks_data) \ if tasks_data and LooseVersion(main_playbook_version) >= LooseVersion('6.0.0') else False # if entities with miss-matched versions were found and skipunavailable is # not set or main playbook fromversion is below 6.0.0, fail the validation if is_version_valid or skip_unavailable: entity_status[entity_id] = True else: entity_status.setdefault(entity_id, False) if entity_name in implemented_entities: implemented_entities.remove(entity_name) invalid_version_entities = [ entity_name for entity_name, status in entity_status.items() if status is False ] if invalid_version_entities: error_message, error_code = Errors.content_entity_version_not_match_playbook_version( playbook_name, invalid_version_entities, main_playbook_version) if self.handle_error(error_message, error_code, file_path): is_valid = False, error_message if implemented_entities: error_message, error_code = Errors.content_entity_is_not_in_id_set( playbook_name, implemented_entities) if self.handle_error(error_message, error_code, file_path): is_valid = False, error_message return is_valid