]> git.proxmox.com Git - pve-manager.git/commitdiff
api: backup/vzdump: add get_storage_param helper
authorFiona Ebner <f.ebner@proxmox.com>
Wed, 16 Nov 2022 14:04:35 +0000 (15:04 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 7 Jun 2023 14:47:08 +0000 (16:47 +0200)
to capture the logic in a single place.

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
PVE/API2/Backup.pm
PVE/API2/VZDump.pm
PVE/VZDump.pm

index 74bf95ca425814b5940599853967043fb075e5f5..25e615d172bb95489d0502be0fd1fcba978b6b8b 100644 (file)
@@ -60,10 +60,9 @@ my sub assert_param_permission_create {
 
     assert_param_permission_common($rpcenv, $user, $param);
 
-    if (!$param->{dumpdir}) {
-       my $storeid = $param->{storage} || 'local';
+    if (my $storeid = PVE::VZDump::get_storage_param($param)) {
        $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
-    } # no else branch, because dumpdir is root-only
+    }
 }
 
 my sub assert_param_permission_update {
@@ -84,8 +83,9 @@ my sub assert_param_permission_update {
     if ($current->{dumpdir}) {
        die "only root\@pam may edit jobs with a 'dumpdir' option.";
     } else {
-       my $storeid = $current->{storage} || 'local';
-       $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
+       if (my $storeid = PVE::VZDump::get_storage_param($current)) {
+           $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
+       }
     }
 }
 
index 8e873c0525c31f44b9615782b0ecc526b1721739..e3dcd0bd22063d5efb020e487a9509cf75c5a2fd 100644 (file)
@@ -27,10 +27,11 @@ my sub assert_param_permission_vzdump {
 
     PVE::API2::Backup::assert_param_permission_common($rpcenv, $user, $param);
 
-    if (!$param->{dumpdir} && (defined($param->{maxfiles}) || defined($param->{'prune-backups'}))) {
-       my $storeid = $param->{storage} || 'local';
-       $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
-    } # no else branch, because dumpdir is root-only
+    if (defined($param->{maxfiles}) || defined($param->{'prune-backups'})) {
+       if (my $storeid = PVE::VZDump::get_storage_param($param)) {
+           $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
+       }
+    }
 }
 
 __PACKAGE__->register_method ({
@@ -108,9 +109,9 @@ __PACKAGE__->register_method ({
        die "you can only backup a single VM with option --stdout\n"
            if $param->{stdout} && scalar(@{$local_vmids}) != 1;
 
-       # If the root-only dumpdir is used rather than a storage, the check will succeed anyways.
-       my $storeid = $param->{storage} || 'local';
-       $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
+       if (my $storeid = PVE::VZDump::get_storage_param($param)) {
+           $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
+       }
 
        my $worker = sub {
            my $upid = shift;
index 64a5fd4fca6f2b6242f2bccf2ac6ad9eea4a8547..c58e5f78fcbd1cab77543410500d88e3fb84f896 100644 (file)
@@ -55,6 +55,13 @@ foreach my $plug (@pve_vzdump_classes) {
     }
 }
 
+sub get_storage_param {
+    my ($param) = @_;
+
+    return if $param->{dumpdir};
+    return $param->{storage} || 'local';
+}
+
 # helper functions
 
 sub debugmsg {
@@ -567,8 +574,8 @@ sub new {
        die "cannot use options 'storage' and 'dumpdir' at the same time\n";
     }
 
-    if (!$opts->{dumpdir} && !$opts->{storage}) {
-       $opts->{storage} = 'local';
+    if (my $storage = get_storage_param($opts)) {
+       $opts->{storage} = $storage;
     }
 
     # Enforced by the API too, but these options might come in via defaults. Drop them if necessary.