verify job cleanup: use Updater/flatten

This commit is contained in:
Dietmar Maurer 2021-09-08 08:40:32 +02:00
parent 5bd77f00e2
commit ffa403b5fd
2 changed files with 26 additions and 67 deletions

View File

@ -175,11 +175,12 @@ pub const VERIFICATION_OUTDATED_AFTER_SCHEMA: Schema = IntegerSchema::new(
}, },
} }
)] )]
#[derive(Serialize,Deserialize)] #[derive(Serialize,Deserialize,Updater)]
#[serde(rename_all="kebab-case")] #[serde(rename_all="kebab-case")]
/// Verification Job /// Verification Job
pub struct VerificationJobConfig { pub struct VerificationJobConfig {
/// unique ID to address this job /// unique ID to address this job
#[updater(skip)]
pub id: String, pub id: String,
/// the datastore ID this verificaiton job affects /// the datastore ID this verificaiton job affects
pub store: String, pub store: String,

View File

@ -5,10 +5,8 @@ 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, VerificationJobConfig, Authid, VerificationJobConfig, VerificationJobConfigUpdater, JOB_ID_SCHEMA,
SINGLE_LINE_COMMENT_SCHEMA, JOB_ID_SCHEMA, IGNORE_VERIFIED_BACKUPS_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
VERIFICATION_OUTDATED_AFTER_SCHEMA, VERIFICATION_SCHEDULE_SCHEMA,
DATASTORE_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
}; };
use pbs_config::verify; use pbs_config::verify;
@ -64,29 +62,11 @@ pub fn list_verification_jobs(
protected: true, protected: true,
input: { input: {
properties: { properties: {
id: { config: {
schema: JOB_ID_SCHEMA, type: VerificationJobConfig,
flatten: true,
}, },
store: {
schema: DATASTORE_SCHEMA,
}, },
"ignore-verified": {
optional: true,
schema: IGNORE_VERIFIED_BACKUPS_SCHEMA,
},
"outdated-after": {
optional: true,
schema: VERIFICATION_OUTDATED_AFTER_SCHEMA,
},
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
schedule: {
optional: true,
schema: VERIFICATION_SCHEDULE_SCHEMA,
},
}
}, },
access: { access: {
permission: &Permission::Anybody, permission: &Permission::Anybody,
@ -95,29 +75,27 @@ pub fn list_verification_jobs(
)] )]
/// Create a new verification job. /// Create a new verification job.
pub fn create_verification_job( pub fn create_verification_job(
param: Value, config: VerificationJobConfig,
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()?;
let user_info = CachedUserInfo::new()?; let user_info = CachedUserInfo::new()?;
let verification_job: VerificationJobConfig = serde_json::from_value(param)?; user_info.check_privs(&auth_id, &["datastore", &config.store], PRIV_DATASTORE_VERIFY, false)?;
user_info.check_privs(&auth_id, &["datastore", &verification_job.store], PRIV_DATASTORE_VERIFY, false)?;
let _lock = verify::lock_config()?; let _lock = verify::lock_config()?;
let (mut config, _digest) = verify::config()?; let (mut section_config, _digest) = verify::config()?;
if config.sections.get(&verification_job.id).is_some() { if section_config.sections.get(&config.id).is_some() {
bail!("job '{}' already exists.", verification_job.id); bail!("job '{}' already exists.", config.id);
} }
config.set_data(&verification_job.id, "verification", &verification_job)?; section_config.set_data(&config.id, "verification", &config)?;
verify::save_config(&config)?; verify::save_config(&section_config)?;
crate::server::jobstate::create_state_file("verificationjob", &verification_job.id)?; crate::server::jobstate::create_state_file("verificationjob", &config.id)?;
Ok(()) Ok(())
} }
@ -178,25 +156,9 @@ pub enum DeletableProperty {
id: { id: {
schema: JOB_ID_SCHEMA, schema: JOB_ID_SCHEMA,
}, },
store: { update: {
optional: true, type: VerificationJobConfigUpdater,
schema: DATASTORE_SCHEMA, flatten: true,
},
"ignore-verified": {
optional: true,
schema: IGNORE_VERIFIED_BACKUPS_SCHEMA,
},
"outdated-after": {
optional: true,
schema: VERIFICATION_OUTDATED_AFTER_SCHEMA,
},
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
schedule: {
optional: true,
schema: VERIFICATION_SCHEDULE_SCHEMA,
}, },
delete: { delete: {
description: "List of properties to delete.", description: "List of properties to delete.",
@ -221,11 +183,7 @@ pub enum DeletableProperty {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn update_verification_job( pub fn update_verification_job(
id: String, id: String,
store: Option<String>, update: VerificationJobConfigUpdater,
ignore_verified: Option<bool>,
outdated_after: Option<i64>,
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,
@ -259,7 +217,7 @@ pub fn update_verification_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;
@ -268,17 +226,17 @@ pub fn update_verification_job(
} }
} }
if let Some(store) = store { if let Some(store) = update.store {
// check new store // check new store
user_info.check_privs(&auth_id, &["datastore", &store], PRIV_DATASTORE_VERIFY, true)?; user_info.check_privs(&auth_id, &["datastore", &store], PRIV_DATASTORE_VERIFY, true)?;
data.store = store; data.store = store;
} }
if ignore_verified.is_some() { data.ignore_verified = ignore_verified; } if update.ignore_verified.is_some() { data.ignore_verified = update.ignore_verified; }
if outdated_after.is_some() { data.outdated_after = outdated_after; } if update.outdated_after.is_some() { data.outdated_after = update.outdated_after; }
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; }
config.set_data(&id, "verification", &data)?; config.set_data(&id, "verification", &data)?;