]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Subnets.pm
subnets: move api to /sdn/vnet/<vnet>/subnets && make vnet option not optionnal
[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'),
49 },
c33dd818
AD
50 },
51 returns => {
52 type => 'array',
53 items => {
54 type => "object",
55 properties => {},
56 },
57 links => [ { rel => 'child', href => "{subnet}" } ],
58 },
59 code => sub {
60 my ($param) = @_;
61
62 my $rpcenv = PVE::RPCEnvironment::get();
63 my $authuser = $rpcenv->get_user();
64
3926d9a7 65 my $vnetid = $param->{vnet};
c33dd818
AD
66
67 my $cfg = PVE::Network::SDN::Subnets::config();
68
69 my @sids = PVE::Network::SDN::Subnets::sdn_subnets_ids($cfg);
70 my $res = [];
71 foreach my $id (@sids) {
72 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
3926d9a7 73 next if !$rpcenv->check_any($authuser, "/sdn/vnets/$vnetid/subnets/$id", $privs, 1);
c33dd818
AD
74
75 my $scfg = &$api_sdn_subnets_config($cfg, $id);
3926d9a7 76 next if !$scfg->{vnet} || $scfg->{vnet} ne $vnetid;
c33dd818
AD
77 push @$res, $scfg;
78 }
79
80 return $res;
81 }});
82
83__PACKAGE__->register_method ({
84 name => 'read',
85 path => '{subnet}',
86 method => 'GET',
87 description => "Read sdn subnet configuration.",
88 permissions => {
3926d9a7 89 check => ['perm', '/sdn/vnets/{vnet}/subnets/{subnet}', ['SDN.Allocate']],
c33dd818
AD
90 },
91
92 parameters => {
93 additionalProperties => 0,
94 properties => {
3926d9a7 95 vnet => get_standard_option('pve-sdn-vnet-id'),
c33dd818
AD
96 subnet => get_standard_option('pve-sdn-subnet-id', {
97 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
98 }),
99 },
100 },
101 returns => { type => 'object' },
102 code => sub {
103 my ($param) = @_;
104
105 my $cfg = PVE::Network::SDN::Subnets::config();
3926d9a7
AD
106 my $scfg = &$api_sdn_subnets_config($cfg, $param->{subnet});
107
108 raise_param_exc({ vnet => "wrong vnet"}) if $param->{vnet} ne $scfg->{vnet};
c33dd818 109
3926d9a7 110 return $scfg;
c33dd818
AD
111 }});
112
113__PACKAGE__->register_method ({
114 name => 'create',
115 protected => 1,
116 path => '',
117 method => 'POST',
118 description => "Create a new sdn subnet object.",
119 permissions => {
3926d9a7 120 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
121 },
122 parameters => PVE::Network::SDN::SubnetPlugin->createSchema(),
123 returns => { type => 'null' },
124 code => sub {
125 my ($param) = @_;
126
127 my $type = extract_param($param, 'type');
128 my $cidr = extract_param($param, 'subnet');
129 my $id = $cidr =~ s/\//-/r;
130
131 # create /etc/pve/sdn directory
132 PVE::Cluster::check_cfs_quorum();
133 mkdir("/etc/pve/sdn") if ! -d '/etc/pve/sdn';
134
135 PVE::Network::SDN::lock_sdn_config(
136 sub {
137
138 my $cfg = PVE::Network::SDN::Subnets::config();
139 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 1, 1);
140
141 my $scfg = undef;
142 if ($scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id, 1)) {
143 die "sdn subnet object ID '$id' already defined\n";
144 }
145
146 $cfg->{ids}->{$id} = $opts;
147 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $cfg);
70b03506
AD
148
149 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
150 my $ipam = $cfg->{ids}->{$id}->{ipam};
151 if ($ipam) {
152 raise_param_exc({ ipam => "$ipam not existing"}) if !$ipam_cfg->{ids}->{$ipam};
153 my $plugin_config = $ipam_cfg->{ids}->{$ipam};
154 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
155 $plugin->add_subnet($plugin_config, $id, $cfg->{ids}->{$id});
156 $plugin->add_ip($plugin_config, $id, $opts->{gateway}, 1) if $opts->{gateway};
157 }
158
c33dd818
AD
159 PVE::Network::SDN::Subnets::write_config($cfg);
160 PVE::Network::SDN::increase_version();
161
162 }, "create sdn subnet object failed");
163
164 return undef;
165 }});
166
167__PACKAGE__->register_method ({
168 name => 'update',
169 protected => 1,
170 path => '{subnet}',
171 method => 'PUT',
172 description => "Update sdn subnet object configuration.",
173 permissions => {
3926d9a7 174 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
175 },
176 parameters => PVE::Network::SDN::SubnetPlugin->updateSchema(),
177 returns => { type => 'null' },
178 code => sub {
179 my ($param) = @_;
180
181 my $id = extract_param($param, 'subnet');
182 my $digest = extract_param($param, 'digest');
183
184 PVE::Network::SDN::lock_sdn_config(
185 sub {
186
187 my $cfg = PVE::Network::SDN::Subnets::config();
70b03506 188 my $scfg = &$api_sdn_subnets_config($cfg, $id);
c33dd818
AD
189
190 PVE::SectionConfig::assert_if_modified($cfg, $digest);
191
192 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 0, 1);
193 $cfg->{ids}->{$id} = $opts;
194
195 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $cfg);
70b03506
AD
196
197 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
198 my $ipam = $cfg->{ids}->{$id}->{ipam};
199 if ($ipam) {
200 raise_param_exc({ ipam => "$ipam not existing"}) if !$ipam_cfg->{ids}->{$ipam};
201 my $plugin_config = $ipam_cfg->{ids}->{$ipam};
202 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
203 $plugin->add_subnet($plugin_config, $id, $cfg->{ids}->{$id});
204
205 if($opts->{gateway} && $scfg->{gateway} && $opts->{gateway} ne $scfg->{gateway}) {
206 $plugin->del_ip($plugin_config, $id, $scfg->{gateway});
207 }
208 if (!defined($opts->{gateway}) && $scfg->{gateway}) {
209 $plugin->del_ip($plugin_config, $id, $scfg->{gateway});
210 }
211 $plugin->add_ip($plugin_config, $id, $opts->{gateway}, 1) if $opts->{gateway};
212 }
213
c33dd818
AD
214 PVE::Network::SDN::Subnets::write_config($cfg);
215 PVE::Network::SDN::increase_version();
216
217 }, "update sdn subnet object failed");
218
219 return undef;
220 }});
221
222__PACKAGE__->register_method ({
223 name => 'delete',
224 protected => 1,
225 path => '{subnet}',
226 method => 'DELETE',
227 description => "Delete sdn subnet object configuration.",
228 permissions => {
3926d9a7 229 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
230 },
231 parameters => {
232 additionalProperties => 0,
233 properties => {
3926d9a7 234 vnet => get_standard_option('pve-sdn-vnet-id'),
c33dd818
AD
235 subnet => get_standard_option('pve-sdn-subnet-id', {
236 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
237 }),
238 },
239 },
240 returns => { type => 'null' },
241 code => sub {
242 my ($param) = @_;
243
244 my $id = extract_param($param, 'subnet');
245
246 PVE::Network::SDN::lock_sdn_config(
247 sub {
c33dd818
AD
248 my $cfg = PVE::Network::SDN::Subnets::config();
249
250 my $scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id);
251
58a7773a
AD
252 my $subnets_cfg = PVE::Network::SDN::Subnets::config();
253 my $vnets_cfg = PVE::Network::SDN::Vnets::config();
c33dd818 254
58a7773a 255 PVE::Network::SDN::SubnetPlugin->on_delete_hook($id, $subnets_cfg, $vnets_cfg);
70b03506
AD
256
257 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
258 my $ipam = $cfg->{ids}->{$id}->{ipam};
259 if ($ipam) {
260 raise_param_exc({ ipam => "$ipam not existing"}) if !$ipam_cfg->{ids}->{$ipam};
261 my $plugin_config = $ipam_cfg->{ids}->{$ipam};
262 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
263 $plugin->del_subnet($plugin_config, $id, $scfg);
264 }
265
266 delete $cfg->{ids}->{$id};
267
c33dd818
AD
268 PVE::Network::SDN::Subnets::write_config($cfg);
269 PVE::Network::SDN::increase_version();
270
271 }, "delete sdn subnet object failed");
272
273
274 return undef;
275 }});
276
2771;