]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Subnets.pm
api: add running/pending zones/vnets/subnets/controllers
[pve-network.git] / PVE / API2 / Network / SDN / Subnets.pm
CommitLineData
c33dd818
AD
1package PVE::API2::Network::SDN::Subnets;
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);
70b03506 9use PVE::Exception qw(raise raise_param_exc);
c33dd818
AD
10use PVE::Network::SDN;
11use PVE::Network::SDN::Subnets;
12use PVE::Network::SDN::SubnetPlugin;
58a7773a 13use PVE::Network::SDN::Vnets;
70b03506
AD
14use PVE::Network::SDN::Ipams;
15use PVE::Network::SDN::Ipams::Plugin;
c33dd818
AD
16
17use Storable qw(dclone);
18use PVE::JSONSchema qw(get_standard_option);
19use PVE::RPCEnvironment;
20
21use PVE::RESTHandler;
22
23use base qw(PVE::RESTHandler);
24
25my $api_sdn_subnets_config = sub {
26 my ($cfg, $id) = @_;
27
28 my $scfg = dclone(PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id));
29 $scfg->{subnet} = $id;
30 $scfg->{cidr} = $id =~ s/-/\//r;
31 $scfg->{digest} = $cfg->{digest};
32
33 return $scfg;
34};
35
36__PACKAGE__->register_method ({
37 name => 'index',
38 path => '',
39 method => 'GET',
40 description => "SDN subnets index.",
41 permissions => {
42 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/subnets/<subnet>'",
43 user => 'all',
44 },
45 parameters => {
46 additionalProperties => 0,
3926d9a7
AD
47 properties => {
48 vnet => get_standard_option('pve-sdn-vnet-id'),
6f5f42e4
AD
49 running => {
50 type => 'boolean',
51 optional => 1,
52 description => "Display running config.",
53 },
54 pending => {
55 type => 'boolean',
56 optional => 1,
57 description => "Display pending config.",
58 },
59 },
c33dd818
AD
60 },
61 returns => {
62 type => 'array',
63 items => {
64 type => "object",
65 properties => {},
66 },
67 links => [ { rel => 'child', href => "{subnet}" } ],
68 },
69 code => sub {
70 my ($param) = @_;
71
72 my $rpcenv = PVE::RPCEnvironment::get();
73 my $authuser = $rpcenv->get_user();
74
3926d9a7 75 my $vnetid = $param->{vnet};
c33dd818 76
6f5f42e4
AD
77 my $cfg = {};
78 if($param->{pending}) {
79 my $running_cfg = PVE::Network::SDN::config();
80 my $config = PVE::Network::SDN::Subnets::config();
81 $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'subnets');
82 } elsif ($param->{running}) {
83 my $running_cfg = PVE::Network::SDN::config();
84 $cfg = $running_cfg->{subnets};
85 } else {
86 $cfg = PVE::Network::SDN::Subnets::config();
87 }
c33dd818
AD
88
89 my @sids = PVE::Network::SDN::Subnets::sdn_subnets_ids($cfg);
90 my $res = [];
91 foreach my $id (@sids) {
92 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
3926d9a7 93 next if !$rpcenv->check_any($authuser, "/sdn/vnets/$vnetid/subnets/$id", $privs, 1);
c33dd818
AD
94
95 my $scfg = &$api_sdn_subnets_config($cfg, $id);
3926d9a7 96 next if !$scfg->{vnet} || $scfg->{vnet} ne $vnetid;
c33dd818
AD
97 push @$res, $scfg;
98 }
99
100 return $res;
101 }});
102
103__PACKAGE__->register_method ({
104 name => 'read',
105 path => '{subnet}',
106 method => 'GET',
107 description => "Read sdn subnet configuration.",
108 permissions => {
3926d9a7 109 check => ['perm', '/sdn/vnets/{vnet}/subnets/{subnet}', ['SDN.Allocate']],
c33dd818
AD
110 },
111
112 parameters => {
113 additionalProperties => 0,
114 properties => {
3926d9a7 115 vnet => get_standard_option('pve-sdn-vnet-id'),
c33dd818
AD
116 subnet => get_standard_option('pve-sdn-subnet-id', {
117 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
118 }),
6f5f42e4
AD
119 running => {
120 type => 'boolean',
121 optional => 1,
122 description => "Display running config.",
123 },
124 pending => {
125 type => 'boolean',
126 optional => 1,
127 description => "Display pending config.",
128 },
129 },
c33dd818
AD
130 },
131 returns => { type => 'object' },
132 code => sub {
133 my ($param) = @_;
134
6f5f42e4
AD
135 my $cfg = {};
136 if($param->{pending}) {
137 my $running_cfg = PVE::Network::SDN::config();
138 my $config = PVE::Network::SDN::Subnets::config();
139 $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'subnets');
140 } elsif ($param->{running}) {
141 my $running_cfg = PVE::Network::SDN::config();
142 $cfg = $running_cfg->{subnets};
143 } else {
144 $cfg = PVE::Network::SDN::Subnets::config();
145 }
146
3926d9a7
AD
147 my $scfg = &$api_sdn_subnets_config($cfg, $param->{subnet});
148
149 raise_param_exc({ vnet => "wrong vnet"}) if $param->{vnet} ne $scfg->{vnet};
c33dd818 150
3926d9a7 151 return $scfg;
c33dd818
AD
152 }});
153
154__PACKAGE__->register_method ({
155 name => 'create',
156 protected => 1,
157 path => '',
158 method => 'POST',
159 description => "Create a new sdn subnet object.",
160 permissions => {
3926d9a7 161 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
162 },
163 parameters => PVE::Network::SDN::SubnetPlugin->createSchema(),
164 returns => { type => 'null' },
165 code => sub {
166 my ($param) = @_;
167
168 my $type = extract_param($param, 'type');
169 my $cidr = extract_param($param, 'subnet');
170 my $id = $cidr =~ s/\//-/r;
171
172 # create /etc/pve/sdn directory
173 PVE::Cluster::check_cfs_quorum();
174 mkdir("/etc/pve/sdn") if ! -d '/etc/pve/sdn';
175
176 PVE::Network::SDN::lock_sdn_config(
177 sub {
178
179 my $cfg = PVE::Network::SDN::Subnets::config();
180 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 1, 1);
181
182 my $scfg = undef;
183 if ($scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id, 1)) {
184 die "sdn subnet object ID '$id' already defined\n";
185 }
186
187 $cfg->{ids}->{$id} = $opts;
e612faf6 188 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $opts);
70b03506 189
c33dd818 190 PVE::Network::SDN::Subnets::write_config($cfg);
c33dd818
AD
191
192 }, "create sdn subnet object failed");
193
194 return undef;
195 }});
196
197__PACKAGE__->register_method ({
198 name => 'update',
199 protected => 1,
200 path => '{subnet}',
201 method => 'PUT',
202 description => "Update sdn subnet object configuration.",
203 permissions => {
3926d9a7 204 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
205 },
206 parameters => PVE::Network::SDN::SubnetPlugin->updateSchema(),
207 returns => { type => 'null' },
208 code => sub {
209 my ($param) = @_;
210
211 my $id = extract_param($param, 'subnet');
212 my $digest = extract_param($param, 'digest');
213
214 PVE::Network::SDN::lock_sdn_config(
215 sub {
216
217 my $cfg = PVE::Network::SDN::Subnets::config();
70b03506 218 my $scfg = &$api_sdn_subnets_config($cfg, $id);
c33dd818
AD
219
220 PVE::SectionConfig::assert_if_modified($cfg, $digest);
221
222 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 0, 1);
223 $cfg->{ids}->{$id} = $opts;
224
9dfa9202
AD
225 raise_param_exc({ ipam => "you can't change ipam"}) if $opts->{ipam} && $scfg->{ipam} && $opts->{ipam} ne $scfg->{ipam};
226
e612faf6 227 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $opts, $scfg);
70b03506 228
c33dd818 229 PVE::Network::SDN::Subnets::write_config($cfg);
c33dd818
AD
230
231 }, "update sdn subnet object failed");
232
233 return undef;
234 }});
235
236__PACKAGE__->register_method ({
237 name => 'delete',
238 protected => 1,
239 path => '{subnet}',
240 method => 'DELETE',
241 description => "Delete sdn subnet object configuration.",
242 permissions => {
3926d9a7 243 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
244 },
245 parameters => {
246 additionalProperties => 0,
247 properties => {
3926d9a7 248 vnet => get_standard_option('pve-sdn-vnet-id'),
c33dd818
AD
249 subnet => get_standard_option('pve-sdn-subnet-id', {
250 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
251 }),
252 },
253 },
254 returns => { type => 'null' },
255 code => sub {
256 my ($param) = @_;
257
258 my $id = extract_param($param, 'subnet');
259
260 PVE::Network::SDN::lock_sdn_config(
261 sub {
c33dd818
AD
262 my $cfg = PVE::Network::SDN::Subnets::config();
263
264 my $scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id);
265
58a7773a
AD
266 my $subnets_cfg = PVE::Network::SDN::Subnets::config();
267 my $vnets_cfg = PVE::Network::SDN::Vnets::config();
c33dd818 268
58a7773a 269 PVE::Network::SDN::SubnetPlugin->on_delete_hook($id, $subnets_cfg, $vnets_cfg);
70b03506
AD
270
271 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
272 my $ipam = $cfg->{ids}->{$id}->{ipam};
273 if ($ipam) {
274 raise_param_exc({ ipam => "$ipam not existing"}) if !$ipam_cfg->{ids}->{$ipam};
275 my $plugin_config = $ipam_cfg->{ids}->{$ipam};
276 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
277 $plugin->del_subnet($plugin_config, $id, $scfg);
278 }
279
280 delete $cfg->{ids}->{$id};
281
c33dd818 282 PVE::Network::SDN::Subnets::write_config($cfg);
c33dd818
AD
283
284 }, "delete sdn subnet object failed");
285
286
287 return undef;
288 }});
289
2901;