]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/config/datastore.rs
ef979de697b3dcfa88dce449a0c91aaf5cb15dd8
[proxmox-backup.git] / src / api2 / config / datastore.rs
1 use std::path::PathBuf;
2
3 use failure::*;
4 use serde_json::Value;
5
6 use proxmox::api::{api, ApiMethod, Router, RpcEnvironment};
7
8 use crate::api2::types::*;
9 use crate::backup::*;
10 use crate::config::datastore;
11
12 #[api(
13 input: {
14 properties: {},
15 },
16 returns: {
17 description: "List the configured datastores.",
18 type: Array,
19 items: {
20 type: datastore::DataStoreConfig,
21 },
22 },
23 )]
24 /// List all datastores
25 pub fn list_datastores(
26 _param: Value,
27 _info: &ApiMethod,
28 _rpcenv: &mut dyn RpcEnvironment,
29 ) -> Result<Value, Error> {
30
31 let (config, digest) = datastore::config()?;
32
33 Ok(config.convert_to_array("name", Some(&digest)))
34 }
35
36 #[api(
37 protected: true,
38 input: {
39 properties: {
40 name: {
41 schema: DATASTORE_SCHEMA,
42 },
43 comment: {
44 optional: true,
45 schema: SINGLE_LINE_COMMENT_SCHEMA,
46 },
47 path: {
48 schema: datastore::DIR_NAME_SCHEMA,
49 },
50 },
51 },
52 )]
53 /// Create new datastore config.
54 pub fn create_datastore(name: String, param: Value) -> Result<(), Error> {
55
56 // fixme: locking ?
57
58 let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?;
59
60 let (mut config, _digest) = datastore::config()?;
61
62 if let Some(_) = config.sections.get(&name) {
63 bail!("datastore '{}' already exists.", name);
64 }
65
66 let path: PathBuf = datastore.path.clone().into();
67
68 let backup_user = crate::backup::backup_user()?;
69 let _store = ChunkStore::create(&name, path, backup_user.uid, backup_user.gid)?;
70
71 config.set_data(&name, "datastore", &datastore)?;
72
73 datastore::save_config(&config)?;
74
75 Ok(())
76 }
77
78 #[api(
79 protected: true,
80 input: {
81 properties: {
82 name: {
83 schema: DATASTORE_SCHEMA,
84 },
85 },
86 },
87 )]
88 /// Remove a datastore configuration.
89 pub fn delete_datastore(name: String) -> Result<(), Error> {
90
91 // fixme: locking ?
92 // fixme: check digest ?
93
94 let (mut config, _digest) = datastore::config()?;
95
96 match config.sections.get(&name) {
97 Some(_) => { config.sections.remove(&name); },
98 None => bail!("datastore '{}' does not exist.", name),
99 }
100
101 datastore::save_config(&config)?;
102
103 Ok(())
104 }
105
106 pub const ROUTER: Router = Router::new()
107 .get(&API_METHOD_LIST_DATASTORES)
108 .post(&API_METHOD_CREATE_DATASTORE)
109 .delete(&API_METHOD_DELETE_DATASTORE);