From a3c709ef21adcf74aa8fe94c2d1d6123380e1c9b Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 10 Dec 2020 08:35:11 +0100 Subject: [PATCH] tape: cli cleanup - avoid api redefinition --- src/api2/tape/drive.rs | 12 +-- src/bin/proxmox_tape/changer.rs | 130 +----------------------- src/bin/proxmox_tape/drive.rs | 170 ++------------------------------ 3 files changed, 19 insertions(+), 293 deletions(-) diff --git a/src/api2/tape/drive.rs b/src/api2/tape/drive.rs index be85373b..ef3e3c35 100644 --- a/src/api2/tape/drive.rs +++ b/src/api2/tape/drive.rs @@ -86,7 +86,7 @@ pub fn load_media(drive: String, changer_id: String) -> Result<(), Error> { #[api( input: { properties: { - name: { + drive: { schema: DRIVE_ID_SCHEMA, }, slot: { @@ -99,18 +99,18 @@ pub fn load_media(drive: String, changer_id: String) -> Result<(), Error> { )] /// Unload media via changer pub fn unload( - name: String, + drive: String, slot: Option, _param: Value, ) -> Result<(), Error> { let (config, _digest) = config::drive::config()?; - let mut drive: LinuxTapeDrive = config.lookup("linux", &name)?; + let mut drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?; - let changer: ScsiTapeChanger = match drive.changer { + let changer: ScsiTapeChanger = match drive_config.changer { Some(ref changer) => config.lookup("changer", changer)?, - None => bail!("drive '{}' has no associated changer", name), + None => bail!("drive '{}' has no associated changer", drive), }; let drivenum: u64 = 0; @@ -118,7 +118,7 @@ pub fn unload( if let Some(slot) = slot { mtx_unload(&changer.path, slot, drivenum) } else { - drive.unload_media() + drive_config.unload_media() } } diff --git a/src/bin/proxmox_tape/changer.rs b/src/bin/proxmox_tape/changer.rs index 02c93a8c..c218c0f7 100644 --- a/src/bin/proxmox_tape/changer.rs +++ b/src/bin/proxmox_tape/changer.rs @@ -15,15 +15,12 @@ use proxmox_backup::{ self, types::{ CHANGER_ID_SCHEMA, - LINUX_DRIVE_PATH_SCHEMA, - ScsiTapeChanger, }, }, tape::{ complete_changer_path, }, config::{ - self, drive::{ complete_drive_name, complete_changer_name, @@ -43,20 +40,20 @@ pub fn changer_commands() -> CommandLineInterface { ) .insert( "remove", - CliCommand::new(&API_METHOD_DELETE_CHANGER) + CliCommand::new(&api2::config::changer::API_METHOD_DELETE_CHANGER) .arg_param(&["name"]) .completion_cb("name", complete_changer_name) ) .insert( "create", - CliCommand::new(&API_METHOD_CREATE_CHANGER) + CliCommand::new(&api2::config::changer::API_METHOD_CREATE_CHANGER) .arg_param(&["name"]) .completion_cb("name", complete_drive_name) .completion_cb("path", complete_changer_path) ) .insert( "update", - CliCommand::new(&API_METHOD_UPDATE_CHANGER) + CliCommand::new(&api2::config::changer::API_METHOD_UPDATE_CHANGER) .arg_param(&["name"]) .completion_cb("name", complete_changer_name) .completion_cb("path", complete_changer_path) @@ -67,7 +64,7 @@ pub fn changer_commands() -> CommandLineInterface { .completion_cb("name", complete_changer_name) ) .insert("transfer", - CliCommand::new(&API_METHOD_TRANSFER) + CliCommand::new(&api2::tape::changer::API_METHOD_TRANSFER) .arg_param(&["name"]) .completion_cb("name", complete_changer_name) ) @@ -76,33 +73,6 @@ pub fn changer_commands() -> CommandLineInterface { cmd_def.into() } -#[api( - input: { - properties: { - name: { - schema: CHANGER_ID_SCHEMA, - }, - path: { - schema: LINUX_DRIVE_PATH_SCHEMA, - }, - }, - }, -)] -/// Create a new changer device -fn create_changer( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::config::changer::API_METHOD_CREATE_CHANGER; - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - #[api( input: { properties: { @@ -210,61 +180,6 @@ fn get_config( Ok(()) } -#[api( - input: { - properties: { - name: { - schema: CHANGER_ID_SCHEMA, - }, - }, - }, -)] -/// Delete a tape changer configuration -fn delete_changer( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::config::changer::API_METHOD_DELETE_CHANGER; - - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - -#[api( - input: { - properties: { - name: { - schema: CHANGER_ID_SCHEMA, - }, - path: { - schema: LINUX_DRIVE_PATH_SCHEMA, - optional: true, - }, - }, - }, -)] -/// Update a tape changer configuration -fn update_changer( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::config::changer::API_METHOD_UPDATE_CHANGER; - - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - - #[api( input: { properties: { @@ -302,40 +217,3 @@ fn get_status( Ok(()) } - -#[api( - input: { - properties: { - name: { - schema: CHANGER_ID_SCHEMA, - }, - from: { - description: "Source slot number", - minimum: 1, - }, - to: { - description: "Destination slot number", - minimum: 1, - }, - }, - }, -)] -/// Transfers media from one slot to another -fn transfer( - name: String, - from: u64, - to: u64, - _param: Value, -) -> Result<(), Error> { - - let (config, _digest) = config::drive::config()?; - - let data: ScsiTapeChanger = config.lookup("changer", &name)?; - - let mut command = std::process::Command::new("mtx"); - command.args(&["-f", &data.path, "transfer", &from.to_string(), &to.to_string()]); - - proxmox_backup::tools::run_command(command, None)?; - - Ok(()) -} diff --git a/src/bin/proxmox_tape/drive.rs b/src/bin/proxmox_tape/drive.rs index fb06afac..8442c35d 100644 --- a/src/bin/proxmox_tape/drive.rs +++ b/src/bin/proxmox_tape/drive.rs @@ -15,8 +15,6 @@ use proxmox_backup::{ self, types::{ DRIVE_ID_SCHEMA, - CHANGER_ID_SCHEMA, - LINUX_DRIVE_PATH_SCHEMA, }, }, tape::{ @@ -41,13 +39,13 @@ pub fn drive_commands() -> CommandLineInterface { ) .insert( "remove", - CliCommand::new(&API_METHOD_DELETE_DRIVE) + CliCommand::new(&api2::config::drive::API_METHOD_DELETE_DRIVE) .arg_param(&["name"]) .completion_cb("name", complete_linux_drive_name) ) .insert( "create", - CliCommand::new(&API_METHOD_CREATE_LINUX_DRIVE) + CliCommand::new(&api2::config::drive::API_METHOD_CREATE_DRIVE) .arg_param(&["name"]) .completion_cb("name", complete_drive_name) .completion_cb("path", complete_drive_path) @@ -55,7 +53,7 @@ pub fn drive_commands() -> CommandLineInterface { ) .insert( "update", - CliCommand::new(&API_METHOD_UPDATE_LINUX_DRIVE) + CliCommand::new(&api2::config::drive::API_METHOD_UPDATE_DRIVE) .arg_param(&["name"]) .completion_cb("name", complete_linux_drive_name) .completion_cb("path", complete_drive_path) @@ -63,52 +61,21 @@ pub fn drive_commands() -> CommandLineInterface { ) .insert( "load", - CliCommand::new(&API_METHOD_LOAD_SLOT) - .arg_param(&["name"]) - .completion_cb("name", complete_linux_drive_name) + CliCommand::new(&api2::tape::drive::API_METHOD_LOAD_SLOT) + .arg_param(&["drive"]) + .completion_cb("drive", complete_linux_drive_name) ) .insert( "unload", - CliCommand::new(&API_METHOD_UNLOAD) - .arg_param(&["name"]) - .completion_cb("name", complete_linux_drive_name) + CliCommand::new(&api2::tape::drive::API_METHOD_UNLOAD) + .arg_param(&["drive"]) + .completion_cb("drive", complete_linux_drive_name) ) ; cmd_def.into() } -#[api( - input: { - properties: { - name: { - schema: DRIVE_ID_SCHEMA, - }, - path: { - schema: LINUX_DRIVE_PATH_SCHEMA, - }, - changer: { - schema: CHANGER_ID_SCHEMA, - optional: true, - }, - }, - }, -)] -/// Create a new drive -fn create_linux_drive( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::config::drive::API_METHOD_CREATE_DRIVE; - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - #[api( input: { properties: { @@ -218,122 +185,3 @@ fn get_config( Ok(()) } - -#[api( - input: { - properties: { - name: { - schema: DRIVE_ID_SCHEMA, - }, - }, - }, -)] -/// Delete a drive configuration -fn delete_drive( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::config::drive::API_METHOD_DELETE_DRIVE; - - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - -#[api( - input: { - properties: { - name: { - schema: DRIVE_ID_SCHEMA, - }, - path: { - schema: LINUX_DRIVE_PATH_SCHEMA, - optional: true, - }, - changer: { - schema: CHANGER_ID_SCHEMA, - optional: true, - }, - }, - }, -)] -/// Update a drive configuration -fn update_linux_drive( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::config::drive::API_METHOD_UPDATE_DRIVE; - - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - -#[api( - input: { - properties: { - drive: { - schema: DRIVE_ID_SCHEMA, - }, - slot: { - type: u64, - description: "Source slot number", - minimum: 1, - }, - }, - }, -)] -/// Load media via changer from slot -fn load_slot( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::tape::drive::API_METHOD_LOAD_SLOT; - - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -} - -#[api( - input: { - properties: { - name: { - schema: DRIVE_ID_SCHEMA, - }, - slot: { - description: "Target slot number. If omitted, defaults to the slot that the drive was loaded from.", - type: u64, - minimum: 1, - optional: true, - }, - }, - }, -)] -/// Unload media via changer -fn unload( - param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { - - let info = &api2::tape::drive::API_METHOD_UNLOAD; - - match info.handler { - ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, - _ => unreachable!(), - }; - - Ok(()) -}