]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/config/datastore.rs
use const api definitions
[proxmox-backup.git] / src / api2 / config / datastore.rs
CommitLineData
6ce50400 1use failure::*;
461e62fc 2//use std::collections::HashMap;
6ce50400 3
ef2f2efb 4use crate::api_schema::*;
dc9a007b 5use crate::api_schema::router::*;
e5064ba6 6use crate::backup::*;
6ce50400 7use serde_json::{json, Value};
a27a3ee4 8use std::path::PathBuf;
6ce50400 9
567713b4
DM
10use crate::config::datastore;
11
255f378a
DM
12pub const GET: ApiMethod = ApiMethod::new(
13 &ApiHandler::Sync(&get_datastore_list),
14 &ObjectSchema::new("Directory index.", &[])
15);
ea0b8b6e 16
6049b71f
DM
17fn get_datastore_list(
18 _param: Value,
19 _info: &ApiMethod,
dd5495d6 20 _rpcenv: &mut dyn RpcEnvironment,
6049b71f 21) -> Result<Value, Error> {
567713b4 22
b65eaac6
DM
23 let config = datastore::config()?;
24
ea0b8b6e
DM
25 Ok(config.convert_to_array("name"))
26}
27
255f378a
DM
28pub const POST: ApiMethod = ApiMethod::new(
29 &ApiHandler::Sync(&create_datastore),
30 &ObjectSchema::new(
31 "Create new datastore.",
32 &[
33 ("name", false, &StringSchema::new("Datastore name.").schema()),
34 ("path", false, &StringSchema::new("Directory path (must exist).").schema()),
35 ],
36 )
37);
ea0b8b6e 38
6049b71f
DM
39fn create_datastore(
40 param: Value,
41 _info: &ApiMethod,
dd5495d6 42 _rpcenv: &mut dyn RpcEnvironment,
6049b71f 43) -> Result<Value, Error> {
ea0b8b6e 44
652c1190
DM
45 // fixme: locking ?
46
47 let mut config = datastore::config()?;
48
49 let name = param["name"].as_str().unwrap();
50
51 if let Some(_) = config.sections.get(name) {
52 bail!("datastore '{}' already exists.", name);
53 }
54
15b64d46 55 let path: PathBuf = param["path"].as_str().unwrap().into();
277fc5a3 56 let _store = ChunkStore::create(name, path)?;
15b64d46 57
652c1190
DM
58 let datastore = json!({
59 "path": param["path"]
60 });
61
62 config.set_data(name, "datastore", datastore);
63
64 datastore::save_config(&config)?;
65
66 Ok(Value::Null)
6ce50400
DM
67}
68
255f378a
DM
69pub const DELETE: ApiMethod = ApiMethod::new(
70 &ApiHandler::Sync(&delete_datastore),
71 &ObjectSchema::new(
72 "Remove a datastore configuration.",
73 &[
74 ("name", false, &StringSchema::new("Datastore name.").schema()),
75 ],
76 )
77);
34d3ba52 78
6049b71f
DM
79fn delete_datastore(
80 param: Value,
81 _info: &ApiMethod,
dd5495d6 82 _rpcenv: &mut dyn RpcEnvironment,
6049b71f 83) -> Result<Value, Error> {
34d3ba52
DM
84 println!("This is a test {}", param);
85
86 // fixme: locking ?
87 // fixme: check digest ?
88
89 let mut config = datastore::config()?;
90
91 let name = param["name"].as_str().unwrap();
92
93 match config.sections.get(name) {
94 Some(_) => { config.sections.remove(name); },
95 None => bail!("datastore '{}' does not exist.", name),
96 }
97
98 datastore::save_config(&config)?;
99
100 Ok(Value::Null)
101}
102
255f378a
DM
103pub const ROUTER: Router = Router::new()
104 .get(&GET)
105 .post(&POST)
106 .delete(&DELETE);