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