public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [RFC proxmox 13/22] firewall-api-types: add FirewallAliasReference type
Date: Mon, 16 Feb 2026 11:43:51 +0100	[thread overview]
Message-ID: <20260216104401.3959270-14-dietmar@proxmox.com> (raw)
In-Reply-To: <20260216104401.3959270-1-dietmar@proxmox.com>

This adds a new type to reference aliases with proper scope handling
(Datacenter, Guest, or None for legacy aliases).

The implementation includes:
- FirewallAliasScope enum for scope variants
- FirewallAliasReference struct with validation
- Proper encapsulation with constructor and accessor methods
- FromStr implementation for parsing alias references

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
---
 proxmox-firewall-api-types/src/alias.rs | 142 ++++++++++++++++++++++++
 proxmox-firewall-api-types/src/lib.rs   |   3 +
 2 files changed, 145 insertions(+)
 create mode 100644 proxmox-firewall-api-types/src/alias.rs

diff --git a/proxmox-firewall-api-types/src/alias.rs b/proxmox-firewall-api-types/src/alias.rs
new file mode 100644
index 00000000..7722148c
--- /dev/null
+++ b/proxmox-firewall-api-types/src/alias.rs
@@ -0,0 +1,142 @@
+use std::fmt;
+use std::str::FromStr;
+
+use anyhow::{bail, Error};
+
+#[cfg(feature = "enum-fallback")]
+use proxmox_fixed_string::FixedString;
+
+/// The scope of an alias.
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum FirewallAliasScope {
+    /// Datacenter scope.
+    Datacenter,
+    /// Guest scope.
+    Guest,
+    /// No scope (e.g. for legacy aliases).
+    None,
+    #[cfg(feature = "enum-fallback")]
+    /// Unknown variants for forward compatibility.
+    UnknownEnumValue(FixedString),
+}
+
+/// A reference to an alias, including its scope.
+#[derive(Debug, Clone, Eq, PartialEq)]
+pub struct FirewallAliasReference {
+    scope: FirewallAliasScope,
+    name: String,
+}
+
+impl FirewallAliasReference {
+    pub fn new(scope: FirewallAliasScope, name: String) -> Result<Self, Error> {
+        verify_alias_name(&name)?;
+        Ok(Self { scope, name })
+    }
+
+    pub fn scope(&self) -> FirewallAliasScope {
+        self.scope
+    }
+
+    pub fn name(&self) -> &str {
+        &self.name
+    }
+}
+
+fn verify_alias_name(name: &str) -> Result<(), Error> {
+    if name.is_empty() {
+        bail!("alias name cannot be empty");
+    }
+
+    if !name.starts_with(|c: char| c.is_ascii_alphabetic()) {
+        bail!("alias name must start with an ASCII letter");
+    }
+
+    if name.contains(|c: char| !c.is_ascii_alphanumeric() && c != '-' && c != '_') {
+        bail!("alias name can only contain ASCII letters, digits, hyphens, and underscores");
+    }
+
+    Ok(())
+}
+
+impl fmt::Display for FirewallAliasReference {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self.scope {
+            FirewallAliasScope::Datacenter => write!(f, "dc/{}", self.name),
+            FirewallAliasScope::Guest => write!(f, "guest/{}", self.name),
+            #[cfg(feature = "enum-fallback")]
+            FirewallAliasScope::UnknownEnumValue(scope) => write!(f, "{}/{}", scope, self.name),
+            FirewallAliasScope::None => self.name.fmt(f),
+        }
+    }
+}
+
+impl FromStr for FirewallAliasReference {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        let s = s.trim();
+
+        if s.is_empty() {
+            bail!("empty firewall alias specification");
+        }
+
+        let (scope, name) = match s.split_once('/') {
+            Some(("dc", alias)) => (FirewallAliasScope::Datacenter, alias),
+            Some(("guest", alias)) => (FirewallAliasScope::Guest, alias),
+            #[cfg(not(feature = "enum-fallback"))]
+            Some((scope, _alias)) => bail!("invalid firewall alias scope: {scope}"),
+            #[cfg(feature = "enum-fallback")]
+            Some((scope, alias)) => (
+                FirewallAliasScope::UnknownEnumValue(FixedString::from_str(scope)?),
+                alias,
+            ),
+            None => (FirewallAliasScope::None, s),
+        };
+
+        Self::new(scope, name.to_string())
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_parse_legacy_alias_name() {
+        for name in ["proxmox_123", "proxmox-123"] {
+            name.parse::<FirewallAliasReference>()
+                .expect("valid alias name");
+        }
+
+        for name in ["1proxmox_123", "-proxmox-123"] {
+            name.parse::<FirewallAliasReference>()
+                .expect_err("invalid alias name");
+        }
+    }
+
+    #[test]
+    fn test_new_alias() {
+        let alias =
+            FirewallAliasReference::new(FirewallAliasScope::Datacenter, "proxmox-123".to_string())
+                .expect("valid alias name");
+        assert_eq!(alias.scope(), FirewallAliasScope::Datacenter);
+        assert_eq!(alias.name(), "proxmox-123");
+
+        FirewallAliasReference::new(FirewallAliasScope::Datacenter, "invalid/name".to_string())
+            .expect_err("invalid alias name");
+    }
+
+    #[test]
+    fn test_parse_alias_name() {
+        for name in ["dc/proxmox_123", "guest/proxmox-123"] {
+            name.parse::<FirewallAliasReference>()
+                .expect("valid alias name");
+        }
+
+        #[cfg(not(feature = "enum-fallback"))]
+        for name in ["proxmox/proxmox_123", "guests/proxmox-123", "dc/", "/name"] {
+            name.parse::<FirewallAliasReference>()
+                .expect_err("invalid alias name");
+        }
+    }
+}
diff --git a/proxmox-firewall-api-types/src/lib.rs b/proxmox-firewall-api-types/src/lib.rs
index abd78c98..8fae5042 100644
--- a/proxmox-firewall-api-types/src/lib.rs
+++ b/proxmox-firewall-api-types/src/lib.rs
@@ -1,3 +1,6 @@
+mod alias;
+pub use alias::{FirewallAliasReference, FirewallAliasScope};
+
 mod conntrack;
 pub use conntrack::FirewallConntrackHelper;
 
