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