]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Action.pm
1 package PMG
::API2
::Action
;
8 use PVE
::Tools
qw(extract_param);
9 use HTTP
::Status
qw(:constants);
10 use PVE
::JSONSchema
qw(get_standard_option);
19 use base
qw(PVE::RESTHandler);
22 description
=> "Action Object ID.",
27 my $format_action_object = sub {
28 my ($og, $action) = @_;
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();
39 my $load_action_with_og = sub {
40 my ($rdb, $id, $exp_otype) = @_;
42 die "internal error" if $id !~ m/^(\d+)_(\d+)$/;
43 my ($ogroup, $objid) = ($1, $2);
45 my $list = $rdb->load_objectgroups('action', $ogroup);
46 my $og = shift @$list ||
47 die "action group '$ogroup' not found\n";
49 my $action = $rdb->load_object_full($objid, $ogroup, $exp_otype);
51 return ($og, $action);
54 __PACKAGE__-
>register_method ({
58 description
=> "Directory index.",
59 permissions
=> { check
=> [ 'admin', 'audit' ] },
61 additionalProperties
=> 0,
69 subdir
=> { type
=> 'string'},
72 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
78 { subdir
=> 'objects' },
80 { subdir
=> 'field' },
81 { subdir
=> 'notification' },
82 { subdir
=> 'disclaimer' },
83 { subdir
=> 'removeattachments' },
88 __PACKAGE__-
>register_method ({
89 name
=> 'list_actions',
92 description
=> "List 'actions' objects.",
94 permissions
=> { check
=> [ 'admin', 'audit' ] },
96 additionalProperties
=> 0,
107 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
112 my $rdb = PMG
::RuleDB-
>new();
114 my $ogroups = $rdb->load_objectgroups('action');
116 foreach my $og (@$ogroups) {
117 my $action = $og->{action
};
119 push @$res, $format_action_object->($og, $action);
125 __PACKAGE__-
>register_method ({
126 name
=> 'delete_action',
127 path
=> 'objects/{id}',
129 description
=> "Delete 'actions' object.",
132 permissions
=> { check
=> [ 'admin' ] },
134 additionalProperties
=> 0,
135 properties
=> { id
=> $id_property }
137 returns
=> { type
=> 'null' },
141 my $rdb = PMG
::RuleDB-
>new();
142 # test if object exists
143 my ($og, $action) = $load_action_with_og->($rdb, $param->{id
});
145 die "unable to delete standard actions\n" if !$action->oisedit();
147 $rdb->delete_group($og->{id
});
152 my $register_action_api = sub {
153 my ($class, $name) = @_;
155 my $otype = $class->otype();
156 my $otype_text = $class->otype_text();
157 my $properties = $class->properties();
159 my $create_properties = {
161 description
=> "Action name.",
166 description
=> "Informational comment.",
172 my $update_properties = {
175 description
=> "Action name.",
181 description
=> "Informational comment.",
187 my $read_properties = { id
=> $id_property };
189 foreach my $key (keys %$properties) {
190 $create_properties->{$key} = $properties->{$key};
191 $update_properties->{$key} = $properties->{$key};
194 __PACKAGE__-
>register_method ({
198 description
=> "Create '$otype_text' object.",
201 permissions
=> { check
=> [ 'admin' ] },
203 additionalProperties
=> 0,
204 properties
=> $create_properties,
207 description
=> "The object ID.",
213 my $rdb = PMG
::RuleDB-
>new();
215 my $obj = $rdb->get_object($otype);
216 $obj->update($param);
218 my $og = $rdb->create_group_with_obj($obj, $param->{name
}, $param->{info
});
220 return "$og->{id}_$obj->{id}";
223 __PACKAGE__-
>register_method ({
224 name
=> "read_$name",
225 path
=> "$name/{id}",
227 description
=> "Read '$otype_text' object settings.",
229 permissions
=> { check
=> [ 'admin', 'audit' ] },
231 additionalProperties
=> 0,
232 properties
=> $read_properties,
237 id
=> { type
=> 'string'},
243 my $rdb = PMG
::RuleDB-
>new();
245 my ($og, $action) = $load_action_with_og->($rdb, $param->{id
}, $otype);
247 return $format_action_object->($og, $action);
250 __PACKAGE__-
>register_method ({
251 name
=> "update_$name",
252 path
=> "$name/{id}",
254 description
=> "Update '$otype_text' object.",
257 permissions
=> { check
=> [ 'admin' ] },
259 additionalProperties
=> 0,
260 properties
=> $update_properties,
262 returns
=> { type
=> 'null' },
266 my $rdb = PMG
::RuleDB-
>new();
268 my ($og, $action) = $load_action_with_og->($rdb, $param->{id
}, $otype);
270 my $name = extract_param
($param, 'name');
271 my $info = extract_param
($param, 'info');
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);
278 return undef if !scalar(keys %$param); # we are done
281 die "no options specified\n"
282 if !scalar(keys %$param);
284 $action->update($param);
288 PMG
::DBTools
::reload_ruledb
();
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');