2 //use std::collections::HashMap;
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
;
10 use crate::config
::datastore
;
12 pub fn get() -> ApiMethod
{
15 ObjectSchema
::new("Directory index."))
18 fn get_datastore_list(_param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
20 let config
= datastore
::config()?
;
22 Ok(config
.convert_to_array("name"))
25 pub fn post() -> ApiMethod
{
28 ObjectSchema
::new("Create new datastore.")
29 .required("name", StringSchema
::new("Datastore name."))
30 .required("path", StringSchema
::new("Directory path (must exist)."))
34 fn create_datastore(param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
38 let mut config
= datastore
::config()?
;
40 let name
= param
["name"].as_str().unwrap();
42 if let Some(_
) = config
.sections
.get(name
) {
43 bail
!("datastore '{}' already exists.", name
);
46 let path
: PathBuf
= param
["path"].as_str().unwrap().into();
47 let _store
= ChunkStore
::create(name
, path
)?
;
49 let datastore
= json
!({
53 config
.set_data(name
, "datastore", datastore
);
55 datastore
::save_config(&config
)?
;
60 pub fn delete() -> ApiMethod
{
63 ObjectSchema
::new("Remove a datastore configuration.")
64 .required("name", StringSchema
::new("Datastore name.")))
67 fn delete_datastore(param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
68 println
!("This is a test {}", param
);
71 // fixme: check digest ?
73 let mut config
= datastore
::config()?
;
75 let name
= param
["name"].as_str().unwrap();
77 match config
.sections
.get(name
) {
78 Some(_
) => { config.sections.remove(name); }
,
79 None
=> bail
!("datastore '{}' does not exist.", name
),
82 datastore
::save_config(&config
)?
;
87 pub fn router() -> Router
{
89 let route
= Router
::new()