From 37ba0aea5b8cb064a0bf16fc957a82d27296fac5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 1 Oct 2015 06:50:19 +0200 Subject: [PATCH] volume_list: moved code from PVE::API2::Storage::Content For better code reuse. --- PVE/API2/Storage/Content.pm | 37 +++++------------------------------- PVE/Storage.pm | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/PVE/API2/Storage/Content.pm b/PVE/API2/Storage/Content.pm index 63dc0a4..68cb713 100644 --- a/PVE/API2/Storage/Content.pm +++ b/PVE/API2/Storage/Content.pm @@ -15,8 +15,6 @@ use PVE::JSONSchema qw(get_standard_option); use base qw(PVE::RESTHandler); -my @ctypes = qw(images vztmpl iso backup); - __PACKAGE__->register_method ({ name => 'index', path => '', @@ -63,42 +61,17 @@ __PACKAGE__->register_method ({ my $authuser = $rpcenv->get_user(); - my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ]; - my $storeid = $param->{storage}; - my $vmid = $param->{vmid}; - my $cfg = cfs_read_file("storage.cfg"); - my $scfg = PVE::Storage::storage_config($cfg, $storeid); + my $vollist = PVE::Storage::volume_list($cfg, $storeid, $param->{vmid}, $param->{content}); my $res = []; - foreach my $ct (@$cts) { - my $data; - if ($ct eq 'images') { - $data = PVE::Storage::vdisk_list ($cfg, $storeid, $param->{vmid}); - } elsif ($ct eq 'iso' && !defined($param->{vmid})) { - $data = PVE::Storage::template_list ($cfg, $storeid, 'iso'); - } elsif ($ct eq 'vztmpl'&& !defined($param->{vmid})) { - $data = PVE::Storage::template_list ($cfg, $storeid, 'vztmpl'); - } elsif ($ct eq 'backup') { - $data = PVE::Storage::template_list ($cfg, $storeid, 'backup'); - foreach my $item (@{$data->{$storeid}}) { - if (defined($vmid)) { - @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}}; - } - } - } - - next if !$data || !$data->{$storeid}; - - foreach my $item (@{$data->{$storeid}}) { - eval { $rpcenv->check_volume_access($authuser, $cfg, undef, $item->{volid}); }; - next if $@; - $item->{content} = $ct; - push @$res, $item; - } + foreach my $item (@$vollist) { + eval { $rpcenv->check_volume_access($authuser, $cfg, undef, $item->{volid}); }; + next if $@; + push @$res, $item; } return $res; diff --git a/PVE/Storage.pm b/PVE/Storage.pm index 3d08caf..3f46eb3 100755 --- a/PVE/Storage.pm +++ b/PVE/Storage.pm @@ -759,6 +759,44 @@ sub vdisk_list { return $res; } +sub volume_list { + my ($cfg, $storeid, $vmid, $content) = @_; + + my @ctypes = qw(images vztmpl iso backup); + + my $cts = $content ? [ $content ] : [ @ctypes ]; + + my $scfg = PVE::Storage::storage_config($cfg, $storeid); + + my $res = []; + foreach my $ct (@$cts) { + my $data; + if ($ct eq 'images') { + $data = vdisk_list($cfg, $storeid, $vmid); + } elsif ($ct eq 'iso' && !defined($vmid)) { + $data = template_list($cfg, $storeid, 'iso'); + } elsif ($ct eq 'vztmpl'&& !defined($vmid)) { + $data = template_list ($cfg, $storeid, 'vztmpl'); + } elsif ($ct eq 'backup') { + $data = template_list ($cfg, $storeid, 'backup'); + foreach my $item (@{$data->{$storeid}}) { + if (defined($vmid)) { + @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}}; + } + } + } + + next if !$data || !$data->{$storeid}; + + foreach my $item (@{$data->{$storeid}}) { + $item->{content} = $ct; + push @$res, $item; + } + } + + return $res; +} + sub uevent_seqnum { my $filename = "/sys/kernel/uevent_seqnum";