src/api2/types.rs: use separate file for schema definitions

This commit is contained in:
Dietmar Maurer 2019-05-09 07:44:09 +02:00
parent bd1507c4fa
commit 4ebf0eabb0
9 changed files with 77 additions and 78 deletions

View File

@ -1,9 +1,4 @@
//use failure::*; pub mod types;
use crate::api_schema::*;
use crate::api_schema::router::*;
use std::sync::Arc;
pub mod config; pub mod config;
pub mod admin; pub mod admin;
pub mod node; pub mod node;
@ -11,21 +6,7 @@ mod version;
mod subscription; mod subscription;
mod access; mod access;
use lazy_static::lazy_static; use crate::api_schema::router::*;
use crate::tools::common_regex;
// common schema definitions
lazy_static! {
pub static ref IP_FORMAT: Arc<ApiStringFormat> = ApiStringFormat::Pattern(&common_regex::IP_REGEX).into();
pub static ref PVE_CONFIG_DIGEST_FORMAT: Arc<ApiStringFormat> =
ApiStringFormat::Pattern(&common_regex::SHA256_HEX_REGEX).into();
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 { pub fn router() -> Router {

View File

@ -1,10 +1,4 @@
use failure::*;
use crate::tools;
use crate::api_schema::*;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use lazy_static::lazy_static;
use std::sync::Arc;
mod tasks; mod tasks;
mod time; mod time;
@ -13,23 +7,6 @@ mod dns;
mod syslog; mod syslog;
mod services; 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 { pub fn router() -> Router {
let route = Router::new() let route = Router::new()

View File

@ -3,8 +3,9 @@ use failure::*;
use crate::tools; use crate::tools;
use crate::api2::*; use crate::api2::*;
//use crate::api_schema::*; use crate::api_schema::*;
//use crate::api_schema::router::*; //use crate::api_schema::router::*;
use crate::api2::types::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -105,23 +106,6 @@ fn get_dns(
read_etc_resolv_conf() read_etc_resolv_conf()
} }
lazy_static! {
pub static ref SEARCH_DOMAIN_SCHEMA: Arc<Schema> =
StringSchema::new("Search domain for host-name lookup.").into();
pub static ref FIRST_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("First name server IP address.")
.format(IP_FORMAT.clone()).into();
pub static ref SECOND_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("Second name server IP address.")
.format(IP_FORMAT.clone()).into();
pub static ref THIRD_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("Third name server IP address.")
.format(IP_FORMAT.clone()).into();
}
pub fn router() -> Router { pub fn router() -> Router {
let route = Router::new() let route = Router::new()
@ -129,7 +113,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
get_dns, get_dns,
ObjectSchema::new("Read DNS settings.") ObjectSchema::new("Read DNS settings.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
).returns( ).returns(
ObjectSchema::new("Returns DNS server IPs and sreach domain.") ObjectSchema::new("Returns DNS server IPs and sreach domain.")
.required("digest", PVE_CONFIG_DIGEST_SCHEMA.clone()) .required("digest", PVE_CONFIG_DIGEST_SCHEMA.clone())
@ -143,7 +127,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
update_dns, update_dns,
ObjectSchema::new("Returns DNS server IPs and sreach domain.") ObjectSchema::new("Returns DNS server IPs and sreach domain.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("search", SEARCH_DOMAIN_SCHEMA.clone()) .required("search", SEARCH_DOMAIN_SCHEMA.clone())
.optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone()) .optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone())
.optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone()) .optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone())

View File

@ -5,6 +5,7 @@ use crate::api_schema::*;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
use crate::api2::types::*;
fn get_network_config( fn get_network_config(
_param: Value, _param: Value,
@ -21,7 +22,7 @@ pub fn router() -> Router {
.get(ApiMethod::new( .get(ApiMethod::new(
get_network_config, get_network_config,
ObjectSchema::new("Read network configuration.") ObjectSchema::new("Read network configuration.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
)); ));
route route

View File

@ -8,6 +8,8 @@ use serde_json::{json, Value};
use std::sync::Arc; use std::sync::Arc;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use crate::api2::types::*;
static SERVICE_NAME_LIST: [&str; 7] = [ static SERVICE_NAME_LIST: [&str; 7] = [
"proxmox-backup", "proxmox-backup",
"proxmox-backup-proxy", "proxmox-backup-proxy",
@ -229,7 +231,7 @@ pub fn router() -> Router {
.get(ApiMethod::new( .get(ApiMethod::new(
get_service_state, get_service_state,
ObjectSchema::new("Read service properties.") ObjectSchema::new("Read service properties.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("service", service_id_schema.clone())) .required("service", service_id_schema.clone()))
) )
) )
@ -240,7 +242,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
start_service, start_service,
ObjectSchema::new("Start service.") ObjectSchema::new("Start service.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("service", service_id_schema.clone()) .required("service", service_id_schema.clone())
).protected(true) ).protected(true)
) )
@ -252,7 +254,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
stop_service, stop_service,
ObjectSchema::new("Stop service.") ObjectSchema::new("Stop service.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("service", service_id_schema.clone()) .required("service", service_id_schema.clone())
).protected(true) ).protected(true)
) )
@ -264,7 +266,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
restart_service, restart_service,
ObjectSchema::new("Restart service.") ObjectSchema::new("Restart service.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("service", service_id_schema.clone()) .required("service", service_id_schema.clone())
).protected(true) ).protected(true)
) )
@ -276,7 +278,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
reload_service, reload_service,
ObjectSchema::new("Reload service.") ObjectSchema::new("Reload service.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("service", service_id_schema.clone()) .required("service", service_id_schema.clone())
).protected(true) ).protected(true)
) )
@ -288,7 +290,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
list_services, list_services,
ObjectSchema::new("Service list.") ObjectSchema::new("Service list.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
).returns( ).returns(
ArraySchema::new( ArraySchema::new(
"Returns a list of systemd services.", "Returns a list of systemd services.",

View File

@ -2,6 +2,8 @@ use failure::*;
use crate::api_schema::*; use crate::api_schema::*;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use crate::api2::types::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::sync::Arc; use std::sync::Arc;
@ -101,7 +103,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
get_syslog, get_syslog,
ObjectSchema::new("Read server time and time zone settings.") ObjectSchema::new("Read server time and time zone settings.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.optional( .optional(
"start", "start",
IntegerSchema::new("Start line number.") IntegerSchema::new("Start line number.")

View File

@ -8,6 +8,7 @@ use std::sync::Arc;
use std::fs::File; use std::fs::File;
use std::io::{BufRead,BufReader}; use std::io::{BufRead,BufReader};
use crate::api2::types::*;
use crate::server::{self, UPID}; use crate::server::{self, UPID};
fn get_task_status( fn get_task_status(
@ -177,7 +178,7 @@ pub fn router() -> Router {
.delete(ApiMethod::new( .delete(ApiMethod::new(
stop_task, stop_task,
ObjectSchema::new("Try to stop a task.") ObjectSchema::new("Try to stop a task.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("upid", upid_schema.clone())).protected(true) .required("upid", upid_schema.clone())).protected(true)
) )
@ -187,7 +188,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
read_task_log, read_task_log,
ObjectSchema::new("Read task log.") ObjectSchema::new("Read task log.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("upid", upid_schema.clone()) .required("upid", upid_schema.clone())
.optional( .optional(
"start", "start",
@ -210,7 +211,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
get_task_status, get_task_status,
ObjectSchema::new("Get task status.") ObjectSchema::new("Get task status.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("upid", upid_schema.clone())) .required("upid", upid_schema.clone()))
) )
) )
@ -221,7 +222,7 @@ pub fn router() -> Router {
.get(ApiMethod::new( .get(ApiMethod::new(
list_tasks, list_tasks,
ObjectSchema::new("List tasks.") ObjectSchema::new("List tasks.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.optional( .optional(
"start", "start",
IntegerSchema::new("List tasks beginning from this offset.") IntegerSchema::new("List tasks beginning from this offset.")

View File

@ -3,6 +3,8 @@ use failure::*;
use crate::tools; use crate::tools;
use crate::api_schema::*; use crate::api_schema::*;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use crate::api2::types::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
use chrono::prelude::*; use chrono::prelude::*;
@ -79,7 +81,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
get_time, get_time,
ObjectSchema::new("Read server time and time zone settings.") ObjectSchema::new("Read server time and time zone settings.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
).returns( ).returns(
ObjectSchema::new("Returns server time and timezone.") ObjectSchema::new("Returns server time and timezone.")
.required("timezone", StringSchema::new("Time zone")) .required("timezone", StringSchema::new("Time zone"))
@ -93,7 +95,7 @@ pub fn router() -> Router {
ApiMethod::new( ApiMethod::new(
set_timezone, set_timezone,
ObjectSchema::new("Set time zone.") ObjectSchema::new("Set time zone.")
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", NODE_SCHEMA.clone())
.required("timezone", StringSchema::new( .required("timezone", StringSchema::new(
"Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")) "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names."))
).protected(true).reload_timezone(true) ).protected(true).reload_timezone(true)

49
src/api2/types.rs Normal file
View File

@ -0,0 +1,49 @@
use failure::*;
use lazy_static::lazy_static;
use std::sync::Arc;
use crate::api_schema::*;
use crate::tools::{self, common_regex};
lazy_static!{
pub static ref IP_FORMAT: Arc<ApiStringFormat> = ApiStringFormat::Pattern(&common_regex::IP_REGEX).into();
pub static ref PVE_CONFIG_DIGEST_FORMAT: Arc<ApiStringFormat> =
ApiStringFormat::Pattern(&common_regex::SHA256_HEX_REGEX).into();
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 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 static ref SEARCH_DOMAIN_SCHEMA: Arc<Schema> =
StringSchema::new("Search domain for host-name lookup.").into();
pub static ref FIRST_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("First name server IP address.")
.format(IP_FORMAT.clone()).into();
pub static ref SECOND_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("Second name server IP address.")
.format(IP_FORMAT.clone()).into();
pub static ref THIRD_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("Third name server IP address.")
.format(IP_FORMAT.clone()).into();
}