torrust_tracker_deployer_lib/application/command_handlers/destroy/mod.rs
1//! Destroy Command Module
2//!
3//! This module implements the delivery-agnostic `DestroyCommandHandler`
4//! for orchestrating infrastructure destruction business logic.
5//!
6//! ## Architecture
7//!
8//! The `DestroyCommandHandler` implements the Command Pattern and uses Dependency Injection
9//! to interact with infrastructure services through interfaces:
10//!
11//! - **Repository Pattern**: Persists environment state via `EnvironmentRepository`
12//! - **Clock Abstraction**: Provides deterministic time for testing via `Clock` trait
13//! - **Domain-Driven Design**: Uses domain objects from `domain::environment`
14//!
15//! ## Design Principles
16//!
17//! - **Delivery-Agnostic**: Works with CLI, REST API, or any delivery mechanism
18//! - **Synchronous**: Follows existing patterns (no async/await)
19//! - **Explicit State Transitions**: Type-safe state machine for environment lifecycle
20//! - **Explicit Errors**: All errors implement `.help()` with actionable guidance
21//! - **Idempotent**: Can be safely executed multiple times on the same environment
22//!
23//! ## Destruction Workflow
24//!
25//! The command handler orchestrates a multi-step workflow:
26//!
27//! 1. **Load environment** - Retrieve environment from repository
28//! 2. **Check current state** - Handle already-destroyed environments gracefully
29//! 3. **Destroy infrastructure** - Remove VMs and resources via `OpenTofu` (if provisioned)
30//! 4. **Clean up state files** - Remove data and build directories
31//!
32//! ## State Management
33//!
34//! The command handler integrates with the type-state pattern for environment lifecycle:
35//!
36//! - Accepts environment in any state (via environment name lookup)
37//! - Transitions to `Environment<Destroying>` at start
38//! - Returns `Environment<Destroyed>` on success
39//! - Transitions to `Environment<DestroyFailed>` on error
40//!
41//! State is persisted after each transition using the injected repository.
42//!
43//! ## Idempotency
44//!
45//! The destroy operation is idempotent - running it multiple times on the same
46//! environment will succeed without errors, whether infrastructure was previously
47//! provisioned or not.
48
49pub mod errors;
50pub mod handler;
51
52#[cfg(test)]
53mod tests;
54
55// Re-export main types for convenience
56pub use errors::DestroyCommandHandlerError;
57pub use handler::DestroyCommandHandler;