From c519fe3c913337e15986a49642c4420a170883b4 Mon Sep 17 00:00:00 2001 From: Wolfgang Link Date: Wed, 24 Feb 2016 12:11:07 +0100 Subject: [PATCH] add list to pveam This function list all the templates off a specified storage. It also gives the size of the template. --- PVE/CLI/pveam.pm | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/PVE/CLI/pveam.pm b/PVE/CLI/pveam.pm index 772c05d9..ba569fc7 100644 --- a/PVE/CLI/pveam.pm +++ b/PVE/CLI/pveam.pm @@ -43,9 +43,74 @@ __PACKAGE__->register_method ({ }}); +__PACKAGE__->register_method ({ + name => 'index', + path => 'index', + method => 'GET', + description => "Get list of all templates on storage", + permissions => { + description => "Show all users the template wich have permission on that storage.", + check => ['perm', '/storage/{storage}', ['Datastore.AllocateTemplate']], + }, + proxyto => 'node', + protected => 1, + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + storage => get_standard_option('pve-storage-id', { + description => "Only list status for specified storage", + completion => \&PVE::Storage::complete_storage_enabled, + }), + }, + }, + returns => { + type => 'array', + items => { + type => "object", + properties => {}, + }, + }, + code => sub { + my ($param) = @_; + + my $rpcenv = PVE::RPCEnvironment::get(); + + my $authuser = $rpcenv->get_user(); + + my $storeid = $param->{storage}; + + my $cfg = PVE::Cluster::cfs_read_file("storage.cfg"); + + die "Storage do not support templates!\n" if !$cfg->{ids}->{$storeid}->{content}->{vztmpl}; + + my $vollist = PVE::Storage::volume_list($cfg, $storeid, undef, 'vztmpl'); + + my $res = []; + foreach my $item (@$vollist) { + eval { $rpcenv->check_volume_access($authuser, $cfg, undef, $item->{volid}); }; + next if $@; + push @$res, $item; + } + + return $res; + }}); + +my $print_list = sub { + my ($list) = @_; + + printf "%-60s %-6s\n", + qw(NAME SIZE); + + foreach my $rec (@$list) { + printf "%-60s %-4.2fMB\n", $rec->{volid}, $rec->{size}/(1024*1024); + } +}; + our $cmddef = { update => [ __PACKAGE__, 'update', []], download => [ 'PVE::API2::Nodes::Nodeinfo', 'apl_download', [ 'storage', 'template'], { node => $nodename } ], + list => [ __PACKAGE__, 'index', [ 'storage' ], { node => $nodename }, $print_list ], }; 1;