Exemple #1
0
    def remove_non_existent_fields(self):
        """
        Remove non-existent fields from a mapper.
        """
        if not self.id_set_file:
            logger.warning(
                f'Skipping formatting of non-existent-fields for {self.source_file} as id_set_path argument is missing'
            )
            return

        content_fields = get_all_incident_and_indicator_fields_from_id_set(
            self.id_set_file, 'mapper') + [
                field.lower() for field in BUILT_IN_FIELDS
            ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS

        mapper = self.data.get('mapping', {})
        mapping_type = self.data.get('type', {})

        for mapping_name in mapper.values():
            internal_mapping_fields = mapping_name.get('internalMapping', {})
            mapping_name['internalMapping'] = {
                inc_name: inc_info
                for inc_name, inc_info in internal_mapping_fields.items()
                if inc_name not in get_invalid_incident_fields_from_mapper(
                    mapper_incident_fields=internal_mapping_fields,
                    mapping_type=mapping_type,
                    content_fields=content_fields,
                )
            }
Exemple #2
0
    def is_incident_field_exist(self, id_set_file, is_circle) -> bool:
        """Checks if incident field is valid - exist in the content.

        Returns:
            bool. True if incident field is valid, else False.
        """
        if not is_circle:
            return True

        if not id_set_file:
            click.secho(
                "Skipping mapper incident field validation. Could not read id_set.json.",
                fg="yellow")
            return True

        layout_incident_fields = []

        layout = self.current_file.get('layout', {})
        layout_sections = layout.get('sections', [])
        for section in layout_sections:
            for field in section.get('fields', []):
                inc_field = field.get('fieldId', '')
                layout_incident_fields.append(
                    inc_field.replace('incident_', ''))

        layout_tabs = layout.get('tabs', [])
        for tab in layout_tabs:
            layout_sections = tab.get('sections', [])

            for section in layout_sections:
                if section and section.get('items'):
                    for item in section.get('items', []):
                        inc_field = item.get('fieldId', '')
                        layout_incident_fields.append(
                            inc_field.replace('incident_',
                                              '').replace('indicator_', ''))

        content_incident_fields = get_all_incident_and_indicator_fields_from_id_set(
            id_set_file, 'layout')

        built_in_fields = [field.lower() for field in BUILT_IN_FIELDS
                           ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS

        invalid_inc_fields_list = []
        for inc_field in layout_incident_fields:
            if inc_field and inc_field.lower(
            ) not in built_in_fields and inc_field not in content_incident_fields:
                invalid_inc_fields_list.append(
                    inc_field
                ) if inc_field not in invalid_inc_fields_list else None

        if invalid_inc_fields_list:
            error_message, error_code = Errors.invalid_incident_field_in_layout(
                invalid_inc_fields_list)
            if self.handle_error(error_message,
                                 error_code,
                                 file_path=self.file_path):
                return False
        return True
Exemple #3
0
 def get_available_content_fields(self):
     """
     Get all the available content indicator/incident fields available + all the built in fields.
     """
     return get_all_incident_and_indicator_fields_from_id_set(
         self.id_set_file, 'layout') + [
             field.lower() for field in BUILT_IN_FIELDS
         ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS
Exemple #4
0
    def get_fields_from_id_set(id_set_file: Dict[str, List]) -> List[str]:
        """
        Get all the available layout fields from the id set.

        Args:
            id_set_file (dict): content of the id set file.

        Returns:
            list[str]: available indicator/incident fields from the id set file.
        """
        return get_all_incident_and_indicator_fields_from_id_set(id_set_file, 'layout') + [
            field.lower() for field in BUILT_IN_FIELDS
        ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS
Exemple #5
0
    def is_incident_field_exist(self, id_set_file, is_circle) -> bool:
        """Checks if incident field is valid - exist in the content.

        Returns:
            bool. True if incident field is valid, else False.
        """
        if not is_circle:
            return True

        if not id_set_file:
            click.secho(
                "Skipping mapper incident field validation. Could not read id_set.json.",
                fg="yellow")
            return True

        built_in_fields = [field.lower() for field in BUILT_IN_FIELDS
                           ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS

        content_incident_fields = get_all_incident_and_indicator_fields_from_id_set(
            id_set_file, 'mapper')

        invalid_inc_fields_list = []
        mapper = self.current_file.get('mapping', {})
        for key, value in mapper.items():
            incident_fields = value.get('internalMapping', {})

            for inc_name, inc_info in incident_fields.items():
                # for incoming mapper
                if self.current_file.get('type', {}) == "mapping-incoming":
                    if inc_name not in content_incident_fields and inc_name.lower(
                    ) not in built_in_fields:
                        invalid_inc_fields_list.append(inc_name)

                # for outgoing mapper
                if self.current_file.get('type', {}) == "mapping-outgoing":
                    # for inc timer type: "field.StartDate, and for using filters: "simple": "".
                    if inc_info['simple'] not in content_incident_fields and inc_info['simple'] not in built_in_fields\
                            and inc_info['simple'].split('.')[0] not in content_incident_fields and inc_info['simple']:
                        invalid_inc_fields_list.append(
                            inc_name) if inc_info['simple'] else None

        if invalid_inc_fields_list:
            error_message, error_code = Errors.invalid_incident_field_in_mapper(
                invalid_inc_fields_list)
            if self.handle_error(error_message,
                                 error_code,
                                 file_path=self.file_path):
                return False
        return True
Exemple #6
0
    def is_incident_field_exist(self, id_set_file: Dict[str, List],
                                is_circle: bool) -> bool:
        """
        Check if the incident fields which are part of the mapper actually exist in the content items (id set).

        Args:
            id_set_file (dict): content of the id set file.
            is_circle (bool): whether running on circle CI or not, True if yes, False if not.

        Returns:
            bool: False if there are incident fields which are part of the mapper that do not exist in content items,
                True if there aren't.
        """
        if not is_circle:
            return True

        if not id_set_file:
            click.secho(
                "Skipping mapper incident field validation. Could not read id_set.json.",
                fg="yellow")
            return True

        content_incident_fields = get_all_incident_and_indicator_fields_from_id_set(
            id_set_file, 'mapper') + [
                field.lower() for field in BUILT_IN_FIELDS
            ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS

        invalid_incident_fields = []
        mapping_type = self.current_file.get('type', {})

        mapper = self.current_file.get('mapping', {})
        for value in mapper.values():
            incident_fields = value.get('internalMapping', {})
            invalid_incident_fields.extend(
                get_invalid_incident_fields_from_mapper(
                    mapper_incident_fields=incident_fields,
                    mapping_type=mapping_type,
                    content_fields=content_incident_fields,
                ))

        if invalid_incident_fields:
            error_message, error_code = Errors.invalid_incident_field_in_mapper(
                invalid_incident_fields)
            if self.handle_error(error_message,
                                 error_code,
                                 file_path=self.file_path):
                return False
        return True
Exemple #7
0
    def is_incident_field_exist(self, id_set_file, is_circle) -> bool:
        """Checks if classifier incident fields is exist in content repo, this validation is only for old classifiers.

        Returns:
            bool. True if incident fields is valid - exist in content repo, else False.
        """
        if not is_circle:
            return True

        if not id_set_file:
            click.secho(
                "Skipping classifier incident field validation. Could not read id_set.json.",
                fg="yellow")
            return True

        built_in_fields = [field.lower() for field in BUILT_IN_FIELDS
                           ] + LAYOUT_AND_MAPPER_BUILT_IN_FIELDS
        content_incident_fields = get_all_incident_and_indicator_fields_from_id_set(
            id_set_file, 'old classifier')

        invalid_inc_fields_list = []
        mapper = self.current_file.get('mapping', {})
        for incident_type, mapping in mapper.items():
            incident_fields = mapping.get('internalMapping', {})

            for inc_name, _ in incident_fields.items():
                if inc_name not in content_incident_fields and inc_name.lower(
                ) not in built_in_fields:
                    invalid_inc_fields_list.append(inc_name)

        if invalid_inc_fields_list:
            error_message, error_code = Errors.invalid_incident_field_in_mapper(
                invalid_inc_fields_list)
            if self.handle_error(error_message,
                                 error_code,
                                 file_path=self.file_path):
                return False
        return True