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

116 lines
2.6 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
2018-12-08 13:44:55 +00:00
use failure::*;
use serde_json::Value;
2018-12-08 13:44:55 +00:00
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment};
use crate::api2::types::*;
use crate::backup::*;
2018-12-08 13:51:08 +00:00
use crate::config::datastore;
#[api(
input: {
properties: {},
},
returns: {
description: "List the configured datastores.",
type: Array,
items: {
type: datastore::DataStoreConfig,
},
},
)]
/// List all datastores
pub fn list_datastores(
_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"))
}
#[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 11:51:31 +00:00
// fixme: locking ?
let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?;
2018-12-09 11:51:31 +00:00
let mut config = datastore::config()?;
2018-12-09 11:51:31 +00:00
if let Some(_) = config.sections.get(&name) {
2018-12-09 11:51:31 +00:00
bail!("datastore '{}' already exists.", name);
}
if let Some(ref comment) = datastore.comment {
if comment.find(|c: char| c.is_control()).is_some() {
bail!("comment must not contain control characters!");
}
}
2018-12-09 11:51:31 +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)?;
config.set_data(&name, "datastore", serde_json::to_value(datastore)?);
2018-12-09 11:51:31 +00:00
datastore::save_config(&config)?;
Ok(())
2018-12-08 13:44:55 +00:00
}
#[api(
protected: true,
input: {
properties: {
name: {
schema: DATASTORE_SCHEMA,
},
},
},
)]
/// Remove a datastore configuration.
pub fn delete_datastore(name: String) -> Result<(), Error> {
// fixme: locking ?
// fixme: check digest ?
let mut config = datastore::config()?;
match config.sections.get(&name) {
Some(_) => { config.sections.remove(&name); },
None => bail!("datastore '{}' does not exist.", name),
}
datastore::save_config(&config)?;
Ok(())
}
2019-11-21 08:36:41 +00:00
pub const ROUTER: Router = Router::new()
.get(&API_METHOD_LIST_DATASTORES)
.post(&API_METHOD_CREATE_DATASTORE)
.delete(&API_METHOD_DELETE_DATASTORE);