api/router.rs: rename ApiUploadMethod to ApiAsyncMethod

We can use this for uploads and downloads ...
This commit is contained in:
Dietmar Maurer 2019-01-19 16:42:43 +01:00
parent 379ea0edb6
commit 50cfb695ae
5 changed files with 47 additions and 19 deletions

View File

@ -13,7 +13,7 @@ pub type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Se
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>; type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>;
type ApiUploadHandlerFn = fn(Parts, Body, Value, &ApiUploadMethod) -> Result<BoxFut, Error>; type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod) -> Result<BoxFut, Error>;
pub struct ApiMethod { pub struct ApiMethod {
pub parameters: ObjectSchema, pub parameters: ObjectSchema,
@ -39,15 +39,15 @@ impl ApiMethod {
} }
} }
pub struct ApiUploadMethod { pub struct ApiAsyncMethod {
pub parameters: ObjectSchema, pub parameters: ObjectSchema,
pub returns: Arc<Schema>, pub returns: Arc<Schema>,
pub handler: ApiUploadHandlerFn, pub handler: ApiAsyncHandlerFn,
} }
impl ApiUploadMethod { impl ApiAsyncMethod {
pub fn new(handler: ApiUploadHandlerFn, parameters: ObjectSchema) -> Self { pub fn new(handler: ApiAsyncHandlerFn, parameters: ObjectSchema) -> Self {
Self { Self {
parameters, parameters,
handler, handler,
@ -72,7 +72,7 @@ pub enum SubRoute {
pub enum MethodDefinition { pub enum MethodDefinition {
None, None,
Simple(ApiMethod), Simple(ApiMethod),
Upload(ApiUploadMethod), Async(ApiAsyncMethod),
} }
pub struct Router { pub struct Router {
@ -137,11 +137,17 @@ impl Router {
self self
} }
pub fn upload(mut self, m: ApiUploadMethod) -> Self { pub fn upload(mut self, m: ApiAsyncMethod) -> Self {
self.post = MethodDefinition::Upload(m); self.post = MethodDefinition::Async(m);
self self
} }
pub fn download(mut self, m: ApiAsyncMethod) -> Self {
self.get = MethodDefinition::Async(m);
self
}
pub fn delete(mut self, m: ApiMethod) -> Self { pub fn delete(mut self, m: ApiMethod) -> Self {
self.delete = MethodDefinition::Simple(m); self.delete = MethodDefinition::Simple(m);
self self

View File

@ -12,7 +12,7 @@ use crate::config::datastore;
use crate::backup::datastore::*; use crate::backup::datastore::*;
mod upload_catar; mod catar;
// this is just a test for mutability/mutex handling - will remove later // this is just a test for mutability/mutex handling - will remove later
fn start_garbage_collection(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn start_garbage_collection(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
@ -75,9 +75,10 @@ pub fn router() -> Router {
.required("store", StringSchema::new("Datastore name."))) .required("store", StringSchema::new("Datastore name.")))
) )
.subdir( .subdir(
"upload_catar", "catar",
Router::new() Router::new()
.upload(upload_catar::api_method_upload_catar())) .download(catar::api_method_download_catar())
.upload(catar::api_method_upload_catar()))
.subdir( .subdir(
"gc", "gc",
Router::new() Router::new()

View File

@ -44,7 +44,7 @@ impl Future for UploadCaTar {
} }
} }
fn upload_catar(parts: Parts, req_body: Body, param: Value, _info: &ApiUploadMethod) -> Result<BoxFut, Error> { fn upload_catar(parts: Parts, req_body: Body, param: Value, _info: &ApiAsyncMethod) -> Result<BoxFut, Error> {
let store = tools::required_string_param(&param, "store")?; let store = tools::required_string_param(&param, "store")?;
let archive_name = tools::required_string_param(&param, "archive_name")?; let archive_name = tools::required_string_param(&param, "archive_name")?;
@ -91,8 +91,8 @@ fn upload_catar(parts: Parts, req_body: Body, param: Value, _info: &ApiUploadMet
Ok(Box::new(resp)) Ok(Box::new(resp))
} }
pub fn api_method_upload_catar() -> ApiUploadMethod { pub fn api_method_upload_catar() -> ApiAsyncMethod {
ApiUploadMethod::new( ApiAsyncMethod::new(
upload_catar, upload_catar,
ObjectSchema::new("Upload .catar backup file.") ObjectSchema::new("Upload .catar backup file.")
.required("store", StringSchema::new("Datastore name.")) .required("store", StringSchema::new("Datastore name."))
@ -105,3 +105,24 @@ pub fn api_method_upload_catar() -> ApiUploadMethod {
) )
} }
fn download_catar(parts: Parts, req_body: Body, param: Value, _info: &ApiAsyncMethod) -> Result<BoxFut, Error> {
bail!("not implemeneted");
}
pub fn api_method_download_catar() -> ApiAsyncMethod {
ApiAsyncMethod::new(
download_catar,
ObjectSchema::new("Download .catar backup file.")
.required("store", StringSchema::new("Datastore name."))
.required("archive_name", StringSchema::new("Backup archive name."))
.required("type", StringSchema::new("Backup type.")
.format(Arc::new(ApiStringFormat::Enum(vec!["ct".into(), "host".into()]))))
.required("id", StringSchema::new("Backup ID."))
.required("time", IntegerSchema::new("Backup time (Unix epoch.)")
.minimum(1547797308))
)
}

View File

@ -32,7 +32,7 @@ fn backup_directory(body: Body, store: &str, archive_name: &str) -> Result<(), E
.append_pair("time", &epoch.to_string()) .append_pair("time", &epoch.to_string())
.finish(); .finish();
let path = format!("api3/json/admin/datastore/{}/upload_catar?{}", store, query); let path = format!("api3/json/admin/datastore/{}/catar?{}", store, query);
client.upload("application/x-proxmox-backup-catar", body, &path)?; client.upload("application/x-proxmox-backup-catar", body, &path)?;

View File

@ -178,8 +178,8 @@ fn handle_sync_api_request(
Box::new(resp) Box::new(resp)
} }
fn handle_upload_api_request( fn handle_async_api_request(
info: &'static ApiUploadMethod, info: &'static ApiAsyncMethod,
formatter: &'static OutputFormatter, formatter: &'static OutputFormatter,
parts: Parts, parts: Parts,
req_body: Body, req_body: Body,
@ -405,8 +405,8 @@ pub fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> BoxFut {
MethodDefinition::Simple(api_method) => { MethodDefinition::Simple(api_method) => {
return handle_sync_api_request(api_method, formatter, parts, body, uri_param); return handle_sync_api_request(api_method, formatter, parts, body, uri_param);
} }
MethodDefinition::Upload(upload_method) => { MethodDefinition::Async(async_method) => {
return handle_upload_api_request(upload_method, formatter, parts, body, uri_param); return handle_async_api_request(async_method, formatter, parts, body, uri_param);
} }
} }
} }