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