]>
Commit | Line | Data |
---|---|---|
a2479cfa | 1 | use std::path::PathBuf; |
6ce50400 | 2 | |
a2479cfa | 3 | use failure::*; |
6ce50400 DM |
4 | use serde_json::{json, Value}; |
5 | ||
a2479cfa WB |
6 | use proxmox::api::{ApiHandler, ApiMethod, Router, RpcEnvironment}; |
7 | use proxmox::api::schema::*; | |
8 | ||
9 | use crate::backup::*; | |
567713b4 DM |
10 | use crate::config::datastore; |
11 | ||
255f378a DM |
12 | pub const GET: ApiMethod = ApiMethod::new( |
13 | &ApiHandler::Sync(&get_datastore_list), | |
14 | &ObjectSchema::new("Directory index.", &[]) | |
15 | ); | |
ea0b8b6e | 16 | |
6049b71f DM |
17 | fn 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 |
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 | ); | |
ea0b8b6e | 38 | |
6049b71f DM |
39 | fn 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 |
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 | ); | |
34d3ba52 | 78 | |
6049b71f DM |
79 | fn 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 |
103 | pub const ROUTER: Router = Router::new() |
104 | .get(&GET) | |
105 | .post(&POST) | |
106 | .delete(&DELETE); |