-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_loader.rs
More file actions
288 lines (248 loc) · 9.88 KB
/
Copy pathconfig_loader.rs
File metadata and controls
288 lines (248 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! Configuration Loader with Figment Integration
//!
//! This module provides configuration loading from JSON files using Figment.
//! Figment is used only in the presentation layer as a delivery mechanism,
//! following DDD architecture boundaries.
use std::path::Path;
use figment::{
providers::{Format, Json},
Figment,
};
use crate::application::command_handlers::create::config::EnvironmentCreationConfig;
use super::errors::{ConfigFormat, CreateSubcommandError};
/// Configuration loader using Figment for JSON file parsing
///
/// This loader is part of the presentation layer and handles the specifics
/// of loading and parsing configuration files. It uses Figment for flexible
/// configuration file handling and validation.
///
/// # Architecture Note
///
/// Figment stays in the presentation layer as a delivery mechanism.
/// The domain layer remains independent of configuration parsing libraries.
pub struct ConfigLoader;
impl ConfigLoader {
/// Load environment creation configuration from a JSON file
///
/// This method loads and parses a JSON configuration file, then validates
/// it according to domain rules. All errors are wrapped in presentation
/// layer error types with helpful guidance.
///
/// # Arguments
///
/// * `config_path` - Path to the JSON configuration file
///
/// # Returns
///
/// * `Ok(EnvironmentCreationConfig)` - Successfully loaded and validated configuration
/// * `Err(CreateSubcommandError)` - File not found, parsing failed, or validation failed
///
/// # Errors
///
/// Returns an error if:
/// - Configuration file doesn't exist
/// - JSON parsing fails (syntax errors, type mismatches)
/// - Domain validation fails (invalid names, missing SSH keys, etc.)
///
/// All errors include detailed troubleshooting guidance via `.help()`.
///
/// # Examples
///
/// ```rust,no_run
/// use std::path::Path;
/// use torrust_tracker_deployer_lib::presentation::commands::create::ConfigLoader;
///
/// let loader = ConfigLoader;
/// let config = loader.load_from_file(Path::new("config/environment.json"))?;
///
/// println!("Loaded environment: {}", config.environment.name);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn load_from_file(
&self,
config_path: &Path,
) -> Result<EnvironmentCreationConfig, CreateSubcommandError> {
// Step 1: Verify file exists
if !config_path.exists() {
return Err(CreateSubcommandError::ConfigFileNotFound {
path: config_path.to_path_buf(),
});
}
// Step 2: Load with Figment
// We don't use defaults here because we want explicit configuration
let config: EnvironmentCreationConfig = Figment::new()
.merge(Json::file(config_path))
.extract()
.map_err(|source| CreateSubcommandError::ConfigParsingFailed {
path: config_path.to_path_buf(),
format: ConfigFormat::Json,
source: Box::new(source),
})?;
// Step 3: Validate using domain rules
// This converts string-based config to domain types and validates
config
.clone()
.to_environment_params()
.map_err(|source| CreateSubcommandError::ConfigValidationFailed { source })?;
Ok(config)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn it_should_load_valid_json_configuration() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("config.json");
// Use absolute paths to SSH keys to ensure they work regardless of current directory
let project_root = env!("CARGO_MANIFEST_DIR");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
// Write a valid configuration file
let config_json = format!(
r#"{{
"environment": {{
"name": "test-env"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}"
}}
}}"#
);
fs::write(&config_path, config_json).unwrap();
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(result.is_ok(), "Should load valid configuration");
let config = result.unwrap();
assert_eq!(config.environment.name, "test-env");
}
#[test]
fn it_should_return_error_for_nonexistent_file() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("nonexistent.json");
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(result.is_err());
match result.unwrap_err() {
CreateSubcommandError::ConfigFileNotFound { path } => {
assert_eq!(path, config_path);
}
_ => panic!("Expected ConfigFileNotFound error"),
}
}
#[test]
fn it_should_return_error_for_invalid_json() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("invalid.json");
// Write invalid JSON (missing closing brace)
fs::write(&config_path, r#"{"environment": {"name": "test""#).unwrap();
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(result.is_err());
match result.unwrap_err() {
CreateSubcommandError::ConfigParsingFailed { path, format, .. } => {
assert_eq!(path, config_path);
assert!(matches!(format, ConfigFormat::Json));
}
_ => panic!("Expected ConfigParsingFailed error"),
}
}
#[test]
fn it_should_return_error_for_missing_required_fields() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("incomplete.json");
// Write JSON with missing required fields
fs::write(&config_path, r#"{"environment": {}}"#).unwrap();
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(result.is_err());
// Should fail at parsing or validation stage
}
#[test]
fn it_should_return_error_for_invalid_environment_name() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("invalid_name.json");
// Use absolute paths to SSH keys to ensure they work regardless of current directory
let project_root = env!("CARGO_MANIFEST_DIR");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
// Write config with invalid environment name
let config_json = format!(
r#"{{
"environment": {{
"name": "Invalid_Name_With_Underscore"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}"
}}
}}"#
);
fs::write(&config_path, config_json).unwrap();
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(result.is_err());
match result.unwrap_err() {
CreateSubcommandError::ConfigValidationFailed { .. } => {
// Expected - validation should catch invalid environment name
}
other => panic!("Expected ConfigValidationFailed, got: {other:?}"),
}
}
#[test]
fn it_should_return_error_for_missing_ssh_keys() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("missing_keys.json");
// Write config with non-existent SSH key files
let config_json = r#"{
"environment": {
"name": "test-env"
},
"ssh_credentials": {
"private_key_path": "/nonexistent/key",
"public_key_path": "/nonexistent/key.pub"
}
}"#;
fs::write(&config_path, config_json).unwrap();
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(result.is_err());
match result.unwrap_err() {
CreateSubcommandError::ConfigValidationFailed { .. } => {
// Expected - validation should catch missing SSH keys
}
other => panic!("Expected ConfigValidationFailed, got: {other:?}"),
}
}
#[test]
fn it_should_handle_config_with_optional_fields_having_defaults() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("with_defaults.json");
// Use absolute paths to SSH keys to ensure they work regardless of current directory
let project_root = env!("CARGO_MANIFEST_DIR");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
// Write config without optional fields (they should use defaults)
let config_json = format!(
r#"{{
"environment": {{
"name": "test-env"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}"
}}
}}"#
);
fs::write(&config_path, config_json).unwrap();
let loader = ConfigLoader;
let result = loader.load_from_file(&config_path);
assert!(
result.is_ok(),
"Should load config with default values for optional fields"
);
}
}