From e5b4b5114eccab84d6d1198d7b4044fbdfb2e7ad Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Tue, 4 Feb 2025 14:44:58 -0500 Subject: [PATCH] fix: improve code formatting and module organization - Corrected code formatting issues such as inconsistent line breaks and unnecessary trailing commas. - Reorganized `mod.rs` to ensure proper order of module declarations. - Fixed misplaced imports in `help.rs` and corrected the rendering method signature. - Cleaned up unused code lines and moved `dummy` module declaration to its correct position. --- harmony-rs/examples/opnsense/src/main.rs | 11 +++-- .../src/domain/topology/load_balancer.rs | 5 ++- harmony-rs/harmony/src/domain/topology/mod.rs | 3 +- .../harmony/src/domain/topology/openshift.rs | 2 +- .../src/infra/opnsense/load_balancer.rs | 42 +++++++++---------- .../harmony/src/modules/k8s/resource.rs | 38 ++++++++--------- harmony-rs/harmony/src/modules/mod.rs | 3 +- harmony-rs/harmony_tui/src/widget/help.rs | 5 ++- harmony-rs/harmony_tui/src/widget/mod.rs | 2 +- harmony-rs/harmony_tui/src/widget/score.rs | 3 +- .../opnsense-config/src/modules/dhcp.rs | 1 - 11 files changed, 59 insertions(+), 56 deletions(-) diff --git a/harmony-rs/examples/opnsense/src/main.rs b/harmony-rs/examples/opnsense/src/main.rs index 1e21543..723a55d 100644 --- a/harmony-rs/examples/opnsense/src/main.rs +++ b/harmony-rs/examples/opnsense/src/main.rs @@ -10,7 +10,10 @@ use harmony::{ inventory::Inventory, maestro::Maestro, modules::{ - dummy::{ErrorScore, PanicScore, SuccessScore}, http::HttpScore, okd::{dhcp::OKDDhcpScore, dns::OKDDnsScore}, tftp::TftpScore + dummy::{ErrorScore, PanicScore, SuccessScore}, + http::HttpScore, + okd::{dhcp::OKDDhcpScore, dns::OKDDnsScore}, + tftp::TftpScore, }, topology::{LogicalHost, UnmanagedRouter, Url}, }; @@ -18,7 +21,6 @@ use harmony_macros::{ip, mac_address}; #[tokio::main] async fn main() { - let firewall = harmony::topology::LogicalHost { ip: ip!("192.168.5.229"), name: String::from("opnsense-1"), @@ -64,7 +66,10 @@ async fn main() { .management(Arc::new(OPNSenseManagementInterface::new()))]), storage_host: vec![], worker_host: vec![], - control_plane_host: vec![PhysicalHost::empty(HostCategory::Server).mac_address(mac_address!("08:00:27:62:EC:C3"))], + control_plane_host: vec![ + PhysicalHost::empty(HostCategory::Server) + .mac_address(mac_address!("08:00:27:62:EC:C3")), + ], }; // TODO regroup smaller scores in a larger one such as this diff --git a/harmony-rs/harmony/src/domain/topology/load_balancer.rs b/harmony-rs/harmony/src/domain/topology/load_balancer.rs index ffafa3c..4ebcba8 100644 --- a/harmony-rs/harmony/src/domain/topology/load_balancer.rs +++ b/harmony-rs/harmony/src/domain/topology/load_balancer.rs @@ -20,7 +20,10 @@ pub trait LoadBalancer: Send + Sync { &self, service: &LoadBalancerService, ) -> Result<(), ExecutorError> { - debug!("Listing LoadBalancer services {:?}", self.list_services().await); + debug!( + "Listing LoadBalancer services {:?}", + self.list_services().await + ); if !self.list_services().await.contains(service) { self.add_service(service).await?; } diff --git a/harmony-rs/harmony/src/domain/topology/mod.rs b/harmony-rs/harmony/src/domain/topology/mod.rs index 79a1230..81dd36e 100644 --- a/harmony-rs/harmony/src/domain/topology/mod.rs +++ b/harmony-rs/harmony/src/domain/topology/mod.rs @@ -1,10 +1,10 @@ +mod ha_cluster; mod host_binding; mod http; mod load_balancer; pub mod openshift; mod router; mod tftp; -mod ha_cluster; pub use ha_cluster::*; pub use load_balancer::*; pub use router::*; @@ -16,7 +16,6 @@ pub use tftp::*; use std::{net::IpAddr, sync::Arc}; - pub type IpAddress = IpAddr; #[derive(Debug, Clone)] diff --git a/harmony-rs/harmony/src/domain/topology/openshift.rs b/harmony-rs/harmony/src/domain/topology/openshift.rs index e1713f9..1790a06 100644 --- a/harmony-rs/harmony/src/domain/topology/openshift.rs +++ b/harmony-rs/harmony/src/domain/topology/openshift.rs @@ -1,5 +1,5 @@ use k8s_openapi::NamespaceResourceScope; -use kube::{api::PostParams, Api, Client, Error, Resource}; +use kube::{Api, Client, Error, Resource, api::PostParams}; use serde::de::DeserializeOwned; pub struct OpenshiftClient { diff --git a/harmony-rs/harmony/src/infra/opnsense/load_balancer.rs b/harmony-rs/harmony/src/infra/opnsense/load_balancer.rs index ae5ecef..dd32a03 100644 --- a/harmony-rs/harmony/src/infra/opnsense/load_balancer.rs +++ b/harmony-rs/harmony/src/infra/opnsense/load_balancer.rs @@ -23,7 +23,9 @@ impl LoadBalancer for OPNSenseFirewall { } async fn add_service(&self, service: &LoadBalancerService) -> Result<(), ExecutorError> { - warn!("TODO : the current implementation does not check / cleanup / merge with existing haproxy services properly. Make sure to manually verify that the configuration is correct after executing any operation here"); + warn!( + "TODO : the current implementation does not check / cleanup / merge with existing haproxy services properly. Make sure to manually verify that the configuration is correct after executing any operation here" + ); let mut config = self.opnsense_config.write().await; let (frontend, backend, servers, healthcheck) = harmony_load_balancer_service_to_haproxy_xml(service); @@ -60,7 +62,9 @@ impl LoadBalancer for OPNSenseFirewall { let mut config = self.opnsense_config.write().await; let load_balancer = config.load_balancer(); if let Some(config) = load_balancer.get_full_config() { - debug!("HAProxy config available in opnsense config, assuming it is already installed, {config:?}"); + debug!( + "HAProxy config available in opnsense config, assuming it is already installed, {config:?}" + ); } else { config.install_package("os-haproxy").await.map_err(|e| { ExecutorError::UnexpectedError(format!( @@ -366,13 +370,10 @@ mod tests { let result = get_servers_for_backend(&backend, &haproxy); // Check the result - assert_eq!( - result, - vec![BackendServer { - address: "192.168.1.1".to_string(), - port: 80, - },] - ); + assert_eq!(result, vec![BackendServer { + address: "192.168.1.1".to_string(), + port: 80, + },]); } #[test] fn test_get_servers_for_backend_no_linked_servers() { @@ -429,18 +430,15 @@ mod tests { // Call the function let result = get_servers_for_backend(&backend, &haproxy); // Check the result - assert_eq!( - result, - vec![ - BackendServer { - address: "some-hostname.test.mcd".to_string(), - port: 80, - }, - BackendServer { - address: "192.168.1.2".to_string(), - port: 8080, - }, - ] - ); + assert_eq!(result, vec![ + BackendServer { + address: "some-hostname.test.mcd".to_string(), + port: 80, + }, + BackendServer { + address: "192.168.1.2".to_string(), + port: 8080, + }, + ]); } } diff --git a/harmony-rs/harmony/src/modules/k8s/resource.rs b/harmony-rs/harmony/src/modules/k8s/resource.rs index ccf40b1..cf45be8 100644 --- a/harmony-rs/harmony/src/modules/k8s/resource.rs +++ b/harmony-rs/harmony/src/modules/k8s/resource.rs @@ -25,16 +25,16 @@ impl K8sResourceScore { } impl< - K: Resource - + std::fmt::Debug - + Sync - + DeserializeOwned - + Default - + serde::Serialize - + 'static - + Send - + Clone, - > Score for K8sResourceScore + K: Resource + + std::fmt::Debug + + Sync + + DeserializeOwned + + Default + + serde::Serialize + + 'static + + Send + + Clone, +> Score for K8sResourceScore where ::DynamicType: Default, { @@ -58,15 +58,15 @@ pub struct K8sResourceInterpret { #[async_trait] impl< - K: Resource - + Clone - + std::fmt::Debug - + DeserializeOwned - + serde::Serialize - + Default - + Send - + Sync, - > Interpret for K8sResourceInterpret + K: Resource + + Clone + + std::fmt::Debug + + DeserializeOwned + + serde::Serialize + + Default + + Send + + Sync, +> Interpret for K8sResourceInterpret where ::DynamicType: Default, { diff --git a/harmony-rs/harmony/src/modules/mod.rs b/harmony-rs/harmony/src/modules/mod.rs index e2169c1..dd5e177 100644 --- a/harmony-rs/harmony/src/modules/mod.rs +++ b/harmony-rs/harmony/src/modules/mod.rs @@ -1,9 +1,8 @@ pub mod dhcp; pub mod dns; +pub mod dummy; pub mod http; pub mod k8s; pub mod load_balancer; pub mod okd; pub mod tftp; -pub mod dummy; - diff --git a/harmony-rs/harmony_tui/src/widget/help.rs b/harmony-rs/harmony_tui/src/widget/help.rs index 0e72ddb..109ba1b 100644 --- a/harmony-rs/harmony_tui/src/widget/help.rs +++ b/harmony-rs/harmony_tui/src/widget/help.rs @@ -10,11 +10,12 @@ impl HelpWidget { impl Widget for HelpWidget { fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) where - Self: Sized { + Self: Sized, + { let text = Paragraph::new("Usage => q/Esc: Quit | j/↑ :Select UP | k/↓: Select Down | Enter: Launch Score\nPageUp/PageDown: Scroll Logs | Shift+G/End: Logs bottom") .centered() .wrap(Wrap { trim: false }); - + Widget::render(text, area, buf) } } diff --git a/harmony-rs/harmony_tui/src/widget/mod.rs b/harmony-rs/harmony_tui/src/widget/mod.rs index a6f65d5..8699569 100644 --- a/harmony-rs/harmony_tui/src/widget/mod.rs +++ b/harmony-rs/harmony_tui/src/widget/mod.rs @@ -1,2 +1,2 @@ -pub mod score; pub mod help; +pub mod score; diff --git a/harmony-rs/harmony_tui/src/widget/score.rs b/harmony-rs/harmony_tui/src/widget/score.rs index 2e9f2d3..af992f7 100644 --- a/harmony-rs/harmony_tui/src/widget/score.rs +++ b/harmony-rs/harmony_tui/src/widget/score.rs @@ -3,10 +3,10 @@ use std::sync::{Arc, RwLock}; use crossterm::event::{Event, KeyCode, KeyEventKind}; use log::{info, warn}; use ratatui::{ + Frame, layout::Rect, style::{Style, Stylize}, widgets::{List, ListState, StatefulWidget, Widget}, - Frame, }; use tokio::sync::mpsc; @@ -78,7 +78,6 @@ impl ScoreListWidget { frame.render_widget(self, area); } - fn clear_execution(&mut self) { match self.execution.take() { Some(execution) => { diff --git a/harmony-rs/opnsense-config/src/modules/dhcp.rs b/harmony-rs/opnsense-config/src/modules/dhcp.rs index ff12578..2c06304 100644 --- a/harmony-rs/opnsense-config/src/modules/dhcp.rs +++ b/harmony-rs/opnsense-config/src/modules/dhcp.rs @@ -183,4 +183,3 @@ impl<'a> DhcpConfig<'a> { self.get_lan_dhcpd().bootfilename = Some(boot_filename.to_string()); } } -