]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/RuleDB.pm
cleanup: move helpers to ObjectGroupHelpers.pm
[pmg-api.git] / PMG / API2 / RuleDB.pm
1 package PMG::API2::RuleDB;
2
3 use strict;
4 use warnings;
5
6 use PVE::INotify;
7 use PVE::RESTHandler;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::RESTEnvironment;
10 use PVE::SafeSyslog;
11 use PVE::Tools qw(extract_param);
12
13 use PMG::DBTools;
14 use PMG::RuleDB;
15
16 use PMG::API2::ObjectGroupHelpers;
17 use PMG::API2::Who;
18
19 use base qw(PVE::RESTHandler);
20
21 __PACKAGE__->register_method ({
22 name => 'index',
23 path => '',
24 method => 'GET',
25 description => "Directory index.",
26 parameters => {
27 additionalProperties => 0,
28 properties => {},
29 },
30 returns => {
31 type => 'array',
32 items => {
33 type => "object",
34 properties => {},
35 },
36 links => [ { rel => 'child', href => "{name}" } ],
37 },
38 code => sub {
39 my ($param) = @_;
40
41 my $result = [
42 { name => 'action' },
43 { name => 'rules' },
44 { name => 'what' },
45 { name => 'when' },
46 { name => 'who' },
47 ];
48
49 return $result;
50 }});
51
52 __PACKAGE__->register_method({
53 name => 'list_rules',
54 path => 'rules',
55 method => 'GET',
56 description => "Get list of rules.",
57 proxyto => 'master',
58 protected => 1,
59 parameters => {
60 additionalProperties => 0,
61 properties => {},
62 },
63 returns => {
64 type => 'array',
65 items => {
66 type => "object",
67 properties => {
68 id => { type => 'integer' },
69 },
70 },
71 links => [ { rel => 'child', href => "{id}" } ],
72 },
73 code => sub {
74 my ($param) = @_;
75
76 my $rdb = PMG::RuleDB->new();
77
78 my $rules = $rdb->load_rules();
79
80 my $res = [];
81
82 my $cond_create_group = sub {
83 my ($res, $name, $groupdata) = @_;
84
85 return if !$groupdata;
86
87 $res->{$name} = PMG::API2::ObjectGroupHelpers::format_object_group($groupdata);
88 };
89
90 foreach my $rule (@$rules) {
91 my ($from, $to, $when, $what, $action) =
92 $rdb->load_groups($rule);
93
94 my $data = {
95 id => $rule->{id},
96 name => $rule->{name},
97 priority => $rule->{priority},
98 active => $rule->{active},
99 };
100
101 $cond_create_group->($data, 'from', $from);
102 $cond_create_group->($data, 'to', $to);
103 $cond_create_group->($data, 'when', $when);
104 $cond_create_group->($data, 'what', $what);
105 $cond_create_group->($data, 'action', $action);
106
107 push @$res, $data;
108 }
109
110 $rdb->close();
111
112 return $res;
113 }});
114
115
116 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'action');
117 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'what');
118 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'when');
119 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'who');
120
121 __PACKAGE__->register_method ({
122 subclass => 'PMG::API2::Who',
123 path => 'who/{ogroup}',
124 });
125
126
127 1;