]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/ObjectGroupHelpers.pm
bump version to 8.1.4
[pmg-api.git] / src / PMG / API2 / ObjectGroupHelpers.pm
1 package PMG::API2::ObjectGroupHelpers;
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 PMG::RESTEnvironment;
10 use PVE::SafeSyslog;
11 use PVE::Tools qw(extract_param);
12
13 use PMG::DBTools;
14 use PMG::RuleDB;
15
16 sub format_rule {
17 my ($rule, $from, $to, $when, $what, $action) = @_;
18
19 my $cond_create_group = sub {
20 my ($res, $name, $groupdata) = @_;
21
22 return if !$groupdata;
23
24 $res->{$name} = format_object_group($groupdata);
25 };
26
27 my $data = {
28 id => $rule->{id},
29 name => $rule->{name},
30 priority => $rule->{priority},
31 active => $rule->{active},
32 direction => $rule->{direction},
33 };
34 my $types = [qw(what when from to)];
35 my $attributes = [qw(and invert)];
36 for my $type ($types->@*) {
37 for my $attribute ($attributes->@*) {
38 my $opt = "${type}-${attribute}";
39 $data->{$opt} = $rule->{$opt} if defined($rule->{$opt});
40 }
41 }
42
43 $cond_create_group->($data, 'from', $from);
44 $cond_create_group->($data, 'to', $to);
45 $cond_create_group->($data, 'when', $when);
46 $cond_create_group->($data, 'what', $what);
47 $cond_create_group->($data, 'action', $action);
48
49 return $data;
50 }
51
52 sub format_object_group {
53 my ($ogroups) = @_;
54
55 my $res = [];
56 foreach my $og (@$ogroups) {
57 my $group = { id => $og->{id}, name => $og->{name}, info => $og->{info} };
58 $group->{and} = $og->{and} if defined($og->{and});
59 $group->{invert} = $og->{invert} if defined($og->{invert});
60 push @$res, $group;
61 }
62 return $res;
63 }
64
65 my $group_attributes = {
66 and => {
67 description => "If set to 1, objects in this group are 'and' combined.",
68 type => 'boolean',
69 default => 0,
70 optional => 1,
71 },
72 invert => {
73 description => "If set to 1, the resulting match is inverted.",
74 type => 'boolean',
75 default => 0,
76 optional => 1,
77 },
78 };
79
80 sub register_group_list_api {
81 my ($apiclass, $oclass) = @_;
82
83 $apiclass->register_method({
84 name => "list_${oclass}_groups",
85 path => $oclass,
86 method => 'GET',
87 description => "Get list of '$oclass' groups.",
88 proxyto => 'master',
89 permissions => { check => [ 'admin', 'audit' ] },
90 parameters => {
91 additionalProperties => 0,
92 properties => {},
93 },
94 returns => {
95 type => 'array',
96 items => {
97 type => "object",
98 properties => {
99 id => { type => 'integer' },
100 },
101 },
102 },
103 code => sub {
104 my ($param) = @_;
105
106 my $rdb = PMG::RuleDB->new();
107
108 my $ogroups = $rdb->load_objectgroups($oclass);
109
110 return format_object_group($ogroups);
111 }});
112
113 my $additional_parameters = {};
114 if ($oclass =~ /^(?:what|when|who)$/i) {
115 $additional_parameters = { $group_attributes->%* };
116 }
117
118 $apiclass->register_method({
119 name => "create_${oclass}_group",
120 path => $oclass,
121 method => 'POST',
122 description => "Create a new '$oclass' group.",
123 proxyto => 'master',
124 protected => 1,
125 permissions => { check => [ 'admin' ] },
126 parameters => {
127 additionalProperties => 0,
128 properties => {
129 name => {
130 description => "Group name.",
131 type => 'string',
132 maxLength => 255,
133 },
134 info => {
135 description => "Informational comment.",
136 type => 'string',
137 maxLength => 255,
138 optional => 1,
139 },
140 $additional_parameters->%*,
141 },
142 },
143 returns => { type => 'integer' },
144 code => sub {
145 my ($param) = @_;
146
147 my $rdb = PMG::RuleDB->new();
148
149 my $og = PMG::RuleDB::Group->new(
150 $param->{name}, $param->{info} // '', $oclass);
151
152 for my $prop (qw(and invert)) {
153 $og->{$prop} = $param->{$prop} if defined($param->{$prop});
154 }
155
156 return $rdb->save_group($og);
157 }});
158 }
159
160 sub register_delete_object_group_api {
161 my ($apiclass, $oclass, $path) = @_;
162
163 $apiclass->register_method({
164 name => 'delete_{$oclass}_group',
165 path => $path,
166 method => 'DELETE',
167 description => "Delete a '$oclass' group.",
168 proxyto => 'master',
169 protected => 1,
170 permissions => { check => [ 'admin' ] },
171 parameters => {
172 additionalProperties => 0,
173 properties => {
174 ogroup => {
175 description => "Object Group ID.",
176 type => 'integer',
177 },
178 },
179 },
180 returns => { type => 'null' },
181 code => sub {
182 my ($param) = @_;
183
184 my $rdb = PMG::RuleDB->new();
185
186 $rdb->delete_group($param->{ogroup});
187
188 return undef;
189 }});
190 }
191
192 sub register_object_group_config_api {
193 my ($apiclass, $oclass, $path) = @_;
194
195 $apiclass->register_method({
196 name => 'get_config',
197 path => $path,
198 method => 'GET',
199 description => "Get '$oclass' group properties",
200 proxyto => 'master',
201 permissions => { check => [ 'admin', 'audit' ] },
202 parameters => {
203 additionalProperties => 0,
204 properties => {
205 ogroup => {
206 description => "Object Group ID.",
207 type => 'integer',
208 },
209 },
210 },
211 returns => {
212 type => "object",
213 properties => {
214 id => { type => 'integer'},
215 name => { type => 'string' },
216 info => { type => 'string' },
217 },
218 },
219 code => sub {
220 my ($param) = @_;
221
222 my $rdb = PMG::RuleDB->new();
223
224 my $list = $rdb->load_objectgroups($oclass, $param->{ogroup});
225 my $og = shift @$list ||
226 die "$oclass group '$param->{ogroup}' not found\n";
227
228 return {
229 id => $og->{id},
230 name => $og->{name},
231 info => $og->{info},
232 };
233
234 }});
235
236 my $additional_parameters = {};
237 if ($oclass =~ /^(?:what|when|who)$/i) {
238 $additional_parameters = { $group_attributes->%* };
239 }
240
241 $apiclass->register_method({
242 name => 'set_config',
243 path => $path,
244 method => 'PUT',
245 description => "Modify '$oclass' group properties",
246 proxyto => 'master',
247 protected => 1,
248 permissions => { check => [ 'admin' ] },
249 parameters => {
250 additionalProperties => 0,
251 properties => {
252 ogroup => {
253 description => "Object Group ID.",
254 type => 'integer',
255 },
256 name => {
257 description => "Group name.",
258 type => 'string',
259 maxLength => 255,
260 optional => 1,
261 },
262 info => {
263 description => "Informational comment.",
264 type => 'string',
265 maxLength => 255,
266 optional => 1,
267 },
268 $additional_parameters->%*,
269 },
270 },
271 returns => { type => "null" },
272 code => sub {
273 my ($param) = @_;
274
275 my $rdb = PMG::RuleDB->new();
276
277 my $ogroup = extract_param($param, 'ogroup');
278
279 die "no options specified\n"
280 if !scalar(keys %$param);
281
282 my $list = $rdb->load_objectgroups($oclass, $ogroup);
283 my $og = shift @$list ||
284 die "$oclass group '$ogroup' not found\n";
285
286 for my $prop (qw(name info and invert)) {
287 $og->{$prop} = $param->{$prop} if defined($param->{$prop});
288 }
289
290 $rdb->save_group($og);
291
292 PMG::DBTools::reload_ruledb();
293
294 return undef;
295 }});
296 }
297
298 sub register_objects_api {
299 my ($apiclass, $oclass, $path) = @_;
300
301 $apiclass->register_method({
302 name => 'objects',
303 path => $path,
304 method => 'GET',
305 description => "List '$oclass' group objects.",
306 proxyto => 'master',
307 permissions => { check => [ 'admin', 'audit' ] },
308 parameters => {
309 additionalProperties => 0,
310 properties => {
311 ogroup => {
312 description => "Object Group ID.",
313 type => 'integer',
314 },
315 },
316 },
317 returns => {
318 type => 'array',
319 items => {
320 type => "object",
321 properties => {
322 id => { type => 'integer'},
323 },
324 },
325 links => [ { rel => 'child', href => "{id}" } ],
326 },
327 code => sub {
328 my ($param) = @_;
329
330 my $rdb = PMG::RuleDB->new();
331
332 my $og = $rdb->load_group_objects($param->{ogroup});
333
334 my $res = [];
335
336 foreach my $obj (@$og) {
337 push @$res, $obj->get_data();
338 }
339
340 return $res;
341 }});
342
343 $apiclass->register_method({
344 name => 'delete_object',
345 path => 'objects/{id}',
346 method => 'DELETE',
347 description => "Remove an object from the '$oclass' group.",
348 proxyto => 'master',
349 protected => 1,
350 permissions => { check => [ 'admin' ] },
351 parameters => {
352 additionalProperties => 0,
353 properties => {
354 ogroup => {
355 description => "Object Group ID.",
356 type => 'integer',
357 },
358 id => {
359 description => "Object ID.",
360 type => 'integer',
361 },
362 },
363 },
364 returns => { type => 'null' },
365 code => sub {
366 my ($param) = @_;
367
368 my $rdb = PMG::RuleDB->new();
369
370 my $obj = $rdb->load_object($param->{id});
371
372 die "object '$param->{id}' does not exists\n" if !defined($obj);
373
374 $rdb->delete_object($obj);
375
376 PMG::DBTools::reload_ruledb();
377
378 return undef;
379 }});
380 }
381
382 1;