[pve-devel] [PATCH v2 pve-manager 37/42] api: add api routes for notification channels

Lukas Wagner l.wagner at proxmox.com
Wed May 24 15:56:44 CEST 2023


Signed-off-by: Lukas Wagner <l.wagner at 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






More information about the pve-devel mailing list