[pve-devel] [PATCH v3 pve-manager 51/66] api: notification: allow fetching notification targets

Lukas Wagner l.wagner at proxmox.com
Mon Jul 17 17:00:36 CEST 2023


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 PVE/API2/Cluster/Notifications.pm | 100 ++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/PVE/API2/Cluster/Notifications.pm b/PVE/API2/Cluster/Notifications.pm
index e358573c..32a873a7 100644
--- a/PVE/API2/Cluster/Notifications.pm
+++ b/PVE/API2/Cluster/Notifications.pm
@@ -78,6 +78,7 @@ __PACKAGE__->register_method ({
 	    { name => 'endpoints' },
 	    { name => 'filters' },
 	    { name => 'groups' },
+	    { name => 'targets' },
 	];
 
 	return $result;
@@ -112,6 +113,105 @@ __PACKAGE__->register_method ({
     }
 });
 
+__PACKAGE__->register_method ({
+    name => 'get_all_targets',
+    path => 'targets',
+    method => 'GET',
+    description => 'Returns a list of all entities that can be used as notification targets' .
+	' (endpoints and groups).',
+    permissions => {
+	description => "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
+	    . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
+	user => 'all',
+    },
+    protected => 1,
+    parameters => {
+	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => 'object',
+	    properties => {
+		name => {
+		    description => 'Name of the endpoint/group.',
+		    type => 'string',
+		    format => 'pve-configid',
+		},
+		'type' => {
+		    description => 'Type of the endpoint or group.',
+		    type  => 'string',
+		    enum => [qw(sendmail gotify group)],
+		},
+		'comment' => {
+		    description => 'Comment',
+		    type        => 'string',
+		    optional    => 1,
+		},
+	    },
+	},
+	links => [ { rel => 'child', href => '{name}' } ],
+    },
+    code => sub {
+	my $config = PVE::Notify::read_config();
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
+	my $can_see_mapping_privs = ['Mapping.Modify', 'Mapping.Use', 'Mapping.Audit'];
+
+	my $endpoints = eval {
+	    my $result = [];
+
+	    for my $endpoint (@{$config->get_sendmail_endpoints()}) {
+		next if !$rpcenv->check_any(
+		    $authuser,
+		    "/mapping/notification/$endpoint->{name}",
+		    $can_see_mapping_privs,
+		    1
+		);
+		push @$result, {
+		    name => $endpoint->{name},
+		    comment => $endpoint->{comment},
+		    type => 'sendmail',
+		};
+	    }
+
+	    for my $endpoint (@{$config->get_gotify_endpoints()}) {
+		next if !$rpcenv->check_any(
+		    $authuser,
+		    "/mapping/notification/$endpoint->{name}",
+		    $can_see_mapping_privs,
+		    1
+		);
+		push @$result, {
+		    name => $endpoint->{name},
+		    comment => $endpoint->{comment},
+		    type => 'gotify',
+		};
+	    }
+
+	    for my $endpoint (@{$config->get_groups()}) {
+		next if !$rpcenv->check_any(
+		    $authuser,
+		    "/mapping/notification/$endpoint->{name}",
+		    $can_see_mapping_privs,
+		    1
+		);
+		push @$result, {
+		    name => $endpoint->{name},
+		    comment => $endpoint->{comment},
+		    type => 'group',
+		};
+	    }
+
+	    $result
+	};
+
+	raise_api_error($@) if ($@);
+	return $endpoints;
+    }
+});
+
 my $group_properties = {
     name => {
 	description => 'Name of the group.',
-- 
2.39.2






More information about the pve-devel mailing list