]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/ObjectGroupHelpers.pm
fix bux #1776: set http_proxy for sa-update
[pmg-api.git] / 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
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);
40
41 return $data;
42 }
43
44 sub format_object_group {
45 my ($ogroups) = @_;
46
47 my $res = [];
48 foreach my $og (@$ogroups) {
49 push @$res, {
50 id => $og->{id}, name => $og->{name}, info => $og->{info}
51 };
52 }
53 return $res;
54 }
55
56 sub register_group_list_api {
57 my ($apiclass, $oclass) = @_;
58
59 $apiclass->register_method({
60 name => "list_${oclass}_groups",
61 path => $oclass,
62 method => 'GET',
63 description => "Get list of '$oclass' groups.",
64 proxyto => 'master',
65 permissions => { check => [ 'admin', 'audit' ] },
66 parameters => {
67 additionalProperties => 0,
68 properties => {},
69 },
70 returns => {
71 type => 'array',
72 items => {
73 type => "object",
74 properties => {
75 id => { type => 'integer' },
76 },
77 },
78 },
79 code => sub {
80 my ($param) = @_;
81
82 my $rdb = PMG::RuleDB->new();
83
84 my $ogroups = $rdb->load_objectgroups($oclass);
85
86 return format_object_group($ogroups);
87 }});
88
89 $apiclass->register_method({
90 name => "create_${oclass}_group",
91 path => $oclass,
92 method => 'POST',
93 description => "Create a new '$oclass' group.",
94 proxyto => 'master',
95 protected => 1,
96 permissions => { check => [ 'admin' ] },
97 parameters => {
98 additionalProperties => 0,
99 properties => {
100 name => {
101 description => "Group name.",
102 type => 'string',
103 maxLength => 255,
104 },
105 info => {
106 description => "Informational comment.",
107 type => 'string',
108 maxLength => 255,
109 optional => 1,
110 },
111 },
112 },
113 returns => { type => 'integer' },
114 code => sub {
115 my ($param) = @_;
116
117 my $rdb = PMG::RuleDB->new();
118
119 my $og = PMG::RuleDB::Group->new(
120 $param->{name}, $param->{info} // '', $oclass);
121
122 return $rdb->save_group($og);
123 }});
124 }
125
126 sub register_delete_object_group_api {
127 my ($apiclass, $oclass, $path) = @_;
128
129 $apiclass->register_method({
130 name => 'delete_{$oclass}_group',
131 path => $path,
132 method => 'DELETE',
133 description => "Delete a '$oclass' group.",
134 proxyto => 'master',
135 protected => 1,
136 permissions => { check => [ 'admin' ] },
137 parameters => {
138 additionalProperties => 0,
139 properties => {
140 ogroup => {
141 description => "Object Group ID.",
142 type => 'integer',
143 },
144 },
145 },
146 returns => { type => 'null' },
147 code => sub {
148 my ($param) = @_;
149
150 my $rdb = PMG::RuleDB->new();
151
152 $rdb->delete_group($param->{ogroup});
153
154 return undef;
155 }});
156 }
157
158 sub register_object_group_config_api {
159 my ($apiclass, $oclass, $path) = @_;
160
161 $apiclass->register_method({
162 name => 'get_config',
163 path => $path,
164 method => 'GET',
165 description => "Get '$oclass' group properties",
166 proxyto => 'master',
167 permissions => { check => [ 'admin', 'audit' ] },
168 parameters => {
169 additionalProperties => 0,
170 properties => {
171 ogroup => {
172 description => "Object Group ID.",
173 type => 'integer',
174 },
175 },
176 },
177 returns => {
178 type => "object",
179 properties => {
180 id => { type => 'integer'},
181 name => { type => 'string' },
182 info => { type => 'string' },
183 },
184 },
185 code => sub {
186 my ($param) = @_;
187
188 my $rdb = PMG::RuleDB->new();
189
190 my $list = $rdb->load_objectgroups($oclass, $param->{ogroup});
191 my $og = shift @$list ||
192 die "$oclass group '$param->{ogroup}' not found\n";
193
194 return {
195 id => $og->{id},
196 name => $og->{name},
197 info => $og->{info},
198 };
199
200 }});
201
202 $apiclass->register_method({
203 name => 'set_config',
204 path => $path,
205 method => 'PUT',
206 description => "Modify '$oclass' group properties",
207 proxyto => 'master',
208 protected => 1,
209 permissions => { check => [ 'admin' ] },
210 parameters => {
211 additionalProperties => 0,
212 properties => {
213 ogroup => {
214 description => "Object Group ID.",
215 type => 'integer',
216 },
217 name => {
218 description => "Group name.",
219 type => 'string',
220 maxLength => 255,
221 optional => 1,
222 },
223 info => {
224 description => "Informational comment.",
225 type => 'string',
226 maxLength => 255,
227 optional => 1,
228 },
229 },
230 },
231 returns => { type => "null" },
232 code => sub {
233 my ($param) = @_;
234
235 my $rdb = PMG::RuleDB->new();
236
237 my $ogroup = extract_param($param, 'ogroup');
238
239 die "no options specified\n"
240 if !scalar(keys %$param);
241
242 my $list = $rdb->load_objectgroups($oclass, $ogroup);
243 my $og = shift @$list ||
244 die "$oclass group '$ogroup' not found\n";
245
246 $og->{name} = $param->{name} if defined($param->{name});
247 $og->{info} = $param->{info} if defined($param->{info});
248
249 $rdb->save_group($og);
250
251 return undef;
252 }});
253 }
254
255 sub register_objects_api {
256 my ($apiclass, $oclass, $path) = @_;
257
258 $apiclass->register_method({
259 name => 'objects',
260 path => $path,
261 method => 'GET',
262 description => "List '$oclass' group objects.",
263 proxyto => 'master',
264 permissions => { check => [ 'admin', 'audit' ] },
265 parameters => {
266 additionalProperties => 0,
267 properties => {
268 ogroup => {
269 description => "Object Group ID.",
270 type => 'integer',
271 },
272 },
273 },
274 returns => {
275 type => 'array',
276 items => {
277 type => "object",
278 properties => {
279 id => { type => 'integer'},
280 },
281 },
282 links => [ { rel => 'child', href => "{id}" } ],
283 },
284 code => sub {
285 my ($param) = @_;
286
287 my $rdb = PMG::RuleDB->new();
288
289 my $og = $rdb->load_group_objects($param->{ogroup});
290
291 my $res = [];
292
293 foreach my $obj (@$og) {
294 push @$res, $obj->get_data();
295 }
296
297 return $res;
298 }});
299
300 $apiclass->register_method({
301 name => 'delete_object',
302 path => 'objects/{id}',
303 method => 'DELETE',
304 description => "Remove an object from the '$oclass' group.",
305 proxyto => 'master',
306 protected => 1,
307 permissions => { check => [ 'admin' ] },
308 parameters => {
309 additionalProperties => 0,
310 properties => {
311 ogroup => {
312 description => "Object Group ID.",
313 type => 'integer',
314 },
315 id => {
316 description => "Object ID.",
317 type => 'integer',
318 },
319 },
320 },
321 returns => { type => 'null' },
322 code => sub {
323 my ($param) = @_;
324
325 my $rdb = PMG::RuleDB->new();
326
327 my $obj = $rdb->load_object($param->{id});
328
329 die "object '$param->{id}' does not exists\n" if !defined($obj);
330
331 $rdb->delete_object($obj);
332
333 PMG::DBTools::reload_ruledb();
334
335 return undef;
336 }});
337 }
338
339 1;