]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Pool.pm
update shipped appliance info index
[pve-manager.git] / PVE / API2 / Pool.pm
1 package PVE::API2::Pool;
2
3 use strict;
4 use warnings;
5 use PVE::Exception qw(raise_param_exc);
6 use PVE::INotify;
7 use PVE::Cluster qw (cfs_read_file cfs_write_file);
8 use PVE::AccessControl;
9 use PVE::Storage;
10
11 use PVE::SafeSyslog;
12
13 use Data::Dumper; # fixme: remove
14
15 use PVE::API2Tools;
16 use PVE::RESTHandler;
17
18 use base qw(PVE::RESTHandler);
19
20 __PACKAGE__->register_method ({
21 name => 'index',
22 path => '',
23 method => 'GET',
24 description => "Pool index.",
25 permissions => {
26 description => "List all pools where you have Pool.Allocate or VM.Allocate permissions on /pool/<pool>.",
27 user => 'all',
28 },
29 parameters => {
30 additionalProperties => 0,
31 properties => {},
32 },
33 returns => {
34 type => 'array',
35 items => {
36 type => "object",
37 properties => {
38 poolid => { type => 'string' },
39 },
40 },
41 links => [ { rel => 'child', href => "{poolid}" } ],
42 },
43 code => sub {
44 my ($param) = @_;
45
46 my $rpcenv = PVE::RPCEnvironment::get();
47 my $authuser = $rpcenv->get_user();
48
49 my $res = [];
50
51 my $usercfg = $rpcenv->{user_cfg};
52
53 foreach my $pool (keys %{$usercfg->{pools}}) {
54 next if !$rpcenv->check_any($authuser, "/pool/$pool", [ 'Pool.Allocate', 'VM.Allocate' ], 1);
55
56 my $entry = { poolid => $pool };
57 my $data = $usercfg->{pools}->{$pool};
58 $entry->{comment} = $data->{comment} if defined($data->{comment});
59 push @$res, $entry;
60 }
61
62 return $res;
63 }});
64
65 __PACKAGE__->register_method ({
66 name => 'create_pool',
67 protected => 1,
68 path => '',
69 method => 'POST',
70 permissions => {
71 check => ['perm', '/pool/{poolid}', ['Pool.Allocate']],
72 },
73 description => "Create new pool.",
74 parameters => {
75 additionalProperties => 0,
76 properties => {
77 poolid => { type => 'string', format => 'pve-poolid' },
78 comment => { type => 'string', optional => 1 },
79 },
80 },
81 returns => { type => 'null' },
82 code => sub {
83 my ($param) = @_;
84
85 PVE::AccessControl::lock_user_config(
86 sub {
87
88 my $usercfg = cfs_read_file("user.cfg");
89
90 my $pool = $param->{poolid};
91
92 die "pool '$pool' already exists\n"
93 if $usercfg->{pools}->{$pool};
94
95 $usercfg->{pools}->{$pool} = { vms => {}, storage => {} };
96
97 $usercfg->{pools}->{$pool}->{comment} = $param->{comment} if $param->{comment};
98
99 cfs_write_file("user.cfg", $usercfg);
100 }, "create pool failed");
101
102 return undef;
103 }});
104
105 __PACKAGE__->register_method ({
106 name => 'update_pool',
107 protected => 1,
108 path => '{poolid}',
109 method => 'PUT',
110 permissions => {
111 description => "You also need the right to modify permissions on any object you add/delete.",
112 check => ['perm', '/pool/{poolid}', ['Pool.Allocate']],
113 },
114 description => "Update pool data.",
115 parameters => {
116 additionalProperties => 0,
117 properties => {
118 poolid => { type => 'string', format => 'pve-poolid' },
119 comment => { type => 'string', optional => 1 },
120 vms => {
121 description => "List of virtual machines.",
122 type => 'string', format => 'pve-vmid-list',
123 optional => 1,
124 },
125 storage => {
126 description => "List of storage IDs.",
127 type => 'string', format => 'pve-storage-id-list',
128 optional => 1,
129 },
130 delete => {
131 description => "Remove vms/storage (instead of adding it).",
132 type => 'boolean',
133 optional => 1,
134 },
135 },
136 },
137 returns => { type => 'null' },
138 code => sub {
139 my ($param) = @_;
140
141 my $rpcenv = PVE::RPCEnvironment::get();
142 my $authuser = $rpcenv->get_user();
143
144 PVE::AccessControl::lock_user_config(
145 sub {
146
147 my $usercfg = cfs_read_file("user.cfg");
148
149 my $pool = $param->{poolid};
150
151 my $data = $usercfg->{pools}->{$pool};
152
153 die "pool '$pool' does not exist\n"
154 if !$data;
155
156 $data->{comment} = $param->{comment} if defined($param->{comment});
157
158 if (defined($param->{vms})) {
159 foreach my $vmid (PVE::Tools::split_list($param->{vms})) {
160 $rpcenv->check_perm_modify($authuser, "/vms/$vmid");
161 if ($param->{delete}) {
162 die "VM $vmid is not a pool member\n"
163 if !$data->{vms}->{$vmid};
164 delete $data->{vms}->{$vmid};
165 delete $usercfg->{vms}->{$vmid};
166 } else {
167 die "VM $vmid is already a pool member\n"
168 if $data->{vms}->{$vmid};
169 die "VM $vmid belongs to pool '$usercfg->{vms}->{$vmid}'\n"
170 if $usercfg->{vms}->{$vmid};
171
172 $data->{vms}->{$vmid} = 1;
173 $usercfg->{vms}->{$vmid} = $pool;
174 }
175 }
176 }
177
178 if (defined($param->{storage})) {
179 foreach my $storeid (PVE::Tools::split_list($param->{storage})) {
180 $rpcenv->check_perm_modify($authuser, "/storage/$storeid");
181 if ($param->{delete}) {
182 die "Storage '$storeid' is not a pool member\n"
183 if !$data->{storage}->{$storeid};
184 delete $data->{storage}->{$storeid};
185 } else {
186 die "Storage '$storeid' is already a pool member\n"
187 if $data->{storage}->{$storeid};
188
189 $data->{storage}->{$storeid} = 1;
190 }
191 }
192 }
193
194 cfs_write_file("user.cfg", $usercfg);
195 }, "update pools failed");
196
197 return undef;
198 }});
199
200 __PACKAGE__->register_method ({
201 name => 'read_pool',
202 path => '{poolid}',
203 method => 'GET',
204 permissions => {
205 check => ['perm', '/pool/{poolid}', ['Pool.Allocate']],
206 },
207 description => "Get pool configuration.",
208 parameters => {
209 additionalProperties => 0,
210 properties => {
211 poolid => {type => 'string', format => 'pve-poolid' },
212 },
213 },
214 returns => {
215 type => "object",
216 additionalProperties => 0,
217 properties => {
218 comment => { type => 'string', optional => 1 },
219 members => {
220 type => 'array',
221 items => {
222 type => "object",
223 additionalProperties => 1,
224 properties => {
225 type => { type => 'string', enum => ['qemu', 'lxc', 'openvz', 'storage'] },
226 id => { type => 'string' },
227 node => { type => 'string' },
228 vmid => { type => 'integer', optional => 1 },
229 storage => { type => 'string', optional => 1 },
230 },
231 },
232 },
233 },
234 },
235 code => sub {
236 my ($param) = @_;
237
238 my $usercfg = cfs_read_file("user.cfg");
239
240 my $vmlist = PVE::Cluster::get_vmlist() || {};
241 my $idlist = $vmlist->{ids} || {};
242
243 my $rrd = PVE::Cluster::rrd_dump();
244
245 my $pool = $param->{poolid};
246
247 my $data = $usercfg->{pools}->{$pool};
248
249 die "pool '$pool' does not exist\n"
250 if !$data;
251
252 my $members = [];
253
254 foreach my $vmid (keys %{$data->{vms}}) {
255 my $vmdata = $idlist->{$vmid};
256 next if !$vmdata;
257 my $entry = PVE::API2Tools::extract_vm_stats($vmid, $vmdata, $rrd);
258 push @$members, $entry;
259 }
260
261 my $nodename = PVE::INotify::nodename();
262 my $cfg = PVE::Storage::config();
263 foreach my $storeid (keys %{$data->{storage}}) {
264 my $scfg = PVE::Storage::storage_config ($cfg, $storeid, 1);
265 next if !$scfg;
266
267 my $node = $nodename;
268 if ($scfg->{nodes}) {
269 if (!$scfg->{nodes}->{$node}) {
270 foreach my $n (sort keys(%{$scfg->{nodes}})) {
271 $node = $n;
272 last;
273 }
274 }
275 }
276
277 my $entry = PVE::API2Tools::extract_storage_stats($storeid, $scfg, $node, $rrd);
278 push @$members, $entry;
279 }
280
281 my $res = { members => $members };
282 $res->{comment} = $data->{comment} if defined($data->{comment});
283
284 return $res;
285 }});
286
287
288 __PACKAGE__->register_method ({
289 name => 'delete_pool',
290 protected => 1,
291 path => '{poolid}',
292 method => 'DELETE',
293 permissions => {
294 description => "You can only delete empty pools (no members).",
295 check => ['perm', '/pool/{poolid}', ['Pool.Allocate']],
296 },
297 description => "Delete pool.",
298 parameters => {
299 additionalProperties => 0,
300 properties => {
301 poolid => { type => 'string', format => 'pve-poolid' },
302 }
303 },
304 returns => { type => 'null' },
305 code => sub {
306 my ($param) = @_;
307
308 my $rpcenv = PVE::RPCEnvironment::get();
309 my $authuser = $rpcenv->get_user();
310
311 PVE::AccessControl::lock_user_config(
312 sub {
313
314 my $vmlist = PVE::Cluster::get_vmlist() || {};
315 my $idlist = $vmlist->{ids} || {};
316
317 my $storecfg = PVE::Storage::config();
318
319 my $usercfg = cfs_read_file("user.cfg");
320
321 my $pool = $param->{poolid};
322
323 my $data = $usercfg->{pools}->{$pool};
324
325 die "pool '$pool' does not exist\n"
326 if !$data;
327
328 foreach my $vmid (keys %{$data->{vms}}) {
329 next if !$idlist->{$vmid};
330 die "pool '$pool' is not empty (contains VM $vmid)\n";
331 }
332
333 foreach my $storeid (keys %{$data->{storage}}) {
334 next if !PVE::Storage::storage_config ($storecfg, $storeid, 1);
335 die "pool '$pool' is not empty (contains storage '$storeid')\n";
336 }
337
338 delete ($usercfg->{pools}->{$pool});
339
340 PVE::AccessControl::delete_pool_acl($pool, $usercfg);
341
342 cfs_write_file("user.cfg", $usercfg);
343 }, "delete pool failed");
344
345 return undef;
346 }});
347
348 1;