]>
Commit | Line | Data |
---|---|---|
15e9b4ed DM |
1 | use failure::*; |
2 | ||
ef2f2efb | 3 | use crate::api_schema::*; |
dc9a007b | 4 | use crate::api_schema::router::*; |
2085142e | 5 | //use crate::server::rest::*; |
15e9b4ed DM |
6 | use serde_json::{json, Value}; |
7 | ||
2085142e DM |
8 | //use hyper::StatusCode; |
9 | //use hyper::rt::{Future, Stream}; | |
7e21da6e | 10 | |
15e9b4ed DM |
11 | use crate::config::datastore; |
12 | ||
e5064ba6 | 13 | use crate::backup::*; |
15e9b4ed | 14 | |
50cfb695 | 15 | mod catar; |
1629d2ad | 16 | |
15e9b4ed | 17 | // this is just a test for mutability/mutex handling - will remove later |
6049b71f DM |
18 | fn start_garbage_collection( |
19 | param: Value, | |
20 | _info: &ApiMethod, | |
21 | _rpcenv: &mut RpcEnvironment, | |
22 | ) -> Result<Value, Error> { | |
15e9b4ed | 23 | |
5a778d92 | 24 | let store = param["store"].as_str().unwrap(); |
15e9b4ed | 25 | |
5a778d92 | 26 | let datastore = DataStore::lookup_datastore(store)?; |
15e9b4ed | 27 | |
5a778d92 | 28 | println!("Starting garbage collection on store {}", store); |
15e9b4ed DM |
29 | |
30 | datastore.garbage_collection()?; | |
31 | ||
32 | Ok(json!(null)) | |
33 | } | |
34 | ||
691c89a0 DM |
35 | pub fn api_method_start_garbage_collection() -> ApiMethod { |
36 | ApiMethod::new( | |
37 | start_garbage_collection, | |
38 | ObjectSchema::new("Start garbage collection.") | |
5a778d92 | 39 | .required("store", StringSchema::new("Datastore name.")) |
691c89a0 DM |
40 | ) |
41 | } | |
42 | ||
6049b71f DM |
43 | fn garbage_collection_status( |
44 | param: Value, | |
45 | _info: &ApiMethod, | |
46 | _rpcenv: &mut RpcEnvironment, | |
47 | ) -> Result<Value, Error> { | |
691c89a0 | 48 | |
5a778d92 | 49 | let store = param["store"].as_str().unwrap(); |
691c89a0 | 50 | |
5a778d92 | 51 | println!("Garbage collection status on store {}", store); |
691c89a0 DM |
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.") | |
5a778d92 | 61 | .required("store", StringSchema::new("Datastore name.")) |
691c89a0 DM |
62 | ) |
63 | } | |
64 | ||
6049b71f DM |
65 | fn get_backup_list( |
66 | param: Value, | |
67 | _info: &ApiMethod, | |
68 | _rpcenv: &mut RpcEnvironment, | |
69 | ) -> Result<Value, Error> { | |
83dbd80b | 70 | |
9f49fe1d | 71 | //let config = datastore::config()?; |
83dbd80b DM |
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(), | |
8c75372b | 84 | "files": info.files, |
83dbd80b DM |
85 | })); |
86 | } | |
87 | ||
88 | let result = json!(list); | |
89 | ||
90 | Ok(result) | |
91 | } | |
7e21da6e | 92 | |
6049b71f DM |
93 | fn get_datastore_list( |
94 | _param: Value, | |
95 | _info: &ApiMethod, | |
96 | _rpcenv: &mut RpcEnvironment, | |
97 | ) -> Result<Value, Error> { | |
15e9b4ed DM |
98 | |
99 | let config = datastore::config()?; | |
100 | ||
5a778d92 | 101 | Ok(config.convert_to_array("store")) |
15e9b4ed DM |
102 | } |
103 | ||
691c89a0 | 104 | |
15e9b4ed DM |
105 | pub fn router() -> Router { |
106 | ||
107 | let datastore_info = Router::new() | |
108 | .get(ApiMethod::new( | |
6049b71f | 109 | |_,_,_| Ok(json!([ |
83dbd80b | 110 | {"subdir": "backups" }, |
29f34b8e | 111 | {"subdir": "catar" }, |
15e9b4ed DM |
112 | {"subdir": "status"}, |
113 | {"subdir": "gc" } | |
114 | ])), | |
115 | ObjectSchema::new("Directory index.") | |
5a778d92 | 116 | .required("store", StringSchema::new("Datastore name."))) |
15e9b4ed | 117 | ) |
83dbd80b DM |
118 | .subdir( |
119 | "backups", | |
120 | Router::new() | |
121 | .get(ApiMethod::new( | |
122 | get_backup_list, | |
123 | ObjectSchema::new("List backups.") | |
124 | .required("store", StringSchema::new("Datastore name."))))) | |
264f52cf | 125 | .subdir( |
50cfb695 | 126 | "catar", |
264f52cf | 127 | Router::new() |
50cfb695 DM |
128 | .download(catar::api_method_download_catar()) |
129 | .upload(catar::api_method_upload_catar())) | |
15e9b4ed DM |
130 | .subdir( |
131 | "gc", | |
132 | Router::new() | |
691c89a0 DM |
133 | .get(api_method_garbage_collection_status()) |
134 | .post(api_method_start_garbage_collection())); | |
7e21da6e | 135 | |
15e9b4ed DM |
136 | |
137 | ||
138 | let route = Router::new() | |
139 | .get(ApiMethod::new( | |
140 | get_datastore_list, | |
141 | ObjectSchema::new("Directory index."))) | |
5a778d92 | 142 | .match_all("store", datastore_info); |
15e9b4ed DM |
143 | |
144 | ||
145 | ||
146 | route | |
147 | } |