From 5af3bcf0624afa99e02503065caf87ff484fb263 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 8 Sep 2021 09:19:35 +0200 Subject: [PATCH] changer config cleanup: use Updater --- pbs-api-types/src/tape/changer.rs | 4 +- src/api2/config/changer.rs | 62 ++++++++++--------------------- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/pbs-api-types/src/tape/changer.rs b/pbs-api-types/src/tape/changer.rs index 24f529df..e36eb32c 100644 --- a/pbs-api-types/src/tape/changer.rs +++ b/pbs-api-types/src/tape/changer.rs @@ -10,6 +10,7 @@ use proxmox::api::{ ArraySchema, IntegerSchema, StringSchema, + Updater, }, }; @@ -62,10 +63,11 @@ Import/Export, i.e. any media in those slots are considered to be }, }, )] -#[derive(Serialize,Deserialize)] +#[derive(Serialize,Deserialize,Updater)] #[serde(rename_all = "kebab-case")] /// SCSI tape changer pub struct ScsiTapeChanger { + #[updater(skip)] pub name: String, pub path: String, #[serde(skip_serializing_if="Option::is_none")] diff --git a/src/api2/config/changer.rs b/src/api2/config/changer.rs index 30baabeb..486313f2 100644 --- a/src/api2/config/changer.rs +++ b/src/api2/config/changer.rs @@ -14,10 +14,9 @@ use pbs_api_types::{ Authid, PROXMOX_CONFIG_DIGEST_SCHEMA, CHANGER_NAME_SCHEMA, - SCSI_CHANGER_PATH_SCHEMA, SLOT_ARRAY_SCHEMA, - EXPORT_SLOT_LIST_SCHEMA, ScsiTapeChanger, + ScsiTapeChangerUpdater, LtoTapeDrive, }; @@ -39,15 +38,9 @@ use crate::{ protected: true, input: { properties: { - name: { - schema: CHANGER_NAME_SCHEMA, - }, - path: { - schema: SCSI_CHANGER_PATH_SCHEMA, - }, - "export-slots": { - schema: EXPORT_SLOT_LIST_SCHEMA, - optional: true, + config: { + type: ScsiTapeChanger, + flatten: true, }, }, }, @@ -56,41 +49,31 @@ use crate::{ }, )] /// Create a new changer device -pub fn create_changer( - name: String, - path: String, - export_slots: Option, -) -> Result<(), Error> { +pub fn create_changer(config: ScsiTapeChanger) -> Result<(), Error> { let _lock = pbs_config::drive::lock()?; - let (mut config, _digest) = pbs_config::drive::config()?; + let (mut section_config, _digest) = pbs_config::drive::config()?; let linux_changers = linux_tape_changer_list(); - check_drive_path(&linux_changers, &path)?; + check_drive_path(&linux_changers, &config.path)?; - let existing: Vec = config.convert_to_typed_array("changer")?; + let existing: Vec = section_config.convert_to_typed_array("changer")?; for changer in existing { - if changer.name == name { - bail!("Entry '{}' already exists", name); + if changer.name == config.name { + bail!("Entry '{}' already exists", config.name); } - if changer.path == path { - bail!("Path '{}' already in use by '{}'", path, changer.name); + if changer.path == config.path { + bail!("Path '{}' already in use by '{}'", config.path, changer.name); } } - let item = ScsiTapeChanger { - name: name.clone(), - path, - export_slots, - }; + section_config.set_data(&config.name, "changer", &config)?; - config.set_data(&name, "changer", &item)?; - - pbs_config::drive::save_config(&config)?; + pbs_config::drive::save_config(§ion_config)?; Ok(()) } @@ -183,13 +166,9 @@ pub enum DeletableProperty { name: { schema: CHANGER_NAME_SCHEMA, }, - path: { - schema: SCSI_CHANGER_PATH_SCHEMA, - optional: true, - }, - "export-slots": { - schema: EXPORT_SLOT_LIST_SCHEMA, - optional: true, + update: { + type: ScsiTapeChangerUpdater, + flatten: true, }, delete: { description: "List of properties to delete.", @@ -212,8 +191,7 @@ pub enum DeletableProperty { /// Update a tape changer configuration pub fn update_changer( name: String, - path: Option, - export_slots: Option, + update: ScsiTapeChangerUpdater, delete: Option>, digest: Option, _param: Value, @@ -240,13 +218,13 @@ pub fn update_changer( } } - if let Some(path) = path { + if let Some(path) = update.path { let changers = linux_tape_changer_list(); check_drive_path(&changers, &path)?; data.path = path; } - if let Some(export_slots) = export_slots { + if let Some(export_slots) = update.export_slots { let slots: Value = parse_property_string( &export_slots, &SLOT_ARRAY_SCHEMA )?;