diff --git a/Cargo.lock b/Cargo.lock index a33b177..a787f9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1671,6 +1671,7 @@ dependencies = [ "env_logger", "harmony", "harmony_cli", + "harmony_types", "logging", "tokio", "url", @@ -1732,6 +1733,7 @@ dependencies = [ "harmony", "harmony_cli", "harmony_macros", + "harmony_types", "tokio", "url", ] @@ -1743,6 +1745,7 @@ dependencies = [ "cidr", "harmony", "harmony_cli", + "harmony_types", "tokio", "url", ] @@ -2293,7 +2296,6 @@ dependencies = [ "opnsense-config", "opnsense-config-xml", "pretty_assertions", - "rand 0.9.1", "reqwest 0.11.27", "russh", "rust-ipmi", @@ -2437,7 +2439,9 @@ dependencies = [ name = "harmony_types" version = "0.1.0" dependencies = [ + "rand 0.9.1", "serde", + "url", ] [[package]] @@ -3849,7 +3853,7 @@ dependencies = [ "env_logger", "log", "pretty_assertions", - "rand 0.8.5", + "rand 0.9.1", "serde", "thiserror 2.0.14", "tokio", diff --git a/Cargo.toml b/Cargo.toml index aec04b8..6be0aa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ tokio = { version = "1.40", features = [ cidr = { features = ["serde"], version = "0.2" } russh = "0.45" russh-keys = "0.45" -rand = "0.8" +rand = "0.9" url = "2.5" kube = { version = "1.1.0", features = [ "config", @@ -67,3 +67,4 @@ serde = { version = "1.0.209", features = ["derive", "rc"] } serde_json = "1.0.127" askama = "0.14" sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite" ] } +reqwest = { version = "0.12", features = ["stream", "rustls-tls", "http2"], default-features = false } diff --git a/examples/application_monitoring_with_tenant/Cargo.toml b/examples/application_monitoring_with_tenant/Cargo.toml index b9b63de..3f162d3 100644 --- a/examples/application_monitoring_with_tenant/Cargo.toml +++ b/examples/application_monitoring_with_tenant/Cargo.toml @@ -7,8 +7,9 @@ license.workspace = true [dependencies] env_logger.workspace = true -harmony = { version = "0.1.0", path = "../../harmony" } -harmony_cli = { version = "0.1.0", path = "../../harmony_cli" } +harmony = { path = "../../harmony" } +harmony_cli = { path = "../../harmony_cli" } +harmony_types = { path = "../../harmony_types" } logging = "0.1.0" tokio.workspace = true url.workspace = true diff --git a/examples/application_monitoring_with_tenant/src/main.rs b/examples/application_monitoring_with_tenant/src/main.rs index 9ab0bf6..7e60703 100644 --- a/examples/application_monitoring_with_tenant/src/main.rs +++ b/examples/application_monitoring_with_tenant/src/main.rs @@ -1,15 +1,16 @@ use std::{path::PathBuf, str::FromStr, sync::Arc}; use harmony::{ - data::Id, inventory::Inventory, modules::{ application::{ApplicationScore, RustWebFramework, RustWebapp, features::Monitoring}, monitoring::alert_channel::webhook_receiver::WebhookReceiver, tenant::TenantScore, }, - topology::{K8sAnywhereTopology, Url, tenant::TenantConfig}, + topology::{K8sAnywhereTopology, tenant::TenantConfig}, }; +use harmony_types::id::Id; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/lamp/src/main.rs b/examples/lamp/src/main.rs index b621156..3dc5a25 100644 --- a/examples/lamp/src/main.rs +++ b/examples/lamp/src/main.rs @@ -2,8 +2,9 @@ use harmony::{ data::Version, inventory::Inventory, modules::lamp::{LAMPConfig, LAMPScore}, - topology::{K8sAnywhereTopology, Url}, + topology::K8sAnywhereTopology, }; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/monitoring/Cargo.toml b/examples/monitoring/Cargo.toml index 1c35330..c9f7d80 100644 --- a/examples/monitoring/Cargo.toml +++ b/examples/monitoring/Cargo.toml @@ -6,8 +6,9 @@ readme.workspace = true license.workspace = true [dependencies] -harmony = { version = "0.1.0", path = "../../harmony" } -harmony_cli = { version = "0.1.0", path = "../../harmony_cli" } -harmony_macros = { version = "0.1.0", path = "../../harmony_macros" } +harmony = { path = "../../harmony" } +harmony_cli = { path = "../../harmony_cli" } +harmony_macros = { path = "../../harmony_macros" } +harmony_types = { path = "../../harmony_types" } tokio.workspace = true url.workspace = true diff --git a/examples/monitoring/src/main.rs b/examples/monitoring/src/main.rs index b0a3939..d06a93e 100644 --- a/examples/monitoring/src/main.rs +++ b/examples/monitoring/src/main.rs @@ -22,8 +22,9 @@ use harmony::{ k8s::pvc::high_pvc_fill_rate_over_two_days, }, }, - topology::{K8sAnywhereTopology, Url}, + topology::K8sAnywhereTopology, }; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/monitoring_with_tenant/Cargo.toml b/examples/monitoring_with_tenant/Cargo.toml index 27fd4dd..10049c6 100644 --- a/examples/monitoring_with_tenant/Cargo.toml +++ b/examples/monitoring_with_tenant/Cargo.toml @@ -7,7 +7,8 @@ license.workspace = true [dependencies] cidr.workspace = true -harmony = { version = "0.1.0", path = "../../harmony" } -harmony_cli = { version = "0.1.0", path = "../../harmony_cli" } +harmony = { path = "../../harmony" } +harmony_cli = { path = "../../harmony_cli" } +harmony_types = { path = "../../harmony_types" } tokio.workspace = true url.workspace = true diff --git a/examples/monitoring_with_tenant/src/main.rs b/examples/monitoring_with_tenant/src/main.rs index 2960944..5b85f78 100644 --- a/examples/monitoring_with_tenant/src/main.rs +++ b/examples/monitoring_with_tenant/src/main.rs @@ -1,7 +1,6 @@ use std::{collections::HashMap, str::FromStr}; use harmony::{ - data::Id, inventory::Inventory, modules::{ monitoring::{ @@ -19,10 +18,12 @@ use harmony::{ tenant::TenantScore, }, topology::{ - K8sAnywhereTopology, Url, + K8sAnywhereTopology, tenant::{ResourceLimits, TenantConfig, TenantNetworkPolicy}, }, }; +use harmony_types::id::Id; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/nanodc/src/main.rs b/examples/nanodc/src/main.rs index 10ba715..a8a17e3 100644 --- a/examples/nanodc/src/main.rs +++ b/examples/nanodc/src/main.rs @@ -18,9 +18,10 @@ use harmony::{ }, tftp::TftpScore, }, - topology::{LogicalHost, UnmanagedRouter, Url}, + topology::{LogicalHost, UnmanagedRouter}, }; use harmony_macros::{ip, mac_address}; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/opnsense/src/main.rs b/examples/opnsense/src/main.rs index e868829..3af30cf 100644 --- a/examples/opnsense/src/main.rs +++ b/examples/opnsense/src/main.rs @@ -15,9 +15,10 @@ use harmony::{ opnsense::OPNsenseShellCommandScore, tftp::TftpScore, }, - topology::{LogicalHost, UnmanagedRouter, Url}, + topology::{LogicalHost, UnmanagedRouter}, }; use harmony_macros::{ip, mac_address}; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/rust/src/main.rs b/examples/rust/src/main.rs index feb92ef..b361edd 100644 --- a/examples/rust/src/main.rs +++ b/examples/rust/src/main.rs @@ -11,8 +11,9 @@ use harmony::{ discord_alert_channel::DiscordWebhook, webhook_receiver::WebhookReceiver, }, }, - topology::{K8sAnywhereTopology, Url}, + topology::K8sAnywhereTopology, }; +use harmony_types::net::Url; #[tokio::main] async fn main() { diff --git a/examples/tenant/src/main.rs b/examples/tenant/src/main.rs index 356ad1b..1b0a3e4 100644 --- a/examples/tenant/src/main.rs +++ b/examples/tenant/src/main.rs @@ -1,11 +1,11 @@ use std::str::FromStr; use harmony::{ - data::Id, inventory::Inventory, modules::tenant::TenantScore, topology::{K8sAnywhereTopology, tenant::TenantConfig}, }; +use harmony_types::id::Id; #[tokio::main] async fn main() { diff --git a/harmony/Cargo.toml b/harmony/Cargo.toml index 26e5f71..07a2480 100644 --- a/harmony/Cargo.toml +++ b/harmony/Cargo.toml @@ -9,7 +9,6 @@ license.workspace = true testing = [] [dependencies] -rand = "0.9" hex = "0.4" reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features = false } russh = "0.45.0" diff --git a/harmony/src/domain/data/mod.rs b/harmony/src/domain/data/mod.rs index 8d90a52..10d6dc5 100644 --- a/harmony/src/domain/data/mod.rs +++ b/harmony/src/domain/data/mod.rs @@ -1,6 +1,4 @@ mod file; -mod id; mod version; pub use file::*; -pub use id::*; pub use version::*; diff --git a/harmony/src/domain/executors/mod.rs b/harmony/src/domain/executors/mod.rs index fb95701..19d0cad 100644 --- a/harmony/src/domain/executors/mod.rs +++ b/harmony/src/domain/executors/mod.rs @@ -1,8 +1,7 @@ use std::fmt; use async_trait::async_trait; - -use super::topology::IpAddress; +use harmony_types::net::IpAddress; #[derive(Debug)] pub enum ExecutorError { diff --git a/harmony/src/domain/hardware/mod.rs b/harmony/src/domain/hardware/mod.rs index d8d63df..20c3596 100644 --- a/harmony/src/domain/hardware/mod.rs +++ b/harmony/src/domain/hardware/mod.rs @@ -199,7 +199,7 @@ pub struct NetworkInterface { #[cfg(test)] use harmony_macros::mac_address; -use crate::data::Id; +use harmony_types::id::Id; #[cfg(test)] impl NetworkInterface { pub fn dummy() -> Self { diff --git a/harmony/src/domain/interpret/mod.rs b/harmony/src/domain/interpret/mod.rs index be671a2..71d2f61 100644 --- a/harmony/src/domain/interpret/mod.rs +++ b/harmony/src/domain/interpret/mod.rs @@ -1,13 +1,11 @@ +use harmony_types::id::Id; use std::error::Error; use async_trait::async_trait; use derive_new::new; use super::{ - data::{Id, Version}, - executors::ExecutorError, - inventory::Inventory, - topology::PreparationError, + data::Version, executors::ExecutorError, inventory::Inventory, topology::PreparationError, }; pub enum InterpretName { diff --git a/harmony/src/domain/score.rs b/harmony/src/domain/score.rs index d2585fe..8efe608 100644 --- a/harmony/src/domain/score.rs +++ b/harmony/src/domain/score.rs @@ -1,3 +1,4 @@ +use harmony_types::id::Id; use std::collections::BTreeMap; use async_trait::async_trait; @@ -5,7 +6,6 @@ use serde::Serialize; use serde_value::Value; use super::{ - data::Id, instrumentation::{self, HarmonyEvent}, interpret::{Interpret, InterpretError, Outcome}, inventory::Inventory, diff --git a/harmony/src/domain/topology/ha_cluster.rs b/harmony/src/domain/topology/ha_cluster.rs index 31d1a7f..707081a 100644 --- a/harmony/src/domain/topology/ha_cluster.rs +++ b/harmony/src/domain/topology/ha_cluster.rs @@ -1,6 +1,7 @@ use async_trait::async_trait; use harmony_macros::ip; use harmony_types::net::MacAddress; +use harmony_types::net::Url; use log::debug; use log::info; @@ -26,7 +27,6 @@ use super::Router; use super::TftpServer; use super::Topology; -use super::Url; use super::k8s::K8sClient; use std::sync::Arc; diff --git a/harmony/src/domain/topology/http.rs b/harmony/src/domain/topology/http.rs index 1d35621..cc6c1f0 100644 --- a/harmony/src/domain/topology/http.rs +++ b/harmony/src/domain/topology/http.rs @@ -1,8 +1,8 @@ use crate::{data::FileContent, executors::ExecutorError}; use async_trait::async_trait; -use super::{IpAddress, Url}; - +use harmony_types::net::IpAddress; +use harmony_types::net::Url; #[async_trait] pub trait HttpServer: Send + Sync { async fn serve_files(&self, url: &Url) -> Result<(), ExecutorError>; diff --git a/harmony/src/domain/topology/load_balancer.rs b/harmony/src/domain/topology/load_balancer.rs index 7cc326e..3a38103 100644 --- a/harmony/src/domain/topology/load_balancer.rs +++ b/harmony/src/domain/topology/load_balancer.rs @@ -4,8 +4,9 @@ use async_trait::async_trait; use log::debug; use serde::Serialize; -use super::{IpAddress, LogicalHost}; +use super::LogicalHost; use crate::executors::ExecutorError; +use harmony_types::net::IpAddress; impl std::fmt::Debug for dyn LoadBalancer { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/harmony/src/domain/topology/mod.rs b/harmony/src/domain/topology/mod.rs index 81b3f12..a1060a5 100644 --- a/harmony/src/domain/topology/mod.rs +++ b/harmony/src/domain/topology/mod.rs @@ -1,4 +1,5 @@ mod ha_cluster; +use harmony_types::net::IpAddress; mod host_binding; mod http; pub mod installable; @@ -32,7 +33,6 @@ use super::{ instrumentation::{self, HarmonyEvent}, }; use std::error::Error; -use std::net::IpAddr; /// Represents a logical view of an infrastructure environment providing specific capabilities. /// @@ -196,35 +196,6 @@ pub trait MultiTargetTopology: Topology { fn current_target(&self) -> DeploymentTarget; } -pub type IpAddress = IpAddr; - -#[derive(Debug, Clone)] -pub enum Url { - LocalFolder(String), - Url(url::Url), -} - -impl Serialize for Url { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - match self { - Url::LocalFolder(path) => serializer.serialize_str(path), - Url::Url(url) => serializer.serialize_str(url.as_str()), - } - } -} - -impl std::fmt::Display for Url { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Url::LocalFolder(path) => write!(f, "{}", path), - Url::Url(url) => write!(f, "{}", url), - } - } -} - /// Represents a logical member of a cluster that provides one or more services. /// /// A LogicalHost can represent various roles within the infrastructure, such as: @@ -263,7 +234,8 @@ impl LogicalHost { /// /// ``` /// use std::str::FromStr; - /// use harmony::topology::{IpAddress, LogicalHost}; + /// use harmony::topology::{LogicalHost}; + /// use harmony_types::net::IpAddress; /// /// let start_ip = IpAddress::from_str("192.168.0.20").unwrap(); /// let hosts = LogicalHost::create_hosts(3, start_ip, "worker"); @@ -319,7 +291,7 @@ fn increment_ip(ip: IpAddress, increment: u32) -> Option { #[cfg(test)] mod tests { - use super::*; + use harmony_types::net::Url; use serde_json; #[test] diff --git a/harmony/src/domain/topology/network.rs b/harmony/src/domain/topology/network.rs index f68d29c..7773ae1 100644 --- a/harmony/src/domain/topology/network.rs +++ b/harmony/src/domain/topology/network.rs @@ -1,12 +1,12 @@ use std::{net::Ipv4Addr, str::FromStr, sync::Arc}; use async_trait::async_trait; -use harmony_types::net::MacAddress; +use harmony_types::net::{IpAddress, MacAddress}; use serde::Serialize; use crate::executors::ExecutorError; -use super::{IpAddress, LogicalHost, k8s::K8sClient}; +use super::{LogicalHost, k8s::K8sClient}; #[derive(Debug)] pub struct DHCPStaticEntry { diff --git a/harmony/src/domain/topology/oberservability/monitoring.rs b/harmony/src/domain/topology/oberservability/monitoring.rs index c2e93d6..1489e83 100644 --- a/harmony/src/domain/topology/oberservability/monitoring.rs +++ b/harmony/src/domain/topology/oberservability/monitoring.rs @@ -4,11 +4,12 @@ use async_trait::async_trait; use log::debug; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, topology::{Topology, installable::Installable}, }; +use harmony_types::id::Id; #[async_trait] pub trait AlertSender: Send + Sync + std::fmt::Debug { diff --git a/harmony/src/domain/topology/tenant/mod.rs b/harmony/src/domain/topology/tenant/mod.rs index 1978563..1ce1bcb 100644 --- a/harmony/src/domain/topology/tenant/mod.rs +++ b/harmony/src/domain/topology/tenant/mod.rs @@ -2,7 +2,7 @@ pub mod k8s; mod manager; pub mod network_policy; -use crate::data::Id; +use harmony_types::id::Id; pub use manager::*; use serde::{Deserialize, Serialize}; use std::str::FromStr; diff --git a/harmony/src/domain/topology/tftp.rs b/harmony/src/domain/topology/tftp.rs index ed99bab..84c21e6 100644 --- a/harmony/src/domain/topology/tftp.rs +++ b/harmony/src/domain/topology/tftp.rs @@ -1,7 +1,7 @@ use crate::executors::ExecutorError; use async_trait::async_trait; -use super::{IpAddress, Url}; +use harmony_types::net::{IpAddress, Url}; #[async_trait] pub trait TftpServer: Send + Sync { diff --git a/harmony/src/infra/executors/russh/mod.rs b/harmony/src/infra/executors/russh/mod.rs index 4bc6e73..168fe75 100644 --- a/harmony/src/infra/executors/russh/mod.rs +++ b/harmony/src/infra/executors/russh/mod.rs @@ -3,11 +3,9 @@ use std::sync::Arc; use russh::{client, keys::key}; -use crate::{ - domain::executors::{ExecutorError, SshClient}, - topology::IpAddress, -}; +use crate::domain::executors::{ExecutorError, SshClient}; +use harmony_types::net::IpAddress; pub struct RusshClient; #[async_trait] diff --git a/harmony/src/infra/hp_ilo/mod.rs b/harmony/src/infra/hp_ilo/mod.rs index ff0e313..b5edbe6 100644 --- a/harmony/src/infra/hp_ilo/mod.rs +++ b/harmony/src/infra/hp_ilo/mod.rs @@ -1,6 +1,6 @@ use crate::hardware::ManagementInterface; -use crate::topology::IpAddress; use derive_new::new; +use harmony_types::net::IpAddress; use harmony_types::net::MacAddress; use log::info; use serde::Serialize; diff --git a/harmony/src/infra/inventory/sqlite.rs b/harmony/src/infra/inventory/sqlite.rs index 03fae14..d079996 100644 --- a/harmony/src/infra/inventory/sqlite.rs +++ b/harmony/src/infra/inventory/sqlite.rs @@ -1,10 +1,10 @@ use crate::{ - data::Id, hardware::PhysicalHost, inventory::{InventoryRepository, RepoError}, }; use async_trait::async_trait; -use log::{info, warn}; +use harmony_types::id::Id; +use log::info; use sqlx::{Pool, Sqlite, SqlitePool}; /// A thread-safe, connection-pooled repository using SQLite. diff --git a/harmony/src/infra/opnsense/dhcp.rs b/harmony/src/infra/opnsense/dhcp.rs index ee4ba6c..272ffc2 100644 --- a/harmony/src/infra/opnsense/dhcp.rs +++ b/harmony/src/infra/opnsense/dhcp.rs @@ -4,10 +4,11 @@ use log::info; use crate::{ executors::ExecutorError, - topology::{DHCPStaticEntry, DhcpServer, IpAddress, LogicalHost, PxeOptions}, + topology::{DHCPStaticEntry, DhcpServer, LogicalHost, PxeOptions}, }; use super::OPNSenseFirewall; +use harmony_types::net::IpAddress; #[async_trait] impl DhcpServer for OPNSenseFirewall { diff --git a/harmony/src/infra/opnsense/dns.rs b/harmony/src/infra/opnsense/dns.rs index d486529..7a58b64 100644 --- a/harmony/src/infra/opnsense/dns.rs +++ b/harmony/src/infra/opnsense/dns.rs @@ -1,11 +1,11 @@ use crate::infra::opnsense::Host; -use crate::infra::opnsense::IpAddress; use crate::infra::opnsense::LogicalHost; use crate::{ executors::ExecutorError, topology::{DnsRecord, DnsServer}, }; use async_trait::async_trait; +use harmony_types::net::IpAddress; use super::OPNSenseFirewall; diff --git a/harmony/src/infra/opnsense/firewall.rs b/harmony/src/infra/opnsense/firewall.rs index cc25f72..589b244 100644 --- a/harmony/src/infra/opnsense/firewall.rs +++ b/harmony/src/infra/opnsense/firewall.rs @@ -1,9 +1,10 @@ use crate::{ executors::ExecutorError, - topology::{Firewall, FirewallRule, IpAddress, LogicalHost}, + topology::{Firewall, FirewallRule, LogicalHost}, }; use super::OPNSenseFirewall; +use harmony_types::net::IpAddress; impl Firewall for OPNSenseFirewall { fn add_rule(&mut self, _rule: FirewallRule) -> Result<(), ExecutorError> { diff --git a/harmony/src/infra/opnsense/http.rs b/harmony/src/infra/opnsense/http.rs index 1d34d4c..fa6fe7d 100644 --- a/harmony/src/infra/opnsense/http.rs +++ b/harmony/src/infra/opnsense/http.rs @@ -1,13 +1,11 @@ use async_trait::async_trait; use log::info; -use crate::{ - data::FileContent, - executors::ExecutorError, - topology::{HttpServer, IpAddress, Url}, -}; +use crate::{data::FileContent, executors::ExecutorError, topology::HttpServer}; use super::OPNSenseFirewall; +use harmony_types::net::IpAddress; +use harmony_types::net::Url; const OPNSENSE_HTTP_ROOT_PATH: &str = "/usr/local/http"; #[async_trait] diff --git a/harmony/src/infra/opnsense/load_balancer.rs b/harmony/src/infra/opnsense/load_balancer.rs index 471348f..9414faf 100644 --- a/harmony/src/infra/opnsense/load_balancer.rs +++ b/harmony/src/infra/opnsense/load_balancer.rs @@ -6,10 +6,11 @@ use uuid::Uuid; use crate::{ executors::ExecutorError, topology::{ - BackendServer, HealthCheck, HttpMethod, HttpStatusCode, IpAddress, LoadBalancer, - LoadBalancerService, LogicalHost, + BackendServer, HealthCheck, HttpMethod, HttpStatusCode, LoadBalancer, LoadBalancerService, + LogicalHost, }, }; +use harmony_types::net::IpAddress; use super::OPNSenseFirewall; diff --git a/harmony/src/infra/opnsense/mod.rs b/harmony/src/infra/opnsense/mod.rs index 0aa5532..3878cfc 100644 --- a/harmony/src/infra/opnsense/mod.rs +++ b/harmony/src/infra/opnsense/mod.rs @@ -11,10 +11,8 @@ pub use management::*; use opnsense_config_xml::Host; use tokio::sync::RwLock; -use crate::{ - executors::ExecutorError, - topology::{IpAddress, LogicalHost}, -}; +use crate::{executors::ExecutorError, topology::LogicalHost}; +use harmony_types::net::IpAddress; #[derive(Debug, Clone)] pub struct OPNSenseFirewall { diff --git a/harmony/src/infra/opnsense/tftp.rs b/harmony/src/infra/opnsense/tftp.rs index 1bf7b6c..275b40d 100644 --- a/harmony/src/infra/opnsense/tftp.rs +++ b/harmony/src/infra/opnsense/tftp.rs @@ -1,10 +1,9 @@ use async_trait::async_trait; use log::info; -use crate::{ - executors::ExecutorError, - topology::{IpAddress, TftpServer, Url}, -}; +use crate::{executors::ExecutorError, topology::TftpServer}; +use harmony_types::net::IpAddress; +use harmony_types::net::Url; use super::OPNSenseFirewall; diff --git a/harmony/src/modules/application/features/helm_argocd_score.rs b/harmony/src/modules/application/features/helm_argocd_score.rs index 0532111..c439727 100644 --- a/harmony/src/modules/application/features/helm_argocd_score.rs +++ b/harmony/src/modules/application/features/helm_argocd_score.rs @@ -4,13 +4,14 @@ use serde::Serialize; use std::str::FromStr; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, modules::helm::chart::{HelmChartScore, HelmRepository}, score::Score, topology::{HelmCommand, K8sclient, Topology}, }; +use harmony_types::id::Id; use super::ArgoApplication; diff --git a/harmony/src/modules/application/features/monitoring.rs b/harmony/src/modules/application/features/monitoring.rs index e8303ce..1c1c00b 100644 --- a/harmony/src/modules/application/features/monitoring.rs +++ b/harmony/src/modules/application/features/monitoring.rs @@ -11,7 +11,7 @@ use crate::{ alert_channel::webhook_receiver::WebhookReceiver, ntfy::ntfy::NtfyScore, }, score::Score, - topology::{HelmCommand, K8sclient, Topology, Url, tenant::TenantManager}, + topology::{HelmCommand, K8sclient, Topology, tenant::TenantManager}, }; use crate::{ modules::prometheus::prometheus::PrometheusApplicationMonitoring, @@ -19,6 +19,7 @@ use crate::{ }; use async_trait::async_trait; use base64::{Engine as _, engine::general_purpose}; +use harmony_types::net::Url; use log::{debug, info}; #[derive(Debug, Clone)] diff --git a/harmony/src/modules/application/mod.rs b/harmony/src/modules/application/mod.rs index beb10d6..8e60984 100644 --- a/harmony/src/modules/application/mod.rs +++ b/harmony/src/modules/application/mod.rs @@ -13,12 +13,13 @@ use async_trait::async_trait; use serde::Serialize; use crate::{ - data::{Id, Version}, + data::Version, instrumentation::{self, HarmonyEvent}, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, topology::Topology, }; +use harmony_types::id::Id; #[derive(Clone, Debug)] pub enum ApplicationFeatureStatus { diff --git a/harmony/src/modules/application/rust.rs b/harmony/src/modules/application/rust.rs index da1e594..40c85bb 100644 --- a/harmony/src/modules/application/rust.rs +++ b/harmony/src/modules/application/rust.rs @@ -15,10 +15,8 @@ use serde::Serialize; use tar::Archive; use crate::config::{REGISTRY_PROJECT, REGISTRY_URL}; -use crate::{ - score::Score, - topology::{Topology, Url}, -}; +use crate::{score::Score, topology::Topology}; +use harmony_types::net::Url; use super::{Application, ApplicationFeature, ApplicationInterpret, HelmPackage, OCICompliant}; diff --git a/harmony/src/modules/dhcp.rs b/harmony/src/modules/dhcp.rs index 81643db..9ef45d3 100644 --- a/harmony/src/modules/dhcp.rs +++ b/harmony/src/modules/dhcp.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use derive_new::new; +use harmony_types::id::Id; use log::info; use serde::Serialize; @@ -7,10 +8,11 @@ use crate::{ domain::{data::Version, interpret::InterpretStatus}, interpret::{Interpret, InterpretError, InterpretName, Outcome}, inventory::Inventory, - topology::{DHCPStaticEntry, DhcpServer, HostBinding, IpAddress, PxeOptions, Topology}, + topology::{DHCPStaticEntry, DhcpServer, HostBinding, PxeOptions, Topology}, }; use crate::domain::score::Score; +use harmony_types::net::IpAddress; #[derive(Debug, new, Clone, Serialize)] pub struct DhcpScore { @@ -135,7 +137,7 @@ impl Interpret for DhcpInterpret { self.status.clone() } - fn get_children(&self) -> Vec { + fn get_children(&self) -> Vec { todo!() } diff --git a/harmony/src/modules/dns.rs b/harmony/src/modules/dns.rs index 07b883d..9608fa1 100644 --- a/harmony/src/modules/dns.rs +++ b/harmony/src/modules/dns.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use derive_new::new; +use harmony_types::id::Id; use log::info; use serde::Serialize; @@ -91,7 +92,7 @@ impl Interpret for DnsInterpret { self.status.clone() } - fn get_children(&self) -> Vec { + fn get_children(&self) -> Vec { todo!() } diff --git a/harmony/src/modules/dummy.rs b/harmony/src/modules/dummy.rs index 2e63797..784d307 100644 --- a/harmony/src/modules/dummy.rs +++ b/harmony/src/modules/dummy.rs @@ -1,4 +1,5 @@ use async_trait::async_trait; +use harmony_types::id::Id; use serde::Serialize; use crate::{ @@ -67,7 +68,7 @@ impl Interpret for DummyInterpret { self.status.clone() } - fn get_children(&self) -> Vec { + fn get_children(&self) -> Vec { todo!() } @@ -113,7 +114,7 @@ impl Interpret for PanicInterpret { InterpretStatus::QUEUED } - fn get_children(&self) -> Vec { + fn get_children(&self) -> Vec { todo!() } diff --git a/harmony/src/modules/helm/chart.rs b/harmony/src/modules/helm/chart.rs index dd94678..9048ce6 100644 --- a/harmony/src/modules/helm/chart.rs +++ b/harmony/src/modules/helm/chart.rs @@ -1,9 +1,10 @@ -use crate::data::{Id, Version}; +use crate::data::Version; use crate::interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}; use crate::inventory::Inventory; use crate::score::Score; use crate::topology::{HelmCommand, Topology}; use async_trait::async_trait; +use harmony_types::id::Id; use helm_wrapper_rs; use helm_wrapper_rs::blocking::{DefaultHelmExecutor, HelmExecutor}; use log::{debug, info, warn}; diff --git a/harmony/src/modules/helm/command.rs b/harmony/src/modules/helm/command.rs index 149d6c6..c4d92c1 100644 --- a/harmony/src/modules/helm/command.rs +++ b/harmony/src/modules/helm/command.rs @@ -8,11 +8,12 @@ use std::process::{Command, Output}; use temp_dir::{self, TempDir}; use temp_file::TempFile; -use crate::data::{Id, Version}; +use crate::data::Version; use crate::interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}; use crate::inventory::Inventory; use crate::score::Score; use crate::topology::{HelmCommand, K8sclient, Topology}; +use harmony_types::id::Id; #[derive(Clone)] pub struct HelmCommandExecutor { diff --git a/harmony/src/modules/http.rs b/harmony/src/modules/http.rs index a04ea73..fd7a5c8 100644 --- a/harmony/src/modules/http.rs +++ b/harmony/src/modules/http.rs @@ -3,12 +3,14 @@ use derive_new::new; use serde::Serialize; use crate::{ - data::{FileContent, Id, Version}, + data::{FileContent, Version}, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, - topology::{HttpServer, Topology, Url}, + topology::{HttpServer, Topology}, }; +use harmony_types::id::Id; +use harmony_types::net::Url; /// Configure an HTTP server that is provided by the Topology /// diff --git a/harmony/src/modules/inventory/mod.rs b/harmony/src/modules/inventory/mod.rs index a7d168b..e8bbd71 100644 --- a/harmony/src/modules/inventory/mod.rs +++ b/harmony/src/modules/inventory/mod.rs @@ -4,12 +4,13 @@ use log::{debug, info}; use serde::{Deserialize, Serialize}; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::Topology, }; +use harmony_types::id::Id; /// This launches an harmony_inventory_agent discovery process /// This will allow us to register/update hosts running harmony_inventory_agent diff --git a/harmony/src/modules/ipxe.rs b/harmony/src/modules/ipxe.rs index f9e8ed3..a7aa472 100644 --- a/harmony/src/modules/ipxe.rs +++ b/harmony/src/modules/ipxe.rs @@ -3,12 +3,13 @@ use derive_new::new; use serde::Serialize; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::Topology, }; +use harmony_types::id::Id; #[derive(Debug, new, Clone, Serialize)] pub struct IpxeScore { diff --git a/harmony/src/modules/k3d/install.rs b/harmony/src/modules/k3d/install.rs index 245cf41..244dff4 100644 --- a/harmony/src/modules/k3d/install.rs +++ b/harmony/src/modules/k3d/install.rs @@ -6,12 +6,13 @@ use serde::Serialize; use crate::{ config::HARMONY_DATA_DIR, - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::Topology, }; +use harmony_types::id::Id; #[derive(Debug, Clone, Serialize)] pub struct K3DInstallationScore { diff --git a/harmony/src/modules/k8s/resource.rs b/harmony/src/modules/k8s/resource.rs index b6709ea..d679326 100644 --- a/harmony/src/modules/k8s/resource.rs +++ b/harmony/src/modules/k8s/resource.rs @@ -5,12 +5,13 @@ use log::info; use serde::{Serialize, de::DeserializeOwned}; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::{K8sclient, Topology}, }; +use harmony_types::id::Id; #[derive(Debug, Clone, Serialize)] pub struct K8sResourceScore { diff --git a/harmony/src/modules/lamp.rs b/harmony/src/modules/lamp.rs index 1a853ea..66ca45e 100644 --- a/harmony/src/modules/lamp.rs +++ b/harmony/src/modules/lamp.rs @@ -3,6 +3,7 @@ use dockerfile_builder::instruction::{CMD, COPY, ENV, EXPOSE, FROM, RUN, WORKDIR use dockerfile_builder::{Dockerfile, instruction_builder::EnvBuilder}; use fqdn::fqdn; use harmony_macros::ingress_path; +use harmony_types::net::Url; use non_blank_string_rs::NonBlankString; use serde_json::json; use std::collections::HashMap; @@ -18,13 +19,14 @@ use crate::config::{REGISTRY_PROJECT, REGISTRY_URL}; use crate::modules::k8s::ingress::K8sIngressScore; use crate::topology::HelmCommand; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, modules::k8s::deployment::K8sDeploymentScore, score::Score, - topology::{K8sclient, Topology, Url}, + topology::{K8sclient, Topology}, }; +use harmony_types::id::Id; use super::helm::chart::HelmChartScore; diff --git a/harmony/src/modules/load_balancer.rs b/harmony/src/modules/load_balancer.rs index cd78f84..033ba2e 100644 --- a/harmony/src/modules/load_balancer.rs +++ b/harmony/src/modules/load_balancer.rs @@ -3,12 +3,13 @@ use log::info; use serde::Serialize; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::{LoadBalancer, LoadBalancerService, Topology}, }; +use harmony_types::id::Id; #[derive(Debug, Clone, Serialize)] pub struct LoadBalancerScore { diff --git a/harmony/src/modules/monitoring/alert_channel/discord_alert_channel.rs b/harmony/src/modules/monitoring/alert_channel/discord_alert_channel.rs index 1d704a4..caab4d1 100644 --- a/harmony/src/modules/monitoring/alert_channel/discord_alert_channel.rs +++ b/harmony/src/modules/monitoring/alert_channel/discord_alert_channel.rs @@ -20,8 +20,9 @@ use crate::{ }, prometheus::prometheus::{Prometheus, PrometheusReceiver}, }, - topology::{Url, oberservability::monitoring::AlertReceiver}, + topology::oberservability::monitoring::AlertReceiver, }; +use harmony_types::net::Url; #[derive(Debug, Clone, Serialize)] pub struct DiscordWebhook { diff --git a/harmony/src/modules/monitoring/alert_channel/webhook_receiver.rs b/harmony/src/modules/monitoring/alert_channel/webhook_receiver.rs index 9a9d5d2..51e63b6 100644 --- a/harmony/src/modules/monitoring/alert_channel/webhook_receiver.rs +++ b/harmony/src/modules/monitoring/alert_channel/webhook_receiver.rs @@ -19,8 +19,9 @@ use crate::{ }, prometheus::prometheus::{Prometheus, PrometheusReceiver}, }, - topology::{Url, oberservability::monitoring::AlertReceiver}, + topology::oberservability::monitoring::AlertReceiver, }; +use harmony_types::net::Url; #[derive(Debug, Clone, Serialize)] pub struct WebhookReceiver { diff --git a/harmony/src/modules/monitoring/application_monitoring/application_monitoring_score.rs b/harmony/src/modules/monitoring/application_monitoring/application_monitoring_score.rs index 51c8ff9..f4707a8 100644 --- a/harmony/src/modules/monitoring/application_monitoring/application_monitoring_score.rs +++ b/harmony/src/modules/monitoring/application_monitoring/application_monitoring_score.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use serde::Serialize; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, modules::{ @@ -15,6 +15,7 @@ use crate::{ score::Score, topology::{PreparationOutcome, Topology, oberservability::monitoring::AlertReceiver}, }; +use harmony_types::id::Id; #[derive(Debug, Clone, Serialize)] pub struct ApplicationMonitoringScore { diff --git a/harmony/src/modules/monitoring/ntfy/ntfy.rs b/harmony/src/modules/monitoring/ntfy/ntfy.rs index 68106b7..87ed580 100644 --- a/harmony/src/modules/monitoring/ntfy/ntfy.rs +++ b/harmony/src/modules/monitoring/ntfy/ntfy.rs @@ -6,13 +6,14 @@ use serde::Serialize; use strum::{Display, EnumString}; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, modules::monitoring::ntfy::helm::ntfy_helm_chart::ntfy_helm_chart_score, score::Score, topology::{HelmCommand, K8sclient, MultiTargetTopology, Topology, k8s::K8sClient}, }; +use harmony_types::id::Id; #[derive(Debug, Clone, Serialize)] pub struct NtfyScore { diff --git a/harmony/src/modules/okd/ipxe.rs b/harmony/src/modules/okd/ipxe.rs index a0b32d4..38de035 100644 --- a/harmony/src/modules/okd/ipxe.rs +++ b/harmony/src/modules/okd/ipxe.rs @@ -1,17 +1,19 @@ use askama::Template; use async_trait::async_trait; use derive_new::new; +use harmony_types::net::Url; use serde::Serialize; use std::net::IpAddr; use crate::{ - data::{FileContent, FilePath, Id, Version}, + data::{FileContent, FilePath, Version}, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, modules::{dhcp::DhcpScore, http::StaticFilesHttpScore, tftp::TftpScore}, score::Score, - topology::{DhcpServer, HttpServer, Router, TftpServer, Topology, Url}, + topology::{DhcpServer, HttpServer, Router, TftpServer, Topology}, }; +use harmony_types::id::Id; #[derive(Debug, new, Clone, Serialize)] pub struct OkdIpxeScore { diff --git a/harmony/src/modules/opnsense/shell.rs b/harmony/src/modules/opnsense/shell.rs index 90be4e6..0b651ff 100644 --- a/harmony/src/modules/opnsense/shell.rs +++ b/harmony/src/modules/opnsense/shell.rs @@ -5,12 +5,13 @@ use serde::Serialize; use tokio::sync::RwLock; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::Topology, }; +use harmony_types::id::Id; #[derive(Debug, Clone)] pub struct OPNsenseShellCommandScore { diff --git a/harmony/src/modules/prometheus/k8s_prometheus_alerting_score.rs b/harmony/src/modules/prometheus/k8s_prometheus_alerting_score.rs index 0af1063..30bc8bd 100644 --- a/harmony/src/modules/prometheus/k8s_prometheus_alerting_score.rs +++ b/harmony/src/modules/prometheus/k8s_prometheus_alerting_score.rs @@ -24,7 +24,7 @@ use crate::modules::monitoring::kube_prometheus::crd::service_monitor::{ use crate::topology::oberservability::monitoring::AlertReceiver; use crate::topology::{K8sclient, Topology, k8s::K8sClient}; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, modules::monitoring::kube_prometheus::crd::{ @@ -37,6 +37,7 @@ use crate::{ }, score::Score, }; +use harmony_types::id::Id; use super::prometheus::PrometheusApplicationMonitoring; diff --git a/harmony/src/modules/storage/ceph/ceph_osd_replacement_score.rs b/harmony/src/modules/storage/ceph/ceph_osd_replacement_score.rs index 708c501..77dd24a 100644 --- a/harmony/src/modules/storage/ceph/ceph_osd_replacement_score.rs +++ b/harmony/src/modules/storage/ceph/ceph_osd_replacement_score.rs @@ -9,12 +9,13 @@ use serde::{Deserialize, Serialize}; use tokio::time::sleep; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::{K8sclient, Topology, k8s::K8sClient}, }; +use harmony_types::id::Id; #[derive(Debug, Clone, Serialize)] pub struct CephRemoveOsd { diff --git a/harmony/src/modules/storage/ceph/ceph_validate_health_score.rs b/harmony/src/modules/storage/ceph/ceph_validate_health_score.rs index 2f7f87c..f6b43ec 100644 --- a/harmony/src/modules/storage/ceph/ceph_validate_health_score.rs +++ b/harmony/src/modules/storage/ceph/ceph_validate_health_score.rs @@ -3,15 +3,15 @@ use std::{sync::Arc, time::Duration}; use async_trait::async_trait; use log::debug; use serde::Serialize; -use tokio::time::Instant; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, topology::{K8sclient, Topology, k8s::K8sClient}, }; +use harmony_types::id::Id; #[derive(Clone, Debug, Serialize)] pub struct CephVerifyClusterHealth { diff --git a/harmony/src/modules/tenant/mod.rs b/harmony/src/modules/tenant/mod.rs index b1a49c2..16386a2 100644 --- a/harmony/src/modules/tenant/mod.rs +++ b/harmony/src/modules/tenant/mod.rs @@ -5,7 +5,7 @@ use async_trait::async_trait; use serde::Serialize; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, @@ -14,6 +14,7 @@ use crate::{ tenant::{TenantConfig, TenantManager}, }, }; +use harmony_types::id::Id; #[derive(Debug, Serialize, Clone)] pub struct TenantScore { diff --git a/harmony/src/modules/tftp.rs b/harmony/src/modules/tftp.rs index 6763ec0..1cc8a8d 100644 --- a/harmony/src/modules/tftp.rs +++ b/harmony/src/modules/tftp.rs @@ -3,12 +3,14 @@ use derive_new::new; use serde::Serialize; use crate::{ - data::{Id, Version}, + data::Version, interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, inventory::Inventory, score::Score, - topology::{Router, TftpServer, Topology, Url}, + topology::{Router, TftpServer, Topology}, }; +use harmony_types::id::Id; +use harmony_types::net::Url; #[derive(Debug, new, Clone, Serialize)] pub struct TftpScore { diff --git a/harmony_inventory_agent/src/local_presence/discover.rs b/harmony_inventory_agent/src/local_presence/discover.rs index 251db99..a2ae216 100644 --- a/harmony_inventory_agent/src/local_presence/discover.rs +++ b/harmony_inventory_agent/src/local_presence/discover.rs @@ -4,8 +4,7 @@ use crate::local_presence::SERVICE_NAME; pub type DiscoveryEvent = ServiceEvent; -pub fn discover_agents(timeout: Option, on_event: impl Fn(DiscoveryEvent) + Send + 'static) -{ +pub fn discover_agents(timeout: Option, on_event: impl Fn(DiscoveryEvent) + Send + 'static) { // Create a new mDNS daemon. let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon"); diff --git a/harmony_types/Cargo.toml b/harmony_types/Cargo.toml index 0b8c068..f02874e 100644 --- a/harmony_types/Cargo.toml +++ b/harmony_types/Cargo.toml @@ -6,4 +6,6 @@ readme.workspace = true license.workspace = true [dependencies] -serde = { version = "1.0.209", features = ["derive"] } +serde.workspace = true +url.workspace = true +rand.workspace = true diff --git a/harmony_types/src/id.rs b/harmony_types/src/id.rs new file mode 100644 index 0000000..98cf1b9 --- /dev/null +++ b/harmony_types/src/id.rs @@ -0,0 +1,72 @@ +use rand::distr::Alphanumeric; +use rand::distr::SampleString; +use std::str::FromStr; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; + +use serde::{Deserialize, Serialize}; + +/// A unique identifier designed for ease of use. +/// +/// You can pass it any String to use and Id, or you can use the default format with `Id::default()` +/// +/// The default format looks like this +/// +/// `462d4c_g2COgai` +/// +/// The first part is the unix timesamp in hexadecimal which makes Id easily sorted by creation time. +/// Second part is a serie of 7 random characters. +/// +/// **It is not meant to be very secure or unique**, it is suitable to generate up to 10 000 items per +/// second with a reasonable collision rate of 0,000014 % as calculated by this calculator : https://kevingal.com/apps/collision.html +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Id { + value: String, +} + +impl Id { + pub fn empty() -> Self { + Id { + value: String::new(), + } + } +} + +impl FromStr for Id { + type Err = (); + + fn from_str(s: &str) -> Result { + Ok(Id { + value: s.to_string(), + }) + } +} + +impl From for Id { + fn from(value: String) -> Self { + Self { value } + } +} + +impl std::fmt::Display for Id { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.value) + } +} + +impl Default for Id { + fn default() -> Self { + let start = SystemTime::now(); + let since_the_epoch = start + .duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + let timestamp = since_the_epoch.as_secs(); + + let hex_timestamp = format!("{:x}", timestamp & 0xffffff); + + let random_part: String = Alphanumeric.sample_string(&mut rand::rng(), 7); + + let value = format!("{}_{}", hex_timestamp, random_part); + Self { value } + } +} diff --git a/harmony_types/src/lib.rs b/harmony_types/src/lib.rs index 9f4930d..7bb1abd 100644 --- a/harmony_types/src/lib.rs +++ b/harmony_types/src/lib.rs @@ -1,28 +1,2 @@ -pub mod net { - use serde::Serialize; - - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)] - pub struct MacAddress(pub [u8; 6]); - - impl MacAddress { - #[cfg(test)] - pub fn dummy() -> Self { - Self([0, 0, 0, 0, 0, 0]) - } - } - - impl From<&MacAddress> for String { - fn from(value: &MacAddress) -> Self { - format!( - "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", - value.0[0], value.0[1], value.0[2], value.0[3], value.0[4], value.0[5] - ) - } - } - - impl std::fmt::Display for MacAddress { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("MacAddress {}", String::from(self))) - } - } -} +pub mod id; +pub mod net; diff --git a/harmony_types/src/net.rs b/harmony_types/src/net.rs new file mode 100644 index 0000000..e2905a1 --- /dev/null +++ b/harmony_types/src/net.rs @@ -0,0 +1,55 @@ +use serde::Serialize; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)] +pub struct MacAddress(pub [u8; 6]); + +impl MacAddress { + #[cfg(test)] + pub fn dummy() -> Self { + Self([0, 0, 0, 0, 0, 0]) + } +} + +impl From<&MacAddress> for String { + fn from(value: &MacAddress) -> Self { + format!( + "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", + value.0[0], value.0[1], value.0[2], value.0[3], value.0[4], value.0[5] + ) + } +} + +impl std::fmt::Display for MacAddress { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("MacAddress {}", String::from(self))) + } +} + +pub type IpAddress = std::net::IpAddr; + +#[derive(Debug, Clone)] +pub enum Url { + LocalFolder(String), + Url(url::Url), +} + +impl Serialize for Url { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + Url::LocalFolder(path) => serializer.serialize_str(path), + Url::Url(url) => serializer.serialize_str(url.as_str()), + } + } +} + +impl std::fmt::Display for Url { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Url::LocalFolder(path) => write!(f, "{}", path), + Url::Url(url) => write!(f, "{}", url), + } + } +}