]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/config/datastore.rs
use const api definitions
[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_schema::router::*;
6 use crate::backup::*;
7 use serde_json::{json, Value};
8 use std::path::PathBuf;
9
10 use crate::config::datastore;
11
12 pub const GET: ApiMethod = ApiMethod::new(
13 &ApiHandler::Sync(&get_datastore_list),
14 &ObjectSchema::new("Directory index.", &[])
15 );
16
17 fn get_datastore_list(
18 _param: Value,
19 _info: &ApiMethod,
20 _rpcenv: &mut dyn RpcEnvironment,
21 ) -> Result<Value, Error> {
22
23 let config = datastore::config()?;
24
25 Ok(config.convert_to_array("name"))
26 }
27
28 pub 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 );
38
39 fn create_datastore(
40 param: Value,
41 _info: &ApiMethod,
42 _rpcenv: &mut dyn RpcEnvironment,
43 ) -> Result<Value, Error> {
44
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
55 let path: PathBuf = param["path"].as_str().unwrap().into();
56 let _store = ChunkStore::create(name, path)?;
57
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)
67 }
68
69 pub 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 );
78
79 fn delete_datastore(
80 param: Value,
81 _info: &ApiMethod,
82 _rpcenv: &mut dyn RpcEnvironment,
83 ) -> Result<Value, Error> {
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
103 pub const ROUTER: Router = Router::new()
104 .get(&GET)
105 .post(&POST)
106 .delete(&DELETE);