]>
Commit | Line | Data |
---|---|---|
e7939454 AD |
1 | package PVE::API2::Network::Vnet; |
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::Vnet; | |
10 | use PVE::Network::Vnet::Plugin; | |
11 | use Storable qw(dclone); | |
12 | use PVE::JSONSchema qw(get_standard_option); | |
13 | use PVE::RPCEnvironment; | |
14 | ||
15 | use PVE::RESTHandler; | |
16 | ||
17 | use base qw(PVE::RESTHandler); | |
18 | ||
19 | my $api_vnet_config = sub { | |
20 | my ($cfg, $vnetid) = @_; | |
21 | ||
22 | my $scfg = dclone(PVE::Network::Vnet::vnet_config($cfg, $vnetid)); | |
23 | $scfg->{vnet} = $vnetid; | |
24 | $scfg->{digest} = $cfg->{digest}; | |
25 | ||
26 | return $scfg; | |
27 | }; | |
28 | ||
29 | __PACKAGE__->register_method ({ | |
30 | name => 'index', | |
31 | path => '', | |
32 | method => 'GET', | |
33 | description => "Vnet index.", | |
34 | permissions => { | |
35 | description => "Only list entries where you have 'NetworkVnet.Audit' or 'NetworkVnet.Allocate' permissions on '/cluster/network/vnet/<vnet>'", | |
36 | user => 'all', | |
37 | }, | |
38 | parameters => { | |
39 | additionalProperties => 0, | |
40 | properties => { | |
41 | type => { | |
42 | description => "Only list vnet of specific type", | |
43 | type => 'string', | |
44 | optional => 1, | |
45 | }, | |
46 | }, | |
47 | }, | |
48 | returns => { | |
49 | type => 'array', | |
50 | items => { | |
51 | type => "object", | |
52 | properties => { vnet => { type => 'string'} }, | |
53 | }, | |
54 | links => [ { rel => 'child', href => "{vnet}" } ], | |
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::Vnet::config(); | |
64 | ||
65 | my @sids = PVE::Network::Vnet::vnets_ids($cfg); | |
66 | my $res = []; | |
67 | foreach my $vnetid (@sids) { | |
68 | # my $privs = [ 'NetworkVnet.Audit', 'NetworkVnet.Allocate' ]; | |
69 | # next if !$rpcenv->check_any($authuser, "/cluster/network/vnet/$vnetid", $privs, 1); | |
70 | ||
71 | my $scfg = &$api_vnet_config($cfg, $vnetid); | |
72 | next if $param->{type} && $param->{type} ne $scfg->{type}; | |
73 | push @$res, $scfg; | |
74 | } | |
75 | ||
76 | return $res; | |
77 | }}); | |
78 | ||
79 | __PACKAGE__->register_method ({ | |
80 | name => 'read', | |
81 | path => '{vnet}', | |
82 | method => 'GET', | |
83 | description => "Read vnet configuration.", | |
84 | # permissions => { | |
85 | # check => ['perm', '/cluster/network/vnet/{vnet}', ['NetworkVnet.Allocate']], | |
86 | # }, | |
87 | ||
88 | parameters => { | |
89 | additionalProperties => 0, | |
90 | properties => { | |
91 | vnet => get_standard_option('pve-vnet-id'), | |
92 | }, | |
93 | }, | |
94 | returns => { type => 'object' }, | |
95 | code => sub { | |
96 | my ($param) = @_; | |
97 | ||
98 | my $cfg = PVE::Network::Vnet::config(); | |
99 | ||
100 | return &$api_vnet_config($cfg, $param->{vnet}); | |
101 | }}); | |
102 | ||
103 | __PACKAGE__->register_method ({ | |
104 | name => 'create', | |
105 | protected => 1, | |
106 | path => '', | |
107 | method => 'POST', | |
108 | description => "Create a new network vnet.", | |
109 | # permissions => { | |
110 | # check => ['perm', '/cluster/network/vnet', ['NetworkVnet.Allocate']], | |
111 | # }, | |
112 | parameters => PVE::Network::Vnet::Plugin->createSchema(), | |
113 | returns => { type => 'null' }, | |
114 | code => sub { | |
115 | my ($param) = @_; | |
116 | ||
117 | my $vnetid = extract_param($param, 'vnet'); | |
118 | my $type = "vnet"; | |
119 | my $plugin = PVE::Network::Vnet::Plugin->lookup($type); | |
120 | my $opts = $plugin->check_config($vnetid, $param, 1, 1); | |
121 | ||
122 | PVE::Network::Vnet::lock_vnet_config( | |
123 | sub { | |
124 | ||
125 | my $cfg = PVE::Network::Vnet::config(); | |
126 | ||
127 | if (my $scfg = PVE::Network::Vnet::vnet_config($cfg, $vnetid, 1)) { | |
128 | die "network vnet ID '$vnetid' already defined\n"; | |
129 | } | |
130 | ||
131 | $cfg->{ids}->{$vnetid} = $opts; | |
132 | ||
133 | PVE::Network::Vnet::write_config($cfg); | |
134 | ||
135 | }, "create network vnet failed"); | |
136 | ||
137 | return undef; | |
138 | }}); | |
139 | ||
140 | __PACKAGE__->register_method ({ | |
141 | name => 'update', | |
142 | protected => 1, | |
143 | path => '{vnet}', | |
144 | method => 'PUT', | |
145 | description => "Update network vnet configuration.", | |
146 | # permissions => { | |
147 | # check => ['perm', '/cluster/network/vnet', ['NetworkVnet.Allocate']], | |
148 | # }, | |
149 | parameters => PVE::Network::Vnet::Plugin->updateSchema(), | |
150 | returns => { type => 'null' }, | |
151 | code => sub { | |
152 | my ($param) = @_; | |
153 | ||
154 | my $vnetid = extract_param($param, 'vnet'); | |
155 | my $digest = extract_param($param, 'digest'); | |
156 | ||
157 | PVE::Network::Vnet::lock_vnet_config( | |
158 | sub { | |
159 | ||
160 | my $cfg = PVE::Network::Vnet::config(); | |
161 | ||
162 | PVE::SectionConfig::assert_if_modified($cfg, $digest); | |
163 | ||
164 | my $scfg = PVE::Network::Vnet::vnet_config($cfg, $vnetid); | |
165 | my $plugin = PVE::Network::Vnet::Plugin->lookup($scfg->{type}); | |
166 | my $opts = $plugin->check_config($vnetid, $param, 0, 1); | |
167 | ||
168 | foreach my $k (%$opts) { | |
169 | $scfg->{$k} = $opts->{$k}; | |
170 | } | |
171 | PVE::Network::Vnet::write_config($cfg); | |
172 | ||
173 | }, "update network vnet failed"); | |
174 | ||
175 | return undef; | |
176 | }}); | |
177 | ||
178 | __PACKAGE__->register_method ({ | |
179 | name => 'delete', | |
180 | protected => 1, | |
181 | path => '{vnet}', # /networkvnets/{vnet} | |
182 | method => 'DELETE', | |
183 | description => "Delete network vnet configuration.", | |
184 | # permissions => { | |
185 | # check => ['perm', '/networkvnets', ['NetworkVnet.Allocate']], | |
186 | # }, | |
187 | parameters => { | |
188 | additionalProperties => 0, | |
189 | properties => { | |
190 | vnet => get_standard_option('pve-vnet-id', { | |
191 | completion => \&PVE::Network::Vnet::complete_vnet, | |
192 | }), | |
193 | }, | |
194 | }, | |
195 | returns => { type => 'null' }, | |
196 | code => sub { | |
197 | my ($param) = @_; | |
198 | ||
199 | my $vnetid = extract_param($param, 'vnet'); | |
200 | ||
201 | PVE::Network::Vnet::lock_vnet_config( | |
202 | sub { | |
203 | ||
204 | my $cfg = PVE::Network::Vnet::config(); | |
205 | ||
206 | my $scfg = PVE::Network::Vnet::vnet_config($cfg, $vnetid); | |
207 | ||
208 | delete $cfg->{ids}->{$vnetid}; | |
209 | ||
210 | PVE::Network::Vnet::write_config($cfg); | |
211 | ||
212 | }, "delete network vnet failed"); | |
213 | ||
214 | ||
215 | return undef; | |
216 | }}); | |
217 | ||
218 | 1; |