public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH datacenter-manager 4/7] api: pbs tasks: add PBS task API
Date: Wed, 12 Nov 2025 10:42:00 +0100	[thread overview]
Message-ID: <20251112094203.112452-5-l.wagner@proxmox.com> (raw)
In-Reply-To: <20251112094203.112452-1-l.wagner@proxmox.com>

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

Signed-off-by: Lukas Wagner <l.wagner@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



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  parent reply	other threads:[~2025-11-12  9:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-12  9:41 [pdm-devel] [PATCH datacenter-manager 0/7] PBS remotes: task API and task cache support Lukas Wagner
2025-11-12  9:41 ` [pdm-devel] [PATCH datacenter-manager 1/7] pdm-api-types: remote upid: add helpers for getting native UPID type Lukas Wagner
2025-11-12  9:41 ` [pdm-devel] [PATCH datacenter-manager 2/7] pbs-client: add bindings for task list, task status, task log Lukas Wagner
2025-11-12 20:23   ` Thomas Lamprecht
2025-11-12  9:41 ` [pdm-devel] [PATCH datacenter-manager 3/7] pdm-api-types: api: factor out schema definitions for task log params Lukas Wagner
2025-11-12  9:42 ` Lukas Wagner [this message]
2025-11-12  9:42 ` [pdm-devel] [PATCH datacenter-manager 5/7] api: pve tasks: use shared helpers for RemoteUpid handling Lukas Wagner
2025-11-12  9:42 ` [pdm-devel] [PATCH datacenter-manager 6/7] remote tasks: fetch/track PBS tasks Lukas Wagner
2025-11-12  9:42 ` [pdm-devel] [PATCH datacenter-manager 7/7] remote updates: re-enable PBS update fetching Lukas Wagner
2025-11-12 20:26   ` Thomas Lamprecht
2025-11-12 20:27 ` [pdm-devel] applied-series: [PATCH datacenter-manager 0/7] PBS remotes: task API and task cache support Thomas Lamprecht

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=20251112094203.112452-5-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pdm-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal