]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/admin/datastore.rs
rename src/api to src/api_schema
[proxmox-backup.git] / src / api2 / admin / datastore.rs
1 use failure::*;
2
3 use crate::api_schema::schema::*;
4 use crate::api_schema::router::*;
5 //use crate::server::rest::*;
6 use serde_json::{json, Value};
7
8 //use hyper::StatusCode;
9 //use hyper::rt::{Future, Stream};
10
11 use crate::config::datastore;
12
13 use crate::backup::*;
14
15 mod catar;
16
17 // this is just a test for mutability/mutex handling - will remove later
18 fn start_garbage_collection(
19 param: Value,
20 _info: &ApiMethod,
21 _rpcenv: &mut RpcEnvironment,
22 ) -> Result<Value, Error> {
23
24 let store = param["store"].as_str().unwrap();
25
26 let datastore = DataStore::lookup_datastore(store)?;
27
28 println!("Starting garbage collection on store {}", store);
29
30 datastore.garbage_collection()?;
31
32 Ok(json!(null))
33 }
34
35 pub fn api_method_start_garbage_collection() -> ApiMethod {
36 ApiMethod::new(
37 start_garbage_collection,
38 ObjectSchema::new("Start garbage collection.")
39 .required("store", StringSchema::new("Datastore name."))
40 )
41 }
42
43 fn garbage_collection_status(
44 param: Value,
45 _info: &ApiMethod,
46 _rpcenv: &mut RpcEnvironment,
47 ) -> Result<Value, Error> {
48
49 let store = param["store"].as_str().unwrap();
50
51 println!("Garbage collection status on store {}", store);
52
53 Ok(json!(null))
54
55 }
56
57 pub fn api_method_garbage_collection_status() -> ApiMethod {
58 ApiMethod::new(
59 garbage_collection_status,
60 ObjectSchema::new("Garbage collection status.")
61 .required("store", StringSchema::new("Datastore name."))
62 )
63 }
64
65 fn get_backup_list(
66 param: Value,
67 _info: &ApiMethod,
68 _rpcenv: &mut RpcEnvironment,
69 ) -> Result<Value, Error> {
70
71 //let config = datastore::config()?;
72
73 let store = param["store"].as_str().unwrap();
74
75 let datastore = DataStore::lookup_datastore(store)?;
76
77 let mut list = vec![];
78
79 for info in datastore.list_backups()? {
80 list.push(json!({
81 "backup_type": info.backup_type,
82 "backup_id": info.backup_id,
83 "backup_time": info.backup_time.timestamp(),
84 }));
85 }
86
87 let result = json!(list);
88
89 Ok(result)
90 }
91
92 fn get_datastore_list(
93 _param: Value,
94 _info: &ApiMethod,
95 _rpcenv: &mut RpcEnvironment,
96 ) -> Result<Value, Error> {
97
98 let config = datastore::config()?;
99
100 Ok(config.convert_to_array("store"))
101 }
102
103
104 pub fn router() -> Router {
105
106 let datastore_info = Router::new()
107 .get(ApiMethod::new(
108 |_,_,_| Ok(json!([
109 {"subdir": "backups" },
110 {"subdir": "catar" },
111 {"subdir": "status"},
112 {"subdir": "gc" }
113 ])),
114 ObjectSchema::new("Directory index.")
115 .required("store", StringSchema::new("Datastore name.")))
116 )
117 .subdir(
118 "backups",
119 Router::new()
120 .get(ApiMethod::new(
121 get_backup_list,
122 ObjectSchema::new("List backups.")
123 .required("store", StringSchema::new("Datastore name.")))))
124 .subdir(
125 "catar",
126 Router::new()
127 .download(catar::api_method_download_catar())
128 .upload(catar::api_method_upload_catar()))
129 .subdir(
130 "gc",
131 Router::new()
132 .get(api_method_garbage_collection_status())
133 .post(api_method_start_garbage_collection()));
134
135
136
137 let route = Router::new()
138 .get(ApiMethod::new(
139 get_datastore_list,
140 ObjectSchema::new("Directory index.")))
141 .match_all("store", datastore_info);
142
143
144
145 route
146 }