2019-11-21 13:36:28 +00:00
|
|
|
use std::path::PathBuf;
|
2018-12-08 13:44:55 +00:00
|
|
|
|
2019-11-21 13:36:28 +00:00
|
|
|
use failure::*;
|
2019-12-20 11:28:28 +00:00
|
|
|
use serde_json::Value;
|
2018-12-08 13:44:55 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment};
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2019-12-11 12:45:23 +00:00
|
|
|
use crate::api2::types::*;
|
2019-11-21 13:36:28 +00:00
|
|
|
use crate::backup::*;
|
2018-12-08 13:51:08 +00:00
|
|
|
use crate::config::datastore;
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "List the configured datastores.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: datastore::DataStoreConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List all datastores
|
|
|
|
pub fn list_datastores(
|
2019-01-26 13:50:37 +00:00
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> 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"))
|
|
|
|
}
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
|
|
|
schema: datastore::COMMENT_SCHEMA,
|
|
|
|
},
|
|
|
|
path: {
|
|
|
|
schema: datastore::DIR_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Create new datastore config.
|
|
|
|
pub fn create_datastore(name: String, param: Value) -> Result<(), Error> {
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2018-12-09 11:51:31 +00:00
|
|
|
// fixme: locking ?
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
let mut config = datastore::config()?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
if let Some(_) = config.sections.get(&name) {
|
2018-12-09 11:51:31 +00:00
|
|
|
bail!("datastore '{}' already exists.", name);
|
|
|
|
}
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
if let Some(ref comment) = datastore.comment {
|
|
|
|
if comment.find(|c: char| c.is_control()).is_some() {
|
2019-12-20 11:28:28 +00:00
|
|
|
bail!("comment must not contain control characters!");
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 11:51:31 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
let path: PathBuf = datastore.path.clone().into();
|
|
|
|
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let _store = ChunkStore::create(&name, path, backup_user.uid, backup_user.gid)?;
|
|
|
|
|
2020-01-11 10:09:27 +00:00
|
|
|
config.set_data(&name, "datastore", &datastore)?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
Ok(())
|
2018-12-08 13:44:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Remove a datastore configuration.
|
|
|
|
pub fn delete_datastore(name: String) -> Result<(), Error> {
|
2018-12-09 15:52:32 +00:00
|
|
|
|
|
|
|
// fixme: locking ?
|
|
|
|
// fixme: check digest ?
|
|
|
|
|
|
|
|
let mut config = datastore::config()?;
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
match config.sections.get(&name) {
|
|
|
|
Some(_) => { config.sections.remove(&name); },
|
2018-12-09 15:52:32 +00:00
|
|
|
None => bail!("datastore '{}' does not exist.", name),
|
|
|
|
}
|
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
Ok(())
|
2018-12-09 15:52:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-01-11 09:42:09 +00:00
|
|
|
.get(&API_METHOD_LIST_DATASTORES)
|
|
|
|
.post(&API_METHOD_CREATE_DATASTORE)
|
|
|
|
.delete(&API_METHOD_DELETE_DATASTORE);
|