reafactor: brocade switch slight improvements #233

Merged
johnride merged 1 commits from fix/brocade into master 2026-02-17 15:16:43 +00:00
8 changed files with 43 additions and 41 deletions

View File

@@ -144,7 +144,7 @@ pub async fn init(
ip_addresses: &[IpAddr], ip_addresses: &[IpAddr],
username: &str, username: &str,
password: &str, password: &str,
options: BrocadeOptions, options: &BrocadeOptions,
) -> Result<Box<dyn BrocadeClient + Send + Sync>, Error> { ) -> Result<Box<dyn BrocadeClient + Send + Sync>, Error> {
let shell = BrocadeShell::init(ip_addresses, username, password, options).await?; let shell = BrocadeShell::init(ip_addresses, username, password, options).await?;

View File

@@ -28,7 +28,7 @@ impl BrocadeShell {
ip_addresses: &[IpAddr], ip_addresses: &[IpAddr],
username: &str, username: &str,
password: &str, password: &str,
options: BrocadeOptions, options: &BrocadeOptions,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let ip = ip_addresses let ip = ip_addresses
.first() .first()

View File

@@ -70,7 +70,7 @@ pub async fn try_init_client(
username: &str, username: &str,
password: &str, password: &str,
ip: &std::net::IpAddr, ip: &std::net::IpAddr,
base_options: BrocadeOptions, base_options: &BrocadeOptions,
) -> Result<BrocadeOptions, Error> { ) -> Result<BrocadeOptions, Error> {
let mut default = SshOptions::default(); let mut default = SshOptions::default();
default.port = base_options.ssh.port; default.port = base_options.ssh.port;

View File

@@ -2,7 +2,10 @@ use brocade::BrocadeOptions;
use cidr::Ipv4Cidr; use cidr::Ipv4Cidr;
use harmony::{ use harmony::{
hardware::{Location, SwitchGroup}, hardware::{Location, SwitchGroup},
infra::{brocade::BrocadeSwitchClient, opnsense::OPNSenseManagementInterface}, infra::{
brocade::{BrocadeSwitchClient, BrocadeSwitchConfig},
opnsense::OPNSenseManagementInterface,
},
inventory::Inventory, inventory::Inventory,
topology::{HAClusterTopology, LogicalHost, UnmanagedRouter}, topology::{HAClusterTopology, LogicalHost, UnmanagedRouter},
}; };
@@ -36,12 +39,11 @@ pub async fn get_topology() -> HAClusterTopology {
dry_run: *harmony::config::DRY_RUN, dry_run: *harmony::config::DRY_RUN,
..Default::default() ..Default::default()
}; };
let switch_client = BrocadeSwitchClient::init( let switch_client = BrocadeSwitchClient::init(BrocadeSwitchConfig {
&switches, ips: switches,
&switch_auth.username, auth: switch_auth,
&switch_auth.password, options: brocade_options,
brocade_options, })
)
.await .await
.expect("Failed to connect to switch"); .expect("Failed to connect to switch");

View File

@@ -31,12 +31,11 @@ pub async fn get_topology() -> HAClusterTopology {
dry_run: *harmony::config::DRY_RUN, dry_run: *harmony::config::DRY_RUN,
..Default::default() ..Default::default()
}; };
let switch_client = BrocadeSwitchClient::init( let switch_client = BrocadeSwitchClient::init(BrocadeSwitchConfig {
&switches, ips: switches,
&switch_auth.username, auth: switch_auth,
&switch_auth.password, options: brocade_options,
brocade_options, })
)
.await .await
.expect("Failed to connect to switch"); .expect("Failed to connect to switch");

View File

@@ -5,15 +5,18 @@ use harmony_types::{
net::{IpAddress, MacAddress}, net::{IpAddress, MacAddress},
switch::{PortDeclaration, PortLocation}, switch::{PortDeclaration, PortLocation},
}; };
use log::info;
use option_ext::OptionExt; use option_ext::OptionExt;
use crate::topology::{PortConfig, SwitchClient, SwitchError}; use crate::{
modules::brocade::BrocadeSwitchAuth,
topology::{PortConfig, SwitchClient, SwitchError},
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BrocadeSwitchConfig { pub struct BrocadeSwitchConfig {
pub ips: Vec<IpAddress>, pub ips: Vec<IpAddress>,
pub username: String, pub auth: BrocadeSwitchAuth,
pub password: String,
pub options: BrocadeOptions, pub options: BrocadeOptions,
} }
@@ -23,13 +26,11 @@ pub struct BrocadeSwitchClient {
} }
impl BrocadeSwitchClient { impl BrocadeSwitchClient {
pub async fn init( pub async fn init(config: BrocadeSwitchConfig) -> Result<Self, brocade::Error> {
ip_addresses: &[IpAddress], let auth = &config.auth;
username: &str, let options = &config.options;
password: &str,
options: BrocadeOptions, let brocade = brocade::init(&config.ips, &auth.username, &auth.password, options).await?;
) -> Result<Self, brocade::Error> {
let brocade = brocade::init(ip_addresses, username, password, options).await?;
Ok(Self { brocade }) Ok(Self { brocade })
} }
} }
@@ -60,13 +61,18 @@ impl SwitchClient for BrocadeSwitchClient {
|| link.remote_port.contains(&interface.port_location) || link.remote_port.contains(&interface.port_location)
}) })
}) })
.map(|interface| (interface.name.clone(), PortOperatingMode::Access)) .map(|interface| (interface.name.clone(), PortOperatingMode::Trunk))
.collect(); .collect();
if interfaces.is_empty() { if interfaces.is_empty() {
return Ok(()); return Ok(());
} }
info!("About to configure interfaces {interfaces:?}");
inquire::Confirm::new("Do you wish to configures interfaces now?")
.prompt()
.map_err(|e| SwitchError::new(e.to_string()))?;
self.brocade self.brocade
.configure_interfaces(&interfaces) .configure_interfaces(&interfaces)
.await .await

