proxmox-backup/src/api2/config/datastore.rs

115 lines
2.8 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
2018-12-08 13:44:55 +00:00
use failure::*;
2018-12-08 13:44:55 +00:00
use serde_json::{json, Value};
use proxmox::api::{ApiHandler, ApiMethod, Router, RpcEnvironment};
use proxmox::api::schema::*;
use crate::api2::types::*;
use crate::backup::*;
2018-12-08 13:51:08 +00:00
use crate::config::datastore;
2019-11-21 08:36:41 +00:00
pub const GET: ApiMethod = ApiMethod::new(
&ApiHandler::Sync(&get_datastore_list),
&ObjectSchema::new("Directory index.", &[])
);
fn get_datastore_list(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
2018-12-08 13:51:08 +00:00
let config = datastore::config()?;
Ok(config.convert_to_array("name"))
}
2019-11-21 08:36:41 +00:00
pub const POST: ApiMethod = ApiMethod::new(
&ApiHandler::Sync(&create_datastore),
&ObjectSchema::new(
"Create new datastore.",
&[
("comment", true, &StringSchema::new("Comment for this Datastore").schema()),
("name", false, &DATASTORE_SCHEMA),
("path", false, &StringSchema::new("Directory path. The directory path is created if it does not already exist.").schema()),
2019-11-21 08:36:41 +00:00
],
)
).protected(true);
fn create_datastore(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
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);
}
if param["comment"].as_str().unwrap().find(|c: char| c.is_control()) != None {
bail!("comment must not contain control characters!");
}
let path: PathBuf = param["path"].as_str().unwrap().into();
let backup_user = crate::backup::backup_user()?;
let _store = ChunkStore::create(name, path, backup_user.uid, backup_user.gid)?;
2018-12-09 11:51:31 +00:00
let datastore = json!({
"path": param["path"],
"comment": param["comment"],
2018-12-09 11:51:31 +00:00
});
config.set_data(name, "datastore", datastore);
datastore::save_config(&config)?;
Ok(Value::Null)
2018-12-08 13:44:55 +00:00
}
2019-11-21 08:36:41 +00:00
pub const DELETE: ApiMethod = ApiMethod::new(
&ApiHandler::Sync(&delete_datastore),
&ObjectSchema::new(
"Remove a datastore configuration.",
&[
("name", false, &DATASTORE_SCHEMA),
2019-11-21 08:36:41 +00:00
],
)
).protected(true);
fn delete_datastore(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
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)
}
2019-11-21 08:36:41 +00:00
pub const ROUTER: Router = Router::new()
.get(&GET)
.post(&POST)
.delete(&DELETE);