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
},
35 $cond_create_group->($data, 'from', $from);
36 $cond_create_group->($data, 'to', $to);
37 $cond_create_group->($data, 'when', $when);
38 $cond_create_group->($data, 'what', $what);
39 $cond_create_group->($data, 'action', $action);
44 sub format_object_group
{
48 foreach my $og (@$ogroups) {
50 id
=> $og->{id
}, name
=> $og->{name
}, info
=> $og->{info
}
56 sub register_group_list_api
{
57 my ($apiclass, $oclass) = @_;
59 $apiclass->register_method({
60 name
=> "list_${oclass}_groups",
63 description
=> "Get list of '$oclass' groups.",
65 permissions
=> { check
=> [ 'admin', 'audit' ] },
67 additionalProperties
=> 0,
75 id
=> { type
=> 'integer' },
82 my $rdb = PMG
::RuleDB-
>new();
84 my $ogroups = $rdb->load_objectgroups($oclass);
86 return format_object_group
($ogroups);
89 $apiclass->register_method({
90 name
=> "create_${oclass}_group",
93 description
=> "Create a new '$oclass' group.",
96 permissions
=> { check
=> [ 'admin' ] },
98 additionalProperties
=> 0,
101 description
=> "Group name.",
106 description
=> "Informational comment.",
113 returns
=> { type
=> 'integer' },
117 my $rdb = PMG
::RuleDB-
>new();
119 my $og = PMG
::RuleDB
::Group-
>new(
120 $param->{name
}, $param->{info
} // '', $oclass);
122 return $rdb->save_group($og);
126 sub register_delete_object_group_api
{
127 my ($apiclass, $oclass, $path) = @_;
129 $apiclass->register_method({
130 name
=> 'delete_{$oclass}_group',
133 description
=> "Delete a '$oclass' group.",
136 permissions
=> { check
=> [ 'admin' ] },
138 additionalProperties
=> 0,
141 description
=> "Object Group ID.",
146 returns
=> { type
=> 'null' },
150 my $rdb = PMG
::RuleDB-
>new();
152 $rdb->delete_group($param->{ogroup
});
158 sub register_object_group_config_api
{
159 my ($apiclass, $oclass, $path) = @_;
161 $apiclass->register_method({
162 name
=> 'get_config',
165 description
=> "Get '$oclass' group properties",
167 permissions
=> { check
=> [ 'admin', 'audit' ] },
169 additionalProperties
=> 0,
172 description
=> "Object Group ID.",
180 id
=> { type
=> 'integer'},
181 name
=> { type
=> 'string' },
182 info
=> { type
=> 'string' },
188 my $rdb = PMG
::RuleDB-
>new();
190 my $list = $rdb->load_objectgroups($oclass, $param->{ogroup
});
191 my $og = shift @$list ||
192 die "$oclass group '$param->{ogroup}' not found\n";
202 $apiclass->register_method({
203 name
=> 'set_config',
206 description
=> "Modify '$oclass' group properties",
209 permissions
=> { check
=> [ 'admin' ] },
211 additionalProperties
=> 0,
214 description
=> "Object Group ID.",
218 description
=> "Group name.",
224 description
=> "Informational comment.",
231 returns
=> { type
=> "null" },
235 my $rdb = PMG
::RuleDB-
>new();
237 my $ogroup = extract_param
($param, 'ogroup');
239 die "no options specified\n"
240 if !scalar(keys %$param);
242 my $list = $rdb->load_objectgroups($oclass, $ogroup);
243 my $og = shift @$list ||
244 die "$oclass group '$ogroup' not found\n";
246 $og->{name
} = $param->{name
} if defined($param->{name
});
247 $og->{info
} = $param->{info
} if defined($param->{info
});
249 $rdb->save_group($og);
255 sub register_objects_api
{
256 my ($apiclass, $oclass, $path) = @_;
258 $apiclass->register_method({
262 description
=> "List '$oclass' group objects.",
264 permissions
=> { check
=> [ 'admin', 'audit' ] },
266 additionalProperties
=> 0,
269 description
=> "Object Group ID.",
279 id
=> { type
=> 'integer'},
282 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
287 my $rdb = PMG
::RuleDB-
>new();
289 my $og = $rdb->load_group_objects($param->{ogroup
});
293 foreach my $obj (@$og) {
294 push @$res, $obj->get_data();
300 $apiclass->register_method({
301 name
=> 'delete_object',
302 path
=> 'objects/{id}',
304 description
=> "Remove an object from the '$oclass' group.",
307 permissions
=> { check
=> [ 'admin' ] },
309 additionalProperties
=> 0,
312 description
=> "Object Group ID.",
316 description
=> "Object ID.",
321 returns
=> { type
=> 'null' },
325 my $rdb = PMG
::RuleDB-
>new();
327 my $obj = $rdb->load_object($param->{id
});
329 die "object '$param->{id}' does not exists\n" if !defined($obj);
331 $rdb->delete_object($obj);
333 PMG
::DBTools
::reload_ruledb
();