]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Transport.pm
followup: indentation and description improvement
[pmg-api.git] / src / PMG / API2 / Transport.pm
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',
24 permissions => { check => [ 'admin', 'audit' ] },
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' },
36 protocol => { type => 'string' },
37 port => { type => 'integer' },
38 use_mx => { type => 'boolean' },
39 comment => { type => 'string'},
40 },
41 },
42 links => [ { rel => 'child', href => "{domain}" } ],
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,
64 permissions => { check => [ 'admin' ] },
65 description => "Add transport map entry.",
66 parameters => {
67 additionalProperties => 0,
68 properties => {
69 domain => {
70 description => "Domain name.",
71 type => 'string', format => 'transport-domain-or-email',
72 },
73 host => {
74 description => "Target host (name or IP address).",
75 type => 'string', format => 'address',
76 },
77 protocol => {
78 description => "Transport protocol.",
79 type => 'string',
80 enum => [qw(smtp lmtp)],
81 default => 'smtp',
82 optional => 1,
83 },
84 port => {
85 description => "Transport port.",
86 type => 'integer',
87 minimum => 1,
88 maximum => 65535,
89 optional => 1,
90 default => 25,
91 },
92 use_mx => {
93 description => "Enable MX lookups (SMTP).",
94 type => 'boolean',
95 optional => 1,
96 default => 1,
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},
119 protocol => $param->{protocol} // 'smtp',
120 port => $param->{port} // 25,
121 use_mx => $param->{use_mx} // 1,
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',
141 permissions => { check => [ 'admin', 'audit' ] },
142 parameters => {
143 additionalProperties => 0,
144 properties => {
145 domain => {
146 description => "Domain name.",
147 type => 'string', format => 'transport-domain-or-email',
148 },
149 },
150 },
151 returns => {
152 type => "object",
153 properties => {
154 domain => { type => 'string'},
155 host => { type => 'string'},
156 protocol => { type => 'string'},
157 port => { type => 'integer'},
158 use_mx => { type => 'boolean'},
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,
180 permissions => { check => [ 'admin' ] },
181 proxyto => 'master',
182 parameters => {
183 additionalProperties => 0,
184 properties => {
185 domain => {
186 description => "Domain name.",
187 type => 'string', format => 'transport-domain-or-email',
188 },
189 host => {
190 description => "Target host (name or IP address).",
191 type => 'string', format => 'address',
192 optional => 1,
193 },
194 protocol => {
195 description => "Transport protocol.",
196 type => 'string',
197 enum => [qw(smtp lmtp)],
198 default => 'smtp',
199 optional => 1,
200 },
201 port => {
202 description => "Transport port.",
203 type => 'integer',
204 minimum => 1,
205 maximum => 65535,
206 optional => 1,
207 },
208 use_mx => {
209 description => "Enable MX lookups (SMTP).",
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
236 for my $prop (qw(host protocol port use_mx comment)) {
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,
256 permissions => { check => [ 'admin' ] },
257 proxyto => 'master',
258 parameters => {
259 additionalProperties => 0,
260 properties => {
261 domain => {
262 description => "Domain name.",
263 type => 'string', format => 'transport-domain-or-email',
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;