Skip to content

Commit 00c3338

Browse files
JanZachmannclaude
andcommitted
refactor: introduce NetworkConfigService pattern
- Rename network_config.rs to network.rs for consistency - Wrap network configuration functions in NetworkConfigService struct - Apply consistent service pattern with static methods - Update all imports to use NetworkConfigService - Add conditional compilation for mock feature in main.rs This refactoring improves code organization by applying the same service pattern used throughout the codebase. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com>
1 parent b12ba17 commit 00c3338

File tree

5 files changed

+92
-60
lines changed

5 files changed

+92
-60
lines changed

src/api.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
},
66
keycloak_client::SingleSignOnProvider,
77
middleware::TOKEN_EXPIRE_HOURS,
8-
network_config::{self, NetworkConfig},
8+
network::{NetworkConfig, NetworkConfigService},
99
omnect_device_service_client::{DeviceServiceClient, FactoryReset, LoadUpdate, RunUpdate},
1010
};
1111
use actix_files::NamedFile;
@@ -163,7 +163,7 @@ where
163163
pub async fn token(session: Session) -> impl Responder {
164164
debug!("token() called");
165165

166-
network_config::cancel_rollback();
166+
NetworkConfigService::cancel_rollback();
167167
Self::session_token(session)
168168
}
169169

@@ -304,10 +304,10 @@ where
304304
}
305305

