]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Cluster/Notifications.pm
b4db7f8e60a7be9acc6214eadfeef6c0761cafc2
[pve-manager.git] / PVE / API2 / Cluster / Notifications.pm
1 package PVE::API2::Cluster::Notifications;
2
3 use warnings;
4 use strict;
5
6 use Storable qw(dclone);
7 use JSON;
8
9 use PVE::Tools qw(extract_param);
10 use PVE::JSONSchema qw(get_standard_option);
11 use PVE::RESTHandler;
12 use PVE::Notify;
13
14 use base qw(PVE::RESTHandler);
15
16 sub make_properties_optional {
17 my ($properties) = @_;
18 $properties = dclone($properties);
19
20 for my $key (keys %$properties) {
21 $properties->{$key}->{optional} = 1 if $key ne 'name';
22 }
23
24 return $properties;
25 }
26
27 sub remove_protected_properties {
28 my ($properties, $to_remove) = @_;
29 $properties = dclone($properties);
30
31 for my $key (keys %$properties) {
32 if (grep /^$key$/, @$to_remove) {
33 delete $properties->{$key};
34 }
35 }
36
37 return $properties;
38 }
39
40 sub raise_api_error {
41 my ($api_error) = @_;
42
43 if (!(ref($api_error) eq 'HASH' && $api_error->{message} && $api_error->{code})) {
44 die $api_error;
45 }
46
47 my $msg = "$api_error->{message}\n";
48 my $exc = PVE::Exception->new($msg, code => $api_error->{code});
49
50 my (undef, $filename, $line) = caller;
51
52 $exc->{filename} = $filename;
53 $exc->{line} = $line;
54
55 die $exc;
56 }
57
58 sub filter_entities_by_privs {
59 my ($rpcenv, $entities) = @_;
60 my $authuser = $rpcenv->get_user();
61
62 my $can_see_mapping_privs = ['Mapping.Modify', 'Mapping.Use', 'Mapping.Audit'];
63
64 my $filtered = [grep {
65 $rpcenv->check_any(
66 $authuser,
67 "/mapping/notification/$_->{name}",
68 $can_see_mapping_privs,
69 1
70 )
71 } @$entities];
72
73 return $filtered;
74 }
75
76 __PACKAGE__->register_method ({
77 name => 'index',
78 path => '',
79 method => 'GET',
80 description => 'Index for notification-related API endpoints.',
81 permissions => { user => 'all' },
82 parameters => {
83 additionalProperties => 0,
84 properties => {},
85 },
86 returns => {
87 type => 'array',
88 items => {
89 type => 'object',
90 properties => {},
91 },
92 links => [ { rel => 'child', href => '{name}' } ],
93 },
94 code => sub {
95 my $result = [
96 { name => 'endpoints' },
97 { name => 'filters' },
98 { name => 'groups' },
99 ];
100
101 return $result;
102 }
103 });
104
105 __PACKAGE__->register_method ({
106 name => 'endpoints_index',
107 path => 'endpoints',
108 method => 'GET',
109 description => 'Index for all available endpoint types.',
110 permissions => { user => 'all' },
111 parameters => {
112 additionalProperties => 0,
113 properties => {},
114 },
115 returns => {
116 type => 'array',
117 items => {
118 type => 'object',
119 properties => {},
120 },
121 links => [ { rel => 'child', href => '{name}' } ],
122 },
123 code => sub {
124 my $result = [
125 { name => 'gotify' },
126 { name => 'sendmail' },
127 ];
128
129 return $result;
130 }
131 });
132
133 my $group_properties = {
134 name => {
135 description => 'Name of the group.',
136 type => 'string',
137 format => 'pve-configid',
138 },
139 'endpoint' => {
140 type => 'array',
141 items => {
142 type => 'string',
143 format => 'pve-configid',
144 },
145 description => 'List of included endpoints',
146 },
147 'comment' => {
148 description => 'Comment',
149 type => 'string',
150 optional => 1,
151 },
152 filter => {
153 description => 'Name of the filter that should be applied.',
154 type => 'string',
155 format => 'pve-configid',
156 optional => 1,
157 },
158 };
159
160 __PACKAGE__->register_method ({
161 name => 'get_groups',
162 path => 'groups',
163 method => 'GET',
164 description => 'Returns a list of all groups',
165 protected => 1,
166 permissions => {
167 description => "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
168 . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
169 user => 'all',
170 },
171 parameters => {
172 additionalProperties => 0,
173 properties => {},
174 },
175 returns => {
176 type => 'array',
177 items => {
178 type => 'object',
179 properties => $group_properties,
180 },
181 links => [ { rel => 'child', href => '{name}' } ],
182 },
183 code => sub {
184 my $config = PVE::Notify::read_config();
185 my $rpcenv = PVE::RPCEnvironment::get();
186
187 my $entities = eval {
188 $config->get_groups();
189 };
190 raise_api_error($@) if $@;
191
192 return filter_entities_by_privs($rpcenv, $entities);
193 }
194 });
195
196 __PACKAGE__->register_method ({
197 name => 'get_group',
198 path => 'groups/{name}',
199 method => 'GET',
200 description => 'Return a specific group',
201 protected => 1,
202 permissions => {
203 check => ['or',
204 ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
205 ['perm', '/mapping/notification/{name}', ['Mapping.Audit']],
206 ],
207 },
208 parameters => {
209 additionalProperties => 0,
210 properties => {
211 name => {
212 type => 'string',
213 format => 'pve-configid',
214 },
215 }
216 },
217 returns => {
218 type => 'object',
219 properties => {
220 %$group_properties,
221 digest => get_standard_option('pve-config-digest'),
222 },
223 },
224 code => sub {
225 my ($param) = @_;
226 my $name = extract_param($param, 'name');
227
228 my $config = PVE::Notify::read_config();
229
230 my $group = eval {
231 $config->get_group($name)
232 };
233
234 raise_api_error($@) if $@;
235 $group->{digest} = $config->digest();
236
237 return $group;
238 }
239 });
240
241 __PACKAGE__->register_method ({
242 name => 'create_group',
243 path => 'groups',
244 protected => 1,
245 method => 'POST',
246 description => 'Create a new group',
247 permissions => {
248 check => ['perm', '/mapping/notification', ['Mapping.Modify']],
249 },
250 parameters => {
251 additionalProperties => 0,
252 properties => $group_properties,
253 },
254 returns => { type => 'null' },
255 code => sub {
256 my ($param) = @_;
257
258 my $name = extract_param($param, 'name');
259 my $endpoint = extract_param($param, 'endpoint');
260 my $comment = extract_param($param, 'comment');
261 my $filter = extract_param($param, 'filter');
262
263 eval {
264 PVE::Notify::lock_config(sub {
265 my $config = PVE::Notify::read_config();
266
267 $config->add_group(
268 $name,
269 $endpoint,
270 $comment,
271 $filter,
272 );
273
274 PVE::Notify::write_config($config);
275 });
276 };
277
278 raise_api_error($@) if $@;
279 return;
280 }
281 });
282
283 __PACKAGE__->register_method ({
284 name => 'update_group',
285 path => 'groups/{name}',
286 protected => 1,
287 method => 'PUT',
288 description => 'Update existing group',
289 permissions => {
290 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
291 },
292 parameters => {
293 additionalProperties => 0,
294 properties => {
295 %{ make_properties_optional($group_properties) },
296 delete => {
297 type => 'array',
298 items => {
299 type => 'string',
300 format => 'pve-configid',
301 },
302 optional => 1,
303 description => 'A list of settings you want to delete.',
304 },
305 digest => get_standard_option('pve-config-digest'),
306 },
307 },
308 returns => { type => 'null' },
309 code => sub {
310 my ($param) = @_;
311
312 my $name = extract_param($param, 'name');
313 my $endpoint = extract_param($param, 'endpoint');
314 my $comment = extract_param($param, 'comment');
315 my $filter = extract_param($param, 'filter');
316 my $digest = extract_param($param, 'digest');
317 my $delete = extract_param($param, 'delete');
318
319 eval {
320 PVE::Notify::lock_config(sub {
321 my $config = PVE::Notify::read_config();
322
323 $config->update_group(
324 $name,
325 $endpoint,
326 $comment,
327 $filter,
328 $delete,
329 $digest,
330 );
331
332 PVE::Notify::write_config($config);
333 });
334 };
335
336 raise_api_error($@) if $@;
337 return;
338 }
339 });
340
341 __PACKAGE__->register_method ({
342 name => 'delete_group',
343 protected => 1,
344 path => 'groups/{name}',
345 method => 'DELETE',
346 description => 'Remove group',
347 permissions => {
348 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
349 },
350 parameters => {
351 additionalProperties => 0,
352 properties => {
353 name => {
354 type => 'string',
355 format => 'pve-configid',
356 },
357 }
358 },
359 returns => { type => 'null' },
360 code => sub {
361 my ($param) = @_;
362 my $name = extract_param($param, 'name');
363
364 eval {
365 PVE::Notify::lock_config(sub {
366 my $config = PVE::Notify::read_config();
367 $config->delete_group($name);
368 PVE::Notify::write_config($config);
369 });
370 };
371
372 raise_api_error($@) if $@;
373 return;
374 }
375 });
376
377 my $sendmail_properties = {
378 name => {
379 description => 'The name of the endpoint.',
380 type => 'string',
381 format => 'pve-configid',
382 },
383 mailto => {
384 type => 'array',
385 items => {
386 type => 'string',
387 format => 'email-or-username',
388 },
389 description => 'List of email recipients',
390 optional => 1,
391 },
392 'mailto-user' => {
393 type => 'array',
394 items => {
395 type => 'string',
396 format => 'pve-userid',
397 },
398 description => 'List of users',
399 optional => 1,
400 },
401 'from-address' => {
402 description => '`From` address for the mail',
403 type => 'string',
404 optional => 1,
405 },
406 author => {
407 description => 'Author of the mail',
408 type => 'string',
409 optional => 1,
410 },
411 'comment' => {
412 description => 'Comment',
413 type => 'string',
414 optional => 1,
415 },
416 filter => {
417 description => 'Name of the filter that should be applied.',
418 type => 'string',
419 format => 'pve-configid',
420 optional => 1,
421 },
422 };
423
424 __PACKAGE__->register_method ({
425 name => 'get_sendmail_endpoints',
426 path => 'endpoints/sendmail',
427 method => 'GET',
428 description => 'Returns a list of all sendmail endpoints',
429 permissions => {
430 description => "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
431 . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
432 user => 'all',
433 },
434 protected => 1,
435 parameters => {
436 additionalProperties => 0,
437 properties => {},
438 },
439 returns => {
440 type => 'array',
441 items => {
442 type => 'object',
443 properties => $sendmail_properties,
444 },
445 links => [ { rel => 'child', href => '{name}' } ],
446 },
447 code => sub {
448 my $config = PVE::Notify::read_config();
449 my $rpcenv = PVE::RPCEnvironment::get();
450
451 my $entities = eval {
452 $config->get_sendmail_endpoints();
453 };
454 raise_api_error($@) if $@;
455
456 return filter_entities_by_privs($rpcenv, $entities);
457 }
458 });
459
460 __PACKAGE__->register_method ({
461 name => 'get_sendmail_endpoint',
462 path => 'endpoints/sendmail/{name}',
463 method => 'GET',
464 description => 'Return a specific sendmail endpoint',
465 permissions => {
466 check => ['or',
467 ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
468 ['perm', '/mapping/notification/{name}', ['Mapping.Audit']],
469 ],
470 },
471 protected => 1,
472 parameters => {
473 additionalProperties => 0,
474 properties => {
475 name => {
476 type => 'string',
477 format => 'pve-configid',
478 },
479 }
480 },
481 returns => {
482 type => 'object',
483 properties => {
484 %$sendmail_properties,
485 digest => get_standard_option('pve-config-digest'),
486 }
487
488 },
489 code => sub {
490 my ($param) = @_;
491 my $name = extract_param($param, 'name');
492
493 my $config = PVE::Notify::read_config();
494 my $endpoint = eval {
495 $config->get_sendmail_endpoint($name)
496 };
497
498 raise_api_error($@) if $@;
499 $endpoint->{digest} = $config->digest();
500
501 return $endpoint;
502 }
503 });
504
505 __PACKAGE__->register_method ({
506 name => 'create_sendmail_endpoint',
507 path => 'endpoints/sendmail',
508 protected => 1,
509 method => 'POST',
510 description => 'Create a new sendmail endpoint',
511 permissions => {
512 check => ['perm', '/mapping/notification', ['Mapping.Modify']],
513 },
514 parameters => {
515 additionalProperties => 0,
516 properties => $sendmail_properties,
517 },
518 returns => { type => 'null' },
519 code => sub {
520 my ($param) = @_;
521
522 my $name = extract_param($param, 'name');
523 my $mailto = extract_param($param, 'mailto');
524 my $mailto_user = extract_param($param, 'mailto-user');
525 my $from_address = extract_param($param, 'from-address');
526 my $author = extract_param($param, 'author');
527 my $comment = extract_param($param, 'comment');
528 my $filter = extract_param($param, 'filter');
529
530 eval {
531 PVE::Notify::lock_config(sub {
532 my $config = PVE::Notify::read_config();
533
534 $config->add_sendmail_endpoint(
535 $name,
536 $mailto,
537 $mailto_user,
538 $from_address,
539 $author,
540 $comment,
541 $filter
542 );
543
544 PVE::Notify::write_config($config);
545 });
546 };
547
548 raise_api_error($@) if $@;
549 return;
550 }
551 });
552
553 __PACKAGE__->register_method ({
554 name => 'update_sendmail_endpoint',
555 path => 'endpoints/sendmail/{name}',
556 protected => 1,
557 method => 'PUT',
558 description => 'Update existing sendmail endpoint',
559 permissions => {
560 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
561 },
562 parameters => {
563 additionalProperties => 0,
564 properties => {
565 %{ make_properties_optional($sendmail_properties) },
566 delete => {
567 type => 'array',
568 items => {
569 type => 'string',
570 format => 'pve-configid',
571 },
572 optional => 1,
573 description => 'A list of settings you want to delete.',
574 },
575 digest => get_standard_option('pve-config-digest'),
576
577 }
578 },
579 returns => { type => 'null' },
580 code => sub {
581 my ($param) = @_;
582
583 my $name = extract_param($param, 'name');
584 my $mailto = extract_param($param, 'mailto');
585 my $mailto_user = extract_param($param, 'mailto-user');
586 my $from_address = extract_param($param, 'from-address');
587 my $author = extract_param($param, 'author');
588 my $comment = extract_param($param, 'comment');
589 my $filter = extract_param($param, 'filter');
590
591 my $delete = extract_param($param, 'delete');
592 my $digest = extract_param($param, 'digest');
593
594 eval {
595 PVE::Notify::lock_config(sub {
596 my $config = PVE::Notify::read_config();
597
598 $config->update_sendmail_endpoint(
599 $name,
600 $mailto,
601 $mailto_user,
602 $from_address,
603 $author,
604 $comment,
605 $filter,
606 $delete,
607 $digest,
608 );
609
610 PVE::Notify::write_config($config);
611 });
612 };
613
614 raise_api_error($@) if $@;
615 return;
616 }
617 });
618
619 __PACKAGE__->register_method ({
620 name => 'delete_sendmail_endpoint',
621 protected => 1,
622 path => 'endpoints/sendmail/{name}',
623 method => 'DELETE',
624 description => 'Remove sendmail endpoint',
625 permissions => {
626 check => ['perm', '/mapping/notification', ['Mapping.Modify']],
627 },
628 parameters => {
629 additionalProperties => 0,
630 properties => {
631 name => {
632 type => 'string',
633 format => 'pve-configid',
634 },
635 }
636 },
637 returns => { type => 'null' },
638 code => sub {
639 my ($param) = @_;
640
641 eval {
642 PVE::Notify::lock_config(sub {
643 my $config = PVE::Notify::read_config();
644 $config->delete_sendmail_endpoint($param->{name});
645 PVE::Notify::write_config($config);
646 });
647 };
648
649 raise_api_error($@) if ($@);
650 return;
651 }
652 });
653
654 my $gotify_properties = {
655 name => {
656 description => 'The name of the endpoint.',
657 type => 'string',
658 format => 'pve-configid',
659 },
660 'server' => {
661 description => 'Server URL',
662 type => 'string',
663 },
664 'token' => {
665 description => 'Secret token',
666 type => 'string',
667 },
668 'comment' => {
669 description => 'Comment',
670 type => 'string',
671 optional => 1,
672 },
673 'filter' => {
674 description => 'Name of the filter that should be applied.',
675 type => 'string',
676 format => 'pve-configid',
677 optional => 1,
678 }
679 };
680
681 __PACKAGE__->register_method ({
682 name => 'get_gotify_endpoints',
683 path => 'endpoints/gotify',
684 method => 'GET',
685 description => 'Returns a list of all gotify endpoints',
686 protected => 1,
687 permissions => {
688 description => "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
689 . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
690 user => 'all',
691 },
692 parameters => {
693 additionalProperties => 0,
694 properties => {},
695 },
696 returns => {
697 type => 'array',
698 items => {
699 type => 'object',
700 properties => remove_protected_properties($gotify_properties, ['token']),
701 },
702 links => [ { rel => 'child', href => '{name}' } ],
703 },
704 code => sub {
705 my $config = PVE::Notify::read_config();
706 my $rpcenv = PVE::RPCEnvironment::get();
707
708 my $entities = eval {
709 $config->get_gotify_endpoints();
710 };
711 raise_api_error($@) if $@;
712
713 return filter_entities_by_privs($rpcenv, $entities);
714 }
715 });
716
717 __PACKAGE__->register_method ({
718 name => 'get_gotify_endpoint',
719 path => 'endpoints/gotify/{name}',
720 method => 'GET',
721 description => 'Return a specific gotify endpoint',
722 protected => 1,
723 permissions => {
724 check => ['or',
725 ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
726 ['perm', '/mapping/notification/{name}', ['Mapping.Audit']],
727 ],
728 },
729 parameters => {
730 additionalProperties => 0,
731 properties => {
732 name => {
733 type => 'string',
734 format => 'pve-configid',
735 description => 'Name of the endpoint.'
736 },
737 }
738 },
739 returns => {
740 type => 'object',
741 properties => {
742 %{ remove_protected_properties($gotify_properties, ['token']) },
743 digest => get_standard_option('pve-config-digest'),
744 }
745 },
746 code => sub {
747 my ($param) = @_;
748 my $name = extract_param($param, 'name');
749
750 my $config = PVE::Notify::read_config();
751 my $endpoint = eval {
752 $config->get_gotify_endpoint($name)
753 };
754
755 raise_api_error($@) if $@;
756 $endpoint->{digest} = $config->digest();
757
758 return $endpoint;
759 }
760 });
761
762 __PACKAGE__->register_method ({
763 name => 'create_gotify_endpoint',
764 path => 'endpoints/gotify',
765 protected => 1,
766 method => 'POST',
767 description => 'Create a new gotify endpoint',
768 permissions => {
769 check => ['perm', '/mapping/notification', ['Mapping.Modify']],
770 },
771 parameters => {
772 additionalProperties => 0,
773 properties => $gotify_properties,
774 },
775 returns => { type => 'null' },
776 code => sub {
777 my ($param) = @_;
778
779 my $name = extract_param($param, 'name');
780 my $server = extract_param($param, 'server');
781 my $token = extract_param($param, 'token');
782 my $comment = extract_param($param, 'comment');
783 my $filter = extract_param($param, 'filter');
784
785 eval {
786 PVE::Notify::lock_config(sub {
787 my $config = PVE::Notify::read_config();
788
789 $config->add_gotify_endpoint(
790 $name,
791 $server,
792 $token,
793 $comment,
794 $filter
795 );
796
797 PVE::Notify::write_config($config);
798 });
799 };
800
801 raise_api_error($@) if $@;
802 return;
803 }
804 });
805
806 __PACKAGE__->register_method ({
807 name => 'update_gotify_endpoint',
808 path => 'endpoints/gotify/{name}',
809 protected => 1,
810 method => 'PUT',
811 description => 'Update existing gotify endpoint',
812 permissions => {
813 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
814 },
815 parameters => {
816 additionalProperties => 0,
817 properties => {
818 %{ make_properties_optional($gotify_properties) },
819 delete => {
820 type => 'array',
821 items => {
822 type => 'string',
823 format => 'pve-configid',
824 },
825 optional => 1,
826 description => 'A list of settings you want to delete.',
827 },
828 digest => get_standard_option('pve-config-digest'),
829 }
830 },
831 returns => { type => 'null' },
832 code => sub {
833 my ($param) = @_;
834
835 my $name = extract_param($param, 'name');
836 my $server = extract_param($param, 'server');
837 my $token = extract_param($param, 'token');
838 my $comment = extract_param($param, 'comment');
839 my $filter = extract_param($param, 'filter');
840
841 my $delete = extract_param($param, 'delete');
842 my $digest = extract_param($param, 'digest');
843
844 eval {
845 PVE::Notify::lock_config(sub {
846 my $config = PVE::Notify::read_config();
847
848 $config->update_gotify_endpoint(
849 $name,
850 $server,
851 $token,
852 $comment,
853 $filter,
854 $delete,
855 $digest,
856 );
857
858 PVE::Notify::write_config($config);
859 });
860 };
861
862 raise_api_error($@) if $@;
863 return;
864 }
865 });
866
867 __PACKAGE__->register_method ({
868 name => 'delete_gotify_endpoint',
869 protected => 1,
870 path => 'endpoints/gotify/{name}',
871 method => 'DELETE',
872 description => 'Remove gotify endpoint',
873 permissions => {
874 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
875 },
876 parameters => {
877 additionalProperties => 0,
878 properties => {
879 name => {
880 type => 'string',
881 format => 'pve-configid',
882 },
883 }
884 },
885 returns => { type => 'null' },
886 code => sub {
887 my ($param) = @_;
888 my $name = extract_param($param, 'name');
889
890 eval {
891 PVE::Notify::lock_config(sub {
892 my $config = PVE::Notify::read_config();
893 $config->delete_gotify_endpoint($name);
894 PVE::Notify::write_config($config);
895 });
896 };
897
898 raise_api_error($@) if $@;
899 return;
900 }
901 });
902
903 my $filter_properties = {
904 name => {
905 description => 'Name of the endpoint.',
906 type => 'string',
907 format => 'pve-configid',
908 },
909 'min-severity' => {
910 type => 'string',
911 description => 'Minimum severity to match',
912 optional => 1,
913 enum => [qw(info notice warning error)],
914 },
915 mode => {
916 type => 'string',
917 description => "Choose between 'and' and 'or' for when multiple properties are specified",
918 optional => 1,
919 enum => [qw(and or)],
920 default => 'and',
921 },
922 'invert-match' => {
923 type => 'boolean',
924 description => 'Invert match of the whole filter',
925 optional => 1,
926 },
927 'comment' => {
928 description => 'Comment',
929 type => 'string',
930 optional => 1,
931 },
932 };
933
934 __PACKAGE__->register_method ({
935 name => 'get_filters',
936 path => 'filters',
937 method => 'GET',
938 description => 'Returns a list of all filters',
939 protected => 1,
940 permissions => {
941 description => "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or"
942 . " 'Mapping.Audit' permissions on '/mapping/notification/<name>'.",
943 user => 'all',
944 },
945 parameters => {
946 additionalProperties => 0,
947 properties => {},
948 },
949 returns => {
950 type => 'array',
951 items => {
952 type => 'object',
953 properties => $filter_properties,
954 },
955 links => [ { rel => 'child', href => '{name}' } ],
956 },
957 code => sub {
958 my $config = PVE::Notify::read_config();
959 my $rpcenv = PVE::RPCEnvironment::get();
960
961 my $entities = eval {
962 $config->get_filters();
963 };
964 raise_api_error($@) if $@;
965
966 return filter_entities_by_privs($rpcenv, $entities);
967 }
968 });
969
970 __PACKAGE__->register_method ({
971 name => 'get_filter',
972 path => 'filters/{name}',
973 method => 'GET',
974 description => 'Return a specific filter',
975 protected => 1,
976 permissions => {
977 check => ['or',
978 ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
979 ['perm', '/mapping/notification/{name}', ['Mapping.Audit']],
980 ],
981 },
982 parameters => {
983 additionalProperties => 0,
984 properties => {
985 name => {
986 type => 'string',
987 format => 'pve-configid',
988 },
989 }
990 },
991 returns => {
992 type => 'object',
993 properties => {
994 %$filter_properties,
995 digest => get_standard_option('pve-config-digest'),
996 },
997 },
998 code => sub {
999 my ($param) = @_;
1000 my $name = extract_param($param, 'name');
1001
1002 my $config = PVE::Notify::read_config();
1003
1004 my $filter = eval {
1005 $config->get_filter($name)
1006 };
1007
1008 raise_api_error($@) if $@;
1009 $filter->{digest} = $config->digest();
1010
1011 return $filter;
1012 }
1013 });
1014
1015 __PACKAGE__->register_method ({
1016 name => 'create_filter',
1017 path => 'filters',
1018 protected => 1,
1019 method => 'POST',
1020 description => 'Create a new filter',
1021 protected => 1,
1022 permissions => {
1023 check => ['perm', '/mapping/notification', ['Mapping.Modify']],
1024 },
1025 parameters => {
1026 additionalProperties => 0,
1027 properties => $filter_properties,
1028 },
1029 returns => { type => 'null' },
1030 code => sub {
1031 my ($param) = @_;
1032
1033 my $name = extract_param($param, 'name');
1034 my $min_severity = extract_param($param, 'min-severity');
1035 my $mode = extract_param($param, 'mode');
1036 my $invert_match = extract_param($param, 'invert-match');
1037 my $comment = extract_param($param, 'comment');
1038
1039 eval {
1040 PVE::Notify::lock_config(sub {
1041 my $config = PVE::Notify::read_config();
1042
1043 $config->add_filter(
1044 $name,
1045 $min_severity,
1046 $mode,
1047 $invert_match,
1048 $comment,
1049 );
1050
1051 PVE::Notify::write_config($config);
1052 });
1053 };
1054
1055 raise_api_error($@) if $@;
1056 return;
1057 }
1058 });
1059
1060 __PACKAGE__->register_method ({
1061 name => 'update_filter',
1062 path => 'filters/{name}',
1063 protected => 1,
1064 method => 'PUT',
1065 description => 'Update existing filter',
1066 permissions => {
1067 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
1068 },
1069 parameters => {
1070 additionalProperties => 0,
1071 properties => {
1072 %{ make_properties_optional($filter_properties) },
1073 delete => {
1074 type => 'array',
1075 items => {
1076 type => 'string',
1077 format => 'pve-configid',
1078 },
1079 optional => 1,
1080 description => 'A list of settings you want to delete.',
1081 },
1082 digest => get_standard_option('pve-config-digest'),
1083 },
1084 },
1085 returns => { type => 'null' },
1086 code => sub {
1087 my ($param) = @_;
1088
1089 my $name = extract_param($param, 'name');
1090 my $min_severity = extract_param($param, 'min-severity');
1091 my $mode = extract_param($param, 'mode');
1092 my $invert_match = extract_param($param, 'invert-match');
1093 my $comment = extract_param($param, 'comment');
1094 my $digest = extract_param($param, 'digest');
1095 my $delete = extract_param($param, 'delete');
1096
1097 eval {
1098 PVE::Notify::lock_config(sub {
1099 my $config = PVE::Notify::read_config();
1100
1101 $config->update_filter(
1102 $name,
1103 $min_severity,
1104 $mode,
1105 $invert_match,
1106 $comment,
1107 $delete,
1108 $digest,
1109 );
1110
1111 PVE::Notify::write_config($config);
1112 });
1113 };
1114
1115 raise_api_error($@) if $@;
1116 return;
1117 }
1118 });
1119
1120 __PACKAGE__->register_method ({
1121 name => 'delete_filter',
1122 protected => 1,
1123 path => 'filters/{name}',
1124 method => 'DELETE',
1125 description => 'Remove filter',
1126 permissions => {
1127 check => ['perm', '/mapping/notification/{name}', ['Mapping.Modify']],
1128 },
1129 parameters => {
1130 additionalProperties => 0,
1131 properties => {
1132 name => {
1133 type => 'string',
1134 format => 'pve-configid',
1135 },
1136 }
1137 },
1138 returns => { type => 'null' },
1139 code => sub {
1140 my ($param) = @_;
1141 my $name = extract_param($param, 'name');
1142
1143 eval {
1144 PVE::Notify::lock_config(sub {
1145 my $config = PVE::Notify::read_config();
1146 $config->delete_filter($name);
1147 PVE::Notify::write_config($config);
1148 });
1149 };
1150
1151 raise_api_error($@) if $@;
1152 return;
1153 }
1154 });
1155
1156 1;