]> git.proxmox.com Git - pve-storage.git/commitdiff
file-restore: pass in volume ID or name
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 23 Apr 2021 10:34:26 +0000 (12:34 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 23 Apr 2021 12:09:54 +0000 (14:09 +0200)
instead of just the snapshot for consistency with other API endpoints,
and possible future extension to VMA backups (where 'snapshot' would be
a rather strange terminology).

add some additional checks (pbs storage type, backup volume type),
completion and magic (allow passing in either a full volume ID with
correct storage, or just the volume name, or just the snapshot for
easier API/CLI usage/convenience).

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
PVE/API2/Storage/FileRestore.pm

index f6dc74abc999e217cf8cbd435c54114a9ce1f10f..a4bad44eeb2cdd9965d6d6a305012f44077f4e6d 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use MIME::Base64;
+use PVE::Exception qw(raise_param_exc);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::PBSClient;
 use PVE::Storage;
@@ -12,6 +13,26 @@ use PVE::Tools qw(extract_param);
 use PVE::RESTHandler;
 use base qw(PVE::RESTHandler);
 
+my $parse_volname_or_id = sub {
+    my ($storeid, $volume) = @_;
+
+    my $volid;
+    my ($sid, $volname) = PVE::Storage::parse_volume_id($volume, 1);
+
+    if (defined($sid)) {
+       raise_param_exc({ volume => "storage ID mismatch ($sid != $storeid)." })
+           if $sid ne $storeid;
+
+       $volid = $volume;
+    } elsif ($volume =~ m/^backup\//) {
+       $volid = "$storeid:$volume";
+    } else {
+       $volid = "$storeid:backup/$volume";
+    }
+
+    return $volid;
+};
+
 __PACKAGE__->register_method ({
     name => 'list',
     path => 'list',
@@ -26,10 +47,13 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id'),
-           snapshot => {
-               description => "Backup snapshot identifier.",
+           storage => get_standard_option('pve-storage-id', {
+               completion => \&PVE::Storage::complete_storage_enabled,
+           }),
+           volume => {
+               description => "Backup volume ID or name. Currently only PBS snapshots are supported.",
                type => 'string',
+               completion => \&PVE::Storage::complete_volume,
            },
            filepath => {
                description => 'base64-path to the directory or file being listed, or "/".',
@@ -80,18 +104,25 @@ __PACKAGE__->register_method ({
 
        my $path = extract_param($param, 'filepath') || "/";
        my $base64 = $path ne "/";
-       my $snap = extract_param($param, 'snapshot');
+
        my $storeid = extract_param($param, 'storage');
+
+       my $volid = $parse_volname_or_id->($storeid, $param->{volume});
        my $cfg = PVE::Storage::config();
        my $scfg = PVE::Storage::storage_config($cfg, $storeid);
 
-       my $volid = "$storeid:backup/$snap";
        PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid);
 
+       raise_param_exc({'storage' => "Only PBS storages supported for file-restore."})
+           if $scfg->{type} ne 'pbs';
+
+       my ($vtype, $snap) = PVE::Storage::parse_volname($cfg, $volid);
+       raise_param_exc({'volume' => 'Not a backup archive.'})
+           if $vtype ne 'backup';
+
        my $client = PVE::PBSClient->new($scfg, $storeid);
        my $ret = $client->file_restore_list($snap, $path, $base64);
 
-
        # 'leaf' is a proper JSON boolean, map to perl-y bool
        # TODO: make PBSClient decode all bools always as 1/0?
        foreach my $item (@$ret) {
@@ -115,10 +146,13 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id'),
-           snapshot => {
-               description => "Backup snapshot identifier.",
+           storage => get_standard_option('pve-storage-id', {
+               completion => \&PVE::Storage::complete_storage_enabled,
+           }),
+           volume => {
+               description => "Backup volume ID or name. Currently only PBS snapshots are supported.",
                type => 'string',
+               completion => \&PVE::Storage::complete_volume,
            },
            filepath => {
                description => 'base64-path to the directory or file to download.',
@@ -137,14 +171,21 @@ __PACKAGE__->register_method ({
        my $user = $rpcenv->get_user();
 
        my $path = extract_param($param, 'filepath');
-       my $snap = extract_param($param, 'snapshot');
        my $storeid = extract_param($param, 'storage');
+       my $volid = $parse_volname_or_id->($storeid, $param->{volume});
+
        my $cfg = PVE::Storage::config();
        my $scfg = PVE::Storage::storage_config($cfg, $storeid);
 
-       my $volid = "$storeid:backup/$snap";
        PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid);
 
+       raise_param_exc({'storage' => "Only PBS storages supported for file-restore."})
+           if $scfg->{type} ne 'pbs';
+
+       my ($vtype, $snap) = PVE::Storage::parse_volname($cfg, $volid);
+       raise_param_exc({'volume' => 'Not a backup archive.'})
+           if $vtype ne 'backup';
+
        my $client = PVE::PBSClient->new($scfg, $storeid);
        my $fifo = $client->file_restore_extract_prepare();