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