[pbs-devel] [PATCH proxmox-backup 2/6] api: admin: sync: add optional 'all' sync type for listing
Dominik Csapak
d.csapak at proxmox.com
Mon Nov 25 12:15:33 CET 2024
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 at 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
More information about the pbs-devel
mailing list