]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Content.pm
fix bug #1280: Can't locate object method "root@pam"
[pve-storage.git] / PVE / API2 / Storage / Content.pm
index 38cd1e94d044dfa658f2df0b0ac61c8197a527c2..f15960039823eea32e6b31e3aec397bb0f967f03 100644 (file)
@@ -2,9 +2,10 @@ package PVE::API2::Storage::Content;
 
 use strict;
 use warnings;
+use Data::Dumper;
 
 use PVE::SafeSyslog;
-use PVE::Cluster qw(cfs_read_file);
+use PVE::Cluster;
 use PVE::Storage;
 use PVE::INotify;
 use PVE::Exception qw(raise_param_exc);
@@ -14,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 => '',
@@ -30,17 +29,20 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => { 
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id'),
+           storage => get_standard_option('pve-storage-id', {
+               completion => \&PVE::Storage::complete_storage_enabled,
+            }),
            content => { 
                description => "Only list content of this type.",
                type => 'string', format => 'pve-storage-content',
                optional => 1,
+               completion => \&PVE::Storage::complete_content_type,
            },
-           vmid => get_standard_option
-               ('pve-vmid', { 
-                   description => "Only list images for this VM",
-                   optional => 1,              
-                }),
+           vmid => get_standard_option('pve-vmid', {
+               description => "Only list images for this VM",
+               optional => 1,
+               completion => \&PVE::Cluster::complete_vmid,
+           }),
        },
     },
     returns => {
@@ -58,33 +60,21 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
 
        my $storeid = $param->{storage};
 
-       my $cfg = cfs_read_file("storage.cfg");
+       my $cfg = PVE::Storage::config();
 
-       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' || defined($param->{vmid})) {
-               $data = PVE::Storage::vdisk_list ($cfg, $storeid, $param->{vmid});
-           } elsif ($ct eq 'iso') {
-               $data = PVE::Storage::template_list ($cfg, $storeid, 'iso');
-           } elsif ($ct eq 'vztmpl') {
-               $data = PVE::Storage::template_list ($cfg, $storeid, 'vztmpl');
-           } elsif ($ct eq 'backup') {
-               $data = PVE::Storage::template_list ($cfg, $storeid, 'backup');
-           }
-
-           next if !$data || !$data->{$storeid};
-
-           foreach my $item (@{$data->{$storeid}}) {
-               $item->{content} = $ct;
-               push @$res, $item;
-           }
+       foreach my $item (@$vollist) {
+           eval {  PVE::Storage::check_volume_access($rpcenv, $authuser, $cfg, undef, $item->{volid}); };
+           next if $@;
+           push @$res, $item;
        }
 
        return $res;    
