]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Cluster/Notifications.pm
1 package PVE
::API2
::Cluster
::Notifications
;
6 use Storable
qw(dclone);
9 use PVE
::Tools
qw(extract_param);
10 use PVE
::JSONSchema
qw(get_standard_option);
14 use base
qw(PVE::RESTHandler);
16 sub make_properties_optional
{
17 my ($properties) = @_;
18 $properties = dclone
($properties);
20 for my $key (keys %$properties) {
21 $properties->{$key}->{optional
} = 1 if $key ne 'name';
30 if (!(ref($api_error) eq 'HASH' && $api_error->{message
} && $api_error->{code
})) {
34 my $msg = "$api_error->{message}\n";
35 my $exc = PVE
::Exception-
>new($msg, code
=> $api_error->{code
});
37 my (undef, $filename, $line) = caller;
39 $exc->{filename
} = $filename;
45 sub filter_entities_by_privs
{
46 my ($rpcenv, $entities) = @_;
47 my $authuser = $rpcenv->get_user();
49 my $can_see_mapping_privs = ['Mapping.Modify', 'Mapping.Use', 'Mapping.Audit'];
51 my $filtered = [grep {
54 "/mapping/notification/$_->{name}",
55 $can_see_mapping_privs,
63 __PACKAGE__-
>register_method ({
67 description
=> 'Index for notification-related API endpoints.',
68 permissions
=> { user
=> 'all' },
70 additionalProperties
=> 0,
79 links
=> [ { rel
=> 'child', href
=> '{name}' } ],
83 { name
=> 'endpoints' },
91 __PACKAGE__-
>register_method ({
92 name
=> 'endpoints_index',
95 description
=> 'Index for all available endpoint types.',
96 permissions
=> { user
=> 'all' },
98 additionalProperties
=> 0,
107 links
=> [ { rel
=> 'child', href
=> '{name}' } ],
111 { name
=> 'sendmail' },
118 my $group_properties = {
120 description
=> 'Name of the group.',
122 format
=> 'pve-configid',
128 format
=> 'pve-configid',
130 description
=> 'List of included endpoints',
133 description
=> 'Comment',
138 description
=> 'Name of the filter that should be applied.',
140 format
=> 'pve-configid',
145 __PACKAGE__-
>register_method ({
146 name
=> 'get_groups',
149 description
=> 'Returns a list of all groups',
152 description
=> "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
153 . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
157 additionalProperties
=> 0,
164 properties
=> $group_properties,
166 links
=> [ { rel
=> 'child', href
=> '{name}' } ],
169 my $config = PVE
::Notify
::read_config
();
170 my $rpcenv = PVE
::RPCEnvironment
::get
();
172 my $entities = eval {
173 $config->get_groups();
175 raise_api_error
($@) if $@;
177 return filter_entities_by_privs
($rpcenv, $entities);
181 __PACKAGE__-
>register_method ({
183 path
=> 'groups/{name}',
185 description
=> 'Return a specific group',
189 ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
190 ['perm', '/mapping/notification/{name}', ['Mapping.Audit']],
194 additionalProperties
=> 0,
198 format
=> 'pve-configid',
206 digest
=> get_standard_option
('pve-config-digest'),
211 my $name = extract_param
($param, 'name');
213 my $config = PVE
::Notify
::read_config
();
216 $config->get_group($name)
219 raise_api_error
($@) if $@;
220 $group->{digest
} = $config->digest();
226 __PACKAGE__-
>register_method ({
227 name
=> 'create_group',
231 description
=> 'Create a new group',
233 check
=> ['perm', '/mapping/notification', ['Mapping.Modify']],
236 additionalProperties
=> 0,
237 properties
=> $group_properties,
239 returns
=> { type
=> 'null' },
243 my $name = extract_param
($param, 'name');
244 my $endpoint = extract_param
($param, 'endpoint');
245 my $comment = extract_param
($param, 'comment');
246 my $filter = extract_param
($param, 'filter');
249 PVE
::Notify
::lock_config
(sub {
250 my $config = PVE
::Notify
::read_config
();
259 PVE
::Notify
::write_config
($config);
263 raise_api_error
($@) if $@;
268 __PACKAGE__-
>register_method ({
269 name
=> 'update_group',
270 path
=> 'groups/{name}',
273 description
=> 'Update existing group',
275 check
=> ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
278 additionalProperties
=> 0,
280 %{ make_properties_optional
($group_properties) },
285 format
=> 'pve-configid',
288 description
=> 'A list of settings you want to delete.',
290 digest
=> get_standard_option
('pve-config-digest'),
293 returns
=> { type
=> 'null' },
297 my $name = extract_param
($param, 'name');
298 my $endpoint = extract_param
($param, 'endpoint');
299 my $comment = extract_param
($param, 'comment');
300 my $filter = extract_param
($param, 'filter');
301 my $digest = extract_param
($param, 'digest');
302 my $delete = extract_param
($param, 'delete');
305 PVE
::Notify
::lock_config
(sub {
306 my $config = PVE
::Notify
::read_config
();
308 $config->update_group(
317 PVE
::Notify
::write_config
($config);
321 raise_api_error
($@) if $@;
326 __PACKAGE__-
>register_method ({
327 name
=> 'delete_group',
329 path
=> 'groups/{name}',
331 description
=> 'Remove group',
333 check
=> ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
336 additionalProperties
=> 0,
340 format
=> 'pve-configid',
344 returns
=> { type
=> 'null' },
347 my $name = extract_param
($param, 'name');
350 PVE
::Notify
::lock_config
(sub {
351 my $config = PVE
::Notify
::read_config
();
352 $config->delete_group($name);
353 PVE
::Notify
::write_config
($config);
357 raise_api_error
($@) if $@;
362 my $sendmail_properties = {
364 description
=> 'The name of the endpoint.',
366 format
=> 'pve-configid',
372 format
=> 'email-or-username',
374 description
=> 'List of email recipients',
381 format
=> 'pve-userid',
383 description
=> 'List of users',
387 description
=> '`From` address for the mail',
392 description
=> 'Author of the mail',
397 description
=> 'Comment',
402 description
=> 'Name of the filter that should be applied.',
404 format
=> 'pve-configid',
409 __PACKAGE__-
>register_method ({
410 name
=> 'get_sendmail_endpoints',
411 path
=> 'endpoints/sendmail',
413 description
=> 'Returns a list of all sendmail endpoints',
415 description
=> "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
416 . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
421 additionalProperties
=> 0,
428 properties
=> $sendmail_properties,
430 links
=> [ { rel
=> 'child', href
=> '{name}' } ],
433 my $config = PVE
::Notify
::read_config
();
434 my $rpcenv = PVE
::RPCEnvironment
::get
();
436 my $entities = eval {
437 $config->get_sendmail_endpoints();
439 raise_api_error
($@) if $@;
441 return filter_entities_by_privs
($rpcenv, $entities);
445 __PACKAGE__-
>register_method ({
446 name
=> 'get_sendmail_endpoint',
447 path
=> 'endpoints/sendmail/{name}',
449 description
=> 'Return a specific sendmail endpoint',
452 ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
453 ['perm', '/mapping/notification/{name}', ['Mapping.Audit']],
458 additionalProperties
=> 0,
462 format
=> 'pve-configid',
469 %$sendmail_properties,
470 digest
=> get_standard_option
('pve-config-digest'),
476 my $name = extract_param
($param, 'name');
478 my $config = PVE
::Notify
::read_config
();
479 my $endpoint = eval {
480 $config->get_sendmail_endpoint($name)
483 raise_api_error
($@) if $@;
484 $endpoint->{digest
} = $config->digest();
490 __PACKAGE__-
>register_method ({
491 name
=> 'create_sendmail_endpoint',
492 path
=> 'endpoints/sendmail',
495 description
=> 'Create a new sendmail endpoint',
497 check
=> ['perm', '/mapping/notification', ['Mapping.Modify']],
500 additionalProperties
=> 0,
501 properties
=> $sendmail_properties,
503 returns
=> { type
=> 'null' },
507 my $name = extract_param
($param, 'name');
508 my $mailto = extract_param
($param, 'mailto');
509 my $mailto_user = extract_param
($param, 'mailto-user');
510 my $from_address = extract_param
($param, 'from-address');
511 my $author = extract_param
($param, 'author');
512 my $comment = extract_param
($param, 'comment');
513 my $filter = extract_param
($param, 'filter');
516 PVE
::Notify
::lock_config
(sub {
517 my $config = PVE
::Notify
::read_config
();
519 $config->add_sendmail_endpoint(
529 PVE
::Notify
::write_config
($config);
533 raise_api_error
($@) if $@;
538 __PACKAGE__-
>register_method ({
539 name
=> 'update_sendmail_endpoint',
540 path
=> 'endpoints/sendmail/{name}',
543 description
=> 'Update existing sendmail endpoint',
545 check
=> ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
548 additionalProperties
=> 0,
550 %{ make_properties_optional
($sendmail_properties) },
555 format
=> 'pve-configid',
558 description
=> 'A list of settings you want to delete.',
560 digest
=> get_standard_option
('pve-config-digest'),
564 returns
=> { type
=> 'null' },
568 my $name = extract_param
($param, 'name');
569 my $mailto = extract_param
($param, 'mailto');
570 my $mailto_user = extract_param
($param, 'mailto-user');
571 my $from_address = extract_param
($param, 'from-address');
572 my $author = extract_param
($param, 'author');
573 my $comment = extract_param
($param, 'comment');
574 my $filter = extract_param
($param, 'filter');
576 my $delete = extract_param
($param, 'delete');
577 my $digest = extract_param
($param, 'digest');
580 PVE
::Notify
::lock_config
(sub {
581 my $config = PVE
::Notify
::read_config
();
583 $config->update_sendmail_endpoint(
595 PVE
::Notify
::write_config
($config);
599 raise_api_error
($@) if $@;
604 __PACKAGE__-
>register_method ({
605 name
=> 'delete_sendmail_endpoint',
607 path
=> 'endpoints/sendmail/{name}',
609 description
=> 'Remove sendmail endpoint',
611 check
=> ['perm', '/mapping/notification', ['Mapping.Modify']],
614 additionalProperties
=> 0,
618 format
=> 'pve-configid',
622 returns
=> { type
=> 'null' },
627 PVE
::Notify
::lock_config
(sub {
628 my $config = PVE
::Notify
::read_config
();
629 $config->delete_sendmail_endpoint($param->{name
});
630 PVE
::Notify
::write_config
($config);
634 raise_api_error
($@) if ($@);