From 2388f585f5992365afb48b3236d855d4c688ebdd Mon Sep 17 00:00:00 2001 From: Ian Letourneau Date: Thu, 25 Sep 2025 17:13:42 -0400 Subject: [PATCH] reorganize modules --- brocade/src/lib.rs | 3 +- examples/okd_installation/src/topology.rs | 2 +- harmony/src/domain/topology/ha_cluster.rs | 55 ++--------------------- harmony/src/domain/topology/network.rs | 6 +++ harmony/src/infra/brocade.rs | 49 ++++++++++++++++++++ harmony/src/infra/mod.rs | 1 + 6 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 harmony/src/infra/brocade.rs diff --git a/brocade/src/lib.rs b/brocade/src/lib.rs index 49cfaae..0df2ec4 100644 --- a/brocade/src/lib.rs +++ b/brocade/src/lib.rs @@ -27,7 +27,8 @@ impl BrocadeClient { username: &str, password: &str, ) -> Result { - let ip = ip_addresses[0]; + let ip = ip_addresses[0]; // FIXME: Find a better way to get master switch IP address + let config = russh::client::Config::default(); let mut client = russh::client::connect(Arc::new(config), (ip, 22), Client {}).await?; diff --git a/examples/okd_installation/src/topology.rs b/examples/okd_installation/src/topology.rs index 02553a5..31062f5 100644 --- a/examples/okd_installation/src/topology.rs +++ b/examples/okd_installation/src/topology.rs @@ -1,6 +1,6 @@ use cidr::Ipv4Cidr; use harmony::{ - hardware::{FirewallGroup, HostCategory, Location, PhysicalHost, SwitchGroup}, + hardware::{Location, SwitchGroup}, infra::opnsense::OPNSenseManagementInterface, inventory::Inventory, topology::{HAClusterTopology, LogicalHost, UnmanagedRouter}, diff --git a/harmony/src/domain/topology/ha_cluster.rs b/harmony/src/domain/topology/ha_cluster.rs index cc07b8b..27c640c 100644 --- a/harmony/src/domain/topology/ha_cluster.rs +++ b/harmony/src/domain/topology/ha_cluster.rs @@ -1,7 +1,5 @@ use async_trait::async_trait; -use brocade::BrocadeClient; use harmony_macros::ip; -use harmony_secret::Secret; use harmony_secret::SecretManager; use harmony_types::net::MacAddress; use harmony_types::net::Url; @@ -9,12 +7,12 @@ use k8s_openapi::api::core::v1::Namespace; use kube::api::ObjectMeta; use log::debug; use log::info; -use serde::Deserialize; -use serde::Serialize; use crate::data::FileContent; use crate::executors::ExecutorError; use crate::hardware::PhysicalHost; +use crate::infra::brocade::BrocadeSwitchAuth; +use crate::infra::brocade::BrocadeSwitchClient; use crate::modules::okd::crd::InstallPlanApproval; use crate::modules::okd::crd::OperatorGroup; use crate::modules::okd::crd::OperatorGroupSpec; @@ -43,6 +41,7 @@ use super::PreparationError; use super::PreparationOutcome; use super::Router; use super::Switch; +use super::SwitchClient; use super::SwitchError; use super::TftpServer; @@ -527,54 +526,6 @@ impl Switch for HAClusterTopology { } } -#[async_trait] -trait SwitchClient: Send + Sync { - async fn find_port(&self, mac_address: &MacAddress) -> Option; - async fn configure_port_channel(&self, switch_ports: Vec) -> Result; -} - -struct BrocadeSwitchClient { - brocade: BrocadeClient, -} - -impl BrocadeSwitchClient { - async fn init( - ip_addresses: &[IpAddress], - username: &str, - password: &str, - ) -> Result { - let brocade = BrocadeClient::init(ip_addresses, username, password).await?; - Ok(Self { brocade }) - } -} - -#[async_trait] -impl SwitchClient for BrocadeSwitchClient { - async fn find_port(&self, mac_address: &MacAddress) -> Option { - let Ok(table) = self.brocade.show_mac_address_table().await else { - return None; - }; - - table - .iter() - .find(|entry| entry.mac_address == *mac_address) - .map(|entry| entry.port_name.clone()) - } - - async fn configure_port_channel(&self, switch_ports: Vec) -> Result { - self.brocade - .configure_port_channel(&switch_ports) - .await - .map_err(|e| SwitchError::new(format!("Failed to configure port channel: {e}"))) - } -} - -#[derive(Secret, Serialize, Deserialize, Debug)] -struct BrocadeSwitchAuth { - username: String, - password: String, -} - #[derive(Debug)] pub struct DummyInfra; diff --git a/harmony/src/domain/topology/network.rs b/harmony/src/domain/topology/network.rs index a1da8dd..fe02a5e 100644 --- a/harmony/src/domain/topology/network.rs +++ b/harmony/src/domain/topology/network.rs @@ -216,6 +216,12 @@ impl std::fmt::Display for SwitchError { impl Error for SwitchError {} +#[async_trait] +pub trait SwitchClient: Send + Sync { + async fn find_port(&self, mac_address: &MacAddress) -> Option; + async fn configure_port_channel(&self, switch_ports: Vec) -> Result; +} + #[cfg(test)] mod test { use std::sync::Arc; diff --git a/harmony/src/infra/brocade.rs b/harmony/src/infra/brocade.rs new file mode 100644 index 0000000..7e03565 --- /dev/null +++ b/harmony/src/infra/brocade.rs @@ -0,0 +1,49 @@ +use async_trait::async_trait; +use brocade::BrocadeClient; +use harmony_secret::Secret; +use harmony_types::net::{IpAddress, MacAddress}; +use serde::{Deserialize, Serialize}; + +use crate::topology::{SwitchClient, SwitchError}; + +pub struct BrocadeSwitchClient { + brocade: BrocadeClient, +} + +impl BrocadeSwitchClient { + pub async fn init( + ip_addresses: &[IpAddress], + username: &str, + password: &str, + ) -> Result { + let brocade = BrocadeClient::init(ip_addresses, username, password).await?; + Ok(Self { brocade }) + } +} + +#[async_trait] +impl SwitchClient for BrocadeSwitchClient { + async fn find_port(&self, mac_address: &MacAddress) -> Option { + let Ok(table) = self.brocade.show_mac_address_table().await else { + return None; + }; + + table + .iter() + .find(|entry| entry.mac_address == *mac_address) + .map(|entry| entry.port_name.clone()) + } + + async fn configure_port_channel(&self, switch_ports: Vec) -> Result { + self.brocade + .configure_port_channel(&switch_ports) + .await + .map_err(|e| SwitchError::new(format!("Failed to configure port channel: {e}"))) + } +} + +#[derive(Secret, Serialize, Deserialize, Debug)] +pub struct BrocadeSwitchAuth { + pub username: String, + pub password: String, +} diff --git a/harmony/src/infra/mod.rs b/harmony/src/infra/mod.rs index c05c7b6..203cf90 100644 --- a/harmony/src/infra/mod.rs +++ b/harmony/src/infra/mod.rs @@ -1,3 +1,4 @@ +pub mod brocade; pub mod executors; pub mod hp_ilo; pub mod intel_amt;