]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/RuleDB.pm
Add default rulename of 'unknown' to Actions
[pmg-api.git] / PMG / API2 / RuleDB.pm
1 package PMG::API2::RuleDB;
2
3 use strict;
4 use warnings;
5
6 use PVE::INotify;
7 use PVE::RESTHandler;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PMG::RESTEnvironment;
10 use PVE::SafeSyslog;
11 use PVE::Tools qw(extract_param);
12
13 use PMG::DBTools;
14 use PMG::RuleDB;
15 use PMG::RuleCache;
16
17 use PMG::API2::ObjectGroupHelpers;
18 use PMG::API2::Who;
19 use PMG::API2::When;
20 use PMG::API2::What;
21 use PMG::API2::Action;
22 use PMG::API2::Rules;
23
24 use base qw(PVE::RESTHandler);
25
26 __PACKAGE__->register_method ({
27 name => 'index',
28 path => '',
29 method => 'GET',
30 description => "Directory index.",
31 parameters => {
32 additionalProperties => 0,
33 properties => {},
34 },
35 permissions => { check => [ 'admin', 'audit' ] },
36 returns => {
37 type => 'array',
38 items => {
39 type => "object",
40 properties => {},
41 },
42 links => [ { rel => 'child', href => "{name}" } ],
43 },
44 code => sub {
45 my ($param) = @_;
46
47 my $result = [
48 { name => 'digest' },
49 { name => 'action' },
50 { name => 'rules' },
51 { name => 'what' },
52 { name => 'when' },
53 { name => 'who' },
54 ];
55
56 return $result;
57 }});
58
59 __PACKAGE__->register_method ({
60 name => 'reset_ruledb',
61 path => '',
62 method => 'POST',
63 description => "Reset PMG rule database back to factory defaults.",
64 permissions => { check => [ 'admin' ] },
65 protected => 1,
66 parameters => {
67 additionalProperties => 0,
68 properties => {}
69 },
70 returns => { type => 'null'},
71 code => sub {
72 my ($param) = @_;
73
74 my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb");
75 my $ruledb = PMG::RuleDB->new($dbh);
76 PMG::DBTools::init_ruledb($ruledb, 1);
77
78 return undef;
79 }});
80
81 __PACKAGE__->register_method({
82 name => 'ruledb_digest',
83 path => 'digest',
84 method => 'GET',
85 description => "Returns the rule database digest. This is used internally for cluster synchronization.",
86 # always run on local node, root@pam only
87 parameters => {
88 additionalProperties => 0,
89 properties => {},
90 },
91 permissions => { check => [ 'admin', 'audit' ] },
92 returns => { type => 'string' },
93 code => sub {
94 my ($param) = @_;
95
96 my $rdb = PMG::RuleDB->new();
97 my $rulecache = PMG::RuleCache->new($rdb);
98
99 return $rulecache->{digest};
100 }});
101
102 __PACKAGE__->register_method({
103 name => 'list_rules',
104 path => 'rules',
105 method => 'GET',
106 description => "Get list of rules.",
107 proxyto => 'master',
108 permissions => { check => [ 'admin', 'audit' ] },
109 parameters => {
110 additionalProperties => 0,
111 properties => {},
112 },
113 returns => {
114 type => 'array',
115 items => {
116 type => "object",
117 properties => {
118 id => { type => 'integer' },
119 },
120 },
121 links => [ { rel => 'child', href => "{id}" } ],
122 },
123 code => sub {
124 my ($param) = @_;
125
126 my $rdb = PMG::RuleDB->new();
127
128 my $rules = $rdb->load_rules();
129
130 my $res = [];
131
132 my $cond_create_group = sub {
133 my ($res, $name, $groupdata) = @_;
134
135 return if !$groupdata;
136
137 $res->{$name} = PMG::API2::ObjectGroupHelpers::format_object_group($groupdata);
138 };
139
140 foreach my $rule (@$rules) {
141 my ($from, $to, $when, $what, $action) =
142 $rdb->load_groups($rule);
143
144 my $data = PMG::API2::ObjectGroupHelpers::format_rule(
145 $rule, $from, $to, $when, $what, $action);
146
147 push @$res, $data;
148 }
149
150 $rdb->close();
151
152 return $res;
153 }});
154
155 __PACKAGE__->register_method({
156 name => 'create_rule',
157 path => 'rules',
158 method => 'POST',
159 description => "Create new rule.",
160 proxyto => 'master',
161 protected => 1,
162 permissions => { check => [ 'admin' ] },
163 parameters => {
164 additionalProperties => 0,
165 properties => {
166 name => {
167 description => "Rule name",
168 type => 'string',
169 },
170 priority => {
171 description => "Rule priotity.",
172 type => 'integer',
173 minimum => 0,
174 maximum => 100,
175 },
176 direction => {
177 description => "Rule direction. Value `0` matches incomming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
178 type => 'integer',
179 minimum => 0,
180 maximum => 2,
181 optional => 1,
182 },
183 active => {
184 description => "Flag to activate rule.",
185 type => 'boolean',
186 optional => 1,
187 },
188 },
189 },
190 returns => { type => 'integer' },
191 code => sub {
192 my ($param) = @_;
193
194 my $rdb = PMG::RuleDB->new();
195
196 my $rule = PMG::RuleDB::Rule->new (
197 $param->{name}, $param->{priority}, $param->{active}, $param->{direction});
198
199 return $rdb->save_rule($rule);
200 }});
201
202 __PACKAGE__->register_method ({
203 subclass => 'PMG::API2::Rules',
204 path => 'rules/{id}',
205 });
206
207
208 __PACKAGE__->register_method ({
209 subclass => 'PMG::API2::Action',
210 path => 'action',
211 });
212
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');
216
217 __PACKAGE__->register_method ({
218 subclass => 'PMG::API2::Who',
219 path => 'who/{ogroup}',
220 });
221
222 __PACKAGE__->register_method ({
223 subclass => 'PMG::API2::When',
224 path => 'when/{ogroup}',
225 });
226
227 __PACKAGE__->register_method ({
228 subclass => 'PMG::API2::What',
229 path => 'what/{ogroup}',
230 });
231
232
233 1;