[pbs-devel] [PATCH proxmox-backup 24/26] cli: manager: add removable-device commands
Hannes Laimer
h.laimer at proxmox.com
Tue Jul 5 15:08:32 CEST 2022
Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
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<Value, Error> {
+ 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<Value, Error> {
+ 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<Value, Error> {
+ 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<Value, Error> {
+ 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<Value, Error> {
+ 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
More information about the pbs-devel
mailing list