src/api2/config/remotes.rs: improve api, implement update and read
This commit is contained in:
		@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user