all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup v7 6/9] api: add POST endpoint for move-namespace
Date: Thu, 16 Apr 2026 19:18:27 +0200	[thread overview]
Message-ID: <20260416171830.266553-7-h.laimer@proxmox.com> (raw)
In-Reply-To: <20260416171830.266553-1-h.laimer@proxmox.com>

Add a dedicated /move-namespace endpoint for moving namespaces to a
new location within the same datastore. Exposes max-depth,
delete-source, and merge-groups as optional parameters.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 src/api2/admin/datastore.rs |  4 ++
 src/api2/admin/namespace.rs | 99 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 101 insertions(+), 2 deletions(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 54895f2b..a91d48dc 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -2957,6 +2957,10 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
     ),
     ("mount", &Router::new().post(&API_METHOD_MOUNT)),
     ("move-group", &Router::new().post(&API_METHOD_MOVE_GROUP)),
+    (
+        "move-namespace",
+        &Router::new().post(&crate::api2::admin::namespace::API_METHOD_MOVE_NAMESPACE),
+    ),
     (
         "namespace",
         // FIXME: move into datastore:: sub-module?!
diff --git a/src/api2/admin/namespace.rs b/src/api2/admin/namespace.rs
index c885ab54..abfaf8ff 100644
--- a/src/api2/admin/namespace.rs
+++ b/src/api2/admin/namespace.rs
@@ -1,12 +1,16 @@
 use anyhow::{bail, Error};
 
 use pbs_config::CachedUserInfo;
-use proxmox_router::{http_bail, ApiMethod, Permission, Router, RpcEnvironment};
+use proxmox_rest_server::WorkerTask;
+use proxmox_router::{
+    http_bail, ApiMethod, Permission, Router, RpcEnvironment, RpcEnvironmentType,
+};
 use proxmox_schema::*;
+use serde_json::{json, Value};
 
 use pbs_api_types::{
     Authid, BackupGroupDeleteStats, BackupNamespace, NamespaceListItem, Operation,
-    DATASTORE_SCHEMA, NS_MAX_DEPTH_SCHEMA, PROXMOX_SAFE_ID_FORMAT,
+    DATASTORE_SCHEMA, NS_MAX_DEPTH_SCHEMA, PROXMOX_SAFE_ID_FORMAT, UPID_SCHEMA,
 };
 
 use pbs_datastore::DataStore;
@@ -192,6 +196,97 @@ pub fn delete_namespace(
     Ok(stats)
 }
 
+#[api(
+    input: {
+        properties: {
+            store: { schema: DATASTORE_SCHEMA },
+            ns: {
+                type: BackupNamespace,
+            },
+            "target-ns": {
+                type: BackupNamespace,
+            },
+            "max-depth": {
+                schema: NS_MAX_DEPTH_SCHEMA,
+                optional: true,
+            },
+            "delete-source": {
+                type: bool,
+                optional: true,
+                default: true,
+                description: "Remove the source namespace after moving all contents. \
+                    Defaults to true.",
+            },
+            "merge-groups": {
+                type: bool,
+                optional: true,
+                default: true,
+                description: "If a group with the same name already exists in the target \
+                    namespace, merge snapshots into it. Requires matching ownership and \
+                    non-overlapping snapshot times.",
+            },
+        },
+    },
+    returns: {
+        schema: UPID_SCHEMA,
+    },
+    access: {
+        permission: &Permission::Anybody,
+        description: "Requires DATASTORE_MODIFY on the parent of 'ns' and on the parent of 'target-ns'.",
+    },
+)]
+/// Move a backup namespace (including all child namespaces and groups) to a new location.
+pub fn move_namespace(
+    store: String,
+    ns: BackupNamespace,
+    target_ns: BackupNamespace,
+    max_depth: Option<usize>,
+    delete_source: bool,
+    merge_groups: bool,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Value, Error> {
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+
+    check_ns_modification_privs(&store, &ns, &auth_id)?;
+    check_ns_modification_privs(&store, &target_ns, &auth_id)?;
+
+    let datastore =
+        DataStore::lookup_datastore(crate::tools::lookup_with(&store, Operation::Write))?;
+
+    // Best-effort pre-checks for a fast synchronous error before spawning a worker.
+    if ns.is_root() {
+        bail!("cannot move root namespace");
+    }
+    if ns == target_ns {
+        bail!("source and target namespace must be different");
+    }
+    if !datastore.namespace_exists(&ns) {
+        bail!("source namespace '{ns}' does not exist");
+    }
+    let target_parent = target_ns.parent();
+    if !target_ns.is_root() && !datastore.namespace_exists(&target_parent) {
+        bail!("target parent namespace '{target_parent}' does not exist");
+    }
+    if ns.contains(&target_ns).is_some() {
+        bail!("cannot move namespace '{ns}' into its own subtree (target: '{target_ns}')");
+    }
+
+    let worker_id = format!("{store}:{ns}:{target_ns}");
+    let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
+
+    let upid_str = WorkerTask::new_thread(
+        "move-namespace",
+        Some(worker_id),
+        auth_id.to_string(),
+        to_stdout,
+        move |_worker| {
+            datastore.move_namespace(&ns, &target_ns, max_depth, delete_source, merge_groups)
+        },
+    )?;
+
+    Ok(json!(upid_str))
+}
+
 pub const ROUTER: Router = Router::new()
     .get(&API_METHOD_LIST_NAMESPACES)
     .post(&API_METHOD_CREATE_NAMESPACE)
-- 
2.47.3





  parent reply	other threads:[~2026-04-16 17:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-16 17:18 [PATCH proxmox-backup v7 0/9] fixes #6195: add support for moving groups and namespaces Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 1/9] ui: show empty groups Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 2/9] datastore: add move-group Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 3/9] datastore: add move-namespace Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 4/9] docs: add section on moving namespaces and groups Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 5/9] api: add POST endpoint for move-group Hannes Laimer
2026-04-16 17:18 ` Hannes Laimer [this message]
2026-04-16 17:18 ` [PATCH proxmox-backup v7 7/9] ui: add move group action Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 8/9] ui: add move namespace action Hannes Laimer
2026-04-16 17:18 ` [PATCH proxmox-backup v7 9/9] cli: add move-namespace and move-group commands Hannes Laimer

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=20260416171830.266553-7-h.laimer@proxmox.com \
    --to=h.laimer@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