1 package PMG
::API2
::ObjectGroupHelpers
;
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PMG
::RESTEnvironment
;
11 use PVE
::Tools
qw(extract_param);
17 my ($rule, $from, $to, $when, $what, $action) = @_;
19 my $cond_create_group = sub {
20 my ($res, $name, $groupdata) = @_;
22 return if !$groupdata;
24 $res->{$name} = format_object_group
($groupdata);
29 name
=> $rule->{name
},
30 priority
=> $rule->{priority
},
31 active
=> $rule->{active
},
32 direction
=> $rule->{direction
},
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});
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);
52 sub format_object_group
{
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
});
65 my $group_attributes = {
67 description
=> "If set to 1, objects in this group are 'and' combined.",
73 description
=> "If set to 1, the resulting match is inverted.",
80 sub register_group_list_api
{
81 my ($apiclass, $oclass) = @_;
83 $apiclass->register_method({
84 name
=> "list_${oclass}_groups",
87 description
=> "Get list of '$oclass' groups.",
89 permissions
=> { check
=> [ 'admin', 'audit' ] },
91 additionalProperties
=> 0,
99 id
=> { type
=> 'integer' },
106 my $rdb = PMG
::RuleDB-
>new();
108 my $ogroups = $rdb->load_objectgroups($oclass);
110 return format_object_group
($ogroups);
113 my $additional_parameters = {};
114 if ($oclass =~ /^(?:what|when|who)$/i) {
115 $additional_parameters = { $group_attributes->%* };
118 $apiclass->register_method({
119 name
=> "create_${oclass}_group",
122 description
=> "Create a new '$oclass' group.",
125 permissions
=> { check
=> [ 'admin' ] },
127 additionalProperties
=> 0,
130 description
=> "Group name.",
135 description
=> "Informational comment.",
140 $additional_parameters->%*,
143 returns
=> { type
=> 'integer' },
147 my $rdb = PMG
::RuleDB-
>new();
149 my $og = PMG
::RuleDB
::Group-
>new(
150 $param->{name
}, $param->{info
} // '', $oclass);
152 for my $prop (qw(and invert)) {
153 $og->{$prop} = $param->{$prop} if defined($param->{$prop});
156 return $rdb->save_group($og);
160 sub register_delete_object_group_api
{
161 my ($apiclass, $oclass, $path) = @_;
163 $apiclass->register_method({
164 name
=> 'delete_{$oclass}_group',
167 description
=> "Delete a '$oclass' group.",
170 permissions
=> { check
=> [ 'admin' ] },
172 additionalProperties
=> 0,
175 description
=> "Object Group ID.",
180 returns
=> { type
=> 'null' },
184 my $rdb = PMG
::RuleDB-
>new();
186 $rdb->delete_group($param->{ogroup
});
192 sub register_object_group_config_api
{
193 my ($apiclass, $oclass, $path) = @_;
195 $apiclass->register_method({
196 name
=> 'get_config',
199 description
=> "Get '$oclass' group properties",
201 permissions
=> { check
=> [ 'admin', 'audit' ] },
203 additionalProperties
=> 0,
206 description
=> "Object Group ID.",
214 id
=> { type
=> 'integer'},
215 name
=> { type
=> 'string' },
216 info
=> { type
=> 'string' },
222 my $rdb = PMG
::RuleDB-
>new();
224 my $list = $rdb->load_objectgroups($oclass, $param->{ogroup
});
225 my $og = shift @$list ||
226 die "$oclass group '$param->{ogroup}' not found\n";
236 my $additional_parameters = {};
237 if ($oclass =~ /^(?:what|when|who)$/i) {
238 $additional_parameters = { $group_attributes->%* };
241 $apiclass->register_method({
242 name
=> 'set_config',
245 description
=> "Modify '$oclass' group properties",
248 permissions
=> { check
=> [ 'admin' ] },
250 additionalProperties
=> 0,
253 description
=> "Object Group ID.",
257 description
=> "Group name.",
263 description
=> "Informational comment.",
268 $additional_parameters->%*,
271 returns
=> { type
=> "null" },
275 my $rdb = PMG
::RuleDB-
>new();
277 my $ogroup = extract_param
($param, 'ogroup');
279 die "no options specified\n"
280 if !scalar(keys %$param);
282 my $list = $rdb->load_objectgroups($oclass, $ogroup);
283 my $og = shift @$list ||
284 die "$oclass group '$ogroup' not found\n";
286 for my $prop (qw(name info and invert)) {
287 $og->{$prop} = $param->{$prop} if defined($param->{$prop});
290 $rdb->save_group($og);
292 PMG
::DBTools
::reload_ruledb
();
298 sub register_objects_api
{
299 my ($apiclass, $oclass, $path) = @_;
301 $apiclass->register_method({
305 description
=> "List '$oclass' group objects.",
307 permissions
=> { check
=> [ 'admin', 'audit' ] },
309 additionalProperties
=> 0,
312 description
=> "Object Group ID.",
322 id
=> { type
=> 'integer'},
325 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
330 my $rdb = PMG
::RuleDB-
>new();
332 my $og = $rdb->load_group_objects($param->{ogroup
});
336 foreach my $obj (@$og) {
337 push @$res, $obj->get_data();
343 $apiclass->register_method({
344 name
=> 'delete_object',
345 path
=> 'objects/{id}',
347 description
=> "Remove an object from the '$oclass' group.",
350 permissions
=> { check
=> [ 'admin' ] },
352 additionalProperties
=> 0,
355 description
=> "Object Group ID.",
359 description
=> "Object ID.",
364 returns
=> { type
=> 'null' },
368 my $rdb = PMG
::RuleDB-
>new();
370 my $obj = $rdb->load_object($param->{id
});
372 die "object '$param->{id}' does not exists\n" if !defined($obj);
374 $rdb->delete_object($obj);
376 PMG
::DBTools
::reload_ruledb
();