]>
git.proxmox.com Git - pmg-api.git/blob - 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
};
38 my $load_action_with_og = sub {
39 my ($rdb, $id, $exp_otype) = @_;
41 die "internal error" if $id !~ m/^(\d+)_(\d+)$/;
42 my ($ogroup, $objid) = ($1, $2);
44 my $list = $rdb->load_objectgroups('action', $ogroup);
45 my $og = shift @$list ||
46 die "action group '$ogroup' not found\n";
48 my $action = $rdb->load_object_full($objid, $ogroup, $exp_otype);
50 return ($og, $action);
53 __PACKAGE__-
>register_method ({
57 description
=> "Directory index.",
58 permissions
=> { check
=> [ 'admin', 'audit' ] },
60 additionalProperties
=> 0,
68 subdir
=> { type
=> 'string'},
71 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
77 { subdir
=> 'objects' },
79 { subdir
=> 'field' },
80 { subdir
=> 'notification' },
81 { subdir
=> 'disclaimer' },
82 { subdir
=> 'removeattachments' },
87 __PACKAGE__-
>register_method ({
88 name
=> 'list_actions',
91 description
=> "List 'actions' objects.",
93 permissions
=> { check
=> [ 'admin', 'audit' ] },
95 additionalProperties
=> 0,
106 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
111 my $rdb = PMG
::RuleDB-
>new();
113 my $ogroups = $rdb->load_objectgroups('action');
115 foreach my $og (@$ogroups) {
116 my $action = $og->{action
};
118 push @$res, $format_action_object->($og, $action);
124 __PACKAGE__-
>register_method ({
125 name
=> 'delete_action',
126 path
=> 'objects/{id}',
128 description
=> "Delete 'actions' object.",
131 permissions
=> { check
=> [ 'admin' ] },
133 additionalProperties
=> 0,
134 properties
=> { id
=> $id_property }
136 returns
=> { type
=> 'null' },
140 my $rdb = PMG
::RuleDB-
>new();
141 # test if object exists
142 my ($og, $action) = $load_action_with_og->($rdb, $param->{id
});
144 die "unable to delete standard actions\n" if !$action->oisedit();
146 $rdb->delete_group($og->{id
});
151 my $register_action_api = sub {
152 my ($class, $name) = @_;
154 my $otype = $class->otype();
155 my $otype_text = $class->otype_text();
156 my $properties = $class->properties();
158 my $create_properties = {
160 description
=> "Action name.",
165 description
=> "Informational comment.",
171 my $update_properties = {
174 description
=> "Action name.",
180 description
=> "Informational comment.",
186 my $read_properties = { id
=> $id_property };
188 foreach my $key (keys %$properties) {
189 $create_properties->{$key} = $properties->{$key};
190 $update_properties->{$key} = $properties->{$key};
193 __PACKAGE__-
>register_method ({
197 description
=> "Create '$otype_text' object.",
200 permissions
=> { check
=> [ 'admin' ] },
202 additionalProperties
=> 0,
203 properties
=> $create_properties,
206 description
=> "The object ID.",
212 my $rdb = PMG
::RuleDB-
>new();
214 my $obj = $rdb->get_object($otype);
215 $obj->update($param);
217 my $og = $rdb->create_group_with_obj($obj, $param->{name
}, $param->{info
});
219 return "$og->{id}_$obj->{id}";
222 __PACKAGE__-
>register_method ({
223 name
=> "read_$name",
224 path
=> "$name/{id}",
226 description
=> "Read '$otype_text' object settings.",
228 permissions
=> { check
=> [ 'admin', 'audit' ] },
230 additionalProperties
=> 0,
231 properties
=> $read_properties,
236 id
=> { type
=> 'string'},
242 my $rdb = PMG
::RuleDB-
>new();
244 my ($og, $action) = $load_action_with_og->($rdb, $param->{id
}, $otype);
246 return $format_action_object->($og, $action);
249 __PACKAGE__-
>register_method ({
250 name
=> "update_$name",
251 path
=> "$name/{id}",
253 description
=> "Update '$otype_text' object.",
256 permissions
=> { check
=> [ 'admin' ] },
258 additionalProperties
=> 0,
259 properties
=> $update_properties,
261 returns
=> { type
=> 'null' },
265 my $rdb = PMG
::RuleDB-
>new();
267 my ($og, $action) = $load_action_with_og->($rdb, $param->{id
}, $otype);
269 my $name = extract_param
($param, 'name');
270 my $info = extract_param
($param, 'info');
272 if (defined($name) || defined($info)) {
273 $og->{name
} = $name if defined($name);
274 $og->{info
} = $info if defined($info);
275 $rdb->save_group($og);
277 return undef if !scalar(keys %$param); # we are done
280 die "no options specified\n"
281 if !scalar(keys %$param);
283 $action->update($param);
287 PMG
::DBTools
::reload_ruledb
();
294 $register_action_api->('PMG::RuleDB::BCC', 'bcc');
295 $register_action_api->('PMG::RuleDB::ModField', 'field');
296 $register_action_api->('PMG::RuleDB::Notify', 'notification');
297 $register_action_api->('PMG::RuleDB::Disclaimer', 'disclaimer');
298 $register_action_api->('PMG::RuleDB::Remove', 'removeattachments');