[pdm-devel] [PATCH datacenter-manager 4/7] api: pbs tasks: add PBS task API

Lukas Wagner l.wagner at proxmox.com
Wed Nov 12 10:42:00 CET 2025


Mostly based off of the PVE task API with the necessary changes for PBS.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 server/src/api/mod.rs       |  22 ++++-
 server/src/api/pbs/mod.rs   |   4 +-
 server/src/api/pbs/tasks.rs | 170 ++++++++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+), 2 deletions(-)
 create mode 100644 server/src/api/pbs/tasks.rs

diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs
index 6a7a65a2..119d57b7 100644
--- a/server/src/api/mod.rs
+++ b/server/src/api/mod.rs
@@ -1,6 +1,7 @@
 //! Common API endpoints
 
-use anyhow::Error;
+use anyhow::{bail, Error};
+use pdm_api_types::{remotes::RemoteType, RemoteUpid};
 use serde_json::{json, Value};
 
 use proxmox_router::{list_subdirs_api_method, Permission, Router, SubdirMap};
@@ -67,3 +68,22 @@ fn version() -> Result<Value, Error> {
         "repoid": pdm_buildcfg::PROXMOX_PKG_REPOID
     }))
 }
+
+/// Check a [`RemoteUpid`] matches the expected remote name and type.
+pub(crate) fn verify_upid(
+    remote: &str,
+    remote_type: RemoteType,
+    upid: &RemoteUpid,
+) -> Result<(), Error> {
+    if upid.remote() != remote {
+        bail!(
+            "remote '{remote}' does not match remote in upid ('{}')",
+            upid.remote()
+        );
+    }
+    if upid.remote_type() != remote_type {
+        bail!("upid does not belong to a {remote_type} remote");
+    }
+
+    Ok(())
+}
diff --git a/server/src/api/pbs/mod.rs b/server/src/api/pbs/mod.rs
index a37fadb5..15e42727 100644
--- a/server/src/api/pbs/mod.rs
+++ b/server/src/api/pbs/mod.rs
@@ -22,6 +22,7 @@ use crate::remote_tasks;
 
 mod node;
 mod rrddata;
+pub mod tasks;
 
 pub const ROUTER: Router = Router::new()
     .get(&list_subdirs_api_method!(SUBDIRS))
@@ -49,7 +50,8 @@ const REMOTE_SUBDIRS: SubdirMap = &sorted!([
     ("nodes", &NODES_ROUTER),
     ("status", &Router::new().get(&API_METHOD_GET_STATUS)),
     ("rrddata", &rrddata::PBS_NODE_RRD_ROUTER),
-    ("datastore", &DATASTORE_ROUTER)
+    ("datastore", &DATASTORE_ROUTER),
+    ("tasks", &tasks::ROUTER),
 ]);
 
 const DATASTORE_ROUTER: Router = Router::new()
