public inbox for pbs-devel@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/6] api: admin: sync: add optional 'all' sync type for listing
Date: Mon, 25 Nov 2024 12:15:33 +0100	[thread overview]
Message-ID: <20241125111537.1504618-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20241125111537.1504618-1-d.csapak@proxmox.com>

so that one can list all sync jobs, both pull and push, at the same
time. To not confuse existing clients that only know of pull syncs, show
only them by default and make the 'all' parameter opt-in. (But add a
todo for 4.x to change that)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/api2/admin/sync.rs               | 66 ++++++++++++++++++++--------
 src/api2/config/datastore.rs         |  9 ++--
 src/api2/config/notifications/mod.rs |  2 +-
 3 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/src/api2/admin/sync.rs b/src/api2/admin/sync.rs
index 479f1a958..2b8fce484 100644
--- a/src/api2/admin/sync.rs
+++ b/src/api2/admin/sync.rs
@@ -1,7 +1,7 @@
 //! Datastore Synchronization Job Management
 
 use anyhow::{bail, format_err, Error};
-use serde::Deserialize;
+use serde::{Deserialize, Serialize};
 use serde_json::Value;
 
 use proxmox_router::{
@@ -23,6 +23,30 @@ use crate::{
     server::sync::do_sync_job,
 };
 
+// FIXME: 4.x make 'all' the default
+#[api()]
+#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "kebab-case")]
+/// The direction of the listed sync jobs: push, pull or all.
+pub enum ListSyncDirection {
+    /// All directions
+    All,
+    /// Sync direction push
+    Push,
+    /// Sync direction pull
+    #[default]
+    Pull,
+}
+
+impl From<SyncDirection> for ListSyncDirection {
+    fn from(value: SyncDirection) -> Self {
+        match value {
+            SyncDirection::Pull => ListSyncDirection::Pull,
+            SyncDirection::Push => ListSyncDirection::Push,
+        }
+    }
+}
+
 #[api(
     input: {
         properties: {
@@ -31,7 +55,7 @@ use crate::{
                 optional: true,
             },
             "sync-direction": {
-                type: SyncDirection,
+                type: ListSyncDirection,
                 optional: true,
             },
         },
@@ -49,7 +73,7 @@ use crate::{
 /// List all configured sync jobs
 pub fn list_config_sync_jobs(
     store: Option<String>,
-    sync_direction: Option<SyncDirection>,
+    sync_direction: Option<ListSyncDirection>,
     _param: Value,
     rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<Vec<SyncJobStatus>, Error> {
@@ -59,23 +83,27 @@ pub fn list_config_sync_jobs(
     let (config, digest) = sync::config()?;
 
     let sync_direction = sync_direction.unwrap_or_default();
-    let job_config_iter = config
-        .convert_to_typed_array(sync_direction.as_config_type_str())?
-        .into_iter()
-        .filter(|job: &SyncJobConfig| {
-            if let Some(store) = &store {
-                &job.store == store
-            } else {
-                true
-            }
-        })
-        .filter(|job: &SyncJobConfig| {
-            check_sync_job_read_access(&user_info, &auth_id, job, sync_direction)
-        });
 
-    let mut list = Vec::new();
+    let mut list = Vec::with_capacity(config.sections.len());
+    for (_, (sync_type, job)) in config.sections.into_iter() {
+        let job: SyncJobConfig = serde_json::from_value(job)?;
+        let direction = SyncDirection::from_config_type_str(&sync_type)?;
+
+        match &store {
+            Some(store) if &job.store != store => continue,
+            _ => {}
+        }
+
+        match &sync_direction {
+            ListSyncDirection::Pull if direction != SyncDirection::Pull => continue,
+            ListSyncDirection::Push if direction != SyncDirection::Push => continue,
+            _ => {}
+        }
+
+        if !check_sync_job_read_access(&user_info, &auth_id, &job, direction) {
+            continue;
+        }
 
-    for job in job_config_iter {
         let last_state = JobState::load("syncjob", &job.id)
             .map_err(|err| format_err!("could not open statefile for {}: {}", &job.id, err))?;
 
@@ -84,7 +112,7 @@ pub fn list_config_sync_jobs(
         list.push(SyncJobStatus {
             config: job,
             status,
-            direction: sync_direction,
+            direction,
         });
     }
 
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 37d1528c7..8c307a233 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -526,9 +526,12 @@ pub async fn delete_datastore(
             delete_verification_job(job.config.id, None, rpcenv)?
         }
         for direction in [SyncDirection::Pull, SyncDirection::Push] {
-            for job in
-                list_config_sync_jobs(Some(name.clone()), Some(direction), Value::Null, rpcenv)?
-            {
+            for job in list_config_sync_jobs(
+                Some(name.clone()),
+                Some(direction.into()),
+                Value::Null,
+                rpcenv,
+            )? {
                 delete_sync_job(job.config.id, None, rpcenv)?
             }
         }
diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs
index f156c8cfd..2081b7b75 100644
--- a/src/api2/config/notifications/mod.rs
+++ b/src/api2/config/notifications/mod.rs
@@ -155,7 +155,7 @@ pub fn get_values(
     }
 
     for direction in [SyncDirection::Pull, SyncDirection::Push] {
-        let sync_jobs = list_config_sync_jobs(None, Some(direction), param.clone(), rpcenv)?;
+        let sync_jobs = list_config_sync_jobs(None, Some(direction.into()), param.clone(), rpcenv)?;
         for job in sync_jobs {
             values.push(MatchableValue {
                 field: "job-id".into(),
-- 
2.39.5



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


  parent reply	other threads:[~2024-11-25 11:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-25 11:15 [pbs-devel] [PATCH proxmox-backup 0/6] sync job ui improvements Dominik Csapak
2024-11-25 11:15 ` [pbs-devel] [PATCH proxmox-backup 1/6] api: admin: sync: add direction to sync job status Dominik Csapak
2024-11-25 11:15 ` Dominik Csapak [this message]
2024-11-25 11:15 ` [pbs-devel] [PATCH proxmox-backup 3/6] cli: manager: sync: add 'sync-direction' parameter to list Dominik Csapak
2024-11-25 11:15 ` [pbs-devel] [PATCH proxmox-backup 4/6] ui: sync jobs: revert to single list for pull/push jobs Dominik Csapak
2024-11-25 11:15 ` [pbs-devel] [PATCH proxmox-backup 5/6] ui: sync jobs: change default sorting to 'store' -> 'direction' -> 'id' Dominik Csapak
2024-11-25 11:15 ` [pbs-devel] [PATCH proxmox-backup 6/6] ui: sync jobs: add search box Dominik Csapak
2024-11-25 11:30 ` [pbs-devel] [PATCH proxmox-backup 0/6] sync job ui improvements Christian Ebner
2024-11-26 15:03 ` [pbs-devel] applied-series: " 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=20241125111537.1504618-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 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