src/api2/types.rs: use separate file for schema definitions
This commit is contained in:
parent
bd1507c4fa
commit
4ebf0eabb0
23
src/api2.rs
23
src/api2.rs
|
@ -1,9 +1,4 @@
|
|||
//use failure::*;
|
||||
|
||||
use crate::api_schema::*;
|
||||
use crate::api_schema::router::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod types;
|
||||
pub mod config;
|
||||
pub mod admin;
|
||||
pub mod node;
|
||||
|
@ -11,21 +6,7 @@ mod version;
|
|||
mod subscription;
|
||||
mod access;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
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();
|
||||
}
|
||||
use crate::api_schema::router::*;
|
||||
|
||||
pub fn router() -> Router {
|
||||
|
||||
|
|
|
@ -1,10 +1,4 @@
|
|||
use failure::*;
|
||||
|
||||
use crate::tools;
|
||||
use crate::api_schema::*;
|
||||
use crate::api_schema::router::*;
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod tasks;
|
||||
mod time;
|
||||
|
@ -13,23 +7,6 @@ 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()
|
||||
|
|
|
@ -3,8 +3,9 @@ use failure::*;
|
|||
|
||||
use crate::tools;
|
||||
use crate::api2::*;
|
||||
//use crate::api_schema::*;
|
||||
use crate::api_schema::*;
|
||||
//use crate::api_schema::router::*;
|
||||
use crate::api2::types::*;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
|
@ -105,23 +106,6 @@ fn get_dns(
|
|||
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 {
|
||||
|
||||
let route = Router::new()
|
||||
|
@ -129,7 +113,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::new(
|
||||
get_dns,
|
||||
ObjectSchema::new("Read DNS settings.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
).returns(
|
||||
ObjectSchema::new("Returns DNS server IPs and sreach domain.")
|
||||
.required("digest", PVE_CONFIG_DIGEST_SCHEMA.clone())
|
||||
|
@ -143,7 +127,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("node", NODE_SCHEMA.clone())
|
||||
.required("search", SEARCH_DOMAIN_SCHEMA.clone())
|
||||
.optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone())
|
||||
.optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone())
|
||||
|
|
|
@ -5,6 +5,7 @@ use crate::api_schema::*;
|
|||
use crate::api_schema::router::*;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::api2::types::*;
|
||||
|
||||
fn get_network_config(
|
||||
_param: Value,
|
||||
|
@ -21,7 +22,7 @@ pub fn router() -> Router {
|
|||
.get(ApiMethod::new(
|
||||
get_network_config,
|
||||
ObjectSchema::new("Read network configuration.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
));
|
||||
|
||||
route
|
||||
|
|
|
@ -8,6 +8,8 @@ use serde_json::{json, Value};
|
|||
use std::sync::Arc;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use crate::api2::types::*;
|
||||
|
||||
static SERVICE_NAME_LIST: [&str; 7] = [
|
||||
"proxmox-backup",
|
||||
"proxmox-backup-proxy",
|
||||
|
@ -229,7 +231,7 @@ pub fn router() -> Router {
|
|||
.get(ApiMethod::new(
|
||||
get_service_state,
|
||||
ObjectSchema::new("Read service properties.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
.required("service", service_id_schema.clone()))
|
||||
)
|
||||
)
|
||||
|
@ -240,7 +242,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::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())
|
||||
).protected(true)
|
||||
)
|
||||
|
@ -252,7 +254,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::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())
|
||||
).protected(true)
|
||||
)
|
||||
|
@ -264,7 +266,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::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())
|
||||
).protected(true)
|
||||
)
|
||||
|
@ -276,7 +278,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::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())
|
||||
).protected(true)
|
||||
)
|
||||
|
@ -288,7 +290,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::new(
|
||||
list_services,
|
||||
ObjectSchema::new("Service list.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
).returns(
|
||||
ArraySchema::new(
|
||||
"Returns a list of systemd services.",
|
||||
|
|
|
@ -2,6 +2,8 @@ use failure::*;
|
|||
|
||||
use crate::api_schema::*;
|
||||
use crate::api_schema::router::*;
|
||||
use crate::api2::types::*;
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
@ -101,7 +103,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::new(
|
||||
get_syslog,
|
||||
ObjectSchema::new("Read server time and time zone settings.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
.optional(
|
||||
"start",
|
||||
IntegerSchema::new("Start line number.")
|
||||
|
|
|
@ -8,6 +8,7 @@ use std::sync::Arc;
|
|||
use std::fs::File;
|
||||
use std::io::{BufRead,BufReader};
|
||||
|
||||
use crate::api2::types::*;
|
||||
use crate::server::{self, UPID};
|
||||
|
||||
fn get_task_status(
|
||||
|
@ -177,7 +178,7 @@ pub fn router() -> Router {
|
|||
.delete(ApiMethod::new(
|
||||
stop_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)
|
||||
|
||||
)
|
||||
|
@ -187,7 +188,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::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())
|
||||
.optional(
|
||||
"start",
|
||||
|
@ -210,7 +211,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::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()))
|
||||
)
|
||||
)
|
||||
|
@ -221,7 +222,7 @@ pub fn router() -> Router {
|
|||
.get(ApiMethod::new(
|
||||
list_tasks,
|
||||
ObjectSchema::new("List tasks.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
.optional(
|
||||
"start",
|
||||
IntegerSchema::new("List tasks beginning from this offset.")
|
||||
|
|
|
@ -3,6 +3,8 @@ use failure::*;
|
|||
use crate::tools;
|
||||
use crate::api_schema::*;
|
||||
use crate::api_schema::router::*;
|
||||
use crate::api2::types::*;
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use chrono::prelude::*;
|
||||
|
@ -79,7 +81,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::new(
|
||||
get_time,
|
||||
ObjectSchema::new("Read server time and time zone settings.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("node", NODE_SCHEMA.clone())
|
||||
).returns(
|
||||
ObjectSchema::new("Returns server time and timezone.")
|
||||
.required("timezone", StringSchema::new("Time zone"))
|
||||
|
@ -93,7 +95,7 @@ pub fn router() -> Router {
|
|||
ApiMethod::new(
|
||||
set_timezone,
|
||||
ObjectSchema::new("Set time zone.")
|
||||
.required("node", crate::api2::node::NODE_SCHEMA.clone())
|
||||
.required("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)
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue