From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.wagner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 9ECBD9B2FF
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 16:06:15 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 80E9A2049D
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 16:05:45 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 16:05:44 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6A47946E4A
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 15:57:35 +0200 (CEST)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed, 24 May 2023 15:56:44 +0200
Message-Id: <20230524135649.934881-38-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20230524135649.934881-1-l.wagner@proxmox.com>
References: <20230524135649.934881-1-l.wagner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.149 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [notifications.pm]
Subject: [pve-devel] [PATCH v2 pve-manager 37/42] api: add api routes for
 notification channels
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 24 May 2023 14:06:15 -0000

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

diff --git a/PVE/API2/Cluster/Notifications.pm b/PVE/API2/Cluster/Notifications.pm
index 7d3bf53d..74538554 100644
--- a/PVE/API2/Cluster/Notifications.pm
+++ b/PVE/API2/Cluster/Notifications.pm
@@ -59,6 +59,7 @@ __PACKAGE__->register_method ({
     code => sub {
 	my $result = [
 	    { name => 'endpoints' },
+	    { name => 'channels' },
 	];
 
 	return $result;
@@ -131,4 +132,254 @@ __PACKAGE__->register_method ({
     }
 });
 
+my $channel_properties = {
+    name => {
+	description => 'Name of the channel.',
+	type => 'string',
+	format => 'pve-configid',
+    },
+    'endpoint' => {
+	type => 'array',
+	items => {
+	    type => 'string',
+	    format => 'pve-configid',
+	},
+	description => 'List of included endpoints',
+	optional => 1,
+    },
+    'comment' => {
+	description => 'Comment',
+	type        => 'string',
+	optional    => 1,
+    },
+};
+
+my $channel_create_properties = {
+    name => {
+	description => 'Name of the channel.',
+	type => 'string',
+	format => 'pve-configid',
+    },
+    'endpoint' => {
+	type => 'string',
+	format => 'pve-configid-list',
+	description => 'List of included endpoints',
+	optional => 1,
+    },
+    'comment' => {
+	description => 'Comment',
+	type        => 'string',
+	optional    => 1,
+    },
+};
+
+# when updating, every property (except for 'name') becomes optional
+my $channel_update_properties = make_properties_optional($channel_create_properties);
+
+__PACKAGE__->register_method ({
+    name => 'get_channels',
+    path => 'channels',
+    method => 'GET',
+    description => 'Returns a list of all channels',
+    protected => 1,
+    permissions => {
+	check => ['perm', '/', ['Sys.Audit']],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => 'object',
+	    properties => $channel_properties,
+	},
+	links => [ { rel => 'child', href => '{name}' } ],
+    },
+    code => sub {
+	my $config = PVE::Notify::read_config();
+
+	my $channels = eval {
+	    $config->get_channels()
+	};
+
+	raise_api_error($@) if ($@);
+	return $channels;
+
+    }
+});
+
+__PACKAGE__->register_method ({
+    name => 'get_channel',
+    path => 'channels/{name}',
+    method => 'GET',
+    description => 'Return a specific channel',
+    protected => 1,
+    permissions => {
+	check => ['perm', '/', ['Sys.Audit']],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    name => {
+		type => 'string',
+		format => 'pve-configid',
+	    },
+	}
+    },
+    returns => {
+	type => 'object',
+	properties => $channel_properties,
+    },
+    code => sub {
+	my ($param) = @_;
+	my $name = extract_param($param, 'name');
+
+	my $config = PVE::Notify::read_config();
+
+	my $channel = eval {
+	    $config->get_channel($name)
+	};
+
+	raise_api_error($@) if ($@);
+	return $channel;
+    }
+});
+
+__PACKAGE__->register_method ({
+    name => 'create_channel',
+    path => 'channels',
+    protected => 1,
+    method => 'POST',
+    description => 'Create a new channel',
+    permissions => {
+	check => ['perm', '/', ['Sys.Modify']],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => $channel_create_properties,
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $name = extract_param($param, 'name');
+	my $endpoint = extract_param($param, 'endpoint');
+	my $comment = extract_param($param, 'comment');
+
+	if (defined $endpoint) {
+	    $endpoint = [PVE::Tools::split_list($endpoint)];
+	}
+
+	PVE::Notify::lock_config(sub {
+	    my $config = PVE::Notify::read_config();
+
+	    $config->add_channel(
+		$name,
+		$endpoint,
+		$comment,
+	    );
+
+	    PVE::Notify::write_config($config);
+	});
+
+	raise_api_error($@) if ($@);
+	return;
+    }
+});
+
+__PACKAGE__->register_method ({
+    name => 'update_channel',
+    path => 'channels/{name}',
+    protected => 1,
+    method => 'PUT',
+    description => 'Update existing channel',
+    permissions => {
+	check => ['perm', '/', ['Sys.Modify']],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    %$channel_update_properties,
+	    delete => {
+		type => 'string',
+		format => 'pve-configid-list',
+		description => 'A list of settings you want to delete.',
+		maxLength => 4096,
+		optional => 1,
+	    },
+	    digest => get_standard_option('pve-config-digest'),
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $name = extract_param($param, 'name');
+	my $endpoint = extract_param($param, 'endpoint');
+	my $comment = extract_param($param, 'comment');
+	my $digest = extract_param($param, 'digest');
+	my $delete = extract_param($param, 'delete');
+
+	if (defined $endpoint) {
+	    $endpoint = [PVE::Tools::split_list($endpoint)];
+	}
+
+	if (defined $delete) {
+	    $delete = [PVE::Tools::split_list($delete)];
+	}
+
+	PVE::Notify::lock_config(sub {
+	    my $config = PVE::Notify::read_config();
+
+	    $config->update_channel(
+		$name,
+		$endpoint,
+		$comment,
+		$delete,
+		$digest,
+	    );
+
+	    PVE::Notify::write_config($config);
+	});
+
+	raise_api_error($@) if ($@);
+	return;
+    }
+});
+
+__PACKAGE__->register_method ({
+    name => 'delete_channel',
+    protected => 1,
+    path => 'channels/{name}',
+    method => 'DELETE',
+    description => 'Remove channel',
+    permissions => {
+	check => ['perm', '/', ['Sys.Modify']],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    name => {
+		type => 'string',
+		format => 'pve-configid',
+	    },
+	}
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+	my $name = extract_param($param, 'name');
+
+	PVE::Notify::lock_config(sub {
+	    my $config = PVE::Notify::read_config();
+	    $config->delete_channel($name);
+	    PVE::Notify::write_config($config);
+	});
+
+	raise_api_error($@) if ($@);
+	return;
+    }
+});
 1;
-- 
2.30.2