2020-01-10 12:28:15 +00:00
|
|
|
use failure::*;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
2020-01-10 18:25:26 +00:00
|
|
|
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment};
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-13 10:47:07 +00:00
|
|
|
use crate::api2::types::*;
|
2020-01-10 12:28:15 +00:00
|
|
|
use crate::config::remotes;
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "The list of configured remotes.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: remotes::Remote,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List all remotes
|
|
|
|
pub fn list_remotes(
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let config = remotes::config()?;
|
|
|
|
|
|
|
|
Ok(config.convert_to_array("name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
2020-01-11 09:42:09 +00:00
|
|
|
protected: true,
|
2020-01-10 12:28:15 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-01-13 10:47:07 +00:00
|
|
|
schema: REMOTE_ID_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
2020-01-13 11:02:13 +00:00
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
host: {
|
|
|
|
schema: remotes::REMOTE_HOST_SCHEMA,
|
|
|
|
},
|
|
|
|
userid: {
|
|
|
|
schema: remotes::REMOTE_USERID_SCHEMA,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
schema: remotes::REMOTE_PASSWORD_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Create new remote.
|
|
|
|
pub fn create_remote(name: String, param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
// fixme: locking ?
|
|
|
|
|
|
|
|
let remote: remotes::Remote = serde_json::from_value(param.clone())?;
|
|
|
|
|
|
|
|
let mut config = remotes::config()?;
|
|
|
|
|
|
|
|
if let Some(_) = config.sections.get(&name) {
|
|
|
|
bail!("remote '{}' already exists.", name);
|
|
|
|
}
|
|
|
|
|
2020-01-11 10:09:27 +00:00
|
|
|
config.set_data(&name, "remote", &remote)?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
|
|
|
remotes::save_config(&config)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
2020-01-11 09:42:09 +00:00
|
|
|
protected: true,
|
2020-01-10 12:28:15 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-01-13 10:47:07 +00:00
|
|
|
schema: REMOTE_ID_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Remove a remote from the configuration file.
|
|
|
|
pub fn delete_remote(name: String) -> Result<(), Error> {
|
|
|
|
|
|
|
|
// fixme: locking ?
|
|
|
|
// fixme: check digest ?
|
|
|
|
|
|
|
|
let mut config = remotes::config()?;
|
|
|
|
|
|
|
|
match config.sections.get(&name) {
|
|
|
|
Some(_) => { config.sections.remove(&name); },
|
|
|
|
None => bail!("remote '{}' does not exist.", name),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_LIST_REMOTES)
|
|
|
|
.post(&API_METHOD_CREATE_REMOTE)
|
|
|
|
.delete(&API_METHOD_DELETE_REMOTE);
|