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