]>
Commit | Line | Data |
---|---|---|
3118b703 DM |
1 | package PMG::API2::Transport; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use PVE::SafeSyslog; | |
8 | use PVE::Tools qw(extract_param); | |
9 | use HTTP::Status qw(:constants); | |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | use PVE::RESTHandler; | |
12 | use PVE::INotify; | |
13 | ||
14 | use PMG::Config; | |
15 | ||
16 | use base qw(PVE::RESTHandler); | |
17 | ||
18 | __PACKAGE__->register_method ({ | |
19 | name => 'index', | |
20 | path => '', | |
21 | method => 'GET', | |
22 | description => "List transport map entries.", | |
23 | proxyto => 'master', | |
e6e62522 | 24 | permissions => { check => [ 'admin', 'audit' ] }, |
3118b703 DM |
25 | parameters => { |
26 | additionalProperties => 0, | |
27 | properties => {}, | |
28 | }, | |
29 | returns => { | |
30 | type => 'array', | |
31 | items => { | |
32 | type => "object", | |
33 | properties => { | |
34 | domain => { type => 'string' }, | |
35 | host => { type => 'string' }, | |
10d97956 | 36 | protocol => { type => 'string' }, |
3118b703 | 37 | port => { type => 'integer' }, |
53904163 | 38 | use_mx => { type => 'boolean' }, |
3118b703 DM |
39 | comment => { type => 'string'}, |
40 | }, | |
41 | }, | |
cc4512f6 | 42 | links => [ { rel => 'child', href => "{domain}" } ], |
3118b703 DM |
43 | }, |
44 | code => sub { | |
45 | my ($param) = @_; | |
46 | ||
47 | my $tmap = PVE::INotify::read_file('transport'); | |
48 | ||
49 | my $res = []; | |
50 | ||
51 | foreach my $domain (sort keys %$tmap) { | |
52 | push @$res, $tmap->{$domain}; | |
53 | } | |
54 | ||
55 | return $res; | |
56 | }}); | |
57 | ||
58 | __PACKAGE__->register_method ({ | |
59 | name => 'create', | |
60 | path => '', | |
61 | method => 'POST', | |
62 | proxyto => 'master', | |
63 | protected => 1, | |
e6e62522 | 64 | permissions => { check => [ 'admin' ] }, |
3118b703 DM |
65 | description => "Add transport map entry.", |
66 | parameters => { | |
67 | additionalProperties => 0, | |
68 | properties => { | |
69 | domain => { | |
70 | description => "Domain name.", | |
22c25daf | 71 | type => 'string', format => 'transport-domain-or-email', |
3118b703 DM |
72 | }, |
73 | host => { | |
74 | description => "Target host (name or IP address).", | |
75 | type => 'string', format => 'address', | |
76 | }, | |
10d97956 JZ |
77 | protocol => { |
78 | description => "Transport protocol.", | |
79 | type => 'string', | |
c072abb1 | 80 | enum => [qw(smtp lmtp)], |
10d97956 JZ |
81 | default => 'smtp', |
82 | optional => 1, | |
83 | }, | |
3118b703 | 84 | port => { |
10d97956 | 85 | description => "Transport port.", |
3118b703 DM |
86 | type => 'integer', |
87 | minimum => 1, | |
88 | maximum => 65535, | |
89 | optional => 1, | |
90 | default => 25, | |
91 | }, | |
53904163 | 92 | use_mx => { |
10d97956 | 93 | description => "Enable MX lookups (SMTP).", |
3118b703 DM |
94 | type => 'boolean', |
95 | optional => 1, | |
53904163 | 96 | default => 1, |
3118b703 DM |
97 | }, |
98 | comment => { | |
99 | description => "Comment.", | |
100 | type => 'string', | |
101 | optional => 1, | |
102 | }, | |
103 | }, | |
104 | }, | |
105 | returns => { type => 'null' }, | |
106 | code => sub { | |
107 | my ($param) = @_; | |
108 | ||
109 | my $code = sub { | |
110 | ||
111 | my $tmap = PVE::INotify::read_file('transport'); | |
112 | ||
113 | die "Transport map entry '$param->{domain}' already exists\n" | |
114 | if $tmap->{$param->{domain}}; | |
115 | ||
116 | $tmap->{$param->{domain}} = { | |
117 | domain => $param->{domain}, | |
118 | host => $param->{host}, | |
10d97956 | 119 | protocol => $param->{protocol} // 'smtp', |
3118b703 | 120 | port => $param->{port} // 25, |
53904163 | 121 | use_mx => $param->{use_mx} // 1, |
3118b703 DM |
122 | comment => $param->{comment} // '', |
123 | }; | |
124 | ||
125 | PVE::INotify::write_file('transport', $tmap); | |
126 | ||
127 | PMG::Config::postmap_pmg_transport(); | |
128 | }; | |
129 | ||
130 | PMG::Config::lock_config($code, "add transport map entry failed"); | |
131 | ||
132 | return undef; | |
133 | }}); | |
134 | ||
135 | __PACKAGE__->register_method ({ | |
136 | name => 'read', | |
137 | path => '{domain}', | |
138 | method => 'GET', | |
139 | description => "Read transport map entry.", | |
140 | proxyto => 'master', | |
e6e62522 | 141 | permissions => { check => [ 'admin', 'audit' ] }, |
3118b703 DM |
142 | parameters => { |
143 | additionalProperties => 0, | |
144 | properties => { | |
145 | domain => { | |
146 | description => "Domain name.", | |
22c25daf | 147 | type => 'string', format => 'transport-domain-or-email', |
3118b703 DM |
148 | }, |
149 | }, | |
150 | }, | |
151 | returns => { | |
152 | type => "object", | |
153 | properties => { | |
154 | domain => { type => 'string'}, | |
155 | host => { type => 'string'}, | |
10d97956 | 156 | protocol => { type => 'string'}, |
3118b703 | 157 | port => { type => 'integer'}, |
53904163 | 158 | use_mx => { type => 'boolean'}, |
3118b703 DM |
159 | comment => { type => 'string'}, |
160 | }, | |
161 | }, | |
162 | code => sub { | |
163 | my ($param) = @_; | |
164 | ||
165 | my $tmap = PVE::INotify::read_file('transport'); | |
166 | ||
167 | if (my $entry = $tmap->{$param->{domain}}) { | |
168 | return $entry; | |
169 | } | |
170 | ||
171 | die "Transport map entry '$param->{domain}' does not exist\n"; | |
172 | }}); | |
173 | ||
174 | __PACKAGE__->register_method ({ | |
175 | name => 'write', | |
176 | path => '{domain}', | |
177 | method => 'PUT', | |
178 | description => "Update transport map entry.", | |
179 | protected => 1, | |
e6e62522 | 180 | permissions => { check => [ 'admin' ] }, |
3118b703 DM |
181 | proxyto => 'master', |
182 | parameters => { | |
183 | additionalProperties => 0, | |
184 | properties => { | |
185 | domain => { | |
186 | description => "Domain name.", | |
22c25daf | 187 | type => 'string', format => 'transport-domain-or-email', |
3118b703 DM |
188 | }, |
189 | host => { | |
190 | description => "Target host (name or IP address).", | |
191 | type => 'string', format => 'address', | |
192 | optional => 1, | |
193 | }, | |
10d97956 JZ |
194 | protocol => { |
195 | description => "Transport protocol.", | |
196 | type => 'string', | |
197 | enum => [qw(smtp lmtp)], | |
198 | default => 'smtp', | |
199 | optional => 1, | |
200 | }, | |
3118b703 | 201 | port => { |
10d97956 | 202 | description => "Transport port.", |
3118b703 DM |
203 | type => 'integer', |
204 | minimum => 1, | |
205 | maximum => 65535, | |
206 | optional => 1, | |
207 | }, | |
53904163 | 208 | use_mx => { |
10d97956 | 209 | description => "Enable MX lookups (SMTP).", |
3118b703 DM |
210 | type => 'boolean', |
211 | optional => 1, | |
212 | }, | |
213 | comment => { | |
214 | description => "Comment.", | |
215 | type => 'string', | |
216 | optional => 1, | |
217 | }, | |
218 | }, | |
219 | }, | |
220 | returns => { type => 'null' }, | |
221 | code => sub { | |
222 | my ($param) = @_; | |
223 | ||
224 | my $code = sub { | |
225 | ||
226 | my $tmap = PVE::INotify::read_file('transport'); | |
227 | ||
228 | my $domain = extract_param($param, 'domain'); | |
229 | ||
230 | my $data = $tmap->{$domain}; | |
231 | ||
232 | die "Transport map entry '$param->{domain}' does not exist\n" if !$data; | |
233 | ||
234 | die "no options specified\n" if !scalar(keys %$param); | |
235 | ||
10d97956 | 236 | for my $prop (qw(host protocol port use_mx comment)) { |
3118b703 DM |
237 | $data->{$prop} = $param->{$prop} if defined($param->{$prop}); |
238 | } | |
239 | ||
240 | PVE::INotify::write_file('transport', $tmap); | |
241 | ||
242 | PMG::Config::postmap_pmg_transport(); | |
243 | }; | |
244 | ||
245 | PMG::Config::lock_config($code, "update transport map entry failed"); | |
246 | ||
247 | return undef; | |
248 | }}); | |
249 | ||
250 | __PACKAGE__->register_method ({ | |
251 | name => 'delete', | |
252 | path => '{domain}', | |
253 | method => 'DELETE', | |
254 | description => "Delete a transport map entry", | |
255 | protected => 1, | |
e6e62522 | 256 | permissions => { check => [ 'admin' ] }, |
3118b703 DM |
257 | proxyto => 'master', |
258 | parameters => { | |
259 | additionalProperties => 0, | |
260 | properties => { | |
261 | domain => { | |
262 | description => "Domain name.", | |
22c25daf | 263 | type => 'string', format => 'transport-domain-or-email', |
3118b703 DM |
264 | }, |
265 | } | |
266 | }, | |
267 | returns => { type => 'null' }, | |
268 | code => sub { | |
269 | my ($param) = @_; | |
270 | ||
271 | my $code = sub { | |
272 | ||
273 | my $tmap = PVE::INotify::read_file('transport'); | |
274 | ||
275 | die "Transport map entry '$param->{domain}' does not exist\n" | |
276 | if !$tmap->{$param->{domain}}; | |
277 | ||
278 | delete $tmap->{$param->{domain}}; | |
279 | ||
280 | PVE::INotify::write_file('transport', $tmap); | |
281 | ||
282 | PMG::Config::postmap_pmg_transport(); | |
283 | }; | |
284 | ||
285 | PMG::Config::lock_config($code, "delete transport map extry failed"); | |
286 | ||
287 | return undef; | |
288 | }}); | |
289 | ||
290 | 1; |