Skip to content

Commit 01be3af

Browse files
committed
step: [#220] update EnvironmentCreationConfig to use TrackerSection
1 parent de1fdb4 commit 01be3af

File tree

7 files changed

+83
-48
lines changed

7 files changed

+83
-48
lines changed

src/application/command_handlers/create/config/environment_config.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ use serde::{Deserialize, Serialize};
88

99
use crate::adapters::ssh::SshCredentials;
1010
use crate::domain::provider::{Provider, ProviderConfig};
11-
use crate::domain::tracker::{
12-
DatabaseConfig, HttpApiConfig, HttpTrackerConfig, TrackerConfig, TrackerCoreConfig,
13-
UdpTrackerConfig,
14-
};
11+
use crate::domain::tracker::TrackerConfig;
1512
use crate::domain::{EnvironmentName, InstanceName};
1613

1714
use super::errors::CreateConfigError;
1815
use super::provider::{HetznerProviderSection, LxdProviderSection, ProviderSection};
1916
use super::ssh_credentials_config::SshCredentialsConfig;
17+
use super::tracker::TrackerSection;
2018

2119
/// Configuration for creating a deployment environment
2220
///
@@ -86,7 +84,10 @@ pub struct EnvironmentCreationConfig {
8684
pub provider: ProviderSection,
8785

8886
/// Tracker deployment configuration
89-
pub tracker: TrackerConfig,
87+
///
88+
/// Uses `TrackerSection` for JSON parsing with String primitives.
89+
/// Converted to domain `TrackerConfig` via `to_environment_params()`.
90+
pub tracker: TrackerSection,
9091
}
9192

9293
/// Environment-specific configuration section
@@ -125,7 +126,7 @@ impl EnvironmentCreationConfig {
125126
/// EnvironmentCreationConfig, EnvironmentSection, SshCredentialsConfig,
126127
/// ProviderSection, LxdProviderSection
127128
/// };
128-
/// use torrust_tracker_deployer_lib::domain::tracker::TrackerConfig;
129+
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
129130
///
130131
/// let config = EnvironmentCreationConfig::new(
131132
/// EnvironmentSection {
@@ -141,15 +142,15 @@ impl EnvironmentCreationConfig {
141142
/// ProviderSection::Lxd(LxdProviderSection {
142143
/// profile_name: "torrust-profile-dev".to_string(),
143144
/// }),
144-
/// TrackerConfig::default(),
145+
/// TrackerSection::default(),
145146
/// );
146147
/// ```
147148
#[must_use]
148149
pub fn new(
149150
environment: EnvironmentSection,
150151
ssh_credentials: SshCredentialsConfig,
151152
provider: ProviderSection,
152-
tracker: TrackerConfig,
153+
tracker: TrackerSection,
153154
) -> Self {
154155
Self {
155156
environment,
@@ -199,8 +200,8 @@ impl EnvironmentCreationConfig {
199200
/// EnvironmentCreationConfig, EnvironmentSection, SshCredentialsConfig,
200201
/// ProviderSection, LxdProviderSection
201202
/// };
203+
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
202204
/// use torrust_tracker_deployer_lib::domain::Environment;
203-
/// use torrust_tracker_deployer_lib::domain::tracker::TrackerConfig;
204205
///
205206
/// let config = EnvironmentCreationConfig::new(
206207
/// EnvironmentSection {
@@ -216,7 +217,7 @@ impl EnvironmentCreationConfig {
216217
/// ProviderSection::Lxd(LxdProviderSection {
217218
/// profile_name: "torrust-profile-dev".to_string(),
218219
/// }),
219-
/// TrackerConfig::default(),
220+
/// TrackerSection::default(),
220221
/// );
221222
///
222223
/// let (name, instance_name, provider_config, credentials, port, tracker) = config.to_environment_params()?;
@@ -261,8 +262,8 @@ impl EnvironmentCreationConfig {
261262
// Convert SSH credentials config to domain type
262263
let ssh_credentials = self.ssh_credentials.to_ssh_credentials()?;
263264

264-
// Get tracker config
265-
let tracker_config = self.tracker;
265+
// Convert TrackerSection (DTO) to domain TrackerConfig (validates bind addresses, etc.)
266+
let tracker_config = self.tracker.to_tracker_config()?;
266267

267268
Ok((
268269
environment_name,
@@ -338,21 +339,21 @@ impl EnvironmentCreationConfig {
338339
port: 22, // default value
339340
},
340341
provider: provider_section,
341-
tracker: TrackerConfig {
342-
core: TrackerCoreConfig {
343-
database: DatabaseConfig::Sqlite {
342+
tracker: TrackerSection {
343+
core: super::tracker::TrackerCoreSection {
344+
database: super::tracker::DatabaseSection::Sqlite {
344345
database_name: "tracker.db".to_string(),
345346
},
346347
private: false,
347348
},
348-
udp_trackers: vec![UdpTrackerConfig {
349-
bind_address: "0.0.0.0:6969".parse().unwrap(),
349+
udp_trackers: vec![super::tracker::UdpTrackerSection {
350+
bind_address: "0.0.0.0:6969".to_string(),
350351
}],
351-
http_trackers: vec![HttpTrackerConfig {
352-
bind_address: "0.0.0.0:7070".parse().unwrap(),
352+
http_trackers: vec![super::tracker::HttpTrackerSection {
353+
bind_address: "0.0.0.0:7070".to_string(),
353354
}],
354-
http_api: HttpApiConfig {
355-
bind_address: "0.0.0.0:1212".parse().unwrap(),
355+
http_api: super::tracker::HttpApiSection {
356+
bind_address: "0.0.0.0:1212".to_string(),
356357
admin_token: "MyAccessToken".to_string(),
357358
},
358359
},
@@ -434,6 +435,7 @@ impl EnvironmentCreationConfig {
434435
mod tests {
435436
use super::*;
436437
use crate::application::command_handlers::create::config::provider::LxdProviderSection;
438+
use crate::application::command_handlers::create::config::tracker::TrackerSection;
437439
use crate::domain::provider::Provider;
438440

439441
/// Helper to create a default LXD provider section for tests
@@ -457,7 +459,7 @@ mod tests {
457459
22,
458460
),
459461
default_lxd_provider("torrust-profile-dev"),
460-
TrackerConfig::default(),
462+
TrackerSection::default(),
461463
);
462464

463465
assert_eq!(config.environment.name, "dev");
@@ -598,7 +600,7 @@ mod tests {
598600
22,
599601
),
600602
default_lxd_provider("torrust-profile-staging"),
601-
TrackerConfig::default(),
603+
TrackerSection::default(),
602604
);
603605

604606
let json = serde_json::to_string(&config).unwrap();
@@ -621,7 +623,7 @@ mod tests {
621623
22,
622624
),
623625
default_lxd_provider("torrust-profile-dev"),
624-
TrackerConfig::default(),
626+
TrackerSection::default(),
625627
);
626628

627629
let result = config.to_environment_params();
@@ -650,7 +652,7 @@ mod tests {
650652
22,
651653
),
652654
default_lxd_provider("torrust-profile-prod"),
653-
TrackerConfig::default(),
655+
TrackerSection::default(),
654656
);
655657

656658
let result = config.to_environment_params();
@@ -677,7 +679,7 @@ mod tests {
677679
22,
678680
),
679681
default_lxd_provider("torrust-profile"),
680-
TrackerConfig::default(),
682+
TrackerSection::default(),
681683
);
682684

683685
let result = config.to_environment_params();
@@ -705,7 +707,7 @@ mod tests {
705707
22,
706708
),
707709
default_lxd_provider("torrust-profile"),
708-
TrackerConfig::default(),
710+
TrackerSection::default(),
709711
);
710712

711713
let result = config.to_environment_params();
@@ -736,7 +738,7 @@ mod tests {
736738
ProviderSection::Lxd(LxdProviderSection {
737739
profile_name: "invalid-".to_string(), // ends with dash - invalid
738740
}),
739-
TrackerConfig::default(),
741+
TrackerSection::default(),
740742
);
741743

742744
let result = config.to_environment_params();
@@ -764,7 +766,7 @@ mod tests {
764766
22,
765767
),
766768
default_lxd_provider("torrust-profile-dev"),
767-
TrackerConfig::default(),
769+
TrackerSection::default(),
768770
);
769771

770772
let result = config.to_environment_params();
@@ -792,7 +794,7 @@ mod tests {
792794
22,
793795
),
794796
default_lxd_provider("torrust-profile-dev"),
795-
TrackerConfig::default(),
797+
TrackerSection::default(),
796798
);
797799

798800
let result = config.to_environment_params();
@@ -820,7 +822,7 @@ mod tests {
820822
22,
821823
),
822824
default_lxd_provider("torrust-profile-dev"),
823-
TrackerConfig::default(),
825+
TrackerSection::default(),
824826
);
825827

826828
let result = config.to_environment_params();
@@ -851,7 +853,7 @@ mod tests {
851853
22,
852854
),
853855
default_lxd_provider("torrust-profile-test-env"),
854-
TrackerConfig::default(),
856+
TrackerSection::default(),
855857
);
856858

857859
let (name, _instance_name, provider_config, credentials, port, _tracker) =
@@ -879,7 +881,7 @@ mod tests {
879881
22,
880882
),
881883
default_lxd_provider("torrust-profile-dev"),
882-
TrackerConfig::default(),
884+
TrackerSection::default(),
883885
);
884886

885887
let json = serde_json::to_string_pretty(&original).unwrap();
@@ -967,7 +969,7 @@ mod tests {
967969
22,
968970
),
969971
default_lxd_provider("test-profile"),
970-
TrackerConfig::default(),
972+
TrackerSection::default(),
971973
);
972974

973975
// Both should serialize to same structure (different values)

src/application/command_handlers/create/config/tracker/tracker_section.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@ impl TrackerSection {
8383
}
8484
}
8585

86+
impl Default for TrackerSection {
87+
/// Returns a default tracker configuration DTO suitable for development and testing
88+
///
89+
/// # Default Values
90+
///
91+
/// - Database: `SQLite` with filename "tracker.db"
92+
/// - Mode: Public tracker (private = false)
93+
/// - UDP trackers: One instance on "0.0.0.0:6969"
94+
/// - HTTP trackers: One instance on "0.0.0.0:7070"
95+
/// - HTTP API: Bind address "0.0.0.0:1212"
96+
/// - Admin token: `MyAccessToken`
97+
fn default() -> Self {
98+
Self {
99+
core: TrackerCoreSection {
100+
database: super::tracker_core_section::DatabaseSection::Sqlite {
101+
database_name: "tracker.db".to_string(),
102+
},
103+
private: false,
104+
},
105+
udp_trackers: vec![UdpTrackerSection {
106+
bind_address: "0.0.0.0:6969".to_string(),
107+
}],
108+
http_trackers: vec![HttpTrackerSection {
109+
bind_address: "0.0.0.0:7070".to_string(),
110+
}],
111+
http_api: HttpApiSection {
112+
bind_address: "0.0.0.0:1212".to_string(),
113+
admin_token: "MyAccessToken".to_string(),
114+
},
115+
}
116+
}
117+
}
118+
86119
#[cfg(test)]
87120
mod tests {
88121
use std::net::SocketAddr;

src/application/command_handlers/create/handler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use super::errors::CreateCommandHandlerError;
4646
/// EnvironmentCreationConfig, EnvironmentSection, LxdProviderSection, ProviderSection,
4747
/// SshCredentialsConfig,
4848
/// };
49-
/// use torrust_tracker_deployer_lib::domain::tracker::TrackerConfig;
49+
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
5050
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
5151
/// use torrust_tracker_deployer_lib::shared::{SystemClock, Clock};
5252
///
@@ -73,7 +73,7 @@ use super::errors::CreateCommandHandlerError;
7373
/// ProviderSection::Lxd(LxdProviderSection {
7474
/// profile_name: "lxd-dev".to_string(),
7575
/// }),
76-
/// TrackerConfig::default(),
76+
/// TrackerSection::default(),
7777
/// );
7878
///
7979
/// // Execute command with working directory
@@ -171,7 +171,7 @@ impl CreateCommandHandler {
171171
/// EnvironmentCreationConfig, EnvironmentSection, LxdProviderSection, ProviderSection,
172172
/// SshCredentialsConfig,
173173
/// };
174-
/// use torrust_tracker_deployer_lib::domain::tracker::TrackerConfig;
174+
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
175175
///
176176
/// # fn example(command: CreateCommandHandler) -> Result<(), Box<dyn std::error::Error>> {
177177
/// let config = EnvironmentCreationConfig::new(
@@ -188,7 +188,7 @@ impl CreateCommandHandler {
188188
/// ProviderSection::Lxd(LxdProviderSection {
189189
/// profile_name: "lxd-staging".to_string(),
190190
/// }),
191-
/// TrackerConfig::default(),
191+
/// TrackerSection::default(),
192192
/// );
193193
///
194194
/// let working_dir = std::path::Path::new(".");

src/application/command_handlers/create/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//! EnvironmentCreationConfig, EnvironmentSection, LxdProviderSection, ProviderSection,
3030
//! SshCredentialsConfig,
3131
//! };
32-
//! use torrust_tracker_deployer_lib::domain::tracker::TrackerConfig;
32+
//! use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
3333
//! use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
3434
//! use torrust_tracker_deployer_lib::shared::{SystemClock, Clock};
3535
//!
@@ -56,7 +56,7 @@
5656
//! ProviderSection::Lxd(LxdProviderSection {
5757
//! profile_name: "lxd-production".to_string(),
5858
//! }),
59-
//! TrackerConfig::default(),
59+
//! TrackerSection::default(),
6060
//! );
6161
//!
6262
//! // Execute command with working directory

src/application/command_handlers/create/tests/builders.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use std::sync::Arc;
99
use chrono::{DateTime, Utc};
1010
use tempfile::TempDir;
1111

12+
use crate::application::command_handlers::create::config::tracker::TrackerSection;
1213
use crate::application::command_handlers::create::config::{
1314
EnvironmentCreationConfig, EnvironmentSection, LxdProviderSection, ProviderSection,
1415
SshCredentialsConfig,
1516
};
1617
use crate::application::command_handlers::create::CreateCommandHandler;
1718
use crate::domain::environment::{Environment, EnvironmentName};
1819
use crate::domain::provider::{LxdConfig, ProviderConfig};
19-
use crate::domain::tracker::TrackerConfig;
2020
use crate::domain::ProfileName;
2121
use crate::infrastructure::persistence::repository_factory::RepositoryFactory;
2222
use crate::shared::Clock;
@@ -270,7 +270,7 @@ pub fn create_valid_test_config(temp_dir: &TempDir, env_name: &str) -> Environme
270270
ProviderSection::Lxd(LxdProviderSection {
271271
profile_name: format!("lxd-{env_name}"),
272272
}),
273-
TrackerConfig::default(),
273+
TrackerSection::default(),
274274
)
275275
}
276276

0 commit comments

Comments
 (0)