]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/Network.pm
vlan, vxlan plugin : add on_delete_hook
[pve-network.git] / PVE / API2 / Network / Network.pm
1 package PVE::API2::Network::Network;
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);
9 use PVE::Network::Network;
10 use PVE::Network::Network::Plugin;
11 use PVE::Network::Network::VlanPlugin;
12 use PVE::Network::Network::VxlanMulticastPlugin;
13 use PVE::Network::Network::VnetPlugin;
14 use Storable qw(dclone);
15 use PVE::JSONSchema qw(get_standard_option);
16 use PVE::RPCEnvironment;
17
18 use PVE::RESTHandler;
19
20 use base qw(PVE::RESTHandler);
21
22 my $network_type_enum = PVE::Network::Network::Plugin->lookup_types();
23
24 my $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
134 my $scfg = undef;
135 if ($scfg = PVE::Network::Network::network_config($cfg, $networkid, 1)) {
136 die "network object ID '$networkid' already defined\n";
137 }
138
139 $cfg->{ids}->{$networkid} = $opts;
140 $plugin->on_update_hook($networkid, $cfg);
141 #also verify transport associated to vnet
142 if($scfg->{type} eq 'vnet') {
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 }
149
150 PVE::Network::Network::write_config($cfg);
151
152 }, "create network object failed");
153
154 return undef;
155 }});
156
157 __PACKAGE__->register_method ({
158 name => 'update',
159 protected => 1,
160 path => '{network}',
161 method => 'PUT',
162 description => "Update network object configuration.",
163 # permissions => {
164 # check => ['perm', '/cluster/network', ['Network.Allocate']],
165 # },
166 parameters => PVE::Network::Network::Plugin->updateSchema(),
167 returns => { type => 'null' },
168 code => sub {
169 my ($param) = @_;
170
171 my $networkid = extract_param($param, 'network');
172 my $digest = extract_param($param, 'digest');
173
174 PVE::Network::Network::lock_network_config(
175 sub {
176
177 my $cfg = PVE::Network::Network::config();
178
179 PVE::SectionConfig::assert_if_modified($cfg, $digest);
180
181 my $scfg = PVE::Network::Network::network_config($cfg, $networkid);
182
183 my $plugin = PVE::Network::Network::Plugin->lookup($scfg->{type});
184 my $opts = $plugin->check_config($networkid, $param, 0, 1);
185
186 foreach my $k (%$opts) {
187 $scfg->{$k} = $opts->{$k};
188 }
189
190 $plugin->on_update_hook($networkid, $cfg);
191 #also verify transport associated to vnet
192 if($scfg->{type} eq 'vnet') {
193 my $transportid = $scfg->{transportzone};
194 die "missing transportzone" if !$transportid;
195 my $transport_cfg = $cfg->{ids}->{$transportid};
196 my $transport_plugin = PVE::Network::Network::Plugin->lookup($transport_cfg->{type});
197 $transport_plugin->on_update_hook($transportid, $cfg);
198 }
199 PVE::Network::Network::write_config($cfg);
200
201 }, "update network object failed");
202
203 return undef;
204 }});
205
206 __PACKAGE__->register_method ({
207 name => 'delete',
208 protected => 1,
209 path => '{network}', # /cluster/network/{network}
210 method => 'DELETE',
211 description => "Delete network object configuration.",
212 # permissions => {
213 # check => ['perm', '/cluster/network', ['Network.Allocate']],
214 # },
215 parameters => {
216 additionalProperties => 0,
217 properties => {
218 network => get_standard_option('pve-network-id', {
219 completion => \&PVE::Network::Network::complete_network,
220 }),
221 },
222 },
223 returns => { type => 'null' },
224 code => sub {
225 my ($param) = @_;
226
227 my $networkid = extract_param($param, 'network');
228
229 PVE::Network::Network::lock_network_config(
230 sub {
231
232 my $cfg = PVE::Network::Network::config();
233
234 my $scfg = PVE::Network::Network::network_config($cfg, $networkid);
235
236 my $plugin = PVE::Network::Network::Plugin->lookup($scfg->{type});
237 $plugin->on_delete_hook($networkid, $cfg);
238
239 delete $cfg->{ids}->{$networkid};
240 PVE::Network::Network::write_config($cfg);
241
242 }, "delete network object failed");
243
244
245 return undef;
246 }});
247
248 1;