306306
if let Err(e) =
307-
network_config::apply_network_config(&api.service_client, &network_config).await
307+
NetworkConfigService::apply_network_config(&api.service_client, &network_config).await
308308
{
309309
error!("set_network_config() failed: {e:#}");
310-
if let Err(err) = network_config::rollback_network_config(&network_config) {
310+
if let Err(err) = NetworkConfigService::rollback_network_config(&network_config) {
311311
error!("Failed to restore network config: {err:#}");
312312
}
313313
return HttpResponse::InternalServerError().body(format!("{e:#}"));

src/certificate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![cfg_attr(feature = "mock", allow(dead_code, unused_imports))]
22

33
use crate::{
4-
common::handle_http_response,
5-
http_client::HttpClientFactory,
6-
omnect_device_service_client::{DeviceServiceClient},
4+
common::handle_http_response, http_client::HttpClientFactory,
5+
omnect_device_service_client::DeviceServiceClient,
76
};
87
use anyhow::{Context, Result};
98
use log::info;
@@ -42,7 +41,10 @@ pub fn key_path() -> String {
4241
}
4342

4443
#[cfg(feature = "mock")]
45-
pub async fn create_module_certificate() -> Result<()> {
44+
pub async fn create_module_certificate<T>(_service_client: &T) -> Result<()>
45+
where
46+
T: DeviceServiceClient,
47+
{
4648
Ok(())
4749
}
4850

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub mod common;
55
pub mod http_client;
66
pub mod keycloak_client;
77
pub mod middleware;
8-
pub mod network_config;
8+
pub mod network;
99
pub mod omnect_device_service_client;

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ mod common;
55
mod http_client;
66
mod keycloak_client;
77
mod middleware;
8-
mod network_config;
8+
mod network;
99
mod omnect_device_service_client;
1010

1111
use crate::{
1212
api::Api,
1313
certificate::create_module_certificate,
1414
common::{centrifugo_config, config_path},
1515
keycloak_client::KeycloakProvider,
16-
network_config::init_server_restart_channel,
16+
network::init_server_restart_channel,
1717
omnect_device_service_client::{DeviceServiceClient, OmnectDeviceServiceClient},
1818
};
1919
use actix_cors::Cors;
@@ -180,7 +180,9 @@ async fn run_server() -> (
180180
tokio::spawn({
181181
let service_client = service_client.clone();
182182
async move {
183-
if let Err(e) = network_config::process_pending_rollback(&service_client).await {
183+
if let Err(e) =
184+
network::NetworkConfigService::process_pending_rollback(&service_client).await
185+
{
184186
error!("failed to check pending rollback: {e:#}");
185187
}
186188
}
Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -113,69 +113,97 @@ struct PendingRollback {
113113
}
114114

115115
// ============================================================================
116-
// Public Functions
116+
// Service
117117
// ============================================================================
118118

119-
pub fn rollback_network_config(network: &NetworkConfig) -> Result<()> {
120-
let config_file = network_config_file!(network.name);
121-
let backup_file = network_backup_file!(network.name);
119+
/// Service for network configuration management operations
120+
pub struct NetworkConfigService;
121+
122+
impl NetworkConfigService {
123+
/// Rollback network configuration to the previous backup
124+
///
125+
/// # Arguments
126+
/// * `network` - Network configuration to rollback
127+
///
128+
/// # Returns
129+
/// Result indicating success or failure
130+
pub fn rollback_network_config(network: &NetworkConfig) -> Result<()> {
131+
let config_file = network_config_file!(network.name);
132+
let backup_file = network_backup_file!(network.name);
133+
134+
if !backup_file.exists() {
135+
return Ok(());
136+
}
122137

123-
if !backup_file.exists() {
124-
return Ok(());
125-
}
138+
fs::copy(&backup_file, &config_file).context(format!(
139+
"failed to restore {} from {}",
140+
config_file.display(),
141+
backup_file.display()
142+
))?;
126143

127-
fs::copy(&backup_file, &config_file).context(format!(
128-
"failed to restore {} from {}",
129-
config_file.display(),
130-
backup_file.display()
131-
))?;
144+
let _ = fs::remove_file(&backup_file);
132145

133-
let _ = fs::remove_file(&backup_file);
146+
Ok(())
147+
}
134148

135-
Ok(())
136-
}
149+
/// Apply network configuration to systemd-networkd
150+
///
151+
/// # Arguments
152+
/// * `service_client` - Device service client for network reload
153+
/// * `network` - Network configuration to apply
154+
///
155+
/// # Returns
156+
/// Result indicating success or failure
157+
pub async fn apply_network_config<T>(service_client: &T, network: &NetworkConfig) -> Result<()>
158+
where
159+
T: DeviceServiceClient,
160+
{
161+
backup_current_network_config(service_client, network).await?;
162+
write_network_config(network)?;
163+
service_client.reload_network().await?;
137164

138-
pub async fn apply_network_config<T>(service_client: &T, network: &NetworkConfig) -> Result<()>
139-
where
140-
T: DeviceServiceClient,
141-
{
142-
backup_current_network_config(service_client, network).await?;
143-
write_network_config(network)?;
144-
service_client.reload_network().await?;
165+
if network.is_server_addr && network.ip_changed {
166+
schedule_server_restart_with_certificate(network).await?;
167+
}
145168

146-
if network.is_server_addr && network.ip_changed {
147-
schedule_server_restart_with_certificate(network).await?;
169+
Ok(())
148170
}
149171

150-
Ok(())
151-
}
152-
153-
pub async fn process_pending_rollback<T>(service_client: &T) -> Result<()>
154-
where
155-
T: DeviceServiceClient,
156-
{
157-
if let Some(pending) = load_rollback!() {
158-
let now = SystemTime::now();
159-
160-
if now >= pending.rollback_time {
161-
execute_rollback(service_client, &pending.network_config, "pending").await;
162-
clear_rollback!();
163-
} else if let Ok(remaining_time) = pending.rollback_time.duration_since(now) {
164-
sleep(remaining_time).await;
172+
/// Process any pending network configuration rollback
173+
///
174+
/// # Arguments
175+
/// * `service_client` - Device service client for rollback operations
176+
///
177+
/// # Returns
178+
/// Result indicating success or failure
179+
pub async fn process_pending_rollback<T>(service_client: &T) -> Result<()>
180+
where
181+
T: DeviceServiceClient,
182+
{
183+
if let Some(pending) = load_rollback!() {
184+
let now = SystemTime::now();
165185

166-
if load_rollback!().is_some() {
167-
execute_rollback(service_client, &pending.network_config, "scheduled").await;
186+
if now >= pending.rollback_time {
187+
execute_rollback(service_client, &pending.network_config, "pending").await;
168188
clear_rollback!();
189+
} else if let Ok(remaining_time) = pending.rollback_time.duration_since(now) {
190+
sleep(remaining_time).await;
191+
192+
if load_rollback!().is_some() {
193+
execute_rollback(service_client, &pending.network_config, "scheduled").await;
194+
clear_rollback!();
195+
}
169196
}
170197
}
198+
Ok(())
171199
}
172-
Ok(())
173-
}
174200

175-
pub fn cancel_rollback() {
176-
if load_rollback!().is_some() {
177-
clear_rollback!();
178-
info!("pending network rollback cancelled");
201+
/// Cancel any pending network configuration rollback
202+
pub fn cancel_rollback() {
203+
if load_rollback!().is_some() {
204+
clear_rollback!();
205+
info!("pending network rollback cancelled");
206+
}
179207
}
180208
}
181209

@@ -230,7 +258,7 @@ async fn rollback_network_and_renew_certificate<T>(
230258
where
231259
T: DeviceServiceClient,
232260
{
233-
rollback_network_config(network)?;
261+
NetworkConfigService::rollback_network_config(network)?;
234262
service_client.reload_network().await?;
235263
trigger_server_restart();
236264

0 commit comments

Comments
 (0)