@@ -104,12 +94,17 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => { 
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id'),
+           storage => get_standard_option('pve-storage-id', {
+               completion => \&PVE::Storage::complete_storage_enabled,
+           }),
            filename => { 
                description => "The name of the file to create.",
                type => 'string',
            },
-           vmid => get_standard_option('pve-vmid', { description => "Specify owner VM" } ),
+           vmid => get_standard_option('pve-vmid', {
+               description => "Specify owner VM",
+               completion => \&PVE::Cluster::complete_vmid,
+           }),
            size => {
                description => "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
                type => 'string',
@@ -117,7 +112,7 @@ __PACKAGE__->register_method ({
            },
            'format' => {
                type => 'string',
-               enum => ['raw', 'qcow2'],
+               enum => ['raw', 'qcow2', 'subvol'],
                requires => 'size',
                optional => 1,
            },
@@ -146,7 +141,7 @@ __PACKAGE__->register_method ({
        }
 
        # extract FORMAT from name
-       if ($name =~ m/\.(raw|qcow2)$/) {
+       if ($name =~ m/\.(raw|qcow2|vmdk)$/) {
            my $fmt = $1;
 
            raise_param_exc({ format => "different storage formats ($param->{format} != $fmt)" }) 
@@ -155,7 +150,7 @@ __PACKAGE__->register_method ({
            $param->{format} = $fmt;
        }
 
-       my $cfg = cfs_read_file('storage.cfg');
+       my $cfg = PVE::Storage::config();
     
        my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid}, 
                                               $param->{format}, 
@@ -174,11 +169,12 @@ my $real_volume_id = sub {
     if ($volume =~ m/:/) {
        eval {
            my ($sid, $volname) = PVE::Storage::parse_volume_id ($volume);
-           raise_param_exc({ storage => "storage ID missmatch" }) 
+           die "storage ID missmatch ($sid != $storeid)\n"
                if $storeid && $sid ne $storeid;
            $volid = $volume;
+           $storeid = $sid;
        };
-       raise_param_exc({ volume => $@}) if $@; 
+       raise_param_exc({ volume => $@ }) if $@; 
           
     } else {
        raise_param_exc({ volume => "no storage speficied - incomplete volume ID" }) 
@@ -187,7 +183,7 @@ my $real_volume_id = sub {
        $volid = "$storeid:$volume";
     }
 
-    return $volid;
+    return wantarray ? ($volid, $storeid) : $volid;
 };
 
 __PACKAGE__->register_method ({
@@ -196,7 +192,8 @@ __PACKAGE__->register_method ({
     method => 'GET',
     description => "Get volume attributes",
     permissions => { 
-       check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
+       description => "You need read access for the volume.",
+       user => 'all',
     },
     protected => 1,
     proxyto => 'node',
@@ -215,18 +212,25 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $volid = &$real_volume_id($param->{storage}, $param->{volume});
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume});
+
+       my $cfg = PVE::Storage::config();
 
-       my $cfg = cfs_read_file('storage.cfg');
+       PVE::Storage::check_volume_access($rpcenv, $authuser, $cfg, undef, $volid);
 
        my $path = PVE::Storage::path($cfg, $volid);
-       my ($size, $format, $used) = PVE::Storage::file_size_info ($path);
+       my ($size, $format, $used, $parent) =  PVE::Storage::file_size_info($path);
+       die "file_size_info on '$volid' failed\n" if !($format && $size);
 
        # fixme: return more attributes?
        return {
            path => $path,
            size => $size,
             used => $used,
+           format => $format,
        };
     }});
 
@@ -236,7 +240,8 @@ __PACKAGE__->register_method ({
     method => 'DELETE',
     description => "Delete volume",
     permissions => { 
-       check => ['perm', '/storage/{storage}', ['Datastore.AllocateSpace']],
+       description => "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM).",
+       user => 'all',
     },
     protected => 1,
     proxyto => 'node',
@@ -244,10 +249,14 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => { 
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id', { optional => 1}),
+           storage => get_standard_option('pve-storage-id', {
+                optional => 1,
+                completion => \&PVE::Storage::complete_storage,
+            }),
            volume => {
                description => "Volume identifier",
-               type => 'string', 
+               type => 'string',
+               completion => \&PVE::Storage::complete_volume,
            },
        },
     },
@@ -255,9 +264,20 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $volid = &$real_volume_id($param->{storage}, $param->{volume});
-       
-       my $cfg = cfs_read_file('storage.cfg');
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $cfg = PVE::Storage::config();
+
+       my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume});
+
+       my ($path, $ownervm, $vtype) = PVE::Storage::path($cfg, $volid);
+       if ($vtype eq 'backup' && $ownervm) {
+           $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
+           $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
+       } else {
+           $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
+       }
 
        PVE::Storage::vdisk_free ($cfg, $volid);
 
@@ -311,7 +331,7 @@ __PACKAGE__->register_method ({
 
        print "DEBUG: COPY $src_volid TO $dst_volid\n";
 
-       my $cfg = cfs_read_file('storage.cfg');
+       my $cfg = PVE::Storage::config();
 
        # do all parameter checks first