From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 0C5A11FF183 for ; Wed, 2 Jul 2025 16:52:59 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 275891EAF8; Wed, 2 Jul 2025 16:51:45 +0200 (CEST) From: Gabriel Goller To: pve-devel@lists.proxmox.com Date: Wed, 2 Jul 2025 16:49:47 +0200 Message-Id: <20250702145101.894299-3-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250702145101.894299-1-g.goller@proxmox.com> References: <20250702145101.894299-1-g.goller@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.019 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH proxmox v4 2/5] network-types: make cidr and mac-address types usable by the api X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" From: Stefan Hanreich Implement ApiType and UpdaterType in order to be able to directly use the CIDR and MacAddress types in the API. Their schema is a string and they get (de-)serialized by the respective FromStr / Display implementations. Signed-off-by: Stefan Hanreich --- proxmox-network-types/Cargo.toml | 1 + proxmox-network-types/src/ip_address.rs | 36 ++++++++++++++++++++++++ proxmox-network-types/src/mac_address.rs | 25 ++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/proxmox-network-types/Cargo.toml b/proxmox-network-types/Cargo.toml index aa6d66bc8d0c..3dda51d4660f 100644 --- a/proxmox-network-types/Cargo.toml +++ b/proxmox-network-types/Cargo.toml @@ -19,3 +19,4 @@ proxmox-schema = { workspace = true, features = [ "api-macro", "api-types" ], op [features] default = [] +api-types = ["dep:proxmox-schema", "dep:regex"] diff --git a/proxmox-network-types/src/ip_address.rs b/proxmox-network-types/src/ip_address.rs index 355547b17ae0..827141b9e86d 100644 --- a/proxmox-network-types/src/ip_address.rs +++ b/proxmox-network-types/src/ip_address.rs @@ -5,6 +5,12 @@ use std::net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr}; use serde_with::{DeserializeFromStr, SerializeDisplay}; use thiserror::Error; +#[cfg(feature = "api-types")] +use proxmox_schema::{ApiType, Schema, UpdaterType}; + +#[cfg(feature = "api-types")] +use proxmox_schema::api_types::{CIDR_SCHEMA, CIDR_V4_SCHEMA, CIDR_V6_SCHEMA}; + /// The family (v4 or v6) of an IP address or CIDR prefix #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Family { @@ -48,6 +54,16 @@ pub enum Cidr { Ipv6(Ipv6Cidr), } +#[cfg(feature = "api-types")] +impl ApiType for Cidr { + const API_SCHEMA: Schema = CIDR_SCHEMA; +} + +#[cfg(feature = "api-types")] +impl UpdaterType for Cidr { + type Updater = Option; +} + impl Cidr { pub fn new_v4(addr: impl Into, mask: u8) -> Result { Ok(Cidr::Ipv4(Ipv4Cidr::new(addr, mask)?)) @@ -139,6 +155,16 @@ pub struct Ipv4Cidr { mask: u8, } +#[cfg(feature = "api-types")] +impl ApiType for Ipv4Cidr { + const API_SCHEMA: Schema = CIDR_V4_SCHEMA; +} + +#[cfg(feature = "api-types")] +impl UpdaterType for Ipv4Cidr { + type Updater = Option; +} + impl Ipv4Cidr { pub fn new(addr: impl Into, mask: u8) -> Result { if mask > IPV4_LENGTH { @@ -217,6 +243,16 @@ pub struct Ipv6Cidr { mask: u8, } +#[cfg(feature = "api-types")] +impl ApiType for Ipv6Cidr { + const API_SCHEMA: Schema = CIDR_V6_SCHEMA; +} + +#[cfg(feature = "api-types")] +impl UpdaterType for Ipv6Cidr { + type Updater = Option; +} + impl Ipv6Cidr { pub fn new(addr: impl Into, mask: u8) -> Result { if mask > IPV6_LENGTH { diff --git a/proxmox-network-types/src/mac_address.rs b/proxmox-network-types/src/mac_address.rs index d347076ebd49..4ad82699fb67 100644 --- a/proxmox-network-types/src/mac_address.rs +++ b/proxmox-network-types/src/mac_address.rs @@ -4,6 +4,9 @@ use std::net::Ipv6Addr; use serde_with::{DeserializeFromStr, SerializeDisplay}; use thiserror::Error; +#[cfg(feature = "api-types")] +use proxmox_schema::{const_regex, ApiStringFormat, ApiType, Schema, StringSchema, UpdaterType}; + #[derive(Error, Debug)] pub enum MacAddressError { #[error("the hostname must be from 1 to 63 characters long")] @@ -12,12 +15,34 @@ pub enum MacAddressError { InvalidSymbols, } +#[cfg(feature = "api-types")] +const_regex! { + pub MAC_ADDRESS_REGEX = r"([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}"; +} + +#[cfg(feature = "api-types")] +pub const MAC_ADDRESS_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&MAC_ADDRESS_REGEX); + /// EUI-48 MAC Address #[derive( Clone, Copy, Debug, DeserializeFromStr, SerializeDisplay, PartialEq, Eq, Hash, PartialOrd, Ord, )] pub struct MacAddress([u8; 6]); +#[cfg(feature = "api-types")] +impl ApiType for MacAddress { + const API_SCHEMA: Schema = StringSchema::new("MAC address") + .min_length(17) + .max_length(17) + .format(&MAC_ADDRESS_FORMAT) + .schema(); +} + +#[cfg(feature = "api-types")] +impl UpdaterType for MacAddress { + type Updater = Option; +} + static LOCAL_PART: [u8; 8] = [0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; static EUI64_MIDDLE_PART: [u8; 2] = [0xFF, 0xFE]; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel