]>
Commit | Line | Data |
---|---|---|
b52f6573 DM |
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 base qw(PVE::RESTHandler); | |
17 | ||
18 | __PACKAGE__->register_method ({ | |
19 | name => 'index', | |
20 | path => '', | |
21 | method => 'GET', | |
22 | description => "Directory index.", | |
23 | parameters => { | |
24 | additionalProperties => 0, | |
bdcc6f0f | 25 | properties => {}, |
b52f6573 DM |
26 | }, |
27 | returns => { | |
28 | type => 'array', | |
29 | items => { | |
30 | type => "object", | |
31 | properties => {}, | |
32 | }, | |
33 | links => [ { rel => 'child', href => "{name}" } ], | |
34 | }, | |
35 | code => sub { | |
36 | my ($param) = @_; | |
37 | ||
38 | my $result = [ | |
e6a59fe6 | 39 | { name => 'actions' }, |
b52f6573 | 40 | { name => 'rules' }, |
e6a59fe6 DM |
41 | { name => 'what' }, |
42 | { name => 'when' }, | |
43 | { name => 'who' }, | |
b52f6573 DM |
44 | ]; |
45 | ||
46 | return $result; | |
47 | }}); | |
48 | ||
e6a59fe6 DM |
49 | my $format_object_group = sub { |
50 | my ($ogroups) = @_; | |
51 | ||
52 | my $res = []; | |
53 | foreach my $og (@$ogroups) { | |
54 | push @$res, { | |
55 | id => $og->{id}, name => $og->{name}, info => $og->{info} | |
56 | }; | |
57 | } | |
58 | return $res; | |
59 | }; | |
60 | ||
b52f6573 DM |
61 | __PACKAGE__->register_method({ |
62 | name => 'list_rules', | |
63 | path => 'rules', | |
64 | method => 'GET', | |
65 | description => "Get list of rules.", | |
bdcc6f0f | 66 | proxyto => 'master', |
b52f6573 DM |
67 | protected => 1, |
68 | parameters => { | |
69 | additionalProperties => 0, | |
bdcc6f0f | 70 | properties => {}, |
b52f6573 DM |
71 | }, |
72 | returns => { | |
73 | type => 'array', | |
74 | items => { | |
75 | type => "object", | |
76 | properties => { | |
77 | } | |
78 | } | |
79 | }, | |
80 | code => sub { | |
81 | my ($param) = @_; | |
82 | ||
b52f6573 DM |
83 | my $dbh = PMG::DBTools::open_ruledb(); |
84 | my $ruledb = PMG::RuleDB->new($dbh); | |
85 | ||
86 | my $rules = $ruledb->load_rules(); | |
87 | ||
88 | my $res = []; | |
89 | ||
90 | my $cond_create_group = sub { | |
91 | my ($res, $name, $groupdata) = @_; | |
92 | ||
93 | return if !$groupdata; | |
94 | ||
e6a59fe6 | 95 | $res->{$name} = $format_object_group->($groupdata); |
b52f6573 DM |
96 | }; |
97 | ||
98 | foreach my $rule (@$rules) { | |
99 | my ($from, $to, $when, $what, $action) = | |
100 | $ruledb->load_groups($rule); | |
101 | ||
102 | my $data = { | |
103 | id => $rule->{id}, | |
104 | name => $rule->{name}, | |
105 | priority => $rule->{priority}, | |
106 | active => $rule->{active}, | |
107 | }; | |
108 | ||
109 | $cond_create_group->($data, 'from', $from); | |
110 | $cond_create_group->($data, 'to', $to); | |
111 | $cond_create_group->($data, 'when', $when); | |
112 | $cond_create_group->($data, 'what', $what); | |
113 | $cond_create_group->($data, 'action', $action); | |
114 | ||
115 | push @$res, $data; | |
116 | } | |
117 | ||
118 | $ruledb->close(); | |
119 | ||
120 | return $res; | |
121 | }}); | |
122 | ||
e6a59fe6 DM |
123 | __PACKAGE__->register_method({ |
124 | name => 'list_actions', | |
125 | path => 'actions', | |
126 | method => 'GET', | |
127 | description => "Get list of 'action' objects.", | |
bdcc6f0f | 128 | proxyto => 'master', |
e6a59fe6 DM |
129 | protected => 1, |
130 | parameters => { | |
131 | additionalProperties => 0, | |
bdcc6f0f | 132 | properties => {}, |
e6a59fe6 DM |
133 | }, |
134 | returns => { | |
135 | type => 'array', | |
136 | items => { | |
137 | type => "object", | |
138 | properties => { | |
139 | } | |
140 | } | |
141 | }, | |
142 | code => sub { | |
143 | my ($param) = @_; | |
144 | ||
145 | my $dbh = PMG::DBTools::open_ruledb(); | |
146 | my $ruledb = PMG::RuleDB->new($dbh); | |
147 | ||
148 | my $ogroups = $ruledb->load_objectgroups('action'); | |
149 | ||
150 | return $format_object_group->($ogroups); | |
151 | }}); | |
152 | ||
153 | __PACKAGE__->register_method({ | |
154 | name => 'list_what_object', | |
155 | path => 'what', | |
156 | method => 'GET', | |
157 | description => "Get list of 'what' objects.", | |
bdcc6f0f | 158 | proxyto => 'master', |
e6a59fe6 DM |
159 | protected => 1, |
160 | parameters => { | |
161 | additionalProperties => 0, | |
bdcc6f0f | 162 | properties => {}, |
e6a59fe6 DM |
163 | }, |
164 | returns => { | |
165 | type => 'array', | |
166 | items => { | |
167 | type => "object", | |
168 | properties => { | |
169 | } | |
170 | } | |
171 | }, | |
172 | code => sub { | |
173 | my ($param) = @_; | |
174 | ||
175 | my $dbh = PMG::DBTools::open_ruledb(); | |
176 | my $ruledb = PMG::RuleDB->new($dbh); | |
177 | ||
178 | my $ogroups = $ruledb->load_objectgroups('what'); | |
179 | ||
180 | return $format_object_group->($ogroups); | |
181 | }}); | |
182 | ||
183 | __PACKAGE__->register_method({ | |
184 | name => 'list_when_object', | |
185 | path => 'when', | |
186 | method => 'GET', | |
187 | description => "Get list of 'when' objects.", | |
bdcc6f0f | 188 | proxyto => 'master', |
e6a59fe6 DM |
189 | protected => 1, |
190 | parameters => { | |
191 | additionalProperties => 0, | |
bdcc6f0f | 192 | properties => {}, |
e6a59fe6 DM |
193 | }, |
194 | returns => { | |
195 | type => 'array', | |
196 | items => { | |
197 | type => "object", | |
198 | properties => { | |
199 | } | |
200 | } | |
201 | }, | |
202 | code => sub { | |
203 | my ($param) = @_; | |
204 | ||
205 | my $dbh = PMG::DBTools::open_ruledb(); | |
206 | my $ruledb = PMG::RuleDB->new($dbh); | |
207 | ||
208 | my $ogroups = $ruledb->load_objectgroups('when'); | |
209 | ||
210 | return $format_object_group->($ogroups); | |
211 | }}); | |
212 | ||
213 | __PACKAGE__->register_method({ | |
214 | name => 'list_who_object', | |
215 | path => 'who', | |
216 | method => 'GET', | |
217 | description => "Get list of 'who' objects.", | |
bdcc6f0f | 218 | proxyto => 'master', |
e6a59fe6 DM |
219 | protected => 1, |
220 | parameters => { | |
221 | additionalProperties => 0, | |
bdcc6f0f | 222 | properties => {}, |
e6a59fe6 DM |
223 | }, |
224 | returns => { | |
225 | type => 'array', | |
226 | items => { | |
227 | type => "object", | |
228 | properties => { | |
229 | } | |
230 | } | |
231 | }, | |
232 | code => sub { | |
233 | my ($param) = @_; | |
234 | ||
235 | my $dbh = PMG::DBTools::open_ruledb(); | |
236 | my $ruledb = PMG::RuleDB->new($dbh); | |
237 | ||
238 | my $ogroups = $ruledb->load_objectgroups('who'); | |
239 | ||
240 | return $format_object_group->($ogroups); | |
241 | }}); | |
242 | ||
b52f6573 | 243 | 1; |