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

190 lines
4.7 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 (with config digest).",
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, digest) = datastore::config()?;
Ok(config.convert_to_array("name", Some(&digest)))
}
#[api(
protected: true,
input: {
properties: {
name: {
schema: DATASTORE_SCHEMA,
},
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
path: {
schema: datastore::DIR_NAME_SCHEMA,
},
},
},
)]
/// Create new datastore config.
pub fn create_datastore(name: String, param: Value) -> Result<(), Error> {
2020-01-15 10:57:12 +00:00
let _lock = crate::tools::open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
2018-12-09 11:51:31 +00:00
let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?;
2018-12-09 11:51:31 +00:00
let (mut config, _digest) = 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);
}
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", &datastore)?;
2018-12-09 11:51:31 +00:00
datastore::save_config(&config)?;
Ok(())
2018-12-08 13:44:55 +00:00
}
#[api(
input: {
properties: {
name: {
schema: DATASTORE_SCHEMA,
},
},
},
returns: {
description: "The datastore configuration (with config digest).",
type: datastore::DataStoreConfig,
},
)]
/// Read a datastore configuration.
pub fn read_datastore(name: String) -> Result<Value, Error> {
let (config, digest) = datastore::config()?;
let mut data = config.lookup_json("datastore", &name)?;
data.as_object_mut().unwrap()
.insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into());
Ok(data)
}
#[api(
protected: true,
input: {
properties: {
name: {
schema: DATASTORE_SCHEMA,
},
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
2020-01-15 11:27:05 +00:00
digest: {
optional: true,
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
},
},
},
)]
/// Create new datastore config.
pub fn update_datastore(
name: String,
comment: Option<String>,
2020-01-15 11:27:05 +00:00
digest: Option<String>,
) -> Result<(), Error> {
2020-01-15 10:57:12 +00:00
let _lock = crate::tools::open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
// pass/compare digest
2020-01-15 11:27:05 +00:00
let (mut config, expected_digest) = datastore::config()?;
if let Some(ref digest) = digest {
let digest = proxmox::tools::hex_to_digest(digest)?;
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
}
let mut data: datastore::DataStoreConfig = config.lookup("datastore", &name)?;
if let Some(comment) = comment {
let comment = comment.trim().to_string();
if comment.is_empty() {
data.comment = None;
} else {
data.comment = Some(comment);
}
}
config.set_data(&name, "datastore", &data)?;
datastore::save_config(&config)?;
Ok(())
}
#[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, _digest) = datastore::config()?;
match config.sections.get(&name) {
Some(_) => { config.sections.remove(&name); },
None => bail!("datastore '{}' does not exist.", name),
}
datastore::save_config(&config)?;
Ok(())
}
const ITEM_ROUTER: Router = Router::new()
.get(&API_METHOD_READ_DATASTORE)
.put(&API_METHOD_UPDATE_DATASTORE)
.delete(&API_METHOD_DELETE_DATASTORE);
2019-11-21 08:36:41 +00:00
pub const ROUTER: Router = Router::new()
.get(&API_METHOD_LIST_DATASTORES)
.post(&API_METHOD_CREATE_DATASTORE)
.match_all("name", &ITEM_ROUTER);