From 8718b2a9f5418d45f63c2e1e30223c90fb53ad77 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Thu, 30 Mar 2023 13:23:23 +0200 Subject: [PATCH] implemented module cascade deletion --- pandora_server/lib/PandoraFMS/Core.pm | 16 ++++++++++++++-- pandora_server/util/pandora_manage.pl | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 4726df4710..6d807183cc 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -3553,9 +3553,21 @@ sub pandora_create_module ($$$$$$$$$$) { ########################################################################## ## Delete a module given its id. ########################################################################## -sub pandora_delete_module ($$;$) { - my ($dbh, $module_id, $conf) = @_; +sub pandora_delete_module { + my $dbh = shift; + my $module_id = shift; + my $conf = shift if @_; + my $cascade = shift if @_; + # Recursively delete descendants (delete in cascade) + if (defined($cascade) && $cascade eq 1) { + my @id_children_modules = get_db_rows($dbh, 'SELECT id_agente_modulo FROM tagente_modulo WHERE parent_module_id = ?', $module_id); + + foreach my $id_child_module (@id_children_modules) { + pandora_delete_module($dbh, $id_child_module->{'id_agente_modulo'}, $conf, 1); + } + } + # Get module data my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.id_agente_modulo=?', $module_id); return unless defined ($module); diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index ac6e97e34a..7cc5a4456a 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -2933,7 +2933,7 @@ sub cli_delete_module() { next; } - pandora_delete_module($dbh,$id_module,$conf); + pandora_delete_module($dbh, $id_module, $conf, 1); } } else { print_log "[INFO] Deleting module '$module_name' from agent '$agent_name' \n\n"; @@ -2943,7 +2943,7 @@ sub cli_delete_module() { my $id_module = get_agent_module_id($dbh,$module_name,$id_agent); exist_check($id_module,'module',$module_name); - pandora_delete_module($dbh,$id_module,$conf); + pandora_delete_module($dbh, $id_module, $conf, 1); } }