-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinitialize.rs
More file actions
93 lines (77 loc) · 2.75 KB
/
initialize.rs
File metadata and controls
93 lines (77 loc) · 2.75 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
//! `OpenTofu` infrastructure initialization step
//!
//! This module provides the `InitializeInfrastructureStep` which handles `OpenTofu`
//! initialization by executing `tofu init`. This step prepares the working directory
//! for infrastructure operations by downloading providers and initializing state.
//!
//! ## Key Features
//!
//! - `OpenTofu` working directory initialization
//! - Provider plugin downloading and installation
//! - Backend configuration and state initialization
//! - Integration with `OpenTofuClient` for command execution
//!
//! ## Initialization Process
//!
//! The step executes `tofu init` which performs:
//! - Provider plugin resolution and download
//! - Backend initialization (local or remote state)
//! - Module downloading if applicable
//! - Working directory setup for subsequent operations
//!
//! This is typically the first step in any infrastructure provisioning workflow.
use std::sync::Arc;
use tracing::{info, instrument};
use crate::adapters::tofu::client::OpenTofuClient;
use crate::shared::command::CommandError;
/// Simple step that initializes `OpenTofu` configuration by executing `tofu init`
pub struct InitializeInfrastructureStep {
opentofu_client: Arc<OpenTofuClient>,
}
impl InitializeInfrastructureStep {
#[must_use]
pub fn new(opentofu_client: Arc<OpenTofuClient>) -> Self {
Self { opentofu_client }
}
/// Execute the `OpenTofu` initialization step
///
/// # Errors
///
/// Returns an error if:
/// * The `OpenTofu` initialization fails
/// * The working directory does not exist or is not accessible
/// * The `OpenTofu` command execution fails
#[instrument(
name = "initialize_infrastructure",
skip_all,
fields(step_type = "infrastructure", operation = "init")
)]
pub fn execute(&self) -> Result<(), CommandError> {
info!(
step = "initialize_infrastructure",
"Initializing OpenTofu infrastructure"
);
// Execute tofu init command
let output = self.opentofu_client.init()?;
info!(
step = "initialize_infrastructure",
status = "success",
"OpenTofu infrastructure initialized successfully"
);
// Log output for debugging if needed
tracing::debug!(output = %output, "OpenTofu init output");
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::adapters::tofu::client::OpenTofuClient;
use super::*;
#[test]
fn it_should_create_initialize_infrastructure_step() {
let opentofu_client = Arc::new(OpenTofuClient::new("/tmp"));
let _step = InitializeInfrastructureStep::new(opentofu_client);
// If we reach this point, the step was created successfully
}
}