]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/RuleDB.pm
1 package PMG
::API2
::RuleDB
;
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PMG
::RESTEnvironment
;
11 use PVE
::Tools
qw(extract_param);
17 use PMG
::API2
::ObjectGroupHelpers
;
21 use PMG
::API2
::Action
;
24 use base
qw(PVE::RESTHandler);
26 __PACKAGE__-
>register_method ({
30 description
=> "Directory index.",
32 additionalProperties
=> 0,
35 permissions
=> { check
=> [ 'admin', 'audit' ] },
42 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
59 __PACKAGE__-
>register_method ({
60 name
=> 'reset_ruledb',
63 description
=> "Reset PMG rule database back to factory defaults.",
64 permissions
=> { check
=> [ 'admin' ] },
67 additionalProperties
=> 0,
70 returns
=> { type
=> 'null'},
74 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
75 my $ruledb = PMG
::RuleDB-
>new($dbh);
76 PMG
::DBTools
::init_ruledb
($ruledb, 1);
81 __PACKAGE__-
>register_method({
82 name
=> 'ruledb_digest',
85 description
=> "Returns the rule database digest. This is used internally for cluster synchronization.",
86 # always run on local node, root@pam only
88 additionalProperties
=> 0,
91 permissions
=> { check
=> [ 'admin', 'audit' ] },
92 returns
=> { type
=> 'string' },
96 my $rdb = PMG
::RuleDB-
>new();
97 my $rulecache = PMG
::RuleCache-
>new($rdb);
99 return $rulecache->{digest
};
102 __PACKAGE__-
>register_method({
103 name
=> 'list_rules',
106 description
=> "Get list of rules.",
108 permissions
=> { check
=> [ 'admin', 'audit' ] },
110 additionalProperties
=> 0,
118 id
=> { type
=> 'integer' },
121 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
126 my $rdb = PMG
::RuleDB-
>new();
128 my $rules = $rdb->load_rules();
132 my $cond_create_group = sub {
133 my ($res, $name, $groupdata) = @_;
135 return if !$groupdata;
137 $res->{$name} = PMG
::API2
::ObjectGroupHelpers
::format_object_group
($groupdata);
140 foreach my $rule (@$rules) {
141 my ($from, $to, $when, $what, $action) =
142 $rdb->load_groups($rule);
144 my $data = PMG
::API2
::ObjectGroupHelpers
::format_rule
(
145 $rule, $from, $to, $when, $what, $action);
155 __PACKAGE__-
>register_method({
156 name
=> 'create_rule',
159 description
=> "Create new rule.",
162 permissions
=> { check
=> [ 'admin' ] },
164 additionalProperties
=> 0,
167 description
=> "Rule name",
171 description
=> "Rule priotity.",
177 description
=> "Rule direction. Value `0` matches incoming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
184 description
=> "Flag to activate rule.",
190 returns
=> { type
=> 'integer' },
194 my $rdb = PMG
::RuleDB-
>new();
196 my $rule = PMG
::RuleDB
::Rule-
>new (
197 $param->{name
}, $param->{priority
}, $param->{active
}, $param->{direction
});
199 return $rdb->save_rule($rule);
202 __PACKAGE__-
>register_method ({
203 subclass
=> 'PMG::API2::Rules',
204 path
=> 'rules/{id}',
208 __PACKAGE__-
>register_method ({
209 subclass
=> 'PMG::API2::Action',
213 PMG
::API2
::ObjectGroupHelpers
::register_group_list_api
(__PACKAGE__
, 'what');
214 PMG
::API2
::ObjectGroupHelpers
::register_group_list_api
(__PACKAGE__
, 'when');
215 PMG
::API2
::ObjectGroupHelpers
::register_group_list_api
(__PACKAGE__
, 'who');
217 __PACKAGE__-
>register_method ({
218 subclass
=> 'PMG::API2::Who',
219 path
=> 'who/{ogroup}',
222 __PACKAGE__-
>register_method ({
223 subclass
=> 'PMG::API2::When',
224 path
=> 'when/{ogroup}',
227 __PACKAGE__-
>register_method ({
228 subclass
=> 'PMG::API2::What',
229 path
=> 'what/{ogroup}',