]>
Commit | Line | Data |
---|---|---|
5b2bb556 DM |
1 | package PMG::API2::Domains; |
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 relay domains.", | |
23 | proxyto => 'master', | |
24 | parameters => { | |
25 | additionalProperties => 0, | |
26 | properties => {}, | |
27 | }, | |
28 | returns => { | |
29 | type => 'array', | |
30 | items => { | |
31 | type => "object", | |
32 | properties => { | |
33 | domain => { type => 'string'}, | |
b7298186 | 34 | comment => { type => 'string'}, |
5b2bb556 DM |
35 | }, |
36 | }, | |
94d4606f | 37 | links => [ { rel => 'child', href => "{domain}" } ], |
5b2bb556 DM |
38 | }, |
39 | code => sub { | |
40 | my ($param) = @_; | |
41 | ||
42 | my $domains = PVE::INotify::read_file('domains'); | |
43 | ||
44 | my $res = []; | |
45 | ||
b7298186 DM |
46 | foreach my $domain (sort keys %$domains) { |
47 | push @$res, $domains->{$domain}; | |
5b2bb556 DM |
48 | } |
49 | ||
50 | return $res; | |
51 | }}); | |
52 | ||
4d39dbdc DM |
53 | __PACKAGE__->register_method ({ |
54 | name => 'create', | |
55 | path => '', | |
56 | method => 'POST', | |
57 | proxyto => 'master', | |
58 | protected => 1, | |
59 | description => "Add relay domain.", | |
60 | parameters => { | |
61 | additionalProperties => 0, | |
62 | properties => { | |
63 | domain => { | |
64 | description => "Domain name.", | |
65 | type => 'string', format => 'dns-name', | |
66 | }, | |
b7298186 DM |
67 | comment => { |
68 | description => "Comment.", | |
69 | type => 'string', | |
70 | optional => 1, | |
71 | }, | |
4d39dbdc DM |
72 | }, |
73 | }, | |
74 | returns => { type => 'null' }, | |
75 | code => sub { | |
76 | my ($param) = @_; | |
77 | ||
78 | my $code = sub { | |
79 | ||
80 | my $domains = PVE::INotify::read_file('domains'); | |
81 | ||
82 | die "Domain '$param->{domain}' already exists\n" | |
b7298186 | 83 | if $domains->{$param->{domain}}; |
4d39dbdc | 84 | |
b7298186 DM |
85 | $domains->{$param->{domain}} = { |
86 | comment => $param->{comment} // '', | |
87 | }; | |
4d39dbdc DM |
88 | |
89 | PVE::INotify::write_file('domains', $domains); | |
c3f4336c DM |
90 | |
91 | PMG::Config::postmap_pmg_domains(); | |
4d39dbdc DM |
92 | }; |
93 | ||
94 | PMG::Config::lock_config($code, "add relay domain failed"); | |
95 | ||
96 | return undef; | |
97 | }}); | |
98 | ||
b7298186 DM |
99 | __PACKAGE__->register_method ({ |
100 | name => 'read', | |
101 | path => '{domain}', | |
102 | method => 'GET', | |
103 | description => "Read Domain data (comment).", | |
104 | proxyto => 'master', | |
105 | parameters => { | |
106 | additionalProperties => 0, | |
107 | properties => { | |
108 | domain => { | |
109 | description => "Domain name.", | |
110 | type => 'string', format => 'dns-name', | |
111 | }, | |
112 | }, | |
113 | }, | |
114 | returns => { | |
115 | type => "object", | |
116 | properties => { | |
117 | domain => { type => 'string'}, | |
118 | comment => { type => 'string'}, | |
119 | }, | |
120 | }, | |
121 | code => sub { | |
122 | my ($param) = @_; | |
123 | ||
124 | my $domains = PVE::INotify::read_file('domains'); | |
125 | ||
126 | die "Domain '$param->{domain}' does not exist\n" | |
127 | if !$domains->{$param->{domain}}; | |
128 | ||
129 | return $domains->{$param->{domain}}; | |
130 | ||
131 | return undef; | |
132 | }}); | |
133 | ||
134 | __PACKAGE__->register_method ({ | |
135 | name => 'write', | |
136 | path => '{domain}', | |
137 | method => 'PUT', | |
138 | description => "Update relay domain data (comment).", | |
139 | protected => 1, | |
140 | proxyto => 'master', | |
141 | parameters => { | |
142 | additionalProperties => 0, | |
143 | properties => { | |
144 | domain => { | |
145 | description => "Domain name.", | |
146 | type => 'string', format => 'dns-name', | |
147 | }, | |
148 | comment => { | |
149 | description => "Comment.", | |
150 | type => 'string', | |
151 | }, | |
152 | }, | |
153 | }, | |
154 | returns => { type => 'null' }, | |
155 | code => sub { | |
156 | my ($param) = @_; | |
157 | ||
158 | my $code = sub { | |
159 | ||
160 | my $domains = PVE::INotify::read_file('domains'); | |
161 | ||
162 | die "Domain '$param->{domain}' does not exist\n" | |
163 | if !$domains->{$param->{domain}}; | |
164 | ||
165 | $domains->{$param->{domain}}->{comment} = $param->{comment}; | |
166 | ||
167 | PVE::INotify::write_file('domains', $domains); | |
168 | ||
169 | PMG::Config::postmap_pmg_domains(); | |
170 | }; | |
171 | ||
172 | PMG::Config::lock_config($code, "update relay domain failed"); | |
173 | ||
174 | return undef; | |
175 | }}); | |
176 | ||
4d39dbdc DM |
177 | __PACKAGE__->register_method ({ |
178 | name => 'delete', | |
179 | path => '{domain}', | |
180 | method => 'DELETE', | |
181 | description => "Delete a relay domain", | |
182 | protected => 1, | |
183 | proxyto => 'master', | |
184 | parameters => { | |
185 | additionalProperties => 0, | |
186 | properties => { | |
187 | domain => { | |
188 | description => "Domain name.", | |
189 | type => 'string', format => 'dns-name', | |
190 | }, | |
191 | } | |
192 | }, | |
193 | returns => { type => 'null' }, | |
194 | code => sub { | |
195 | my ($param) = @_; | |
196 | ||
197 | my $code = sub { | |
198 | ||
199 | my $domains = PVE::INotify::read_file('domains'); | |
200 | ||
201 | die "Domain '$param->{domain}' does not exist\n" | |
b7298186 | 202 | if !$domains->{$param->{domain}}; |
4d39dbdc | 203 | |
b7298186 | 204 | delete $domains->{$param->{domain}}; |
4d39dbdc | 205 | |
b7298186 | 206 | PVE::INotify::write_file('domains', $domains); |
c3f4336c DM |
207 | |
208 | PMG::Config::postmap_pmg_domains(); | |
4d39dbdc DM |
209 | }; |
210 | ||
211 | PMG::Config::lock_config($code, "delete relay domain failed"); | |
212 | ||
213 | return undef; | |
214 | }}); | |
215 | ||
5b2bb556 | 216 | 1; |