From 0c476c437cb45e298ac39b35cc1e37c152eb9202 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rajmund=20Hru=C5=A1ka?= <rajmund.hruska@cesnet.cz>
Date: Wed, 9 Nov 2022 13:35:51 +0100
Subject: [PATCH] Feature: Report extra detectors. (Redmine issue: #7577)

---
 lib/mentat/module/detmngr.py | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/lib/mentat/module/detmngr.py b/lib/mentat/module/detmngr.py
index 0e0da5818..7c9bf49bd 100644
--- a/lib/mentat/module/detmngr.py
+++ b/lib/mentat/module/detmngr.py
@@ -219,7 +219,7 @@ class MentatDetmngrScript(mentat.script.fetcher.FetcherScript):
         :return: Structure containing information about changes.
         :rtype: dict
         """
-        result = {'create': [], 'delete': {}, 'update': {}}
+        result = {'create': [], 'delete': [], 'update': {}}
         det_db = {}
 
         det_file = self.c(self.CONFIG_DETECTORS_FILE)
@@ -235,6 +235,7 @@ class MentatDetmngrScript(mentat.script.fetcher.FetcherScript):
             det_db[detector.name] = detector
 
         self._detectors_create_missing(det_db, det_file_data, det_file_type, result, status_only)
+        self._detectors_report_extra(det_db, det_file_data, det_file_type, result, status_only)
         return result
 
     def _load_detectors_file(self, detectors_file):
@@ -302,3 +303,23 @@ class MentatDetmngrScript(mentat.script.fetcher.FetcherScript):
                 self.logger.warning("'%s' Creating new detector.", gkey)
                 self.sqlservice.session.add(sqldet)
                 self.sqlservice.session.commit()
+
+    def _detectors_report_extra(self, det_db, det_file_data, det_file_type, result, status_only):
+        """
+        Report extra detectors from database.
+
+        :param dict det_db: Detectors loaded from the database.
+        :param dict det_file_data: Detectors loaded from the reference detectors file.
+        :param str det_file_type: Source of the detectors in the reference detectors file.
+        :param dict result: Structure containing processing log, will be appended to script runlog.
+        :param bool status_only: Do not actually perform any database operations, just report status.
+        """
+        for detector_name in sorted(det_db.keys()):
+            det = det_db[detector_name]
+
+            # For deletion consider only detectors with the same origin (source) as
+            # the loaded detectors file.
+            if det.source == det_file_type and detector_name not in det_file_data:
+                detkey = '{}::{}'.format(det.name, det.source)
+                result['delete'].append(detkey)
+                self.logger.warning("'%s' Detector was not found in the loaded detectors file, consider deletion.", detkey)
-- 
GitLab