]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/config/datastore.rs
rename api3 back to api2
[proxmox-backup.git] / src / api2 / config / datastore.rs
1 use failure::*;
2 //use std::collections::HashMap;
3
4 use crate::api::schema::*;
5 use crate::api::router::*;
6 use crate::backup::chunk_store::*;
7 use serde_json::{json, Value};
8 use std::path::PathBuf;
9
10 use crate::config::datastore;
11
12 pub fn get() -> ApiMethod {
13 ApiMethod::new(
14 get_datastore_list,
15 ObjectSchema::new("Directory index."))
16 }
17
18 fn get_datastore_list(_param: Value, _info: &ApiMethod) -> Result<Value, Error> {
19
20 let config = datastore::config()?;
21
22 Ok(config.convert_to_array("name"))
23 }
24
25 pub fn post() -> ApiMethod {
26 ApiMethod::new(
27 create_datastore,
28 ObjectSchema::new("Create new datastore.")
29 .required("name", StringSchema::new("Datastore name."))
30 .required("path", StringSchema::new("Directory path (must exist)."))
31 )
32 }
33
34 fn create_datastore(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
35
36 // fixme: locking ?
37
38 let mut config = datastore::config()?;
39
40 let name = param["name"].as_str().unwrap();
41
42 if let Some(_) = config.sections.get(name) {
43 bail!("datastore '{}' already exists.", name);
44 }
45
46 let path: PathBuf = param["path"].as_str().unwrap().into();
47 let _store = ChunkStore::create(name, path)?;
48
49 let datastore = json!({
50 "path": param["path"]
51 });
52
53 config.set_data(name, "datastore", datastore);
54
55 datastore::save_config(&config)?;
56
57 Ok(Value::Null)
58 }
59
60 pub fn delete() -> ApiMethod {
61 ApiMethod::new(
62 delete_datastore,
63 ObjectSchema::new("Remove a datastore configuration.")
64 .required("name", StringSchema::new("Datastore name.")))
65 }
66
67 fn delete_datastore(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
68 println!("This is a test {}", param);
69
70 // fixme: locking ?
71 // fixme: check digest ?
72
73 let mut config = datastore::config()?;
74
75 let name = param["name"].as_str().unwrap();
76
77 match config.sections.get(name) {
78 Some(_) => { config.sections.remove(name); },
79 None => bail!("datastore '{}' does not exist.", name),
80 }
81
82 datastore::save_config(&config)?;
83
84 Ok(Value::Null)
85 }
86
87 pub fn router() -> Router {
88
89 let route = Router::new()
90 .get(get())
91 .post(post())
92 .delete(delete());
93
94
95 route
96 }