From 08195ac8231136d3638fd3be68d0dd0fbf8133de Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 14 Jan 2020 14:20:16 +0100 Subject: [PATCH] src/api2/config/remotes.rs: improve api, implement update and read --- src/api2/config/remotes.rs | 85 ++++++++++++++++++++++++++++++- src/bin/proxmox-backup-manager.rs | 5 ++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/api2/config/remotes.rs b/src/api2/config/remotes.rs index 871ab998..8ef6460e 100644 --- a/src/api2/config/remotes.rs +++ b/src/api2/config/remotes.rs @@ -73,6 +73,84 @@ pub fn create_remote(name: String, param: Value) -> Result<(), Error> { Ok(()) } +#[api( + input: { + properties: { + name: { + schema: REMOTE_ID_SCHEMA, + }, + }, + }, +)] +/// Read remote configuration data. +pub fn read_remote(name: String) -> Result { + let (config, digest) = remotes::config()?; + let mut data = config.lookup_json("remote", &name)?; + data.as_object_mut().unwrap() + .insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into()); + Ok(data) +} + +#[api( + protected: true, + input: { + properties: { + name: { + schema: REMOTE_ID_SCHEMA, + }, + comment: { + optional: true, + schema: SINGLE_LINE_COMMENT_SCHEMA, + }, + host: { + optional: true, + schema: DNS_NAME_OR_IP_SCHEMA, + }, + userid: { + optional: true, + schema: PROXMOX_USER_ID_SCHEMA, + }, + password: { + optional: true, + schema: remotes::REMOTE_PASSWORD_SCHEMA, + }, + }, + }, +)] +/// Update remote configuration. +pub fn update_remote( + name: String, + comment: Option, + host: Option, + userid: Option, + password: Option, +) -> Result<(), Error> { + + // fixme: locking ? + // pass/compare digest + let (mut config, _digest) = remotes::config()?; + + let mut data: remotes::Remote = config.lookup("remote", &name)?; + + if let Some(comment) = comment { + let comment = comment.trim().to_string(); + if comment.is_empty() { + data.comment = None; + } else { + data.comment = Some(comment); + } + } + if let Some(host) = host { data.host = host; } + if let Some(userid) = userid { data.userid = userid; } + if let Some(password) = password { data.password = password; } + + config.set_data(&name, "remote", &data)?; + + remotes::save_config(&config)?; + + Ok(()) +} + #[api( protected: true, input: { @@ -99,7 +177,12 @@ pub fn delete_remote(name: String) -> Result<(), Error> { Ok(()) } +const ITEM_ROUTER: Router = Router::new() + .get(&API_METHOD_READ_REMOTE) + .put(&API_METHOD_UPDATE_REMOTE) + .delete(&API_METHOD_DELETE_REMOTE); + pub const ROUTER: Router = Router::new() .get(&API_METHOD_LIST_REMOTES) .post(&API_METHOD_CREATE_REMOTE) - .delete(&API_METHOD_DELETE_REMOTE); + .match_all("name", &ITEM_ROUTER); diff --git a/src/bin/proxmox-backup-manager.rs b/src/bin/proxmox-backup-manager.rs index cc5e697d..354536a4 100644 --- a/src/bin/proxmox-backup-manager.rs +++ b/src/bin/proxmox-backup-manager.rs @@ -56,6 +56,11 @@ fn remotes_commands() -> CommandLineInterface { CliCommand::new(&api2::config::remotes::API_METHOD_CREATE_REMOTE) .arg_param(&["name"]) ) + .insert( + "update", + CliCommand::new(&api2::config::remotes::API_METHOD_UPDATE_REMOTE) + .arg_param(&["name"]) + ) .insert( "remove", CliCommand::new(&api2::config::remotes::API_METHOD_DELETE_REMOTE)