-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.rs
More file actions
290 lines (273 loc) · 10.3 KB
/
handler.rs
File metadata and controls
290 lines (273 loc) · 10.3 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
289
290
//! Create Command Implementation
//!
//! This module implements the `CreateCommandHandler` that orchestrates environment
//! creation business logic. It follows the Command Pattern with dependency
//! injection and is delivery-agnostic.
use std::sync::Arc;
use tracing::{info, instrument};
use crate::application::command_handlers::create::config::EnvironmentCreationConfig;
use crate::domain::environment::repository::EnvironmentRepository;
use crate::domain::environment::{Created, Environment};
use crate::shared::Clock;
use super::errors::CreateCommandHandlerError;
/// Command to create a new deployment environment
///
/// This command is delivery-agnostic and can be used from CLI, REST API,
/// GraphQL, or any other delivery mechanism. It orchestrates the business
/// logic for environment creation without knowledge of how the configuration
/// was obtained.
///
/// # Architecture
///
/// The command follows these design principles:
///
/// - **Synchronous**: No async/await, following existing patterns
/// - **Dependency Injection**: Uses `Arc<dyn Trait>` for testability
/// - **Repository Pattern**: Delegates persistence to repository
/// - **Explicit Errors**: All failures return structured errors with `.help()`
///
/// # Business Logic Flow
///
/// 1. Convert configuration to domain objects
/// 2. Check if environment already exists (prevent duplicates)
/// 3. Create environment entity using `Environment::new()`
/// 4. Persist via repository (repository handles directory creation)
///
/// # Examples
///
/// ```rust,no_run
/// use std::sync::Arc;
/// use torrust_tracker_deployer_lib::application::command_handlers::create::CreateCommandHandler;
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::{
/// EnvironmentCreationConfig, EnvironmentSection, LxdProviderSection, ProviderSection,
/// SshCredentialsConfig,
/// };
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
/// use torrust_tracker_deployer_lib::shared::{SystemClock, Clock};
///
/// // Setup dependencies
/// let repository_factory = RepositoryFactory::new(std::time::Duration::from_secs(30));
/// let repository = repository_factory.create(std::path::PathBuf::from("."));
/// let clock: Arc<dyn Clock> = Arc::new(SystemClock);
///
/// // Create command
/// let command = CreateCommandHandler::new(repository, clock);
///
/// // Prepare configuration
/// let config = EnvironmentCreationConfig::new(
/// EnvironmentSection {
/// name: "dev".to_string(),
/// instance_name: None, // Auto-generate from environment name
/// },
/// SshCredentialsConfig::new(
/// "fixtures/testing_rsa".to_string(),
/// "fixtures/testing_rsa.pub".to_string(),
/// "torrust".to_string(),
/// 22,
/// ),
/// ProviderSection::Lxd(LxdProviderSection {
/// profile_name: "lxd-dev".to_string(),
/// }),
/// TrackerSection::default(),
/// None, // prometheus
/// None, // grafana
/// None, // https
/// );
///
/// // Execute command with working directory
/// let working_dir = std::path::Path::new(".");
/// let environment = command.execute(config, working_dir)?;
/// println!("Created environment: {}", environment.name());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct CreateCommandHandler {
/// Repository for persisting environment state
pub(crate) environment_repository: Arc<dyn EnvironmentRepository>,
/// Clock for timestamp generation (injected for testability)
pub(crate) clock: Arc<dyn Clock>,
}
impl CreateCommandHandler {
/// Create a new `CreateCommandHandler` with required dependencies
///
/// # Arguments
///
/// * `environment_repository` - Repository for persisting environment state
/// * `clock` - Clock for timestamp generation (for future use)
///
/// # Examples
///
/// ```rust,no_run
/// use std::sync::Arc;
/// use torrust_tracker_deployer_lib::application::command_handlers::create::CreateCommandHandler;
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
/// use torrust_tracker_deployer_lib::shared::{SystemClock, Clock};
///
/// let repository_factory = RepositoryFactory::new(std::time::Duration::from_secs(30));
/// let repository = repository_factory.create(std::path::PathBuf::from("."));
/// let clock: Arc<dyn Clock> = Arc::new(SystemClock);
///
/// let command = CreateCommandHandler::new(repository, clock);
/// ```
#[must_use]
pub fn new(
environment_repository: Arc<dyn EnvironmentRepository>,
clock: Arc<dyn Clock>,
) -> Self {
Self {
environment_repository,
clock,
}
}
/// Execute the create command with validated configuration
///
/// This method orchestrates the complete environment creation workflow:
/// 1. Converts configuration to domain objects
/// 2. Validates environment uniqueness
/// 3. Creates the environment entity
/// 4. Persists the environment state
///
/// # Arguments
///
/// * `config` - Validated environment configuration from domain layer
///
/// # Returns
///
/// * `Ok(Environment<Created>)` - Successfully created environment
/// * `Err(CreateCommandHandlerError)` - Business logic or persistence failure
///
/// # Business Rules
///
/// 1. Configuration must convert to valid domain objects
/// 2. Environment name must be unique (no duplicates)
/// 3. Repository handles directory creation atomically during save
/// 4. Environment state must be persisted successfully
///
/// # Errors
///
/// Returns an error if:
/// - Configuration validation fails
/// - Environment with the same name already exists
/// - Repository persistence fails
///
/// All errors implement `.help()` with detailed troubleshooting guidance.
///
/// # Panics
///
/// This function does not panic in practice. The internal `.expect()` call
/// when generating the profile name is theoretically unreachable because
/// valid environment names always produce valid profile names.
///
/// # Examples
///
/// ```rust,no_run
/// use torrust_tracker_deployer_lib::application::command_handlers::create::CreateCommandHandler;
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::{
/// EnvironmentCreationConfig, EnvironmentSection, LxdProviderSection, ProviderSection,
/// SshCredentialsConfig,
/// };
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::TrackerSection;
///
/// # fn example(command: CreateCommandHandler) -> Result<(), Box<dyn std::error::Error>> {
/// let config = EnvironmentCreationConfig::new(
/// EnvironmentSection {
/// name: "staging".to_string(),
/// instance_name: None, // Auto-generate from environment name
/// },
/// SshCredentialsConfig::new(
/// "keys/stage_key".to_string(),
/// "keys/stage_key.pub".to_string(),
/// "torrust".to_string(),
/// 22,
/// ),
/// ProviderSection::Lxd(LxdProviderSection {
/// profile_name: "lxd-staging".to_string(),
/// }),
/// TrackerSection::default(),
/// None, // prometheus
/// None, // grafana
/// None, // https
/// );
///
/// let working_dir = std::path::Path::new(".");
/// let environment = command.execute(config, working_dir)?;
/// println!("Created: {}", environment.name());
/// # Ok(())
/// # }
/// ```
#[instrument(
name = "create_command",
skip_all,
fields(
command_type = "create",
environment = %config.environment.name
)
)]
pub fn execute(
&self,
config: EnvironmentCreationConfig,
working_dir: &std::path::Path,
) -> Result<Environment<Created>, CreateCommandHandlerError> {
let (
environment_name,
_instance_name,
provider_config,
ssh_credentials,
ssh_port,
tracker_config,
prometheus_config,
grafana_config,
https_config,
) = config
.to_environment_params()
.map_err(CreateCommandHandlerError::InvalidConfiguration)?;
if self
.environment_repository
.exists(&environment_name)
.map_err(CreateCommandHandlerError::RepositoryError)?
{
return Err(CreateCommandHandlerError::EnvironmentAlreadyExists {
name: environment_name.as_str().to_string(),
});
}
let environment = Environment::with_working_dir_and_tracker(
environment_name,
provider_config,
ssh_credentials,
ssh_port,
tracker_config,
prometheus_config,
grafana_config,
https_config,
working_dir,
self.clock.now(),
);
self.environment_repository
.save(&environment.clone().into_any())
.map_err(CreateCommandHandlerError::RepositoryError)?;
info!(
command = "create",
environment = %environment.name(),
"Environment created successfully"
);
Ok(environment)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_create_command_with_dependencies() {
use crate::infrastructure::persistence::repository_factory::RepositoryFactory;
use crate::shared::SystemClock;
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let repository_factory = RepositoryFactory::new(std::time::Duration::from_secs(30));
let repository = repository_factory.create(temp_dir.path().to_path_buf());
let clock: Arc<dyn Clock> = Arc::new(SystemClock);
let command = CreateCommandHandler::new(repository, clock);
// Verify the command was created (basic structure test)
assert_eq!(Arc::strong_count(&command.environment_repository), 1);
assert_eq!(Arc::strong_count(&command.clock), 1);
}
}