]>
Commit | Line | Data |
---|---|---|
92b6f291 AD |
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 | ||
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; | |
e8d5906e | 140 | $plugin->on_update_hook($networkid, $scfg); |
92b6f291 AD |
141 | |
142 | PVE::Network::Network::write_config($cfg); | |
143 | ||
144 | }, "create network object failed"); | |
145 | ||
146 | return undef; | |
147 | }}); | |
148 | ||
149 | __PACKAGE__->register_method ({ | |
150 | name => 'update', | |
151 | protected => 1, | |
152 | path => '{network}', | |
153 | method => 'PUT', | |
154 | description => "Update network object configuration.", | |
155 | # permissions => { | |
156 | # check => ['perm', '/cluster/network', ['Network.Allocate']], | |
157 | # }, | |
158 | parameters => PVE::Network::Network::Plugin->updateSchema(), | |
159 | returns => { type => 'null' }, | |
160 | code => sub { | |
161 | my ($param) = @_; | |
162 | ||
163 | my $networkid = extract_param($param, 'network'); | |
164 | my $digest = extract_param($param, 'digest'); | |
165 | ||
166 | PVE::Network::Network::lock_network_config( | |
167 | sub { | |
168 | ||
169 | my $cfg = PVE::Network::Network::config(); | |
170 | ||
171 | PVE::SectionConfig::assert_if_modified($cfg, $digest); | |
172 | ||
173 | my $scfg = PVE::Network::Network::network_config($cfg, $networkid); | |
174 | ||
175 | my $plugin = PVE::Network::Network::Plugin->lookup($scfg->{type}); | |
176 | my $opts = $plugin->check_config($networkid, $param, 0, 1); | |
177 | ||
178 | foreach my $k (%$opts) { | |
179 | $scfg->{$k} = $opts->{$k}; | |
180 | } | |
e8d5906e AD |
181 | |
182 | $plugin->on_update_hook($networkid, $scfg); | |
183 | ||
92b6f291 AD |
184 | PVE::Network::Network::write_config($cfg); |
185 | ||
186 | }, "update network object failed"); | |
187 | ||
188 | return undef; | |
189 | }}); | |
190 | ||
191 | __PACKAGE__->register_method ({ | |
192 | name => 'delete', | |
193 | protected => 1, | |
194 | path => '{network}', # /cluster/network/{network} | |
195 | method => 'DELETE', | |
196 | description => "Delete network object configuration.", | |
197 | # permissions => { | |
198 | # check => ['perm', '/cluster/network', ['Network.Allocate']], | |
199 | # }, | |
200 | parameters => { | |
201 | additionalProperties => 0, | |
202 | properties => { | |
203 | network => get_standard_option('pve-network-id', { | |
204 | completion => \&PVE::Network::Network::complete_network, | |
205 | }), | |
206 | }, | |
207 | }, | |
208 | returns => { type => 'null' }, | |
209 | code => sub { | |
210 | my ($param) = @_; | |
211 | ||
212 | my $networkid = extract_param($param, 'network'); | |
213 | ||
214 | PVE::Network::Network::lock_network_config( | |
215 | sub { | |
216 | ||
217 | my $cfg = PVE::Network::Network::config(); | |
218 | ||
219 | my $scfg = PVE::Network::Network::network_config($cfg, $networkid); | |
220 | ||
fe0c6b9e AD |
221 | my $plugin = PVE::Network::Network::Plugin->lookup($scfg->{type}); |
222 | $plugin->on_delete_hook($networkid, $scfg); | |
92b6f291 AD |
223 | |
224 | delete $cfg->{ids}->{$networkid}; | |
92b6f291 AD |
225 | PVE::Network::Network::write_config($cfg); |
226 | ||
227 | }, "delete network object failed"); | |
228 | ||
229 | ||
230 | return undef; | |
231 | }}); | |
232 | ||
233 | 1; |