src/api2/config/remote.rs: add delete parameter to update method

This commit is contained in:
Dietmar Maurer 2020-04-29 09:04:17 +02:00
parent ec67af9af3
commit 5211705ff1
1 changed files with 30 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use anyhow::{bail, Error}; use anyhow::{bail, Error};
use serde_json::Value; use serde_json::Value;
use ::serde::{Deserialize, Serialize};
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission}; use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
@ -52,7 +53,7 @@ pub fn list_remotes(
let (config, digest) = remote::config()?; let (config, digest) = remote::config()?;
let value = config.convert_to_array("name", Some(&digest), &["password"]); let value = config.convert_to_array("name", Some(&digest), &["password"]);
Ok(value.into()) Ok(value.into())
} }
@ -130,6 +131,16 @@ pub fn read_remote(name: String) -> Result<Value, Error> {
.insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into()); .insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into());
Ok(data) Ok(data)
} }
#[api()]
#[derive(Serialize, Deserialize)]
#[allow(non_camel_case_types)]
/// Deletable property name
pub enum DeletableProperty {
/// Delete the comment property.
comment,
/// Delete the fingerprint property.
fingerprint,
}
#[api( #[api(
protected: true, protected: true,
@ -158,6 +169,14 @@ pub fn read_remote(name: String) -> Result<Value, Error> {
optional: true, optional: true,
schema: CERT_FINGERPRINT_SHA256_SCHEMA, schema: CERT_FINGERPRINT_SHA256_SCHEMA,
}, },
delete: {
description: "List of properties to delete.",
type: Array,
optional: true,
items: {
type: DeletableProperty,
}
},
digest: { digest: {
optional: true, optional: true,
schema: PROXMOX_CONFIG_DIGEST_SCHEMA, schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
@ -176,6 +195,7 @@ pub fn update_remote(
userid: Option<String>, userid: Option<String>,
password: Option<String>, password: Option<String>,
fingerprint: Option<String>, fingerprint: Option<String>,
delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -190,6 +210,15 @@ pub fn update_remote(
let mut data: remote::Remote = config.lookup("remote", &name)?; let mut data: remote::Remote = config.lookup("remote", &name)?;
if let Some(delete) = delete {
for delete_prop in delete {
match delete_prop {
DeletableProperty::comment => { data.comment = None; },
DeletableProperty::fingerprint => { data.fingerprint = None; },
}
}
}
if let Some(comment) = comment { if let Some(comment) = comment {
let comment = comment.trim().to_string(); let comment = comment.trim().to_string();
if comment.is_empty() { if comment.is_empty() {
@ -202,7 +231,6 @@ pub fn update_remote(
if let Some(userid) = userid { data.userid = userid; } if let Some(userid) = userid { data.userid = userid; }
if let Some(password) = password { data.password = password; } if let Some(password) = password { data.password = password; }
// fixme: howto delete a fingeprint?
if let Some(fingerprint) = fingerprint { data.fingerprint = Some(fingerprint); } if let Some(fingerprint) = fingerprint { data.fingerprint = Some(fingerprint); }
config.set_data(&name, "remote", &data)?; config.set_data(&name, "remote", &data)?;