]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Vnets.pm
subnets: move api to /sdn/vnet/<vnet>/subnets && make vnet option not optionnal
[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;
20
21use PVE::RESTHandler;
22
23use base qw(PVE::RESTHandler);
24
3926d9a7
AD
25__PACKAGE__->register_method ({
26 subclass => "PVE::API2::Network::SDN::Subnets",
27 path => '{vnet}/subnets',
28});
29
4140be9e
AD
30my $api_sdn_vnets_config = sub {
31 my ($cfg, $id) = @_;
32
33 my $scfg = dclone(PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $id));
34 $scfg->{vnet} = $id;
35 $scfg->{digest} = $cfg->{digest};
36
37 return $scfg;
38};
39
40__PACKAGE__->register_method ({
41 name => 'index',
42 path => '',
43 method => 'GET',
44 description => "SDN vnets index.",
45 permissions => {
2bd2787a
TL
46 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate'"
47 ." permissions on '/sdn/vnets/<vnet>'",
4140be9e
AD
48 user => 'all',
49 },
50 parameters => {
2bd2787a 51 additionalProperties => 0,
4140be9e
AD
52 },
53 returns => {
54 type => 'array',
55 items => {
56 type => "object",
57 properties => {},
58 },
59 links => [ { rel => 'child', href => "{vnet}" } ],
60 },
61 code => sub {
62 my ($param) = @_;
63
64 my $rpcenv = PVE::RPCEnvironment::get();
65 my $authuser = $rpcenv->get_user();
66
4140be9e
AD
67 my $cfg = PVE::Network::SDN::Vnets::config();
68
69 my @sids = PVE::Network::SDN::Vnets::sdn_vnets_ids($cfg);
70 my $res = [];
71 foreach my $id (@sids) {
3551b612
AD
72 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
73 next if !$rpcenv->check_any($authuser, "/sdn/vnets/$id", $privs, 1);
4140be9e
AD
74
75 my $scfg = &$api_sdn_vnets_config($cfg, $id);
76 push @$res, $scfg;
77 }
78
79 return $res;
80 }});
81
82__PACKAGE__->register_method ({
83 name => 'read',
84 path => '{vnet}',
85 method => 'GET',
86 description => "Read sdn vnet configuration.",
3551b612
AD
87 permissions => {
88 check => ['perm', '/sdn/vnets/{vnet}', ['SDN.Allocate']],
89 },
4140be9e 90 parameters => {
2bd2787a
TL
91 additionalProperties => 0,
92 properties => {
93 vnet => get_standard_option('pve-sdn-vnet-id', {
94 completion => \&PVE::Network::SDN::Vnets::complete_sdn_vnets,
95 }),
96 },
4140be9e
AD
97 },
98 returns => { type => 'object' },
99 code => sub {
100 my ($param) = @_;
101
102 my $cfg = PVE::Network::SDN::Vnets::config();
103
2bd2787a 104 return $api_sdn_vnets_config->($cfg, $param->{vnet});
4140be9e
AD
105 }});
106
107__PACKAGE__->register_method ({
108 name => 'create',
109 protected => 1,
110 path => '',
111 method => 'POST',
112 description => "Create a new sdn vnet object.",
3551b612
AD
113 permissions => {
114 check => ['perm', '/sdn/vnets', ['SDN.Allocate']],
115 },
4140be9e
AD
116 parameters => PVE::Network::SDN::VnetPlugin->createSchema(),
117 returns => { type => 'null' },
118 code => sub {
119 my ($param) = @_;
120
121 my $type = extract_param($param, 'type');
122 my $id = extract_param($param, 'vnet');
123
2bd2787a
TL
124 PVE::Cluster::check_cfs_quorum();
125 mkdir("/etc/pve/sdn");
4140be9e 126
2bd2787a
TL
127 PVE::Network::SDN::lock_sdn_config(sub {
128 my $cfg = PVE::Network::SDN::Vnets::config();
129 my $opts = PVE::Network::SDN::VnetPlugin->check_config($id, $param, 1, 1);
1d44ce70 130
2bd2787a
TL
131 if (PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $id, 1)) {
132 die "sdn vnet object ID '$id' already defined\n";
133 }
134 $cfg->{ids}->{$id} = $opts;
4140be9e 135
2bd2787a
TL
136 my $zone_cfg = PVE::Network::SDN::Zones::config();
137 my $zoneid = $cfg->{ids}->{$id}->{zone};
138 my $plugin_config = $zone_cfg->{ids}->{$zoneid};
139 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
5ca07ed9 140 $plugin->vnet_update_hook($cfg->{ids}->{$id});
4140be9e 141
58a7773a
AD
142 my $subnet_cfg = PVE::Network::SDN::Subnets::config();
143
144 PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg, $subnet_cfg);
f9bc9640 145
2bd2787a
TL
146 PVE::Network::SDN::Vnets::write_config($cfg);
147 PVE::Network::SDN::increase_version();
f9bc9640 148
2bd2787a 149 }, "create sdn vnet object failed");
4140be9e
AD
150
151 return undef;
152 }});
153
154__PACKAGE__->register_method ({
155 name => 'update',
156 protected => 1,
157 path => '{vnet}',
158 method => 'PUT',
159 description => "Update sdn vnet object configuration.",
3551b612
AD
160 permissions => {
161 check => ['perm', '/sdn/vnets', ['SDN.Allocate']],
162 },
4140be9e
AD
163 parameters => PVE::Network::SDN::VnetPlugin->updateSchema(),
164 returns => { type => 'null' },
165 code => sub {
166 my ($param) = @_;
167
168 my $id = extract_param($param, 'vnet');
169 my $digest = extract_param($param, 'digest');
170
2bd2787a 171 PVE::Network::SDN::lock_sdn_config(sub {
4140be9e
AD
172 my $cfg = PVE::Network::SDN::Vnets::config();
173
174 PVE::SectionConfig::assert_if_modified($cfg, $digest);
175
4140be9e 176 my $opts = PVE::Network::SDN::VnetPlugin->check_config($id, $param, 0, 1);
3834801f 177 $cfg->{ids}->{$id} = $opts;
4140be9e 178
1d44ce70
AD
179 my $zone_cfg = PVE::Network::SDN::Zones::config();
180 my $zoneid = $cfg->{ids}->{$id}->{zone};
2bd2787a
TL
181 my $plugin_config = $zone_cfg->{ids}->{$zoneid};
182 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
5ca07ed9 183 $plugin->vnet_update_hook($cfg->{ids}->{$id});
2bd2787a 184
58a7773a
AD
185 my $subnet_cfg = PVE::Network::SDN::Subnets::config();
186
187 PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg, $subnet_cfg);
4140be9e
AD
188
189 PVE::Network::SDN::Vnets::write_config($cfg);
f9bc9640
AD
190 PVE::Network::SDN::increase_version();
191
2bd2787a 192 }, "update sdn vnet object failed");
4140be9e
AD
193
194 return undef;
2bd2787a
TL
195 }
196});
4140be9e
AD
197
198__PACKAGE__->register_method ({
199 name => 'delete',
200 protected => 1,
201 path => '{vnet}',
202 method => 'DELETE',
203 description => "Delete sdn vnet object configuration.",
3551b612
AD
204 permissions => {
205 check => ['perm', '/sdn/vnets', ['SDN.Allocate']],
206 },
4140be9e 207 parameters => {
2bd2787a 208 additionalProperties => 0,
4140be9e
AD
209 properties => {
210 vnet => get_standard_option('pve-sdn-vnet-id', {
2bd2787a
TL
211 completion => \&PVE::Network::SDN::Vnets::complete_sdn_vnets,
212 }),
4140be9e
AD
213 },
214 },
215 returns => { type => 'null' },
216 code => sub {
217 my ($param) = @_;
218
219 my $id = extract_param($param, 'vnet');
220
2bd2787a
TL
221 PVE::Network::SDN::lock_sdn_config(sub {
222 my $cfg = PVE::Network::SDN::Vnets::config();
223 my $scfg = PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $id); # check if exists
224 my $vnet_cfg = PVE::Network::SDN::Vnets::config();
4140be9e 225
2bd2787a 226 PVE::Network::SDN::VnetPlugin->on_delete_hook($id, $vnet_cfg);
4140be9e 227
2bd2787a
TL
228 delete $cfg->{ids}->{$id};
229 PVE::Network::SDN::Vnets::write_config($cfg);
230 PVE::Network::SDN::increase_version();
f9bc9640 231
2bd2787a 232 }, "delete sdn vnet object failed");
4140be9e
AD
233
234
235 return undef;
2bd2787a
TL
236 }
237});
4140be9e
AD
238
2391;