]> git.proxmox.com Git - pve-manager.git/commitdiff
api: notification: disallow removing targets if they are used
authorLukas Wagner <l.wagner@proxmox.com>
Thu, 3 Aug 2023 12:17:03 +0000 (14:17 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 16 Aug 2023 09:11:08 +0000 (11:11 +0200)
Check notification targets configured in datacenter.cfg and jobs.cfg,
failing if the group/endpoint to be removed is still in use there.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
PVE/API2/Cluster/Notifications.pm

index 065d6690e12ab4c583b3de4af7edf0d7db993359..9c9e824387cf282a68444a1aa565a0d30599bb26 100644 (file)
@@ -6,6 +6,7 @@ use strict;
 use Storable qw(dclone);
 use JSON;
 
+use PVE::Exception qw(raise_param_exc);
 use PVE::Tools qw(extract_param);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RESTHandler;
@@ -73,6 +74,31 @@ sub filter_entities_by_privs {
     return $filtered;
 }
 
+sub target_used_by {
+    my ($target) = @_;
+
+    my $used_by = [];
+
+    # Check keys in datacenter.cfg
+    my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
+    for my $key (qw(target-package-updates target-replication target-fencing)) {
+       if ($dc_conf->{notify} && $dc_conf->{notify}->{$key} eq $target) {
+           push @$used_by, $key;
+       }
+    }
+
+    # Check backup jobs
+    my $jobs_conf = PVE::Cluster::cfs_read_file('jobs.cfg');
+    for my $key (keys %{$jobs_conf->{ids}}) {
+       my $job = $jobs_conf->{ids}->{$key};
+       if ($job->{'notification-target'} eq $target) {
+           push @$used_by, $key;
+       }
+    }
+
+    return join(', ', @$used_by);
+}
+
 __PACKAGE__->register_method ({
     name => 'index',
     path => '',
@@ -482,6 +508,11 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
        my $name = extract_param($param, 'name');
 
+       my $used_by = target_used_by($name);
+       if ($used_by) {
+           raise_param_exc({'name' => "Cannot remove $name, used by: $used_by"});
+       }
+
        eval {
            PVE::Notify::lock_config(sub {
                my $config = PVE::Notify::read_config();
@@ -758,11 +789,17 @@ __PACKAGE__->register_method ({
     returns => { type => 'null' },
     code => sub {
        my ($param) = @_;
+       my $name = extract_param($param, 'name');
+
+       my $used_by = target_used_by($name);
+       if ($used_by) {
+           raise_param_exc({'name' => "Cannot remove $name, used by: $used_by"});
+       }
 
        eval {
            PVE::Notify::lock_config(sub {
                my $config = PVE::Notify::read_config();
-               $config->delete_sendmail_endpoint($param->{name});
+               $config->delete_sendmail_endpoint($name);
                PVE::Notify::write_config($config);
            });
        };
@@ -1008,6 +1045,11 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
        my $name = extract_param($param, 'name');
 
+       my $used_by = target_used_by($name);
+       if ($used_by) {
+           raise_param_exc({'name' => "Cannot remove $name, used by: $used_by"});
+       }
+
        eval {
            PVE::Notify::lock_config(sub {
                my $config = PVE::Notify::read_config();