diff --git a/server/src/api/pbs/tasks.rs b/server/src/api/pbs/tasks.rs
new file mode 100644
index 00000000..5c86bff8
--- /dev/null
+++ b/server/src/api/pbs/tasks.rs
@@ -0,0 +1,170 @@
+//! Access to PBS tasks.
+
+use anyhow::Error;
+
+use proxmox_router::{list_subdirs_api_method, Permission, Router, RpcEnvironment, SubdirMap};
+use proxmox_schema::api;
+use proxmox_sortable_macro::sortable;
+
+use pdm_api_types::remotes::{RemoteType, REMOTE_ID_SCHEMA};
+use pdm_api_types::{
+    RemoteUpid, PRIV_RESOURCE_AUDIT, PRIV_RESOURCE_MANAGE, TASKLOG_DOWNLOAD_PARAM_SCHEMA,
+    TASKLOG_LIMIT_PARAM_SCHEMA, TASKLOG_START_PARAM_SCHEMA,
+};
+
+use crate::pbs_client;
+
+pub const ROUTER: Router = Router::new()
+    .get(&API_METHOD_LIST_TASKS)
+    .match_all("upid", &UPID_API_ROUTER);
+
+pub const UPID_API_ROUTER: Router = Router::new()
+    .get(&list_subdirs_api_method!(UPID_API_SUBDIRS))
+    .delete(&API_METHOD_STOP_TASK)
+    .subdirs(UPID_API_SUBDIRS);
+
+#[sortable]
+const UPID_API_SUBDIRS: SubdirMap = &sorted!([
+    ("log", &Router::new().get(&API_METHOD_READ_TASK_LOG)),
+    ("status", &Router::new().get(&API_METHOD_GET_TASK_STATUS)),
+]);
+
+#[api(
+    input: {
+        properties: {
+            remote: { schema: REMOTE_ID_SCHEMA },
+        },
+    },
+    access: {
+        // FIXME: fine-grained task filtering?
+        permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_AUDIT, false),
+    },
+    returns: { type: pve_api_types::TaskStatus },
+)]
+/// Get the list of tasks either for a specific node, or query all at once.
+async fn list_tasks(remote: String) -> Result<Vec<pbs_api_types::TaskListItem>, Error> {
+    let (remotes, _) = pdm_config::remotes::config()?;
+    let client = pbs_client::connect_to_remote(&remotes, &remote)?;
+
+    Ok(client.get_task_list(Default::default()).await?)
+}
+
+#[api(
+    input: {
+        properties: {
+            remote: { schema: REMOTE_ID_SCHEMA },
+            upid: { type: RemoteUpid },
+        },
+    },
+    access: {
+        // FIXME: fine-grained task filtering?
+        permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_MANAGE, false),
+    },
+)]
+/// Get the status of a task from a Proxmox VE instance.
+async fn stop_task(remote: String, upid: RemoteUpid) -> Result<(), Error> {
+    let (remotes, _) = pdm_config::remotes::config()?;
+
+    crate::api::verify_upid(&remote, RemoteType::Pbs, &upid)?;
+
+    let client = pbs_client::connect_to_remote(&remotes, upid.remote())?;
+
+    Ok(client.stop_task(upid.upid()).await?)
+}
+
+#[api(
+    input: {
+        properties: {
+            remote: { schema: REMOTE_ID_SCHEMA },
+            upid: { type: RemoteUpid },
+            wait: {
+                description: "wait for the task to finish before returning its result",
+                type: Boolean,
+                optional: true,
+                default: false,
+            },
+        },
+    },
+    access: {
+        // FIXME: fine-grained task filtering?
+        permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_AUDIT, false),
+    },
+    returns: { type: pve_api_types::TaskStatus },
+)]
+/// Get the status of a task from a Proxmox VE instance.
+pub async fn get_task_status(
+    remote: String,
+    upid: RemoteUpid,
+    wait: bool,
+) -> Result<pbs_client::TaskStatus, Error> {
+    let (remotes, _) = pdm_config::remotes::config()?;
+
+    crate::api::verify_upid(&remote, RemoteType::Pbs, &upid)?;
+
+    let client = pbs_client::connect_to_remote(&remotes, upid.remote())?;
+
+    loop {
+        let status = client.get_task_status(upid.upid()).await?;
+        if !wait || !status.is_running() {
+            break Ok(status);
+        }
+    }
+}
+
+// FIXME: make *actually* streaming with router support!
+#[api(
+    input: {
+        properties: {
+            remote: { schema: REMOTE_ID_SCHEMA },
+            upid: { type: RemoteUpid },
+            start: {
+                schema: TASKLOG_START_PARAM_SCHEMA,
+                optional: true,
+            },
+            limit: {
+                schema: TASKLOG_LIMIT_PARAM_SCHEMA,
+                optional: true,
+            },
+            download: {
+                schema: TASKLOG_DOWNLOAD_PARAM_SCHEMA,
+                optional: true,
+            }
+        },
+    },
+    access: {
+        // FIXME: fine-grained task filtering?
+        permission: &Permission::Privilege(&["resource", "{remote}"], PRIV_RESOURCE_AUDIT, false),
+    },
+    returns: {
+        type: Array,
+        items: {
+            type: pbs_client::TaskLogLine,
+        },
+        description: "Array of task log lines",
+    },
+)]
+/// Read a task log.
+async fn read_task_log(
+    remote: String,
+    upid: RemoteUpid,
+    download: Option<bool>,
+    start: Option<u64>,
+    limit: Option<u64>,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Vec<pbs_client::TaskLogLine>, Error> {
+    let (remotes, _) = pdm_config::remotes::config()?;
+
+    crate::api::verify_upid(&remote, RemoteType::Pbs, &upid)?;
+
+    let client = pbs_client::connect_to_remote(&remotes, &remote)?;
+
+    let response = client
+        .get_task_log(upid.upid(), download, limit, start)
+        .await?;
+
+    for (key, value) in response.attribs {
+        rpcenv[&key] = value;
+    }
+
+    Ok(response.data)
+}
-- 
2.47.3





More information about the pdm-devel mailing list