]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Action.pm
bump version to 8.1.4
[pmg-api.git] / src / PMG / API2 / Action.pm
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
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};
34 $data->{editable} = $action->oisedit();
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 };
53
54 __PACKAGE__->register_method ({
55 name => 'index',
56 path => '',
57 method => 'GET',
58 description => "Directory index.",
59 permissions => { check => [ 'admin', 'audit' ] },
60 parameters => {
61 additionalProperties => 0,
62 properties => {},
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 [
78 { subdir => 'objects' },
79 { subdir => 'bcc' },
80 { subdir => 'field' },
81 { subdir => 'notification' },
82 { subdir => 'disclaimer' },
83 { subdir => 'removeattachments' },
84 ];
85
86 }});
87
88 __PACKAGE__->register_method ({
89 name => 'list_actions',
90 path => 'objects',
91 method => 'GET',
92 description => "List 'actions' objects.",
93 proxyto => 'master',
94 permissions => { check => [ 'admin', 'audit' ] },
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;
119 push @$res, $format_action_object->($og, $action);
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.",
130 proxyto => 'master',
131 protected => 1,
132 permissions => { check => [ 'admin' ] },
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();
142 # test if object exists
143 my ($og, $action) = $load_action_with_og->($rdb, $param->{id});
144
145 die "unable to delete standard actions\n" if !$action->oisedit();
146
147 $rdb->delete_group($og->{id});
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
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 };
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',
200 protected => 1,
201 permissions => { check => [ 'admin' ] },
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
218 my $og = $rdb->create_group_with_obj($obj, $param->{name}, $param->{info});
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',
229 permissions => { check => [ 'admin', 'audit' ] },
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
245 my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype);
246
247 return $format_action_object->($og, $action);
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',
256 protected => 1,
257 permissions => { check => [ 'admin' ] },
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
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 }
280
281 die "no options specified\n"
282 if !scalar(keys %$param);
283
284 $action->update($param);
285
286 $action->save($rdb);
287
288 PMG::DBTools::reload_ruledb();
289
290 return undef;
291 }});
292
293 };
294
295 $register_action_api->('PMG::RuleDB::BCC', 'bcc');
296 $register_action_api->('PMG::RuleDB::ModField', 'field');
297 $register_action_api->('PMG::RuleDB::Notify', 'notification');
298 $register_action_api->('PMG::RuleDB::Disclaimer', 'disclaimer');
299 $register_action_api->('PMG::RuleDB::Remove', 'removeattachments');
300
301 1;