From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 12E5F76754 for ; Fri, 23 Apr 2021 12:34:41 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0B6EE27D3C for ; Fri, 23 Apr 2021 12:34:41 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4646827D32 for ; Fri, 23 Apr 2021 12:34:40 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1FFBA44FA8 for ; Fri, 23 Apr 2021 12:34:40 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pve-devel@lists.proxmox.com Date: Fri, 23 Apr 2021 12:34:26 +0200 Message-Id: <20210423103426.1245672-3-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210423103426.1245672-1-f.gruenbichler@proxmox.com> References: <20210422153457.12265-11-s.reiter@proxmox.com> <20210423103426.1245672-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.017 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH storage 2/2] file-restore: pass in volume ID or name X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Apr 2021 10:34:41 -0000 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 --- Notes: requires corresponding patch on the GUI side as well since the parameter was renamed the snapshot name convenience part could be dropped by replacing + } elsif ($volume =~ m/^backup\//) { + $volid = "$storeid:$volume"; + } else { + $volid = "$storeid:backup/$volume"; + } with + } else { + $volid = "$storeid:$volume"; + } but since it basically applies as-is to VMA backups and passing in just the filename, it might be nice to have.. PVE/API2/Storage/FileRestore.pm | 63 +++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/PVE/API2/Storage/FileRestore.pm b/PVE/API2/Storage/FileRestore.pm index f6dc74a..a4bad44 100644 --- a/PVE/API2/Storage/FileRestore.pm +++ b/PVE/API2/Storage/FileRestore.pm @@ -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(); -- 2.20.1