]>
Commit | Line | Data |
---|---|---|
11a86e67 DM |
1 | package PMG::API2::Action; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use PVE::SafeSyslog; | |
8 | use PVE::Tools qw(extract_param); | |
9 | use HTTP::Status qw(:constants); | |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | use PVE::RESTHandler; | |
12 | use PVE::INotify; | |
13 | ||
14 | use PMG::Config; | |
15 | ||
16 | use PMG::RuleDB::BCC; | |
17 | use PMG::RuleDB; | |
18 | ||
19 | use base qw(PVE::RESTHandler); | |
20 | ||
bdf383f3 DM |
21 | my $id_property = { |
22 | description => "Action Object ID.", | |
23 | type => 'string', | |
24 | pattern => '\d+_\d+', | |
25 | }; | |
26 | ||
27 | my $format_action_object = sub { | |
28 | my ($og, $action) = @_; | |
29 | ||
30 | my $data = $action->get_data(); | |
31 | $data->{id} = "$data->{ogroup}_$data->{id}"; | |
32 | $data->{name} = $og->{name}; | |
33 | $data->{info} = $og->{info}; | |
a279d288 | 34 | $data->{editable} = $action->oisedit(); |
bdf383f3 DM |
35 | |
36 | return $data; | |
37 | }; | |
38 | ||
39 | my $load_action_with_og = sub { | |
40 | my ($rdb, $id, $exp_otype) = @_; | |
41 | ||
42 | die "internal error" if $id !~ m/^(\d+)_(\d+)$/; | |
43 | my ($ogroup, $objid) = ($1, $2); | |
44 | ||
45 | my $list = $rdb->load_objectgroups('action', $ogroup); | |
46 | my $og = shift @$list || | |
47 | die "action group '$ogroup' not found\n"; | |
48 | ||
49 | my $action = $rdb->load_object_full($objid, $ogroup, $exp_otype); | |
50 | ||
51 | return ($og, $action); | |
52 | }; | |
59434740 | 53 | |
11a86e67 DM |
54 | __PACKAGE__->register_method ({ |
55 | name => 'index', | |
56 | path => '', | |
57 | method => 'GET', | |
58 | description => "Directory index.", | |
b07f4d68 | 59 | permissions => { check => [ 'admin', 'audit' ] }, |
11a86e67 DM |
60 | parameters => { |
61 | additionalProperties => 0, | |
59434740 | 62 | properties => {}, |
11a86e67 DM |
63 | }, |
64 | returns => { | |
65 | type => 'array', | |
66 | items => { | |
67 | type => "object", | |
68 | properties => { | |
69 | subdir => { type => 'string'}, | |
70 | }, | |
71 | }, | |
72 | links => [ { rel => 'child', href => "{subdir}" } ], | |
73 | }, | |
74 | code => sub { | |
75 | my ($param) = @_; | |
76 | ||
77 | return [ | |
11a86e67 DM |
78 | { subdir => 'objects' }, |
79 | { subdir => 'bcc' }, | |
b678cdfe | 80 | { subdir => 'field' }, |
4cb22484 | 81 | { subdir => 'notification' }, |
f1d2c9e5 | 82 | { subdir => 'disclaimer' }, |
46eec8b4 | 83 | { subdir => 'removeattachments' }, |
11a86e67 DM |
84 | ]; |
85 | ||
86 | }}); | |
87 | ||
59434740 DM |
88 | __PACKAGE__->register_method ({ |
89 | name => 'list_actions', | |
90 | path => 'objects', | |
91 | method => 'GET', | |
92 | description => "List 'actions' objects.", | |
b97da1ac | 93 | proxyto => 'master', |
b07f4d68 | 94 | permissions => { check => [ 'admin', 'audit' ] }, |
59434740 DM |
95 | parameters => { |
96 | additionalProperties => 0, | |
97 | properties => {}, | |
98 | }, | |
99 | returns => { | |
100 | type => 'array', | |
101 | items => { | |
102 | type => "object", | |
103 | properties => { | |
104 | id => $id_property, | |
105 | }, | |
106 | }, | |
107 | links => [ { rel => 'child', href => "{id}" } ], | |
108 | }, | |
109 | code => sub { | |
110 | my ($param) = @_; | |
111 | ||
112 | my $rdb = PMG::RuleDB->new(); | |
113 | ||
114 | my $ogroups = $rdb->load_objectgroups('action'); | |
115 | my $res = []; | |
116 | foreach my $og (@$ogroups) { | |
117 | my $action = $og->{action}; | |
118 | next if !$action; | |
bdf383f3 | 119 | push @$res, $format_action_object->($og, $action); |
59434740 DM |
120 | } |
121 | ||
122 | return $res; | |
123 | }}); | |
124 | ||
125 | __PACKAGE__->register_method ({ | |
126 | name => 'delete_action', | |
127 | path => 'objects/{id}', | |
128 | method => 'DELETE', | |
129 | description => "Delete 'actions' object.", | |
b97da1ac DM |
130 | proxyto => 'master', |
131 | protected => 1, | |
b07f4d68 | 132 | permissions => { check => [ 'admin' ] }, |
59434740 DM |
133 | parameters => { |
134 | additionalProperties => 0, | |
135 | properties => { id => $id_property } | |
136 | }, | |
137 | returns => { type => 'null' }, | |
138 | code => sub { | |
139 | my ($param) = @_; | |
140 | ||
141 | my $rdb = PMG::RuleDB->new(); | |
59434740 | 142 | # test if object exists |
bdf383f3 | 143 | my ($og, $action) = $load_action_with_og->($rdb, $param->{id}); |
53333505 | 144 | |
47c733fc | 145 | die "unable to delete standard actions\n" if !$action->oisedit(); |
53333505 | 146 | |
bdf383f3 | 147 | $rdb->delete_group($og->{id}); |
59434740 DM |
148 | |
149 | return undef; | |
150 | }}); | |
151 | ||
152 | my $register_action_api = sub { | |
153 | my ($class, $name) = @_; | |
154 | ||
155 | my $otype = $class->otype(); | |
156 | my $otype_text = $class->otype_text(); | |
157 | my $properties = $class->properties(); | |
158 | ||
bdf383f3 DM |
159 | my $create_properties = { |
160 | name => { | |
161 | description => "Action name.", | |
162 | type => 'string', | |
163 | maxLength => 255, | |
164 | }, | |
165 | info => { | |
166 | description => "Informational comment.", | |
167 | type => 'string', | |
168 | maxLength => 255, | |
169 | optional => 1, | |
170 | }, | |
171 | }; | |
172 | my $update_properties = { | |
173 | id => $id_property, | |
174 | name => { | |
175 | description => "Action name.", | |
176 | type => 'string', | |
177 | maxLength => 255, | |
178 | optional => 1, | |
179 | }, | |
180 | info => { | |
181 | description => "Informational comment.", | |
182 | type => 'string', | |
183 | maxLength => 255, | |
184 | optional => 1, | |
185 | }, | |
186 | }; | |
59434740 DM |
187 | my $read_properties = { id => $id_property }; |
188 | ||
189 | foreach my $key (keys %$properties) { | |
190 | $create_properties->{$key} = $properties->{$key}; | |
191 | $update_properties->{$key} = $properties->{$key}; | |
192 | } | |
193 | ||
194 | __PACKAGE__->register_method ({ | |
195 | name => $name, | |
196 | path => $name, | |
197 | method => 'POST', | |
198 | description => "Create '$otype_text' object.", | |
199 | proxyto => 'master', | |
b97da1ac | 200 | protected => 1, |
b07f4d68 | 201 | permissions => { check => [ 'admin' ] }, |
59434740 DM |
202 | parameters => { |
203 | additionalProperties => 0, | |
204 | properties => $create_properties, | |
205 | }, | |
206 | returns => { | |
207 | description => "The object ID.", | |
208 | type => 'string', | |
209 | }, | |
210 | code => sub { | |
211 | my ($param) = @_; | |
212 | ||
213 | my $rdb = PMG::RuleDB->new(); | |
214 | ||
215 | my $obj = $rdb->get_object($otype); | |
216 | $obj->update($param); | |
217 | ||
9973fc66 | 218 | my $og = $rdb->create_group_with_obj($obj, $param->{name}, $param->{info}); |
59434740 DM |
219 | |
220 | return "$og->{id}_$obj->{id}"; | |
221 | }}); | |
222 | ||
223 | __PACKAGE__->register_method ({ | |
224 | name => "read_$name", | |
225 | path => "$name/{id}", | |
226 | method => 'GET', | |
227 | description => "Read '$otype_text' object settings.", | |
228 | proxyto => 'master', | |
b07f4d68 | 229 | permissions => { check => [ 'admin', 'audit' ] }, |
59434740 DM |
230 | parameters => { |
231 | additionalProperties => 0, | |
232 | properties => $read_properties, | |
233 | }, | |
234 | returns => { | |
235 | type => "object", | |
236 | properties => { | |
237 | id => { type => 'string'}, | |
238 | }, | |
239 | }, | |
240 | code => sub { | |
241 | my ($param) = @_; | |
242 | ||
243 | my $rdb = PMG::RuleDB->new(); | |
244 | ||
bdf383f3 | 245 | my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype); |
59434740 | 246 | |
bdf383f3 | 247 | return $format_action_object->($og, $action); |
59434740 DM |
248 | }}); |
249 | ||
250 | __PACKAGE__->register_method ({ | |
251 | name => "update_$name", | |
252 | path => "$name/{id}", | |
253 | method => 'PUT', | |
254 | description => "Update '$otype_text' object.", | |
255 | proxyto => 'master', | |
b97da1ac | 256 | protected => 1, |
b07f4d68 | 257 | permissions => { check => [ 'admin' ] }, |
59434740 DM |
258 | parameters => { |
259 | additionalProperties => 0, | |
260 | properties => $update_properties, | |
261 | }, | |
262 | returns => { type => 'null' }, | |
263 | code => sub { | |
264 | my ($param) = @_; | |
265 | ||
266 | my $rdb = PMG::RuleDB->new(); | |
267 | ||
bdf383f3 DM |
268 | my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype); |
269 | ||
270 | my $name = extract_param($param, 'name'); | |
271 | my $info = extract_param($param, 'info'); | |
272 | ||
273 | if (defined($name) || defined($info)) { | |
274 | $og->{name} = $name if defined($name); | |
275 | $og->{info} = $info if defined($info); | |
276 | $rdb->save_group($og); | |
277 | ||
278 | return undef if !scalar(keys %$param); # we are done | |
279 | } | |
59434740 | 280 | |
bdf383f3 DM |
281 | die "no options specified\n" |
282 | if !scalar(keys %$param); | |
59434740 DM |
283 | |
284 | $action->update($param); | |
285 | ||
286 | $action->save($rdb); | |
287 | ||
cbef3ff8 DM |
288 | PMG::DBTools::reload_ruledb(); |
289 | ||
59434740 DM |
290 | return undef; |
291 | }}); | |
292 | ||
293 | }; | |
294 | ||
295 | $register_action_api->('PMG::RuleDB::BCC', 'bcc'); | |
b678cdfe | 296 | $register_action_api->('PMG::RuleDB::ModField', 'field'); |
4cb22484 | 297 | $register_action_api->('PMG::RuleDB::Notify', 'notification'); |
f1d2c9e5 | 298 | $register_action_api->('PMG::RuleDB::Disclaimer', 'disclaimer'); |
46eec8b4 | 299 | $register_action_api->('PMG::RuleDB::Remove', 'removeattachments'); |
11a86e67 | 300 | |
11a86e67 | 301 | 1; |