]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/Network.pm
create api: test if $scfg vnet exist
[pve-network.git] / PVE / API2 / Network / Network.pm
CommitLineData
92b6f291
AD
1package PVE::API2::Network::Network;
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);
9use PVE::Network::Network;
10use PVE::Network::Network::Plugin;
11use PVE::Network::Network::VlanPlugin;
12use PVE::Network::Network::VxlanMulticastPlugin;
13use PVE::Network::Network::VnetPlugin;
14use Storable qw(dclone);
15use PVE::JSONSchema qw(get_standard_option);
16use PVE::RPCEnvironment;
17
18use PVE::RESTHandler;
19
20use base qw(PVE::RESTHandler);
21
22my $network_type_enum = PVE::Network::Network::Plugin->lookup_types();
23
24my $api_network_config = sub {
25 my ($cfg, $networkid) = @_;
26
27 my $scfg = dclone(PVE::Network::Network::network_config($cfg, $networkid));
28 $scfg->{network} = $networkid;
29 $scfg->{digest} = $cfg->{digest};
30
31 return $scfg;
32};
33
34__PACKAGE__->register_method ({
35 name => 'index',
36 path => '',
37 method => 'GET',
38 description => "Network index.",
39 permissions => {
40 description => "Only list entries where you have 'Network.Audit' or 'Network.Allocate' permissions on '/cluster/network/<network>'",
41 user => 'all',
42 },
43 parameters => {
44 additionalProperties => 0,
45 properties => {
46 type => {
47 description => "Only list network of specific type",
48 type => 'string',
49 enum => $network_type_enum,
50 optional => 1,
51 },
52 },
53 },
54 returns => {
55 type => 'array',
56 items => {
57 type => "object",
58 properties => { network => { type => 'string'} },
59 },
60 links => [ { rel => 'child', href => "{network}" } ],
61 },
62 code => sub {
63 my ($param) = @_;
64
65 my $rpcenv = PVE::RPCEnvironment::get();
66 my $authuser = $rpcenv->get_user();
67
68
69 my $cfg = PVE::Network::Network::config();
70
71 my @sids = PVE::Network::Network::networks_ids($cfg);
72 my $res = [];
73 foreach my $networkid (@sids) {
74# my $privs = [ 'Network.Audit', 'Network.Allocate' ];
75# next if !$rpcenv->check_any($authuser, "/cluster/network/$networkid", $privs, 1);
76
77 my $scfg = &$api_network_config($cfg, $networkid);
78 next if $param->{type} && $param->{type} ne $scfg->{type};
79 push @$res, $scfg;
80 }
81
82 return $res;
83 }});
84
85__PACKAGE__->register_method ({
86 name => 'read',
87 path => '{network}',
88 method => 'GET',
89 description => "Read network configuration.",
90# permissions => {
91# check => ['perm', '/cluster/network/{network}', ['Network.Allocate']],
92# },
93
94 parameters => {
95 additionalProperties => 0,
96 properties => {
97 network => get_standard_option('pve-network-id'),
98 },
99 },
100 returns => { type => 'object' },
101 code => sub {
102 my ($param) = @_;
103
104 my $cfg = PVE::Network::Network::config();
105
106 return &$api_network_config($cfg, $param->{network});
107 }});
108
109__PACKAGE__->register_method ({
110 name => 'create',
111 protected => 1,
112 path => '',
113 method => 'POST',
114 description => "Create a new network object.",
115# permissions => {
116# check => ['perm', '/cluster/network', ['Network.Allocate']],
117# },
118 parameters => PVE::Network::Network::Plugin->createSchema(),
119 returns => { type => 'null' },
120 code => sub {
121 my ($param) = @_;
122
123 my $type = extract_param($param, 'type');
124 my $networkid = extract_param($param, 'network');
125
126 my $plugin = PVE::Network::Network::Plugin->lookup($type);
127 my $opts = $plugin->check_config($networkid, $param, 1, 1);
128
129 PVE::Network::Network::lock_network_config(
130 sub {
131
132 my $cfg = PVE::Network::Network::config();
133
aa38ba46
AD
134 my $scfg = undef;
135 if ($scfg = PVE::Network::Network::network_config($cfg, $networkid, 1)) {
92b6f291
AD
136 die "network object ID '$networkid' already defined\n";
137 }
138
139 $cfg->{ids}->{$networkid} = $opts;
6a2db30a 140 $plugin->on_update_hook($networkid, $cfg);
b487260d 141 #also verify transport associated to vnet
e57e08d3 142 if($scfg && $scfg->{type} eq 'vnet') {
b487260d
AD
143 my $transportid = $scfg->{transportzone};
144 die "missing transportzone" if !$transportid;
145 my $transport_cfg = $cfg->{ids}->{$transportid};
146 my $transport_plugin = PVE::Network::Network::Plugin->lookup($transport_cfg->{type});
147 $transport_plugin->on_update_hook($transportid, $cfg);
148 }
92b6f291
AD
149
150 PVE::Network::Network::write_config($cfg);
151
152 }, "create network object failed");
153
154 return undef;
155 }});
156
80c0e9c4
AD
157__PACKAGE__->register_method ({
158 name => 'apply_configuration',
159 protected => 1,
160 path => '',
161 method => 'PUT',
162 description => "Apply network changes.",
163# permissions => {
164# check => ['perm', '/cluster/network', ['Network.Allocate']],
165# },
166 returns => { type => 'null' },
167 code => sub {
168 my ($param) = @_;
169
170 die "no network changes to apply" if !-e "/etc/pve/networks.cfg.new";
171 rename("/etc/pve/networks.cfg.new", "/etc/pve/networks.cfg")
172 || die "applying networks.cfg changes failed - $!\n";
173
174
175 return undef;
176 }});
177
178__PACKAGE__->register_method ({
179 name => 'revert_configuration',
180 protected => 1,
181 path => '',
182 method => 'DELETE',
183 description => "Revert network changes.",
184# permissions => {
185# check => ['perm', '/cluster/network', ['Network.Allocate']],
186# },
187 returns => { type => 'null' },
188 code => sub {
189 my ($param) = @_;
190
191 die "no network changes to revert" if !-e "/etc/pve/networks.cfg.new";
192 unlink "/etc/pve/networks.cfg.new";
193
194 return undef;
195 }});
196
92b6f291
AD
197__PACKAGE__->register_method ({
198 name => 'update',
199 protected => 1,
200 path => '{network}',
201 method => 'PUT',
202 description => "Update network object configuration.",
203# permissions => {
204# check => ['perm', '/cluster/network', ['Network.Allocate']],
205# },
206 parameters => PVE::Network::Network::Plugin->updateSchema(),
207 returns => { type => 'null' },
208 code => sub {
209 my ($param) = @_;
210
211 my $networkid = extract_param($param, 'network');
212 my $digest = extract_param($param, 'digest');
213
214 PVE::Network::Network::lock_network_config(
215 sub {
216
217 my $cfg = PVE::Network::Network::config();
218
219 PVE::SectionConfig::assert_if_modified($cfg, $digest);
220
221 my $scfg = PVE::Network::Network::network_config($cfg, $networkid);
222
223 my $plugin = PVE::Network::Network::Plugin->lookup($scfg->{type});
224 my $opts = $plugin->check_config($networkid, $param, 0, 1);
225
226 foreach my $k (%$opts) {
227 $scfg->{$k} = $opts->{$k};
228 }
e8d5906e 229
6a2db30a 230 $plugin->on_update_hook($networkid, $cfg);
b487260d
AD
231 #also verify transport associated to vnet
232 if($scfg->{type} eq 'vnet') {
233 my $transportid = $scfg->{transportzone};
234 die "missing transportzone" if !$transportid;
235 my $transport_cfg = $cfg->{ids}->{$transportid};
236 my $transport_plugin = PVE::Network::Network::Plugin->lookup($transport_cfg->{type});
237 $transport_plugin->on_update_hook($transportid, $cfg);
238 }
92b6f291
AD
239 PVE::Network::Network::write_config($cfg);
240
241 }, "update network object failed");
242
243 return undef;
244 }});
245
246__PACKAGE__->register_method ({
247 name => 'delete',
248 protected => 1,
249 path => '{network}', # /cluster/network/{network}
250 method => 'DELETE',
251 description => "Delete network object configuration.",
252# permissions => {
253# check => ['perm', '/cluster/network', ['Network.Allocate']],
254# },
255 parameters => {
256 additionalProperties => 0,
257 properties => {
258 network => get_standard_option('pve-network-id', {
259 completion => \&PVE::Network::Network::complete_network,
260 }),
261 },
262 },
263 returns => { type => 'null' },
264 code => sub {
265 my ($param) = @_;
266
267 my $networkid = extract_param($param, 'network');
268
269 PVE::Network::Network::lock_network_config(
270 sub {
271
272 my $cfg = PVE::Network::Network::config();
273
274 my $scfg = PVE::Network::Network::network_config($cfg, $networkid);
275
fe0c6b9e 276 my $plugin = PVE::Network::Network::Plugin->lookup($scfg->{type});
a8ad2789 277 $plugin->on_delete_hook($networkid, $cfg);
92b6f291
AD
278
279 delete $cfg->{ids}->{$networkid};
92b6f291
AD
280 PVE::Network::Network::write_config($cfg);
281
282 }, "delete network object failed");
283
284
285 return undef;
286 }});
287
2881;