-- 
2.47.3




  parent reply	other threads:[~2026-02-16 10:45 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-16 10:43 [RFC proxmox 00/22] New crate for firewall api types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 01/22] firewall-api-types: add new " Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 02/22] firewall-api-types: add README.md Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 03/22] firewall-api-types: add firewall policy types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 04/22] firewall-api-types: add logging types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 05/22] firewall-api-types: add FirewallClusterOptions Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 06/22] firewall-api-types: add FirewallGuestOptions Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 07/22] firewall-api-types: add FirewallConntrackHelper enum Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 08/22] firewall-api-types: add FirewallNodeOptions struct Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 09/22] firewall-api-types: add FirewallRef type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 10/22] firewall-api-types: add FirewallPortList types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 11/22] firewall-api-types: add FirewallIcmpType Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 12/22] firewall-api-types: add FirewallIpsetReference type Dietmar Maurer
2026-02-16 10:43 ` Dietmar Maurer [this message]
2026-02-16 10:43 ` [RFC proxmox 14/22] firewall-api-types: add firewall address types Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 15/22] firewall-api-types: add FirewallRule type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 16/22] firewall-api-types: use ConfigDigest from proxmox-config-digest crate Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 17/22] firewall-api-types: use COMMENT_SCHEMA from proxmox-schema crate Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 18/22] firewall-api-types: add FirewallRuleUpdater type Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 19/22] firewall-api-types: refactor FirewallRule and add FirewallRuleListEntry Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 20/22] firewall-api-types: add DeletableFirewallRuleProperty enum Dietmar Maurer
2026-02-16 10:43 ` [RFC proxmox 21/22] firewall-api-types: add FirewallAliasEntry API type Dietmar Maurer
2026-02-16 10:44 ` [RFC proxmox 22/22] firewall-api-types: add FirewallIpsetListEntry and FirewallIpsetEntry api types Dietmar Maurer
2026-02-17  6:17 ` [RFC proxmox 00/22] New crate for firewall " Hannes Laimer
2026-02-17  6:39   ` Dietmar Maurer
2026-02-17  8:17     ` Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260216104401.3959270-14-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal