From 68e77657e613d30355d3e62519e15b334e5a4a32 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 23 Jul 2021 11:15:31 +0200 Subject: [PATCH] datastore config: cleanup code (use flatten attribute) --- src/api2/config/datastore.rs | 79 ++++----------------- src/bin/proxmox_backup_manager/datastore.rs | 54 ++------------ src/config/datastore.rs | 3 +- 3 files changed, 19 insertions(+), 117 deletions(-) diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs index a55e3bdc..7edc43d8 100644 --- a/src/api2/config/datastore.rs +++ b/src/api2/config/datastore.rs @@ -7,7 +7,6 @@ use ::serde::{Deserialize, Serialize}; use proxmox::api::{api, Router, RpcEnvironment, Permission}; use proxmox::api::section_config::SectionConfigData; use proxmox::api::schema::parse_property_string; - use pbs_datastore::task::TaskState; use crate::api2::config::sync::delete_sync_job; @@ -20,7 +19,7 @@ use crate::api2::admin::{ use crate::api2::types::*; use crate::backup::*; use crate::config::cached_user_info::CachedUserInfo; -use crate::config::datastore::{self, DataStoreConfig, DIR_NAME_SCHEMA}; +use crate::config::datastore::{self, DataStoreConfig}; use crate::config::acl::{PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY}; use crate::server::{jobstate, WorkerTask}; @@ -31,7 +30,7 @@ use crate::server::{jobstate, WorkerTask}; returns: { description: "List the configured datastores (with config digest).", type: Array, - items: { type: datastore::DataStoreConfig }, + items: { type: DataStoreConfig }, }, access: { permission: &Permission::Anybody, @@ -80,63 +79,13 @@ pub(crate) fn do_create_datastore( Ok(()) } -// fixme: impl. const fn get_object_schema(datastore::DataStoreConfig::API_SCHEMA), -// but this need support for match inside const fn -// see: https://github.com/rust-lang/rust/issues/49146 - #[api( protected: true, input: { properties: { - name: { - schema: DATASTORE_SCHEMA, - }, - path: { - schema: DIR_NAME_SCHEMA, - }, - comment: { - optional: true, - schema: SINGLE_LINE_COMMENT_SCHEMA, - }, - "notify-user": { - optional: true, - type: Userid, - }, - "notify": { - optional: true, - schema: DATASTORE_NOTIFY_STRING_SCHEMA, - }, - "gc-schedule": { - optional: true, - schema: GC_SCHEDULE_SCHEMA, - }, - "prune-schedule": { - optional: true, - schema: PRUNE_SCHEDULE_SCHEMA, - }, - "keep-last": { - optional: true, - schema: PRUNE_SCHEMA_KEEP_LAST, - }, - "keep-hourly": { - optional: true, - schema: PRUNE_SCHEMA_KEEP_HOURLY, - }, - "keep-daily": { - optional: true, - schema: PRUNE_SCHEMA_KEEP_DAILY, - }, - "keep-weekly": { - optional: true, - schema: PRUNE_SCHEMA_KEEP_WEEKLY, - }, - "keep-monthly": { - optional: true, - schema: PRUNE_SCHEMA_KEEP_MONTHLY, - }, - "keep-yearly": { - optional: true, - schema: PRUNE_SCHEMA_KEEP_YEARLY, + config: { + type: DataStoreConfig, + flatten: true, }, }, }, @@ -146,28 +95,26 @@ pub(crate) fn do_create_datastore( )] /// Create new datastore config. pub fn create_datastore( - param: Value, + config: DataStoreConfig, rpcenv: &mut dyn RpcEnvironment, ) -> Result { let lock = datastore::lock_config()?; - let datastore: datastore::DataStoreConfig = serde_json::from_value(param)?; + let (section_config, _digest) = datastore::config()?; - let (config, _digest) = datastore::config()?; - - if config.sections.get(&datastore.name).is_some() { - bail!("datastore '{}' already exists.", datastore.name); + if section_config.sections.get(&config.name).is_some() { + bail!("datastore '{}' already exists.", config.name); } let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; WorkerTask::new_thread( "create-datastore", - Some(datastore.name.to_string()), + Some(config.name.to_string()), auth_id, false, - move |worker| do_create_datastore(lock, config, datastore, Some(&worker)), + move |worker| do_create_datastore(lock, section_config, config, Some(&worker)), ) } @@ -179,7 +126,7 @@ pub fn create_datastore( }, }, }, - returns: { type: datastore::DataStoreConfig }, + returns: { type: DataStoreConfig }, access: { permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_AUDIT, false), }, @@ -334,7 +281,7 @@ pub fn update_datastore( crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; } - let mut data: datastore::DataStoreConfig = config.lookup("datastore", &name)?; + let mut data: DataStoreConfig = config.lookup("datastore", &name)?; if let Some(delete) = delete { for delete_prop in delete { diff --git a/src/bin/proxmox_backup_manager/datastore.rs b/src/bin/proxmox_backup_manager/datastore.rs index a2436786..07d6111f 100644 --- a/src/bin/proxmox_backup_manager/datastore.rs +++ b/src/bin/proxmox_backup_manager/datastore.rs @@ -7,7 +7,7 @@ use pbs_client::{connect_to_localhost, view_task_result}; use proxmox_backup::config; use proxmox_backup::api2::{self, types::* }; -use proxmox_backup::config::datastore::DIR_NAME_SCHEMA; +use proxmox_backup::config::datastore::DataStoreConfig; #[api( input: { @@ -74,55 +74,9 @@ fn show_datastore(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result