From c62a6acb2e8581d67b03a3e62a7a76ef96200ecf Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 11 Aug 2021 13:56:18 +0200 Subject: [PATCH] drive config cleanup: derive and use Updater --- src/api2/config/drive.rs | 66 ++++++++++++------------------------ src/api2/types/tape/drive.rs | 5 +-- 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/api2/config/drive.rs b/src/api2/config/drive.rs index e2626e14..db001e4e 100644 --- a/src/api2/config/drive.rs +++ b/src/api2/config/drive.rs @@ -17,10 +17,8 @@ use crate::{ Authid, PROXMOX_CONFIG_DIGEST_SCHEMA, DRIVE_NAME_SCHEMA, - CHANGER_NAME_SCHEMA, - CHANGER_DRIVENUM_SCHEMA, - LTO_DRIVE_PATH_SCHEMA, LtoTapeDrive, + LtoTapeDriveUpdater, ScsiTapeChanger, }, tape::{ @@ -33,19 +31,9 @@ use crate::{ protected: true, input: { properties: { - name: { - schema: DRIVE_NAME_SCHEMA, - }, - path: { - schema: LTO_DRIVE_PATH_SCHEMA, - }, - changer: { - schema: CHANGER_NAME_SCHEMA, - optional: true, - }, - "changer-drivenum": { - schema: CHANGER_DRIVENUM_SCHEMA, - optional: true, + config: { + type: LtoTapeDrive, + flatten: true, }, }, }, @@ -54,32 +42,30 @@ use crate::{ }, )] /// Create a new drive -pub fn create_drive(param: Value) -> Result<(), Error> { +pub fn create_drive(config: LtoTapeDrive) -> Result<(), Error> { let _lock = config::drive::lock()?; - let (mut config, _digest) = config::drive::config()?; - - let item: LtoTapeDrive = serde_json::from_value(param)?; + let (mut section_config, _digest) = config::drive::config()?; let lto_drives = lto_tape_device_list(); - check_drive_path(<o_drives, &item.path)?; + check_drive_path(<o_drives, &config.path)?; - let existing: Vec = config.convert_to_typed_array("lto")?; + let existing: Vec = section_config.convert_to_typed_array("lto")?; for drive in existing { - if drive.name == item.name { - bail!("Entry '{}' already exists", item.name); + if drive.name == config.name { + bail!("Entry '{}' already exists", config.name); } - if drive.path == item.path { - bail!("Path '{}' already used in drive '{}'", item.path, drive.name); + if drive.path == config.path { + bail!("Path '{}' already used in drive '{}'", config.path, drive.name); } } - config.set_data(&item.name, "lto", &item)?; + section_config.set_data(&config.name, "lto", &config)?; - config::drive::save_config(&config)?; + config::drive::save_config(§ion_config)?; Ok(()) } @@ -175,17 +161,9 @@ pub enum DeletableProperty { name: { schema: DRIVE_NAME_SCHEMA, }, - path: { - schema: LTO_DRIVE_PATH_SCHEMA, - optional: true, - }, - changer: { - schema: CHANGER_NAME_SCHEMA, - optional: true, - }, - "changer-drivenum": { - schema: CHANGER_DRIVENUM_SCHEMA, - optional: true, + update: { + type: LtoTapeDriveUpdater, + flatten: true, }, delete: { description: "List of properties to delete.", @@ -208,9 +186,7 @@ pub enum DeletableProperty { /// Update a drive configuration pub fn update_drive( name: String, - path: Option, - changer: Option, - changer_drivenum: Option, + update: LtoTapeDriveUpdater, delete: Option>, digest: Option, _param: Value, @@ -239,18 +215,18 @@ pub fn update_drive( } } - if let Some(path) = path { + if let Some(path) = update.path { let lto_drives = lto_tape_device_list(); check_drive_path(<o_drives, &path)?; data.path = path; } - if let Some(changer) = changer { + if let Some(changer) = update.changer { let _: ScsiTapeChanger = config.lookup("changer", &changer)?; data.changer = Some(changer); } - if let Some(changer_drivenum) = changer_drivenum { + if let Some(changer_drivenum) = update.changer_drivenum { if changer_drivenum == 0 { data.changer_drivenum = None; } else { diff --git a/src/api2/types/tape/drive.rs b/src/api2/types/tape/drive.rs index f50b1d5f..4c6efb63 100644 --- a/src/api2/types/tape/drive.rs +++ b/src/api2/types/tape/drive.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use proxmox::api::{ api, - schema::{Schema, IntegerSchema, StringSchema}, + schema::{Schema, IntegerSchema, StringSchema, Updater}, }; use crate::api2::types::{ @@ -69,10 +69,11 @@ pub struct VirtualTapeDrive { }, } )] -#[derive(Serialize,Deserialize)] +#[derive(Serialize,Deserialize,Updater)] #[serde(rename_all = "kebab-case")] /// Lto SCSI tape driver pub struct LtoTapeDrive { + #[updater(skip)] pub name: String, pub path: String, #[serde(skip_serializing_if="Option::is_none")]