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 E90F8C06B0 for ; Thu, 11 Jan 2024 11:41:08 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id BDC19F5E2 for ; Thu, 11 Jan 2024 11:40:38 +0100 (CET) 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 for ; Thu, 11 Jan 2024 11:40:37 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id AB252490B8 for ; Thu, 11 Jan 2024 11:40:37 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 11 Jan 2024 11:40:33 +0100 Message-Id: <20240111104036.3341854-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240111104036.3341854-1-d.csapak@proxmox.com> References: <20240111104036.3341854-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.020 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [media.rs] Subject: [pbs-devel] [PATCH proxmox-backup 2/5] api: tape: optinally accept uuid for destroy/move media X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Jan 2024 10:41:09 -0000 so we can uniquely identify the tapes with duplicate labels. The change is intended to be backwards compatible. Signed-off-by: Dominik Csapak --- src/api2/tape/media.rs | 84 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/src/api2/tape/media.rs b/src/api2/tape/media.rs index 5031817c..07bed86a 100644 --- a/src/api2/tape/media.rs +++ b/src/api2/tape/media.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use anyhow::{bail, format_err, Error}; use proxmox_router::{list_subdirs_api_method, Permission, Router, RpcEnvironment, SubdirMap}; -use proxmox_schema::api; +use proxmox_schema::{api, param_bail}; use proxmox_uuid::Uuid; use pbs_api_types::{ @@ -290,6 +290,11 @@ pub async fn list_media( properties: { "label-text": { schema: MEDIA_LABEL_SCHEMA, + optional: true, + }, + uuid: { + schema: MEDIA_UUID_SCHEMA, + optional: true, }, "vault-name": { schema: VAULT_NAME_SCHEMA, @@ -299,15 +304,33 @@ pub async fn list_media( }, )] /// Change Tape location to vault (if given), or offline. -pub fn move_tape(label_text: String, vault_name: Option) -> Result<(), Error> { +pub fn move_tape( + label_text: Option, + uuid: Option, + vault_name: Option, +) -> Result<(), Error> { let mut inventory = Inventory::load(TAPE_STATUS_DIR)?; - let uuid = inventory - .find_media_by_label_text(&label_text)? - .ok_or_else(|| format_err!("no such media '{}'", label_text))? - .label - .uuid - .clone(); + let uuid = match (uuid, label_text) { + (Some(_), Some(_)) => { + param_bail!( + "format-text", + format_err!("A uuid is given, no label-text is expected.") + ); + } + (None, None) => { + param_bail!( + "uuid", + format_err!("No label-text is given, a uuid is required.") + ); + } + (Some(uuid), None) => uuid, + (None, Some(label_text)) => match inventory.find_media_by_label_text(&label_text) { + Ok(Some(media_id)) => media_id.label.uuid.clone(), + Ok(None) => bail!("no such media '{}'", label_text), + Err(err) => bail!("error getting media from unique label: {err}"), + }, + }; if let Some(vault_name) = vault_name { inventory.set_media_location_vault(&uuid, &vault_name)?; @@ -323,6 +346,11 @@ pub fn move_tape(label_text: String, vault_name: Option) -> Result<(), E properties: { "label-text": { schema: MEDIA_LABEL_SCHEMA, + optional: true, + }, + uuid: { + schema: MEDIA_UUID_SCHEMA, + optional: true, }, force: { description: "Force removal (even if media is used in a media set).", @@ -333,22 +361,46 @@ pub fn move_tape(label_text: String, vault_name: Option) -> Result<(), E }, )] /// Destroy media (completely remove from database) -pub fn destroy_media(label_text: String, force: Option) -> Result<(), Error> { +pub fn destroy_media( + label_text: Option, + uuid: Option, + force: Option, +) -> Result<(), Error> { let force = force.unwrap_or(false); let mut inventory = Inventory::load(TAPE_STATUS_DIR)?; - let media_id = inventory - .find_media_by_label_text(&label_text)? - .ok_or_else(|| format_err!("no such media '{}'", label_text))?; + let (media_id, text) = match (uuid, label_text) { + (Some(_), Some(_)) => { + param_bail!( + "format-text", + format_err!("A uuid is given, no label-text is expected.") + ); + } + (None, None) => { + param_bail!( + "uuid", + format_err!("No label-text is given, a uuid is required.") + ); + } + (Some(uuid), None) => ( + inventory + .lookup_media(&uuid) + .ok_or_else(|| format_err!("no such media '{}'", uuid))?, + uuid.to_string(), + ), + (None, Some(label_text)) => ( + inventory + .find_media_by_label_text(&label_text)? + .ok_or_else(|| format_err!("no such media '{}'", label_text))?, + label_text, + ), + }; if !force { if let Some(ref set) = media_id.media_set_label { if !set.unassigned() { - bail!( - "media '{}' contains data (please use 'force' flag to remove.", - label_text - ); + bail!("media '{text}' contains data (please use 'force' flag to remove."); } } } -- 2.30.2