src/api2/node.rs: add node parameter

This commit is contained in:
Dietmar Maurer 2019-04-09 14:43:30 +02:00
parent 7ae0bf52dd
commit d8d40dd0c2
8 changed files with 52 additions and 10 deletions

View File

@ -26,13 +26,12 @@ lazy_static! {
pub static ref PVE_CONFIG_DIGEST_SCHEMA: Arc<Schema> =
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(

View File

@ -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<Schema> = 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()

View File

@ -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())

View File

@ -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
}

View File

@ -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.",

View File

@ -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)

View File

@ -24,7 +24,7 @@ fn get_task_status(
})
} else {
json!({
"status": "running",
"status": "stopped",
})
};
@ -46,7 +46,7 @@ fn extract_upid(param: &Value) -> Result<UPID, Error> {
fn read_task_log(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut RpcEnvironment,
rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> {
let upid = extract_upid(&param)?;
@ -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<Schema> = Arc::new(
let upid_schema: Arc<Schema> = 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.")

View File

@ -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)
);