]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Subnets.pm
api: generate 'running-config' state instead of version increase on apply
[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;
e612faf6 147 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $opts);
70b03506 148
c33dd818 149 PVE::Network::SDN::Subnets::write_config($cfg);
c33dd818
AD
150
151 }, "create sdn subnet object failed");
152
153 return undef;
154 }});
155
156__PACKAGE__->register_method ({
157 name => 'update',
158 protected => 1,
159 path => '{subnet}',
160 method => 'PUT',
161 description => "Update sdn subnet object configuration.",
162 permissions => {
3926d9a7 163 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
164 },
165 parameters => PVE::Network::SDN::SubnetPlugin->updateSchema(),
166 returns => { type => 'null' },
167 code => sub {
168 my ($param) = @_;
169
170 my $id = extract_param($param, 'subnet');
171 my $digest = extract_param($param, 'digest');
172
173 PVE::Network::SDN::lock_sdn_config(
174 sub {
175
176 my $cfg = PVE::Network::SDN::Subnets::config();
70b03506 177 my $scfg = &$api_sdn_subnets_config($cfg, $id);
c33dd818
AD
178
179 PVE::SectionConfig::assert_if_modified($cfg, $digest);
180
181 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 0, 1);
182 $cfg->{ids}->{$id} = $opts;
183
9dfa9202
AD
184 raise_param_exc({ ipam => "you can't change ipam"}) if $opts->{ipam} && $scfg->{ipam} && $opts->{ipam} ne $scfg->{ipam};
185
e612faf6 186 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $opts, $scfg);
70b03506 187
c33dd818 188 PVE::Network::SDN::Subnets::write_config($cfg);
c33dd818
AD
189
190 }, "update sdn subnet object failed");
191
192 return undef;
193 }});
194
195__PACKAGE__->register_method ({
196 name => 'delete',
197 protected => 1,
198 path => '{subnet}',
199 method => 'DELETE',
200 description => "Delete sdn subnet object configuration.",
201 permissions => {
3926d9a7 202 check => ['perm', '/sdn/vnets/{vnet}/subnets', ['SDN.Allocate']],
c33dd818
AD
203 },
204 parameters => {
205 additionalProperties => 0,
206 properties => {
3926d9a7 207 vnet => get_standard_option('pve-sdn-vnet-id'),
c33dd818
AD
208 subnet => get_standard_option('pve-sdn-subnet-id', {
209 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
210 }),
211 },
212 },
213 returns => { type => 'null' },
214 code => sub {
215 my ($param) = @_;
216
217 my $id = extract_param($param, 'subnet');
218
219 PVE::Network::SDN::lock_sdn_config(
220 sub {
c33dd818
AD
221 my $cfg = PVE::Network::SDN::Subnets::config();
222
223 my $scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id);
224
58a7773a
AD
225 my $subnets_cfg = PVE::Network::SDN::Subnets::config();
226 my $vnets_cfg = PVE::Network::SDN::Vnets::config();
c33dd818 227
58a7773a 228 PVE::Network::SDN::SubnetPlugin->on_delete_hook($id, $subnets_cfg, $vnets_cfg);
70b03506
AD
229
230 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
231 my $ipam = $cfg->{ids}->{$id}->{ipam};
232 if ($ipam) {
233 raise_param_exc({ ipam => "$ipam not existing"}) if !$ipam_cfg->{ids}->{$ipam};
234 my $plugin_config = $ipam_cfg->{ids}->{$ipam};
235 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
236 $plugin->del_subnet($plugin_config, $id, $scfg);
237 }
238
239 delete $cfg->{ids}->{$id};
240
c33dd818 241 PVE::Network::SDN::Subnets::write_config($cfg);
c33dd818
AD
242
243 }, "delete sdn subnet object failed");
244
245
246 return undef;
247 }});
248
2491;