2018-12-08 13:44:55 +00:00
|
|
|
use failure::*;
|
2018-12-08 13:55:54 +00:00
|
|
|
//use std::collections::HashMap;
|
2018-12-08 13:44:55 +00:00
|
|
|
|
|
|
|
use crate::api::schema::*;
|
|
|
|
use crate::api::router::*;
|
2019-02-12 13:13:31 +00:00
|
|
|
use crate::backup::*;
|
2018-12-08 13:44:55 +00:00
|
|
|
use serde_json::{json, Value};
|
2018-12-16 12:57:59 +00:00
|
|
|
use std::path::PathBuf;
|
2018-12-08 13:44:55 +00:00
|
|
|
|
2018-12-08 13:51:08 +00:00
|
|
|
use crate::config::datastore;
|
|
|
|
|
2018-12-09 10:59:32 +00:00
|
|
|
pub fn get() -> ApiMethod {
|
|
|
|
ApiMethod::new(
|
|
|
|
get_datastore_list,
|
|
|
|
ObjectSchema::new("Directory index."))
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn get_datastore_list(
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2018-12-08 13:51:08 +00:00
|
|
|
|
2018-12-09 09:23:19 +00:00
|
|
|
let config = datastore::config()?;
|
|
|
|
|
2018-12-09 10:59:32 +00:00
|
|
|
Ok(config.convert_to_array("name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post() -> ApiMethod {
|
|
|
|
ApiMethod::new(
|
|
|
|
create_datastore,
|
|
|
|
ObjectSchema::new("Create new datastore.")
|
|
|
|
.required("name", StringSchema::new("Datastore name."))
|
|
|
|
.required("path", StringSchema::new("Directory path (must exist)."))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn create_datastore(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2018-12-09 11:51:31 +00:00
|
|
|
// fixme: locking ?
|
|
|
|
|
|
|
|
let mut config = datastore::config()?;
|
|
|
|
|
|
|
|
let name = param["name"].as_str().unwrap();
|
|
|
|
|
|
|
|
if let Some(_) = config.sections.get(name) {
|
|
|
|
bail!("datastore '{}' already exists.", name);
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:41:14 +00:00
|
|
|
let path: PathBuf = param["path"].as_str().unwrap().into();
|
2018-12-19 12:40:26 +00:00
|
|
|
let _store = ChunkStore::create(name, path)?;
|
2018-12-13 13:41:14 +00:00
|
|
|
|
2018-12-09 11:51:31 +00:00
|
|
|
let datastore = json!({
|
|
|
|
"path": param["path"]
|
|
|
|
});
|
|
|
|
|
|
|
|
config.set_data(name, "datastore", datastore);
|
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
2018-12-08 13:44:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 15:52:32 +00:00
|
|
|
pub fn delete() -> ApiMethod {
|
|
|
|
ApiMethod::new(
|
|
|
|
delete_datastore,
|
|
|
|
ObjectSchema::new("Remove a datastore configuration.")
|
|
|
|
.required("name", StringSchema::new("Datastore name.")))
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn delete_datastore(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2018-12-09 15:52:32 +00:00
|
|
|
println!("This is a test {}", param);
|
|
|
|
|
|
|
|
// fixme: locking ?
|
|
|
|
// fixme: check digest ?
|
|
|
|
|
|
|
|
let mut config = datastore::config()?;
|
|
|
|
|
|
|
|
let name = param["name"].as_str().unwrap();
|
|
|
|
|
|
|
|
match config.sections.get(name) {
|
|
|
|
Some(_) => { config.sections.remove(name); },
|
|
|
|
None => bail!("datastore '{}' does not exist.", name),
|
|
|
|
}
|
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2018-12-08 13:44:55 +00:00
|
|
|
pub fn router() -> Router {
|
|
|
|
|
|
|
|
let route = Router::new()
|
2018-12-09 10:59:32 +00:00
|
|
|
.get(get())
|
2018-12-09 15:52:32 +00:00
|
|
|
.post(post())
|
|
|
|
.delete(delete());
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2018-12-08 13:44:55 +00:00
|
|
|
|
|
|
|
route
|
|
|
|
}
|