all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 2/5] api: tape: optinally accept uuid for destroy/move media
Date: Thu, 11 Jan 2024 11:40:33 +0100	[thread overview]
Message-ID: <20240111104036.3341854-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20240111104036.3341854-1-d.csapak@proxmox.com>

so we can uniquely identify the tapes with duplicate labels.
The change is intended to be backwards compatible.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 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<String>) -> Result<(), Error> {
+pub fn move_tape(
+    label_text: Option<String>,
+    uuid: Option<Uuid>,
+    vault_name: Option<String>,
+) -> 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<String>) -> 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<String>) -> Result<(), E
     },
 )]
 /// Destroy media (completely remove from database)
-pub fn destroy_media(label_text: String, force: Option<bool>) -> Result<(), Error> {
+pub fn destroy_media(
+    label_text: Option<String>,
+    uuid: Option<Uuid>,
+    force: Option<bool>,
+) -> 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





  parent reply	other threads:[~2024-01-11 10:41 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11 10:40 [pbs-devel] [PATCH proxmox-backup 0/5] improve duplicate label-text handling Dominik Csapak
2024-01-11 10:40 ` [pbs-devel] [PATCH proxmox-backup 1/5] tape: handle duplicate label-texts in inventory Dominik Csapak
2024-01-12  7:29   ` Dietmar Maurer
2024-01-12  7:35     ` Dominik Csapak
2024-01-12  8:01       ` Dietmar Maurer
2024-01-12  8:09         ` Dominik Csapak
2024-01-11 10:40 ` Dominik Csapak [this message]
2024-01-11 10:40 ` [pbs-devel] [PATCH proxmox-backup 3/5] api: tape: don't allow duplicate media label-texts Dominik Csapak
2024-01-11 10:40 ` [pbs-devel] [PATCH proxmox-backup 4/5] ui: tape inventory: use uuid as id Dominik Csapak
2024-01-11 10:40 ` [pbs-devel] [PATCH proxmox-backup 5/5] ui: tape: add remove media button Dominik Csapak
2024-01-11 10:43 ` [pbs-devel] [PATCH proxmox-backup 0/5] improve duplicate label-text handling Dominik Csapak
2024-01-12  9:27 ` [pbs-devel] applied: " Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240111104036.3341854-3-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal