]>
Commit | Line | Data |
---|---|---|
ee4f339e AD |
1 | package PVE::API2::Network::SDN::Dns; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::SafeSyslog; | |
7 | use PVE::Tools qw(extract_param); | |
8 | use PVE::Cluster qw(cfs_read_file cfs_write_file); | |
9 | use PVE::Network::SDN; | |
10 | use PVE::Network::SDN::Dns; | |
11 | use PVE::Network::SDN::Dns::Plugin; | |
12 | use PVE::Network::SDN::Dns::PowerdnsPlugin; | |
13 | ||
14 | use Storable qw(dclone); | |
15 | use PVE::JSONSchema qw(get_standard_option); | |
16 | use PVE::RPCEnvironment; | |
17 | ||
18 | use PVE::RESTHandler; | |
19 | ||
20 | use base qw(PVE::RESTHandler); | |
21 | ||
22 | my $sdn_dns_type_enum = PVE::Network::SDN::Dns::Plugin->lookup_types(); | |
23 | ||
24 | my $api_sdn_dns_config = sub { | |
25 | my ($cfg, $id) = @_; | |
26 | ||
27 | my $scfg = dclone(PVE::Network::SDN::Dns::sdn_dns_config($cfg, $id)); | |
28 | $scfg->{dns} = $id; | |
29 | $scfg->{digest} = $cfg->{digest}; | |
30 | ||
31 | return $scfg; | |
32 | }; | |
33 | ||
34 | __PACKAGE__->register_method ({ | |
35 | name => 'index', | |
36 | path => '', | |
37 | method => 'GET', | |
38 | description => "SDN dns index.", | |
39 | permissions => { | |
40 | description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/dns/<dns>'", | |
41 | user => 'all', | |
42 | }, | |
43 | parameters => { | |
44 | additionalProperties => 0, | |
45 | properties => { | |
46 | type => { | |
47 | description => "Only list sdn dns of specific type", | |
48 | type => 'string', | |
49 | enum => $sdn_dns_type_enum, | |
50 | optional => 1, | |
51 | }, | |
52 | }, | |
53 | }, | |
54 | returns => { | |
55 | type => 'array', | |
56 | items => { | |
57 | type => "object", | |
58 | properties => { dns => { type => 'string'}, | |
59 | type => { type => 'string'}, | |
60 | }, | |
61 | }, | |
62 | links => [ { rel => 'child', href => "{dns}" } ], | |
63 | }, | |
64 | code => sub { | |
65 | my ($param) = @_; | |
66 | ||
67 | my $rpcenv = PVE::RPCEnvironment::get(); | |
68 | my $authuser = $rpcenv->get_user(); | |
69 | ||
70 | ||
71 | my $cfg = PVE::Network::SDN::Dns::config(); | |
72 | ||
73 | my @sids = PVE::Network::SDN::Dns::sdn_dns_ids($cfg); | |
74 | my $res = []; | |
75 | foreach my $id (@sids) { | |
76 | my $privs = [ 'SDN.Audit', 'SDN.Allocate' ]; | |
77 | next if !$rpcenv->check_any($authuser, "/sdn/dns/$id", $privs, 1); | |
78 | ||
79 | my $scfg = &$api_sdn_dns_config($cfg, $id); | |
80 | next if $param->{type} && $param->{type} ne $scfg->{type}; | |
81 | ||
82 | my $plugin_config = $cfg->{ids}->{$id}; | |
83 | my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($plugin_config->{type}); | |
84 | push @$res, $scfg; | |
85 | } | |
86 | ||
87 | return $res; | |
88 | }}); | |
89 | ||
90 | __PACKAGE__->register_method ({ | |
91 | name => 'read', | |
92 | path => '{dns}', | |
93 | method => 'GET', | |
94 | description => "Read sdn dns configuration.", | |
95 | permissions => { | |
96 | check => ['perm', '/sdn/dns/{dns}', ['SDN.Allocate']], | |
97 | }, | |
98 | ||
99 | parameters => { | |
100 | additionalProperties => 0, | |
101 | properties => { | |
102 | dns => get_standard_option('pve-sdn-dns-id'), | |
103 | }, | |
104 | }, | |
105 | returns => { type => 'object' }, | |
106 | code => sub { | |
107 | my ($param) = @_; | |
108 | ||
109 | my $cfg = PVE::Network::SDN::Dns::config(); | |
110 | ||
111 | return &$api_sdn_dns_config($cfg, $param->{dns}); | |
112 | }}); | |
113 | ||
114 | __PACKAGE__->register_method ({ | |
115 | name => 'create', | |
116 | protected => 1, | |
117 | path => '', | |
118 | method => 'POST', | |
119 | description => "Create a new sdn dns object.", | |
120 | permissions => { | |
121 | check => ['perm', '/sdn/dns', ['SDN.Allocate']], | |
122 | }, | |
123 | parameters => PVE::Network::SDN::Dns::Plugin->createSchema(), | |
124 | returns => { type => 'null' }, | |
125 | code => sub { | |
126 | my ($param) = @_; | |
127 | ||
128 | my $type = extract_param($param, 'type'); | |
129 | my $id = extract_param($param, 'dns'); | |
130 | ||
131 | my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($type); | |
132 | my $opts = $plugin->check_config($id, $param, 1, 1); | |
133 | ||
134 | # create /etc/pve/sdn directory | |
135 | PVE::Cluster::check_cfs_quorum(); | |
136 | mkdir("/etc/pve/sdn"); | |
137 | ||
138 | PVE::Network::SDN::lock_sdn_config( | |
139 | sub { | |
140 | ||
141 | my $dns_cfg = PVE::Network::SDN::Dns::config(); | |
142 | ||
143 | my $scfg = undef; | |
144 | if ($scfg = PVE::Network::SDN::Dns::sdn_dns_config($dns_cfg, $id, 1)) { | |
145 | die "sdn dns object ID '$id' already defined\n"; | |
146 | } | |
147 | ||
148 | $dns_cfg->{ids}->{$id} = $opts; | |
149 | ||
150 | my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($opts->{type}); | |
151 | $plugin->on_update_hook($opts); | |
152 | ||
153 | PVE::Network::SDN::Dns::write_config($dns_cfg); | |
154 | ||
155 | }, "create sdn dns object failed"); | |
156 | ||
157 | return undef; | |
158 | }}); | |
159 | ||
160 | __PACKAGE__->register_method ({ | |
161 | name => 'update', | |
162 | protected => 1, | |
163 | path => '{dns}', | |
164 | method => 'PUT', | |
165 | description => "Update sdn dns object configuration.", | |
166 | permissions => { | |
167 | check => ['perm', '/sdn/dns', ['SDN.Allocate']], | |
168 | }, | |
169 | parameters => PVE::Network::SDN::Dns::Plugin->updateSchema(), | |
170 | returns => { type => 'null' }, | |
171 | code => sub { | |
172 | my ($param) = @_; | |
173 | ||
174 | my $id = extract_param($param, 'dns'); | |
175 | my $digest = extract_param($param, 'digest'); | |
176 | ||
177 | PVE::Network::SDN::lock_sdn_config( | |
178 | sub { | |
179 | ||
180 | my $dns_cfg = PVE::Network::SDN::Dns::config(); | |
181 | ||
182 | PVE::SectionConfig::assert_if_modified($dns_cfg, $digest); | |
183 | ||
184 | my $scfg = PVE::Network::SDN::Dns::sdn_dns_config($dns_cfg, $id); | |
185 | ||
186 | my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($scfg->{type}); | |
187 | my $opts = $plugin->check_config($id, $param, 0, 1); | |
188 | ||
189 | foreach my $k (%$opts) { | |
190 | $scfg->{$k} = $opts->{$k}; | |
191 | } | |
192 | ||
193 | $plugin->on_update_hook($scfg); | |
194 | ||
195 | PVE::Network::SDN::Dns::write_config($dns_cfg); | |
196 | ||
197 | }, "update sdn dns object failed"); | |
198 | ||
199 | return undef; | |
200 | }}); | |
201 | ||
202 | __PACKAGE__->register_method ({ | |
203 | name => 'delete', | |
204 | protected => 1, | |
205 | path => '{dns}', | |
206 | method => 'DELETE', | |
207 | description => "Delete sdn dns object configuration.", | |
208 | permissions => { | |
209 | check => ['perm', '/sdn/dns', ['SDN.Allocate']], | |
210 | }, | |
211 | parameters => { | |
212 | additionalProperties => 0, | |
213 | properties => { | |
214 | dns => get_standard_option('pve-sdn-dns-id', { | |
215 | completion => \&PVE::Network::SDN::Dns::complete_sdn_dns, | |
216 | }), | |
217 | }, | |
218 | }, | |
219 | returns => { type => 'null' }, | |
220 | code => sub { | |
221 | my ($param) = @_; | |
222 | ||
223 | my $id = extract_param($param, 'dns'); | |
224 | ||
225 | PVE::Network::SDN::lock_sdn_config( | |
226 | sub { | |
227 | ||
228 | my $cfg = PVE::Network::SDN::Dns::config(); | |
229 | ||
230 | my $scfg = PVE::Network::SDN::Dns::sdn_dns_config($cfg, $id); | |
231 | ||
232 | my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($scfg->{type}); | |
233 | ||
234 | delete $cfg->{ids}->{$id}; | |
235 | PVE::Network::SDN::Dns::write_config($cfg); | |
236 | ||
237 | }, "delete sdn dns object failed"); | |
238 | ||
239 | return undef; | |
240 | }}); | |
241 | ||
242 | 1; |