api: pass RpcEnvirnment to api handlers

This commit is contained in:
Dietmar Maurer 2019-01-26 14:50:37 +01:00
parent d96d82736d
commit 6049b71f41
18 changed files with 252 additions and 109 deletions

View File

@ -11,7 +11,14 @@ use hyper::http::request::Parts;
pub type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Send>; pub type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Send>;
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>; pub trait RpcEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value);
fn get_result_attrib(&self, name: &str) -> Option<&Value>;
}
type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result<Value, Error>;
type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod) -> Result<BoxFut, Error>; type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod) -> Result<BoxFut, Error>;

View File

@ -30,7 +30,11 @@ lazy_static! {
fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn test_sync_api_handler(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
println!("This is a test {}", param); println!("This is a test {}", param);
// let force: Option<bool> = Some(false); // let force: Option<bool> = Some(false);
@ -51,7 +55,7 @@ pub fn router() -> Router {
let route4 = Router::new() let route4 = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
|param, _info| { |param, _info, _rpcenv| {
println!("This is a clousure handler: {}", param); println!("This is a clousure handler: {}", param);
Ok(json!(null)) Ok(json!(null))
@ -74,7 +78,7 @@ pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
|_,_| Ok(json!([ |_,_,_| Ok(json!([
{"subdir": "config"}, {"subdir": "config"},
{"subdir": "admin"}, {"subdir": "admin"},
{"subdir": "nodes"}, {"subdir": "nodes"},

View File

@ -8,7 +8,7 @@ pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
|_,_| Ok(json!([ |_,_,_| Ok(json!([
{"subdir": "datastore"} {"subdir": "datastore"}
])), ])),
ObjectSchema::new("Directory index."))) ObjectSchema::new("Directory index.")))

View File

@ -15,7 +15,11 @@ use crate::backup::datastore::*;
mod 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,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let store = param["store"].as_str().unwrap(); let store = param["store"].as_str().unwrap();
@ -36,7 +40,11 @@ pub fn api_method_start_garbage_collection() -> ApiMethod {
) )
} }
fn garbage_collection_status(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn garbage_collection_status(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let store = param["store"].as_str().unwrap(); let store = param["store"].as_str().unwrap();
@ -54,7 +62,11 @@ pub fn api_method_garbage_collection_status() -> ApiMethod {
) )
} }
fn get_backup_list(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_backup_list(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let config = datastore::config()?; let config = datastore::config()?;
@ -77,7 +89,11 @@ fn get_backup_list(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(result) Ok(result)
} }
fn get_datastore_list(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_datastore_list(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let config = datastore::config()?; let config = datastore::config()?;
@ -89,7 +105,7 @@ pub fn router() -> Router {
let datastore_info = Router::new() let datastore_info = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
|_,_| Ok(json!([ |_,_,_| Ok(json!([
{"subdir": "backups" }, {"subdir": "backups" },
{"subdir": "catar" }, {"subdir": "catar" },
{"subdir": "status"}, {"subdir": "status"},

View File

@ -11,7 +11,7 @@ pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
|_,_| Ok(json!([ |_,_,_| Ok(json!([
{"subdir": "datastore"}, {"subdir": "datastore"},
])), ])),
ObjectSchema::new("Directory index."))) ObjectSchema::new("Directory index.")))

View File

@ -15,7 +15,11 @@ pub fn get() -> ApiMethod {
ObjectSchema::new("Directory index.")) ObjectSchema::new("Directory index."))
} }
fn get_datastore_list(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_datastore_list(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let config = datastore::config()?; let config = datastore::config()?;
@ -31,7 +35,11 @@ pub fn post() -> ApiMethod {
) )
} }
fn create_datastore(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn create_datastore(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
// fixme: locking ? // fixme: locking ?
@ -64,7 +72,11 @@ pub fn delete() -> ApiMethod {
.required("name", StringSchema::new("Datastore name."))) .required("name", StringSchema::new("Datastore name.")))
} }
fn delete_datastore(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn delete_datastore(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
println!("This is a test {}", param); println!("This is a test {}", param);
// fixme: locking ? // fixme: locking ?

View File

@ -11,7 +11,7 @@ pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
|_,_| Ok(json!([ |_,_,_| Ok(json!([
{"subdir": "network"}, {"subdir": "network"},
{"subdir": "syslog"}, {"subdir": "syslog"},
{"subdir": "time"}, {"subdir": "time"},

View File

@ -51,7 +51,11 @@ fn read_etc_resolv_conf() -> Result<Value, Error> {
Ok(result) Ok(result)
} }
fn update_dns(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn update_dns(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
lazy_static! { lazy_static! {
static ref MUTEX: Arc<Mutex<usize>> = Arc::new(Mutex::new(0)); static ref MUTEX: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
@ -93,7 +97,11 @@ fn update_dns(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(Value::Null) Ok(Value::Null)
} }
fn get_dns(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_dns(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
read_etc_resolv_conf() read_etc_resolv_conf()
} }

View File

@ -6,7 +6,11 @@ use crate::api::router::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
fn get_network_config(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_network_config(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
Ok(json!({})) Ok(json!({}))
} }

View File

@ -72,7 +72,11 @@ fn dump_journal(
Ok((count, lines)) Ok((count, lines))
} }
fn get_syslog(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_syslog(
param: Value,
_info: &ApiMethod,
rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let (count, lines) = dump_journal( let (count, lines) = dump_journal(
param["start"].as_u64(), param["start"].as_u64(),
@ -81,7 +85,7 @@ fn get_syslog(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
param["until"].as_str(), param["until"].as_str(),
param["service"].as_str())?; param["service"].as_str())?;
//fixme: $restenv->set_result_attrib('total', $count); rpcenv.set_result_attrib("total", Value::from(count));
Ok(json!(lines)) Ok(json!(lines))
} }

View File

@ -14,7 +14,11 @@ fn read_etc_localtime() -> Result<String, Error> {
Ok(line.trim().to_owned()) Ok(line.trim().to_owned())
} }
fn get_time(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_time(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let datetime = Local::now(); let datetime = Local::now();
let offset = datetime.offset(); let offset = datetime.offset();
@ -32,7 +36,11 @@ extern "C" { fn tzset(); }
// Note:: this needs root rights ?? // Note:: this needs root rights ??
fn set_timezone(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn set_timezone(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let timezone = tools::required_string_param(&param, "timezone")?; let timezone = tools::required_string_param(&param, "timezone")?;

View File

@ -6,7 +6,11 @@ use crate::api::router::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
fn get_subscription(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_subscription(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let url = "https://www.proxmox.com/en/proxmox-backup-server/pricing"; let url = "https://www.proxmox.com/en/proxmox-backup-server/pricing";
Ok(json!({ Ok(json!({

View File

@ -8,7 +8,11 @@ const PROXMOX_PKG_VERSION: &'static str = env!("PROXMOX_PKG_VERSION");
const PROXMOX_PKG_RELEASE: &'static str = env!("PROXMOX_PKG_RELEASE"); const PROXMOX_PKG_RELEASE: &'static str = env!("PROXMOX_PKG_RELEASE");
const PROXMOX_PKG_REPOID: &'static str = env!("PROXMOX_PKG_REPOID"); const PROXMOX_PKG_REPOID: &'static str = env!("PROXMOX_PKG_REPOID");
fn get_version(_param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn get_version(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
Ok(json!({ Ok(json!({
"version": PROXMOX_PKG_VERSION, "version": PROXMOX_PKG_VERSION,

View File

@ -18,10 +18,6 @@ use proxmox_backup::catar::decoder::*;
use proxmox_backup::tools::*; use proxmox_backup::tools::*;
fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
param[name].as_str().expect(&format!("missing parameter '{}'", name))
}
fn print_goodby_entries(buffer: &[u8]) -> Result<(), Error> { fn print_goodby_entries(buffer: &[u8]) -> Result<(), Error> {
println!("GOODBY START: {}", buffer.len()); println!("GOODBY START: {}", buffer.len());
@ -53,9 +49,13 @@ fn print_goodby_entries(buffer: &[u8]) -> Result<(), Error> {
Ok(()) Ok(())
} }
fn print_filenames(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn print_filenames(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let archive = required_string_param(&param, "archive"); let archive = tools::required_string_param(&param, "archive")?;
let file = std::fs::File::open(archive)?; let file = std::fs::File::open(archive)?;
let mut reader = std::io::BufReader::new(file); let mut reader = std::io::BufReader::new(file);
@ -72,9 +72,13 @@ fn print_filenames(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(Value::Null) Ok(Value::Null)
} }
fn dump_archive(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn dump_archive(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let archive = required_string_param(&param, "archive"); let archive = tools::required_string_param(&param, "archive")?;
let mut file = std::fs::File::open(archive)?; let mut file = std::fs::File::open(archive)?;
println!("CATAR {}", archive); println!("CATAR {}", archive);
@ -117,10 +121,14 @@ fn dump_archive(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(Value::Null) Ok(Value::Null)
} }
fn create_archive(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn create_archive(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let archive = required_string_param(&param, "archive"); let archive = tools::required_string_param(&param, "archive")?;
let source = required_string_param(&param, "source"); let source = tools::required_string_param(&param, "source")?;
let source = std::path::PathBuf::from(source); let source = std::path::PathBuf::from(source);

View File

@ -65,7 +65,11 @@ fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target
} }
*/ */
fn list_backups(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn list_backups(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let store = tools::required_string_param(&param, "store")?; let store = tools::required_string_param(&param, "store")?;
@ -78,7 +82,11 @@ fn list_backups(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(result) Ok(result)
} }
fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn create_backup(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let filename = tools::required_string_param(&param, "filename")?; let filename = tools::required_string_param(&param, "filename")?;
let store = tools::required_string_param(&param, "store")?; let store = tools::required_string_param(&param, "store")?;

View File

@ -9,6 +9,28 @@ use crate::api::router::*;
//use crate::api::config::*; //use crate::api::config::*;
use crate::getopts; use crate::getopts;
struct CliEnvironment {
result_attributes: HashMap<String, Value>,
}
impl CliEnvironment {
fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for CliEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}
pub fn print_cli_usage() { pub fn print_cli_usage() {
eprintln!("Usage: TODO"); eprintln!("Usage: TODO");
@ -260,10 +282,11 @@ pub fn run_cli_command(def: &CommandLineInterface) -> Result<(), Error> {
CommandLineInterface::Nested(map) => handle_nested_command(map, args), CommandLineInterface::Nested(map) => handle_nested_command(map, args),
}; };
let mut rpcenv = CliEnvironment::new();
let res = match invocation { let res = match invocation {
Err(e) => return Err(UsageError(e).into()), Err(e) => return Err(UsageError(e).into()),
Ok(invocation) => (invocation.0.info.handler)(invocation.1, &invocation.0.info)?, Ok(invocation) => (invocation.0.info.handler)(invocation.1, &invocation.0.info, &mut rpcenv)?,
}; };
println!("Result: {}", serde_json::to_string_pretty(&res).unwrap()); println!("Result: {}", serde_json::to_string_pretty(&res).unwrap());

View File

@ -1,25 +1,22 @@
use failure::*; use failure::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
use crate::api::router::RpcEnvironment;
use hyper::{Body, Response, StatusCode}; use hyper::{Body, Response, StatusCode};
use hyper::header; use hyper::header;
pub struct OutputFormatter { pub struct OutputFormatter {
pub format_result: fn(data: Result<Value, Error>) -> Response<Body>, pub format_result: fn(data: Value, rpcenv: &RpcEnvironment) -> Response<Body>,
pub format_error: fn(err: Error) -> Response<Body>,
} }
fn json_format_result(data: Result<Value, Error>) -> Response<Body> { static json_content_type: &str = "application/json;charset=UTF-8";
let content_type = "application/json;charset=UTF-8";
match data { fn json_response(result: Value) -> Response<Body> {
Ok(value) => {
let result = json!({
"data": value
});
// todo: set result.total result.changes
let json_str = result.to_string(); let json_str = result.to_string();
@ -28,42 +25,66 @@ fn json_format_result(data: Result<Value, Error>) -> Response<Body> {
let mut response = Response::new(raw.into()); let mut response = Response::new(raw.into());
response.headers_mut().insert( response.headers_mut().insert(
header::CONTENT_TYPE, header::CONTENT_TYPE,
header::HeaderValue::from_static(content_type)); header::HeaderValue::from_static(json_content_type));
return response;
response
} }
Err(err) => {
fn json_format_result(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
let mut result = json!({
"data": data
});
if let Some(total) = rpcenv.get_result_attrib("total").and_then(|v| v.as_u64()) {
result["total"] = Value::from(total);
}
if let Some(changes) = rpcenv.get_result_attrib("changes") {
result["changes"] = changes.clone();
}
json_response(result)
}
fn json_format_error(err: Error) -> Response<Body> {
let mut response = Response::new(Body::from(err.to_string())); let mut response = Response::new(Body::from(err.to_string()));
response.headers_mut().insert( response.headers_mut().insert(
header::CONTENT_TYPE, header::CONTENT_TYPE,
header::HeaderValue::from_static(content_type)); header::HeaderValue::from_static(json_content_type));
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;
return response;
} response
}
} }
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter { pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
format_result: json_format_result, format_result: json_format_result,
format_error: json_format_error,
}; };
fn extjs_format_result(data: Result<Value, Error>) -> Response<Body> { fn extjs_format_result(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
let content_type = "application/json;charset=UTF-8"; let mut result = json!({
"data": data,
let result = match data {
Ok(value) => {
let result = json!({
"data": value,
"success": true "success": true
}); });
// todo: set result.total result.changes if let Some(total) = rpcenv.get_result_attrib("total").and_then(|v| v.as_u64()) {
result["total"] = Value::from(total);
result
} }
Err(err) => {
let mut errors = vec![];
if let Some(changes) = rpcenv.get_result_attrib("changes") {
result["changes"] = changes.clone();
}
json_response(result)
}
fn extjs_format_error(err: Error) -> Response<Body> {
let mut errors = vec![];
errors.push(err.to_string()); errors.push(err.to_string());
let result = json!({ let result = json!({
@ -71,21 +92,10 @@ fn extjs_format_result(data: Result<Value, Error>) -> Response<Body> {
"success": false "success": false
}); });
result json_response(result)
}
};
let json_str = result.to_string();
let raw = json_str.into_bytes();
let mut response = Response::new(raw.into());
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(content_type));
response
} }
pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter { pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter {
format_result: extjs_format_result, format_result: extjs_format_result,
format_error: extjs_format_error,
}; };

View File

@ -26,6 +26,27 @@ use hyper::service::{Service, NewService};
use hyper::rt::{Future, Stream}; use hyper::rt::{Future, Stream};
use hyper::header; use hyper::header;
struct RestEnvironment {
result_attributes: HashMap<String, Value>,
}
impl RestEnvironment {
fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for RestEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}
pub struct RestServer { pub struct RestServer {
pub api_config: Arc<ApiConfig>, pub api_config: Arc<ApiConfig>,
} }
@ -169,10 +190,12 @@ fn handle_sync_api_request(
let resp = params let resp = params
.and_then(move |params| { .and_then(move |params| {
let res = (info.handler)(params, info)?; let mut rpcenv = RestEnvironment::new();
Ok(res) let resp = match (info.handler)(params, info, &mut rpcenv) {
}).then(move |result| { Ok(data) => (formatter.format_result)(data, &rpcenv),
Ok((formatter.format_result)(result)) Err(err) => (formatter.format_error)(err),
};
Ok(resp)
}); });
Box::new(resp) Box::new(resp)
@ -203,7 +226,7 @@ fn handle_async_api_request(
let params = match parse_parameter_strings(&param_list, &info.parameters, true) { let params = match parse_parameter_strings(&param_list, &info.parameters, true) {
Ok(v) => v, Ok(v) => v,
Err(err) => { Err(err) => {
let resp = (formatter.format_result)(Err(Error::from(err))); let resp = (formatter.format_error)(Error::from(err));
return Box::new(future::ok(resp)); return Box::new(future::ok(resp));
} }
}; };
@ -211,7 +234,7 @@ fn handle_async_api_request(
match (info.handler)(parts, req_body, params, info) { match (info.handler)(parts, req_body, params, info) {
Ok(future) => future, Ok(future) => future,
Err(err) => { Err(err) => {
let resp = (formatter.format_result)(Err(Error::from(err))); let resp = (formatter.format_error)(Error::from(err));
Box::new(future::ok(resp)) Box::new(future::ok(resp))
} }
} }