From d8d40dd0c2ab7ef199aec2a3d15c85512cb15b8e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 9 Apr 2019 14:43:30 +0200 Subject: [PATCH] src/api2/node.rs: add node parameter --- src/api2.rs | 3 +-- src/api2/node.rs | 22 ++++++++++++++++++++++ src/api2/node/dns.rs | 2 ++ src/api2/node/network.rs | 4 +++- src/api2/node/services.rs | 9 ++++++++- src/api2/node/syslog.rs | 3 ++- src/api2/node/tasks.rs | 12 +++++++++--- src/api2/node/time.rs | 7 +++++-- 8 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/api2.rs b/src/api2.rs index 7c8cddac..b92d9ab3 100644 --- a/src/api2.rs +++ b/src/api2.rs @@ -26,13 +26,12 @@ lazy_static! { pub static ref PVE_CONFIG_DIGEST_SCHEMA: Arc = StringSchema::new("Prevent changes if current configuration file has different SHA256 digest. This can be used to prevent concurrent modifications.") .format(PVE_CONFIG_DIGEST_FORMAT.clone()).into(); - } pub fn router() -> Router { let nodes = Router::new() - .subdir("localhost", node::router()); + .match_all("node", node::router()); let route = Router::new() .get(ApiMethod::new( diff --git a/src/api2/node.rs b/src/api2/node.rs index 1ec90a5b..486add2f 100644 --- a/src/api2/node.rs +++ b/src/api2/node.rs @@ -1,6 +1,11 @@ +use failure::*; + +use crate::tools; use crate::api_schema::*; use crate::api_schema::router::*; use serde_json::{json}; +use lazy_static::lazy_static; +use std::sync::Arc; mod tasks; mod time; @@ -9,6 +14,23 @@ mod dns; mod syslog; mod services; +lazy_static!{ + + pub static ref NODE_SCHEMA: Arc = Arc::new( + StringSchema::new("Node name (or 'localhost')") + .format( + Arc::new(ApiStringFormat::VerifyFn(|node| { + if node == "localhost" || node == tools::nodename() { + Ok(()) + } else { + Err(format_err!("no such node '{}'", node)) + } + })) + ) + .into() + ); +} + pub fn router() -> Router { let route = Router::new() diff --git a/src/api2/node/dns.rs b/src/api2/node/dns.rs index 7bccb0e5..9b59d98c 100644 --- a/src/api2/node/dns.rs +++ b/src/api2/node/dns.rs @@ -129,6 +129,7 @@ pub fn router() -> Router { ApiMethod::new( get_dns, ObjectSchema::new("Read DNS settings.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) ).returns( ObjectSchema::new("Returns DNS server IPs and sreach domain.") .required("digest", PVE_CONFIG_DIGEST_SCHEMA.clone()) @@ -142,6 +143,7 @@ pub fn router() -> Router { ApiMethod::new( update_dns, ObjectSchema::new("Returns DNS server IPs and sreach domain.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("search", SEARCH_DOMAIN_SCHEMA.clone()) .optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone()) .optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone()) diff --git a/src/api2/node/network.rs b/src/api2/node/network.rs index 334fde3e..3ba1debc 100644 --- a/src/api2/node/network.rs +++ b/src/api2/node/network.rs @@ -20,7 +20,9 @@ pub fn router() -> Router { let route = Router::new() .get(ApiMethod::new( get_network_config, - ObjectSchema::new("Read network configuration."))); + ObjectSchema::new("Read network configuration.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) + )); route } diff --git a/src/api2/node/services.rs b/src/api2/node/services.rs index 2c689621..7123c18f 100644 --- a/src/api2/node/services.rs +++ b/src/api2/node/services.rs @@ -226,6 +226,7 @@ pub fn router() -> Router { Ok(Value::from(result)) }, ObjectSchema::new("Directory index.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("service", service_id_schema.clone())) ) .subdir( @@ -234,7 +235,8 @@ pub fn router() -> Router { .get(ApiMethod::new( get_service_state, ObjectSchema::new("Read service properties.") - .required("service", service_id_schema.clone())) + .required("node", crate::api2::node::NODE_SCHEMA.clone()) + .required("service", service_id_schema.clone())) ) ) .subdir( @@ -244,6 +246,7 @@ pub fn router() -> Router { ApiMethod::new( start_service, ObjectSchema::new("Start service.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("service", service_id_schema.clone()) ).protected(true) ) @@ -255,6 +258,7 @@ pub fn router() -> Router { ApiMethod::new( stop_service, ObjectSchema::new("Stop service.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("service", service_id_schema.clone()) ).protected(true) ) @@ -266,6 +270,7 @@ pub fn router() -> Router { ApiMethod::new( restart_service, ObjectSchema::new("Restart service.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("service", service_id_schema.clone()) ).protected(true) ) @@ -277,6 +282,7 @@ pub fn router() -> Router { ApiMethod::new( reload_service, ObjectSchema::new("Reload service.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("service", service_id_schema.clone()) ).protected(true) ) @@ -288,6 +294,7 @@ pub fn router() -> Router { ApiMethod::new( list_services, ObjectSchema::new("Service list.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) ).returns( ArraySchema::new( "Returns a list of systemd services.", diff --git a/src/api2/node/syslog.rs b/src/api2/node/syslog.rs index 204d6d38..3aff37f1 100644 --- a/src/api2/node/syslog.rs +++ b/src/api2/node/syslog.rs @@ -101,7 +101,8 @@ pub fn router() -> Router { ApiMethod::new( get_syslog, ObjectSchema::new("Read server time and time zone settings.") - .optional( + .required("node", crate::api2::node::NODE_SCHEMA.clone()) + .optional( "start", IntegerSchema::new("Start line number.") .minimum(0) diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs index 1016b7ca..be5d34f1 100644 --- a/src/api2/node/tasks.rs +++ b/src/api2/node/tasks.rs @@ -24,7 +24,7 @@ fn get_task_status( }) } else { json!({ - "status": "running", + "status": "stopped", }) }; @@ -46,7 +46,7 @@ fn extract_upid(param: &Value) -> Result { fn read_task_log( param: Value, _info: &ApiMethod, - _rpcenv: &mut RpcEnvironment, + rpcenv: &mut RpcEnvironment, ) -> Result { let upid = extract_upid(¶m)?; @@ -78,6 +78,8 @@ fn read_task_log( } } + rpcenv.set_result_attrib("total", Value::from(count)); + Ok(json!(lines)) } @@ -141,7 +143,7 @@ fn list_tasks( pub fn router() -> Router { - let upid_schema : Arc = Arc::new( + let upid_schema: Arc = Arc::new( StringSchema::new("Unique Process/Task ID.") .max_length(256) .into() @@ -157,6 +159,7 @@ pub fn router() -> Router { Ok(Value::from(result)) }, ObjectSchema::new("Directory index.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("upid", upid_schema.clone())) ) .subdir( @@ -165,6 +168,7 @@ pub fn router() -> Router { ApiMethod::new( read_task_log, ObjectSchema::new("Read task log.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("upid", upid_schema.clone()) .optional( "start", @@ -187,6 +191,7 @@ pub fn router() -> Router { ApiMethod::new( get_task_status, ObjectSchema::new("Get task status.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("upid", upid_schema.clone())) ) ); @@ -196,6 +201,7 @@ pub fn router() -> Router { .get(ApiMethod::new( list_tasks, ObjectSchema::new("List tasks.") + .required("node", crate::api2::node::NODE_SCHEMA.clone()) .optional( "start", IntegerSchema::new("List tasks beginning from this offset.") diff --git a/src/api2/node/time.rs b/src/api2/node/time.rs index 67407206..a77227eb 100644 --- a/src/api2/node/time.rs +++ b/src/api2/node/time.rs @@ -79,7 +79,8 @@ pub fn router() -> Router { ApiMethod::new( get_time, ObjectSchema::new("Read server time and time zone settings.") - ).returns( + .required("node", crate::api2::node::NODE_SCHEMA.clone()) + ).returns( ObjectSchema::new("Returns server time and timezone.") .required("timezone", StringSchema::new("Time zone")) .required("time", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC.") @@ -92,7 +93,9 @@ pub fn router() -> Router { ApiMethod::new( set_timezone, ObjectSchema::new("Set time zone.") - .required("timezone", StringSchema::new("Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")) + .required("node", crate::api2::node::NODE_SCHEMA.clone()) + .required("timezone", StringSchema::new( + "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")) ).protected(true).reload_timezone(true) );