]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Zones.pm
sdn: rename config to running_config
[pve-network.git] / PVE / API2 / Network / SDN / Zones.pm
CommitLineData
4140be9e
AD
1package PVE::API2::Network::SDN::Zones;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Tools qw(extract_param);
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
f9bc9640 9use PVE::Network::SDN;
4140be9e
AD
10use PVE::Network::SDN::Vnets;
11use PVE::Network::SDN::Zones;
e8736dac 12use PVE::Network::SDN::Subnets;
4ad78442 13use PVE::Network::SDN::Dns;
4140be9e
AD
14use PVE::Network::SDN::Zones::Plugin;
15use PVE::Network::SDN::Zones::VlanPlugin;
16use PVE::Network::SDN::Zones::QinQPlugin;
17use PVE::Network::SDN::Zones::VxlanPlugin;
18use PVE::Network::SDN::Zones::EvpnPlugin;
19use PVE::Network::SDN::Zones::FaucetPlugin;
880ae857 20use PVE::Network::SDN::Zones::SimplePlugin;
4140be9e
AD
21
22use Storable qw(dclone);
23use PVE::JSONSchema qw(get_standard_option);
24use PVE::RPCEnvironment;
4ad78442 25use PVE::Exception qw(raise raise_param_exc);
4140be9e
AD
26
27use PVE::RESTHandler;
28
29use base qw(PVE::RESTHandler);
30
31my $sdn_zones_type_enum = PVE::Network::SDN::Zones::Plugin->lookup_types();
32
33my $api_sdn_zones_config = sub {
34 my ($cfg, $id) = @_;
35
36 my $scfg = dclone(PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id));
37 $scfg->{zone} = $id;
38 $scfg->{digest} = $cfg->{digest};
39
c2b9c173 40 if ($scfg->{nodes}) {
e382bf71
AD
41 $scfg->{nodes} = PVE::Network::SDN::encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
42 }
43
44 if ($scfg->{exitnodes}) {
45 $scfg->{exitnodes} = PVE::Network::SDN::encode_value($scfg->{type}, 'exitnodes', $scfg->{exitnodes});
c2b9c173
AD
46 }
47
6f5f42e4
AD
48 my $pending = $scfg->{pending};
49 if ($pending->{nodes}) {
e382bf71
AD
50 $pending->{nodes} = PVE::Network::SDN::encode_value($scfg->{type}, 'nodes', $pending->{nodes});
51 }
52
53 if ($pending->{exitnodes}) {
54 $pending->{exitnodes} = PVE::Network::SDN::encode_value($scfg->{type}, 'exitnodes', $pending->{exitnodes});
6f5f42e4
AD
55 }
56
4140be9e
AD
57 return $scfg;
58};
59
60__PACKAGE__->register_method ({
61 name => 'index',
62 path => '',
63 method => 'GET',
64 description => "SDN zones index.",
65 permissions => {
3551b612 66 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/zones/<zone>'",
4140be9e
AD
67 user => 'all',
68 },
69 parameters => {
70 additionalProperties => 0,
71 properties => {
72 type => {
73 description => "Only list sdn zones of specific type",
74 type => 'string',
75 enum => $sdn_zones_type_enum,
76 optional => 1,
77 },
6f5f42e4
AD
78 running => {
79 type => 'boolean',
80 optional => 1,
81 description => "Display running config.",
82 },
83 pending => {
84 type => 'boolean',
85 optional => 1,
86 description => "Display pending config.",
87 },
4140be9e
AD
88 },
89 },
90 returns => {
91 type => 'array',
92 items => {
93 type => "object",
0f700635 94 properties => { zone => { type => 'string'},
4140be9e 95 type => { type => 'string'},
6f5f42e4 96 mtu => { type => 'integer', optional => 1 },
4ad78442
AD
97 dns => { type => 'string', optional => 1},
98 reversedns => { type => 'string', optional => 1},
99 dnszone => { type => 'string', optional => 1},
331e2330 100 ipam => { type => 'string', optional => 1},
6f5f42e4
AD
101 pending => { optional => 1},
102 state => { type => 'string', optional => 1},
103 nodes => { type => 'string', optional => 1},
4140be9e
AD
104 },
105 },
106 links => [ { rel => 'child', href => "{zone}" } ],
107 },
108 code => sub {
109 my ($param) = @_;
110
111 my $rpcenv = PVE::RPCEnvironment::get();
112 my $authuser = $rpcenv->get_user();
113
6f5f42e4
AD
114 my $cfg = {};
115 if($param->{pending}) {
d73c7c36 116 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
117 my $config = PVE::Network::SDN::Zones::config();
118 $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'zones');
119 } elsif ($param->{running}) {
d73c7c36 120 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
121 $cfg = $running_cfg->{zones};
122 } else {
123 $cfg = PVE::Network::SDN::Zones::config();
124 }
4140be9e
AD
125
126 my @sids = PVE::Network::SDN::Zones::sdn_zones_ids($cfg);
127 my $res = [];
128 foreach my $id (@sids) {
3551b612
AD
129 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
130 next if !$rpcenv->check_any($authuser, "/sdn/zones/$id", $privs, 1);
4140be9e
AD
131
132 my $scfg = &$api_sdn_zones_config($cfg, $id);
133 next if $param->{type} && $param->{type} ne $scfg->{type};
134
135 my $plugin_config = $cfg->{ids}->{$id};
136 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
137 push @$res, $scfg;
138 }
139
140 return $res;
141 }});
142
143__PACKAGE__->register_method ({
144 name => 'read',
145 path => '{zone}',
146 method => 'GET',
147 description => "Read sdn zone configuration.",
3551b612
AD
148 permissions => {
149 check => ['perm', '/sdn/zones/{zone}', ['SDN.Allocate']],
150 },
4140be9e
AD
151
152 parameters => {
153 additionalProperties => 0,
154 properties => {
155 zone => get_standard_option('pve-sdn-zone-id'),
6f5f42e4
AD
156 running => {
157 type => 'boolean',
158 optional => 1,
159 description => "Display running config.",
160 },
161 pending => {
162 type => 'boolean',
163 optional => 1,
164 description => "Display pending config.",
165 }
4140be9e
AD
166 },
167 },
168 returns => { type => 'object' },
169 code => sub {
170 my ($param) = @_;
171
6f5f42e4
AD
172 my $cfg = {};
173 if($param->{pending}) {
d73c7c36 174 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
175 my $config = PVE::Network::SDN::Zones::config();
176 $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'zones');
177 } elsif ($param->{running}) {
d73c7c36 178 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
179 $cfg = $running_cfg->{zones};
180 } else {
181 $cfg = PVE::Network::SDN::Zones::config();
182 }
4140be9e
AD
183
184 return &$api_sdn_zones_config($cfg, $param->{zone});
185 }});
186
187__PACKAGE__->register_method ({
188 name => 'create',
189 protected => 1,
190 path => '',
191 method => 'POST',
192 description => "Create a new sdn zone object.",
3551b612
AD
193 permissions => {
194 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
195 },
4140be9e
AD
196 parameters => PVE::Network::SDN::Zones::Plugin->createSchema(),
197 returns => { type => 'null' },
198 code => sub {
199 my ($param) = @_;
200
201 my $type = extract_param($param, 'type');
202 my $id = extract_param($param, 'zone');
203
204 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($type);
205 my $opts = $plugin->check_config($id, $param, 1, 1);
206
45c3f15c
AD
207 # create /etc/pve/sdn directory
208 PVE::Cluster::check_cfs_quorum();
209 mkdir("/etc/pve/sdn");
210
f9bc9640 211 PVE::Network::SDN::lock_sdn_config(
4140be9e
AD
212 sub {
213
a2b32a94
AD
214 my $zone_cfg = PVE::Network::SDN::Zones::config();
215 my $controller_cfg = PVE::Network::SDN::Controllers::config();
4ad78442 216 my $dns_cfg = PVE::Network::SDN::Dns::config();
4140be9e
AD
217
218 my $scfg = undef;
a2b32a94 219 if ($scfg = PVE::Network::SDN::Zones::sdn_zones_config($zone_cfg, $id, 1)) {
4140be9e
AD
220 die "sdn zone object ID '$id' already defined\n";
221 }
4ad78442
AD
222
223 my $dnsserver = $opts->{dns};
224 my $reversednsserver = $opts->{reversedns};
225 my $dnszone = $opts->{dnszone};
226 raise_param_exc({ dns => "$dnsserver don't exist"}) if $dnsserver && !$dns_cfg->{ids}->{$dnsserver};
227 raise_param_exc({ reversedns => "$reversednsserver don't exist"}) if $reversednsserver && !$dns_cfg->{ids}->{$reversednsserver};
228 raise_param_exc({ dnszone => "missing dns server"}) if $dnszone && !$dnsserver;
4140be9e 229
331e2330
AD
230 my $ipam = $opts->{ipam};
231 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
232 raise_param_exc({ ipam => "$ipam not existing"}) if $ipam && !$ipam_cfg->{ids}->{$ipam};
233
a2b32a94
AD
234 $zone_cfg->{ids}->{$id} = $opts;
235 $plugin->on_update_hook($id, $zone_cfg, $controller_cfg);
4140be9e 236
a2b32a94 237 PVE::Network::SDN::Zones::write_config($zone_cfg);
4140be9e
AD
238
239 }, "create sdn zone object failed");
240
241 return undef;
242 }});
243
4140be9e
AD
244__PACKAGE__->register_method ({
245 name => 'update',
246 protected => 1,
247 path => '{zone}',
248 method => 'PUT',
249 description => "Update sdn zone object configuration.",
3551b612
AD
250 permissions => {
251 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
252 },
4140be9e
AD
253 parameters => PVE::Network::SDN::Zones::Plugin->updateSchema(),
254 returns => { type => 'null' },
255 code => sub {
256 my ($param) = @_;
257
258 my $id = extract_param($param, 'zone');
259 my $digest = extract_param($param, 'digest');
260
f9bc9640 261 PVE::Network::SDN::lock_sdn_config(
4140be9e
AD
262 sub {
263
a2b32a94
AD
264 my $zone_cfg = PVE::Network::SDN::Zones::config();
265 my $controller_cfg = PVE::Network::SDN::Controllers::config();
4ad78442 266 my $dns_cfg = PVE::Network::SDN::Dns::config();
4140be9e 267
a2b32a94 268 PVE::SectionConfig::assert_if_modified($zone_cfg, $digest);
4140be9e 269
a2b32a94 270 my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($zone_cfg, $id);
4140be9e
AD
271
272 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($scfg->{type});
273 my $opts = $plugin->check_config($id, $param, 0, 1);
274
e8736dac
AD
275 if($opts->{ipam} ne $scfg->{ipam}) {
276
277 #don't allow ipam change if subnet are defined
278 my $subnets_cfg = PVE::Network::SDN::Subnets::config();
279 foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
280 my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($subnets_cfg, $subnetid);
281 raise_param_exc({ ipam => "can't change ipam if subnet if already defined for this zone"}) if $subnet->{zone} eq $id;
282 }
283 }
284
4140be9e
AD
285 foreach my $k (%$opts) {
286 $scfg->{$k} = $opts->{$k};
287 }
288
4ad78442
AD
289 my $dnsserver = $opts->{dns};
290 my $reversednsserver = $opts->{reversedns};
291 my $dnszone = $opts->{dnszone};
292 raise_param_exc({ dns => "$dnsserver don't exist"}) if $dnsserver && !$dns_cfg->{ids}->{$dnsserver};
293 raise_param_exc({ reversedns => "$reversednsserver don't exist"}) if $reversednsserver && !$dns_cfg->{ids}->{$reversednsserver};
294 raise_param_exc({ dnszone => "missing dns server"}) if $dnszone && !$dnsserver;
295
331e2330
AD
296 my $ipam = $opts->{ipam};
297 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
298 raise_param_exc({ ipam => "$ipam not existing"}) if $ipam && !$ipam_cfg->{ids}->{$ipam};
299
a2b32a94 300 $plugin->on_update_hook($id, $zone_cfg, $controller_cfg);
4140be9e 301
a2b32a94 302 PVE::Network::SDN::Zones::write_config($zone_cfg);
4140be9e
AD
303
304 }, "update sdn zone object failed");
305
306 return undef;
307 }});
308
309__PACKAGE__->register_method ({
310 name => 'delete',
311 protected => 1,
312 path => '{zone}',
313 method => 'DELETE',
314 description => "Delete sdn zone object configuration.",
3551b612
AD
315 permissions => {
316 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
317 },
4140be9e
AD
318 parameters => {
319 additionalProperties => 0,
320 properties => {
321 zone => get_standard_option('pve-sdn-zone-id', {
322 completion => \&PVE::Network::SDN::Zones::complete_sdn_zones,
323 }),
324 },
325 },
326 returns => { type => 'null' },
327 code => sub {
328 my ($param) = @_;
329
330 my $id = extract_param($param, 'zone');
331
f9bc9640 332 PVE::Network::SDN::lock_sdn_config(
4140be9e
AD
333 sub {
334
335 my $cfg = PVE::Network::SDN::Zones::config();
336
337 my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id);
338
339 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($scfg->{type});
340
341 my $vnet_cfg = PVE::Network::SDN::Vnets::config();
342
343 $plugin->on_delete_hook($id, $vnet_cfg);
344
345 delete $cfg->{ids}->{$id};
346 PVE::Network::SDN::Zones::write_config($cfg);
347
348 }, "delete sdn zone object failed");
349
350
351 return undef;
352 }});
353
3541;