drive config cleanup: derive and use Updater

This commit is contained in:
Dietmar Maurer 2021-08-11 13:56:18 +02:00
parent e4a5c072b4
commit c62a6acb2e
2 changed files with 24 additions and 47 deletions

View File

@ -17,10 +17,8 @@ use crate::{
Authid, Authid,
PROXMOX_CONFIG_DIGEST_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
DRIVE_NAME_SCHEMA, DRIVE_NAME_SCHEMA,
CHANGER_NAME_SCHEMA,
CHANGER_DRIVENUM_SCHEMA,
LTO_DRIVE_PATH_SCHEMA,
LtoTapeDrive, LtoTapeDrive,
LtoTapeDriveUpdater,
ScsiTapeChanger, ScsiTapeChanger,
}, },
tape::{ tape::{
@ -33,19 +31,9 @@ use crate::{
protected: true, protected: true,
input: { input: {
properties: { properties: {
name: { config: {
schema: DRIVE_NAME_SCHEMA, type: LtoTapeDrive,
}, flatten: true,
path: {
schema: LTO_DRIVE_PATH_SCHEMA,
},
changer: {
schema: CHANGER_NAME_SCHEMA,
optional: true,
},
"changer-drivenum": {
schema: CHANGER_DRIVENUM_SCHEMA,
optional: true,
}, },
}, },
}, },
@ -54,32 +42,30 @@ use crate::{
}, },
)] )]
/// Create a new drive /// 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 _lock = config::drive::lock()?;
let (mut config, _digest) = config::drive::config()?; let (mut section_config, _digest) = config::drive::config()?;
let item: LtoTapeDrive = serde_json::from_value(param)?;
let lto_drives = lto_tape_device_list(); let lto_drives = lto_tape_device_list();
check_drive_path(&lto_drives, &item.path)?; check_drive_path(&lto_drives, &config.path)?;
let existing: Vec<LtoTapeDrive> = config.convert_to_typed_array("lto")?; let existing: Vec<LtoTapeDrive> = section_config.convert_to_typed_array("lto")?;
for drive in existing { for drive in existing {
if drive.name == item.name { if drive.name == config.name {
bail!("Entry '{}' already exists", item.name); bail!("Entry '{}' already exists", config.name);
} }
if drive.path == item.path { if drive.path == config.path {
bail!("Path '{}' already used in drive '{}'", item.path, drive.name); 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(&section_config)?;
Ok(()) Ok(())
} }
@ -175,17 +161,9 @@ pub enum DeletableProperty {
name: { name: {
schema: DRIVE_NAME_SCHEMA, schema: DRIVE_NAME_SCHEMA,
}, },
path: { update: {
schema: LTO_DRIVE_PATH_SCHEMA, type: LtoTapeDriveUpdater,
optional: true, flatten: true,
},
changer: {
schema: CHANGER_NAME_SCHEMA,
optional: true,
},
"changer-drivenum": {
schema: CHANGER_DRIVENUM_SCHEMA,
optional: true,
}, },
delete: { delete: {
description: "List of properties to delete.", description: "List of properties to delete.",
@ -208,9 +186,7 @@ pub enum DeletableProperty {
/// Update a drive configuration /// Update a drive configuration
pub fn update_drive( pub fn update_drive(
name: String, name: String,
path: Option<String>, update: LtoTapeDriveUpdater,
changer: Option<String>,
changer_drivenum: Option<u64>,
delete: Option<Vec<DeletableProperty>>, delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
_param: Value, _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(); let lto_drives = lto_tape_device_list();
check_drive_path(&lto_drives, &path)?; check_drive_path(&lto_drives, &path)?;
data.path = path; data.path = path;
} }
if let Some(changer) = changer { if let Some(changer) = update.changer {
let _: ScsiTapeChanger = config.lookup("changer", &changer)?; let _: ScsiTapeChanger = config.lookup("changer", &changer)?;
data.changer = Some(changer); data.changer = Some(changer);
} }
if let Some(changer_drivenum) = changer_drivenum { if let Some(changer_drivenum) = update.changer_drivenum {
if changer_drivenum == 0 { if changer_drivenum == 0 {
data.changer_drivenum = None; data.changer_drivenum = None;
} else { } else {

View File

@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use proxmox::api::{ use proxmox::api::{
api, api,
schema::{Schema, IntegerSchema, StringSchema}, schema::{Schema, IntegerSchema, StringSchema, Updater},
}; };
use crate::api2::types::{ use crate::api2::types::{
@ -69,10 +69,11 @@ pub struct VirtualTapeDrive {
}, },
} }
)] )]
#[derive(Serialize,Deserialize)] #[derive(Serialize,Deserialize,Updater)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
/// Lto SCSI tape driver /// Lto SCSI tape driver
pub struct LtoTapeDrive { pub struct LtoTapeDrive {
#[updater(skip)]
pub name: String, pub name: String,
pub path: String, pub path: String,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]