src/api2/types.rs: define BLOCKDEVICE_NAME_SCHEMA

This commit is contained in:
Dietmar Maurer 2020-06-07 07:15:15 +02:00
parent fa2bdc1309
commit 9069debcd8
4 changed files with 16 additions and 9 deletions

View File

@ -11,7 +11,7 @@ use crate::tools::disks::{
get_disks, get_smart_data, get_disks, get_smart_data,
}; };
use crate::api2::types::NODE_SCHEMA; use crate::api2::types::{NODE_SCHEMA, BLOCKDEVICE_NAME_SCHEMA};
#[api( #[api(
protected: true, protected: true,
@ -72,8 +72,7 @@ pub fn list_disks(
schema: NODE_SCHEMA, schema: NODE_SCHEMA,
}, },
disk: { disk: {
description: "Block device name.", schema: BLOCKDEVICE_NAME_SCHEMA,
type: String,
}, },
healthonly: { healthonly: {
description: "If true returns only the health status.", description: "If true returns only the health status.",

View File

@ -74,6 +74,8 @@ const_regex!{
pub CERT_FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$"; pub CERT_FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$";
pub ACL_PATH_REGEX = concat!(r"^(?:/|", r"(?:/", PROXMOX_SAFE_ID_REGEX_STR!(), ")+", r")$"); pub ACL_PATH_REGEX = concat!(r"^(?:/|", r"(?:/", PROXMOX_SAFE_ID_REGEX_STR!(), ")+", r")$");
pub BLOCKDEVICE_NAME_REGEX = r"^(:?(:?h|s|x?v)d[a-z]+)|(:?nvme\d+n\d+)$";
} }
pub const SYSTEMD_DATETIME_FORMAT: ApiStringFormat = pub const SYSTEMD_DATETIME_FORMAT: ApiStringFormat =
@ -133,6 +135,8 @@ pub const CIDR_V6_FORMAT: ApiStringFormat =
pub const CIDR_FORMAT: ApiStringFormat = pub const CIDR_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&CIDR_REGEX); ApiStringFormat::Pattern(&CIDR_REGEX);
pub const BLOCKDEVICE_NAME_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&BLOCKDEVICE_NAME_REGEX);
pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.") pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.")
.format(&PASSWORD_FORMAT) .format(&PASSWORD_FORMAT)
@ -353,6 +357,11 @@ pub const PROXMOX_GROUP_ID_SCHEMA: Schema = StringSchema::new("Group ID")
.max_length(64) .max_length(64)
.schema(); .schema();
pub const BLOCKDEVICE_NAME_SCHEMA: Schema = StringSchema::new("Block device name (/sys/block/<name>).")
.format(&BLOCKDEVICE_NAME_FORMAT)
.min_length(3)
.max_length(64)
.schema();
// Complex type definitions // Complex type definitions

View File

@ -48,8 +48,7 @@ fn list_disks(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value
input: { input: {
properties: { properties: {
disk: { disk: {
description: "Block device name.", schema: BLOCKDEVICE_NAME_SCHEMA,
type: String,
}, },
"output-format": { "output-format": {
schema: OUTPUT_FORMAT, schema: OUTPUT_FORMAT,

View File

@ -19,6 +19,8 @@ use proxmox::sys::linux::procfs::{MountInfo, mountinfo::Device};
use proxmox::{io_bail, io_format_err}; use proxmox::{io_bail, io_format_err};
use proxmox::api::api; use proxmox::api::api;
use crate::api2::types::BLOCKDEVICE_NAME_REGEX;
mod zfs; mod zfs;
pub use zfs::*; pub use zfs::*;
mod lvm; mod lvm;
@ -29,8 +31,6 @@ pub use smart::*;
lazy_static::lazy_static!{ lazy_static::lazy_static!{
static ref ISCSI_PATH_REGEX: regex::Regex = static ref ISCSI_PATH_REGEX: regex::Regex =
regex::Regex::new(r"host[^/]*/session[^/]*").unwrap(); regex::Regex::new(r"host[^/]*/session[^/]*").unwrap();
static ref BLOCKDEV_REGEX: regex::Regex =
regex::Regex::new(r"^(:?(:?h|s|x?v)d[a-z]+)|(:?nvme\d+n\d+)$").unwrap();
} }
bitflags! { bitflags! {
@ -727,7 +727,7 @@ pub fn get_disks(
let mut result = HashMap::new(); let mut result = HashMap::new();
for item in crate::tools::fs::scan_subdir(libc::AT_FDCWD, "/sys/block", &BLOCKDEV_REGEX)? { for item in crate::tools::fs::scan_subdir(libc::AT_FDCWD, "/sys/block", &BLOCKDEVICE_NAME_REGEX)? {
let item = item?; let item = item?;
let name = item.file_name().to_str().unwrap().to_string(); let name = item.file_name().to_str().unwrap().to_string();
@ -823,7 +823,7 @@ pub fn get_disks(
pub fn complete_disk_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> { pub fn complete_disk_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
let mut list = Vec::new(); let mut list = Vec::new();
let dir = match crate::tools::fs::scan_subdir(libc::AT_FDCWD, "/sys/block", &BLOCKDEV_REGEX) { let dir = match crate::tools::fs::scan_subdir(libc::AT_FDCWD, "/sys/block", &BLOCKDEVICE_NAME_REGEX) {
Ok(dir) => dir, Ok(dir) => dir,
Err(_) => return list, Err(_) => return list,
}; };