tape: proxmox-tape - use API instead of direct functions calls

This commit is contained in:
Dietmar Maurer 2021-01-29 11:49:11 +01:00
parent e68269fcaf
commit c297835b01
2 changed files with 81 additions and 84 deletions

View File

@ -1180,12 +1180,12 @@ pub const SUBDIRS: SubdirMap = &sorted!([
( (
"barcode-label-media", "barcode-label-media",
&Router::new() &Router::new()
.put(&API_METHOD_BARCODE_LABEL_MEDIA) .post(&API_METHOD_BARCODE_LABEL_MEDIA)
), ),
( (
"catalog", "catalog",
&Router::new() &Router::new()
.put(&API_METHOD_CATALOG_MEDIA) .post(&API_METHOD_CATALOG_MEDIA)
), ),
( (
"clean", "clean",

View File

@ -5,7 +5,6 @@ use proxmox::{
api::{ api::{
api, api,
cli::*, cli::*,
ApiHandler,
RpcEnvironment, RpcEnvironment,
section_config::SectionConfigData, section_config::SectionConfigData,
}, },
@ -24,7 +23,6 @@ use proxmox_backup::{
client::{ client::{
connect_to_localhost, connect_to_localhost,
view_task_result, view_task_result,
wait_for_local_worker,
}, },
api2::{ api2::{
self, self,
@ -478,27 +476,28 @@ async fn inventory(
schema: DRIVE_NAME_SCHEMA, schema: DRIVE_NAME_SCHEMA,
optional: true, optional: true,
}, },
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}, },
}, },
)] )]
/// Label media with barcodes from changer device /// Label media with barcodes from changer device
async fn barcode_label_media( async fn barcode_label_media(param: Value) -> Result<(), Error> {
mut param: Value,
rpcenv: &mut dyn RpcEnvironment, let output_format = get_output_format(&param);
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into(); let drive = lookup_drive_name(&param, &config)?;
let info = &api2::tape::drive::API_METHOD_BARCODE_LABEL_MEDIA; let mut client = connect_to_localhost()?;
let result = match info.handler { let path = format!("api2/json/tape/drive/{}/barcode-label-media", drive);
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, let result = client.post(&path, Some(param)).await?;
_ => unreachable!(),
};
wait_for_local_worker(result.as_str().unwrap()).await?; view_task_result(&mut client, result, &output_format).await?;
Ok(()) Ok(())
} }
@ -594,31 +593,30 @@ fn debug_scan(param: Value) -> Result<(), Error> {
schema: DRIVE_NAME_SCHEMA, schema: DRIVE_NAME_SCHEMA,
optional: true, optional: true,
}, },
"output-format": { "output-format": {
schema: OUTPUT_FORMAT, schema: OUTPUT_FORMAT,
optional: true, optional: true,
}, },
}, },
}, },
)] )]
/// Read Cartridge Memory (Medium auxiliary memory attributes) /// Read Cartridge Memory (Medium auxiliary memory attributes)
fn cartridge_memory( async fn cartridge_memory(param: Value) -> Result<(), Error> {
mut param: Value,
rpcenv: &mut dyn RpcEnvironment, let output_format = get_output_format(&param);
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into(); let drive = lookup_drive_name(&param, &config)?;
let client = connect_to_localhost()?;
let path = format!("api2/json/tape/drive/{}/cartridge-memory", drive);
let mut result = client.get(&path, Some(param)).await?;
let mut data = result["data"].take();
let output_format = get_output_format(&param);
let info = &api2::tape::drive::API_METHOD_CARTRIDGE_MEMORY; let info = &api2::tape::drive::API_METHOD_CARTRIDGE_MEMORY;
let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
let options = default_table_format_options() let options = default_table_format_options()
.column(ColumnConfig::new("id")) .column(ColumnConfig::new("id"))
.column(ColumnConfig::new("name")) .column(ColumnConfig::new("name"))
@ -636,34 +634,34 @@ fn cartridge_memory(
schema: DRIVE_NAME_SCHEMA, schema: DRIVE_NAME_SCHEMA,
optional: true, optional: true,
}, },
"output-format": { "output-format": {
schema: OUTPUT_FORMAT, schema: OUTPUT_FORMAT,
optional: true, optional: true,
}, },
}, },
}, },
)] )]
/// Read Volume Statistics (SCSI log page 17h) /// Read Volume Statistics (SCSI log page 17h)
fn volume_statistics( async fn volume_statistics(param: Value) -> Result<(), Error> {
mut param: Value,
rpcenv: &mut dyn RpcEnvironment, let output_format = get_output_format(&param);
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into(); let drive = lookup_drive_name(&param, &config)?;
let client = connect_to_localhost()?;
let path = format!("api2/json/tape/drive/{}/volume-statistics", drive);
let mut result = client.get(&path, Some(param)).await?;
let mut data = result["data"].take();
let output_format = get_output_format(&param);
let info = &api2::tape::drive::API_METHOD_VOLUME_STATISTICS; let info = &api2::tape::drive::API_METHOD_VOLUME_STATISTICS;
let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
let options = default_table_format_options(); let options = default_table_format_options();
format_and_print_result_full(&mut data, &info.returns, &output_format, &options); format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(()) Ok(())
} }
@ -682,23 +680,22 @@ fn volume_statistics(
}, },
)] )]
/// Get drive/media status /// Get drive/media status
fn status( async fn status(param: Value) -> Result<(), Error> {
mut param: Value,
rpcenv: &mut dyn RpcEnvironment, let output_format = get_output_format(&param);
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into(); let drive = lookup_drive_name(&param, &config)?;
let client = connect_to_localhost()?;
let path = format!("api2/json/tape/drive/{}/status", drive);
let mut result = client.get(&path, Some(param)).await?;
let mut data = result["data"].take();
let output_format = get_output_format(&param);
let info = &api2::tape::drive::API_METHOD_STATUS; let info = &api2::tape::drive::API_METHOD_STATUS;
let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
let render_percentage = |value: &Value, _record: &Value| { let render_percentage = |value: &Value, _record: &Value| {
match value.as_f64() { match value.as_f64() {
Some(wearout) => Ok(format!("{:.2}%", wearout*100.0)), Some(wearout) => Ok(format!("{:.2}%", wearout*100.0)),
@ -722,6 +719,7 @@ fn status(
; ;
format_and_print_result_full(&mut data, &info.returns, &output_format, &options); format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(()) Ok(())
} }
@ -732,27 +730,28 @@ fn status(
schema: DRIVE_NAME_SCHEMA, schema: DRIVE_NAME_SCHEMA,
optional: true, optional: true,
}, },
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}, },
}, },
)] )]
/// Clean drive /// Clean drive
async fn clean_drive( async fn clean_drive(param: Value) -> Result<(), Error> {
mut param: Value,
rpcenv: &mut dyn RpcEnvironment, let output_format = get_output_format(&param);
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into(); let drive = lookup_drive_name(&param, &config)?;
let info = &api2::tape::drive::API_METHOD_CLEAN_DRIVE; let mut client = connect_to_localhost()?;
let result = match info.handler { let path = format!("api2/json/tape/drive/{}/clean-drive", drive);
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, let result = client.post(&path, Some(param)).await?;
_ => unreachable!(),
};
wait_for_local_worker(result.as_str().unwrap()).await?; view_task_result(&mut client, result, &output_format).await?;
Ok(()) Ok(())
} }
@ -796,6 +795,7 @@ async fn backup(param: Value) -> Result<(), Error> {
Ok(()) Ok(())
} }
#[api( #[api(
input: { input: {
properties: { properties: {
@ -806,23 +806,23 @@ async fn backup(param: Value) -> Result<(), Error> {
description: "Media set UUID.", description: "Media set UUID.",
type: String, type: String,
}, },
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}, },
}, },
)] )]
/// Restore data from media-set /// Restore data from media-set
async fn restore( async fn restore(param: Value) -> Result<(), Error> {
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
let info = &api2::tape::restore::API_METHOD_RESTORE; let output_format = get_output_format(&param);
let result = match info.handler { let mut client = connect_to_localhost()?;
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
wait_for_local_worker(result.as_str().unwrap()).await?; let result = client.post("api2/json/tape/restore", Some(param)).await?;
view_task_result(&mut client, result, &output_format).await?;
Ok(()) Ok(())
} }
@ -852,23 +852,20 @@ async fn restore(
}, },
)] )]
/// Scan media and record content /// Scan media and record content
async fn catalog_media( async fn catalog_media(param: Value) -> Result<(), Error> {
mut param: Value,
rpcenv: &mut dyn RpcEnvironment, let output_format = get_output_format(&param);
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into(); let drive = lookup_drive_name(&param, &config)?;
let info = &api2::tape::drive::API_METHOD_CATALOG_MEDIA; let mut client = connect_to_localhost()?;
let result = match info.handler { let path = format!("api2/json/tape/drive/{}/catalog", drive);
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, let result = client.post(&path, Some(param)).await?;
_ => unreachable!(),
};
wait_for_local_worker(result.as_str().unwrap()).await?; view_task_result(&mut client, result, &output_format).await?;
Ok(()) Ok(())
} }