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