From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 779787A93F for ; Tue, 5 Jul 2022 15:09:47 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CFFCD2F55F for ; Tue, 5 Jul 2022 15:08:58 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 5 Jul 2022 15:08:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 8018D4046A for ; Tue, 5 Jul 2022 15:08:50 +0200 (CEST) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Tue, 5 Jul 2022 13:08:32 +0000 Message-Id: <20220705130834.14285-27-h.laimer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220705130834.14285-1-h.laimer@proxmox.com> References: <20220705130834.14285-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.041 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [mod.rs, proxmox-backup-manager.rs] Subject: [pbs-devel] [PATCH proxmox-backup 24/26] cli: manager: add removable-device commands X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jul 2022 13:09:47 -0000 Signed-off-by: Hannes Laimer --- src/bin/proxmox-backup-manager.rs | 1 + src/bin/proxmox_backup_manager/mod.rs | 2 + .../removable_device.rs | 209 ++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 src/bin/proxmox_backup_manager/removable_device.rs diff --git a/src/bin/proxmox-backup-manager.rs b/src/bin/proxmox-backup-manager.rs index 0ca3e990..fba02019 100644 --- a/src/bin/proxmox-backup-manager.rs +++ b/src/bin/proxmox-backup-manager.rs @@ -425,6 +425,7 @@ async fn run() -> Result<(), Error> { .insert("user", user_commands()) .insert("openid", openid_commands()) .insert("remote", remote_commands()) + .insert("removable-device", removable_device_commands()) .insert("traffic-control", traffic_control_commands()) .insert("garbage-collection", garbage_collection_commands()) .insert("acme", acme_mgmt_cli()) diff --git a/src/bin/proxmox_backup_manager/mod.rs b/src/bin/proxmox_backup_manager/mod.rs index 9788f637..888caf01 100644 --- a/src/bin/proxmox_backup_manager/mod.rs +++ b/src/bin/proxmox_backup_manager/mod.rs @@ -14,6 +14,8 @@ mod prune; pub use prune::*; mod remote; pub use remote::*; +mod removable_device; +pub use removable_device::*; mod sync; pub use sync::*; mod verify; diff --git a/src/bin/proxmox_backup_manager/removable_device.rs b/src/bin/proxmox_backup_manager/removable_device.rs new file mode 100644 index 00000000..383840b2 --- /dev/null +++ b/src/bin/proxmox_backup_manager/removable_device.rs @@ -0,0 +1,209 @@ +use anyhow::Error; +use serde_json::{json, Value}; + +use proxmox_router::{cli::*, ApiHandler, RpcEnvironment}; +use proxmox_schema::api; + +use pbs_api_types::{RemovableDeviceConfig, DEVICE_NAME_SCHEMA, DEVICE_UUID_SCHEMA}; +use pbs_client::view_task_result; + +use proxmox_backup::api2; +use proxmox_backup::client_helpers::connect_to_localhost; + +#[api( + input: { + properties: { + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + } + } +)] +/// Removable device list. +fn list_removable_devices(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result { + let output_format = get_output_format(¶m); + + let info = &api2::config::removable_device::API_METHOD_LIST_REMOVABLE_DEVICE; + let mut data = match info.handler { + ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, + _ => unreachable!(), + }; + + let options = default_table_format_options() + .column(ColumnConfig::new("name")) + .column(ColumnConfig::new("store")) + .column(ColumnConfig::new("initialized")) + .column(ColumnConfig::new("uuid")); + + format_and_print_result_full(&mut data, &info.returns, &output_format, &options); + + Ok(Value::Null) +} + +#[api( + input: { + properties: { + name: { + schema: DEVICE_NAME_SCHEMA, + }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + } + } +)] +/// Show removable devive configuration +fn show_removable_device(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result { + let output_format = get_output_format(¶m); + + let info = &api2::config::removable_device::API_METHOD_READ_REMOVABLE_DEVICE; + let mut data = match info.handler { + ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, + _ => unreachable!(), + }; + + let options = default_table_format_options(); + format_and_print_result_full(&mut data, &info.returns, &output_format, &options); + + Ok(Value::Null) +} + +#[api( + protected: true, + input: { + properties: { + config: { + type: RemovableDeviceConfig, + flatten: true, + }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + }, + }, +)] +/// Create new removable device config. +async fn create_removable_device(mut param: Value) -> Result { + let output_format = extract_output_format(&mut param); + + let client = connect_to_localhost()?; + + let result = client + .post("api2/json/config/removable-device", Some(param)) + .await?; + + view_task_result(&client, result, &output_format).await?; + + Ok(Value::Null) +} + +#[api( + protected: true, + input: { + properties: { + name: { + schema: DEVICE_NAME_SCHEMA, + }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + }, + }, +)] +/// Mount removable device. +async fn mount_removable_device(name: String, mut param: Value) -> Result { + let output_format = extract_output_format(&mut param); + + let client = connect_to_localhost()?; + + let path = format!("api2/json/admin/removable-device/{}/mount", name); + + let result = client.post(&path, None).await?; + + view_task_result(&client, result, &output_format).await?; + + Ok(Value::Null) +} + +#[api( + protected: true, + input: { + properties: { + uuid: { + schema: DEVICE_UUID_SCHEMA, + }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + }, + }, +)] +/// Mount removable device by uuid, this just triggers the mounting in the backend and ignores the result. +async fn mount_uuid_device(uuid: String) -> Result { + let client = connect_to_localhost()?; + + let path = "api2/json/admin/mount-device"; + + client.post(&path, Some(json!({ "uuid": uuid }))).await?; + + Ok(Value::Null) +} + +pub fn removable_device_commands() -> CommandLineInterface { + let cmd_def = CliCommandMap::new() + .insert("list", CliCommand::new(&API_METHOD_LIST_REMOVABLE_DEVICES)) + .insert( + "show", + CliCommand::new(&API_METHOD_SHOW_REMOVABLE_DEVICE) + .arg_param(&["name"]) + .completion_cb( + "name", + pbs_config::removable_device::complete_removable_device_name, + ), + ) + .insert( + "create", + CliCommand::new(&API_METHOD_CREATE_REMOVABLE_DEVICE) + .arg_param(&["name"]) + .completion_cb("store", pbs_config::datastore::complete_datastore_name), + ) + .insert( + "mount", + CliCommand::new(&API_METHOD_MOUNT_REMOVABLE_DEVICE) + .arg_param(&["name"]) + .completion_cb( + "name", + pbs_config::removable_device::complete_removable_device_name, + ), + ) + .insert( + "mount-uuid", + CliCommand::new(&API_METHOD_MOUNT_UUID_DEVICE).arg_param(&["uuid"]), + ) + .insert( + "update", + CliCommand::new(&api2::config::removable_device::API_METHOD_UPDATE_REMOVABLE_DEVICE) + .arg_param(&["name"]) + .completion_cb( + "name", + pbs_config::removable_device::complete_removable_device_name, + ) + .completion_cb("store", pbs_config::datastore::complete_datastore_name), + ) + .insert( + "remove", + CliCommand::new(&api2::config::removable_device::API_METHOD_DELETE_REMOVABLE_DEVICE) + .arg_param(&["name"]) + .completion_cb( + "name", + pbs_config::removable_device::complete_removable_device_name, + ), + ); + + cmd_def.into() +} -- 2.30.2