gioser

tawasuyu/brahman

Rust 326TOML 123JSON 14Markdown 114554 Definiciones

⎇ 3f8a3ea4b6d3de529e42ae418d486bc81cca308e crates/modules/matilda/matilda-core/src/host.rs Rust · 46 líneas · 1.3 KB Historia Quién tocó qué crudo

feat(matilda): administración de servidores — core + config + plan 3f8a3ea4b · sergio ·

1//! `Host` — un servidor administrado.
2
3use serde::{Deserialize, Serialize};
4
5/// Un servidor bajo administración. La clave única es `name`.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct Host {
8    /// Nombre lógico — clave de inventario, no necesariamente el hostname.
9    pub name: String,
10    /// IP o nombre DNS por el que se alcanza.
11    pub address: String,
12    /// Etiquetas libres — `"prod"`, `"db"`, `"edge"`.
13    pub tags: Vec<String>,
14}
15
16impl Host {
17    pub fn new(name: impl Into<String>, address: impl Into<String>) -> Self {
18        Self { name: name.into(), address: address.into(), tags: Vec::new() }
19    }
20
21    /// Añade una etiqueta (encadenable). No duplica.
22    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
23        let tag = tag.into();
24        if !self.tags.contains(&tag) {
25            self.tags.push(tag);
26        }
27        self
28    }
29
30    /// `true` si el host lleva la etiqueta `tag`.
31    pub fn has_tag(&self, tag: &str) -> bool {
32        self.tags.iter().any(|t| t == tag)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn with_tag_dedups() {
42        let h = Host::new("edge-1", "10.0.0.1").with_tag("prod").with_tag("prod");
43        assert_eq!(h.tags.len(), 1);
44        assert!(h.has_tag("prod"));
45    }
46}