View File

@@ -104,12 +104,7 @@ impl Topology for SwitchTopology {
impl SwitchTopology { impl SwitchTopology {
pub async fn new(config: BrocadeSwitchConfig) -> Self { pub async fn new(config: BrocadeSwitchConfig) -> Self {
let client = BrocadeSwitchClient::init( let client = BrocadeSwitchClient::init(config)
&config.ips,
&config.username,
&config.password,
config.options,
)
.await .await
.expect("Failed to connect to switch"); .expect("Failed to connect to switch");

View File

@@ -40,15 +40,15 @@ pub struct BrocadeEnableSnmpInterpret {
#[derive(Secret, Clone, Debug, JsonSchema, Serialize, Deserialize)] #[derive(Secret, Clone, Debug, JsonSchema, Serialize, Deserialize)]
pub struct BrocadeSwitchAuth { pub struct BrocadeSwitchAuth {
username: String, pub username: String,
password: String, pub password: String,
} }
#[derive(Secret, Clone, Debug, JsonSchema, Serialize, Deserialize)] #[derive(Secret, Clone, Debug, JsonSchema, Serialize, Deserialize)]
pub struct BrocadeSnmpAuth { pub struct BrocadeSnmpAuth {
username: String, pub username: String,
auth_password: String, pub auth_password: String,
des_password: String, pub des_password: String,
} }
#[async_trait] #[async_trait]
@@ -72,7 +72,7 @@ impl<T: Topology> Interpret<T> for BrocadeEnableSnmpInterpret {
&switch_addresses, &switch_addresses,
&config.username, &config.username,
&config.password, &config.password,
BrocadeOptions { &BrocadeOptions {
dry_run: self.score.dry_run, dry_run: self.score.dry_run,
..Default::default() ..Default::default()
}, },