sync job cleanup: use Updater/flatten

This commit is contained in:
Dietmar Maurer 2021-09-08 08:28:09 +02:00
parent 802189f7f5
commit 5bd77f00e2
2 changed files with 26 additions and 83 deletions

View File

@ -353,10 +353,11 @@ pub struct TapeBackupJobStatus {
}, },
} }
)] )]
#[derive(Serialize,Deserialize,Clone)] #[derive(Serialize,Deserialize,Clone,Updater)]
#[serde(rename_all="kebab-case")] #[serde(rename_all="kebab-case")]
/// Sync Job /// Sync Job
pub struct SyncJobConfig { pub struct SyncJobConfig {
#[updater(skip)]
pub id: String, pub id: String,
pub store: String, pub store: String,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]

View File

@ -5,9 +5,7 @@ use ::serde::{Deserialize, Serialize};
use proxmox::api::{api, Permission, Router, RpcEnvironment}; use proxmox::api::{api, Permission, Router, RpcEnvironment};
use pbs_api_types::{ use pbs_api_types::{
Authid, SyncJobConfig, Authid, SyncJobConfig, SyncJobConfigUpdater, JOB_ID_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
SINGLE_LINE_COMMENT_SCHEMA, JOB_ID_SCHEMA, REMOTE_ID_SCHEMA, DATASTORE_SCHEMA,
REMOVE_VANISHED_BACKUPS_SCHEMA, SYNC_SCHEDULE_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
}; };
use pbs_config::sync; use pbs_config::sync;
@ -112,33 +110,9 @@ pub fn list_sync_jobs(
protected: true, protected: true,
input: { input: {
properties: { properties: {
id: { config: {
schema: JOB_ID_SCHEMA, type: SyncJobConfig,
}, flatten: true,
store: {
schema: DATASTORE_SCHEMA,
},
owner: {
type: Authid,
optional: true,
},
remote: {
schema: REMOTE_ID_SCHEMA,
},
"remote-store": {
schema: DATASTORE_SCHEMA,
},
"remove-vanished": {
schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
optional: true,
},
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
schedule: {
optional: true,
schema: SYNC_SCHEDULE_SCHEMA,
}, },
}, },
}, },
@ -149,7 +123,7 @@ pub fn list_sync_jobs(
)] )]
/// Create a new sync job. /// Create a new sync job.
pub fn create_sync_job( pub fn create_sync_job(
param: Value, config: SyncJobConfig,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> { ) -> Result<(), Error> {
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
@ -157,22 +131,21 @@ pub fn create_sync_job(
let _lock = sync::lock_config()?; let _lock = sync::lock_config()?;
let sync_job: SyncJobConfig = serde_json::from_value(param)?; if !check_sync_job_modify_access(&user_info, &auth_id, &config) {
if !check_sync_job_modify_access(&user_info, &auth_id, &sync_job) {
bail!("permission check failed"); bail!("permission check failed");
} }
let (mut config, _digest) = sync::config()?; let (mut section_config, _digest) = sync::config()?;
if config.sections.get(&sync_job.id).is_some() { if section_config.sections.get(&config.id).is_some() {
bail!("job '{}' already exists.", sync_job.id); bail!("job '{}' already exists.", config.id);
} }
config.set_data(&sync_job.id, "sync", &sync_job)?; section_config.set_data(&config.id, "sync", &config)?;
sync::save_config(&config)?; sync::save_config(&section_config)?;
crate::server::jobstate::create_state_file("syncjob", &sync_job.id)?; crate::server::jobstate::create_state_file("syncjob", &config.id)?;
Ok(()) Ok(())
} }
@ -234,33 +207,9 @@ pub enum DeletableProperty {
id: { id: {
schema: JOB_ID_SCHEMA, schema: JOB_ID_SCHEMA,
}, },
store: { update: {
schema: DATASTORE_SCHEMA, type: SyncJobConfigUpdater,
optional: true, flatten: true,
},
owner: {
type: Authid,
optional: true,
},
remote: {
schema: REMOTE_ID_SCHEMA,
optional: true,
},
"remote-store": {
schema: DATASTORE_SCHEMA,
optional: true,
},
"remove-vanished": {
schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
optional: true,
},
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
schedule: {
optional: true,
schema: SYNC_SCHEDULE_SCHEMA,
}, },
delete: { delete: {
description: "List of properties to delete.", description: "List of properties to delete.",
@ -285,13 +234,7 @@ pub enum DeletableProperty {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn update_sync_job( pub fn update_sync_job(
id: String, id: String,
store: Option<String>, update: SyncJobConfigUpdater,
owner: Option<Authid>,
remote: Option<String>,
remote_store: Option<String>,
remove_vanished: Option<bool>,
comment: Option<String>,
schedule: Option<String>,
delete: Option<Vec<DeletableProperty>>, delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
@ -301,7 +244,6 @@ pub fn update_sync_job(
let _lock = sync::lock_config()?; let _lock = sync::lock_config()?;
// pass/compare digest
let (mut config, expected_digest) = sync::config()?; let (mut config, expected_digest) = sync::config()?;
if let Some(ref digest) = digest { if let Some(ref digest) = digest {
@ -322,7 +264,7 @@ pub fn update_sync_job(
} }
} }
if let Some(comment) = comment { if let Some(comment) = update.comment {
let comment = comment.trim().to_string(); let comment = comment.trim().to_string();
if comment.is_empty() { if comment.is_empty() {
data.comment = None; data.comment = None;
@ -331,14 +273,14 @@ pub fn update_sync_job(
} }
} }
if let Some(store) = store { data.store = store; } if let Some(store) = update.store { data.store = store; }
if let Some(remote) = remote { data.remote = remote; } if let Some(remote) = update.remote { data.remote = remote; }
if let Some(remote_store) = remote_store { data.remote_store = remote_store; } if let Some(remote_store) = update.remote_store { data.remote_store = remote_store; }
if let Some(owner) = owner { data.owner = Some(owner); } if let Some(owner) = update.owner { data.owner = Some(owner); }
let schedule_changed = data.schedule != schedule; let schedule_changed = data.schedule != update.schedule;
if schedule.is_some() { data.schedule = schedule; } if update.schedule.is_some() { data.schedule = update.schedule; }
if remove_vanished.is_some() { data.remove_vanished = remove_vanished; } if update.remove_vanished.is_some() { data.remove_vanished = update.remove_vanished; }
if !check_sync_job_modify_access(&user_info, &auth_id, &data) { if !check_sync_job_modify_access(&user_info, &auth_id, &data) {
bail!("permission check failed"); bail!("permission check failed");