]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/api2/config/remotes.rs: improve api, implement update and read
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 14 Jan 2020 13:20:16 +0000 (14:20 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 14 Jan 2020 13:20:16 +0000 (14:20 +0100)
src/api2/config/remotes.rs
src/bin/proxmox-backup-manager.rs

index 871ab99870da0523bd16e4170e694b94a75e2cc2..8ef6460e48f2d8e5d32ab80c4c539adb23ff9332 100644 (file)
@@ -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<Value, Error> {
+    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<String>,
+    host: Option<String>,
+    userid: Option<String>,
+    password: Option<String>,
+) -> 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);
index cc5e697d128b8d24dba1f3c32933d1f2e32ad0d2..354536a4857e6e354a20b3d12c6a464c2506bd05 100644 (file)
@@ -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)