-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.rs
More file actions
95 lines (79 loc) · 2.99 KB
/
Copy pathvalidate.rs
File metadata and controls
95 lines (79 loc) · 2.99 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
//! `OpenTofu` infrastructure validation step
//!
//! This module provides the `ValidateInfrastructureStep` which handles `OpenTofu`
//! validation by executing `tofu validate`. This step validates the syntax and
//! internal consistency of configuration files without creating a plan or applying changes.
//!
//! ## Key Features
//!
//! - Configuration syntax validation and error detection
//! - Internal consistency checks for resource definitions
//! - Provider schema validation against installed providers
//! - Integration with `OpenTofuClient` for command execution
//!
//! ## Validation Process
//!
//! The step executes `tofu validate` which:
//! - Validates syntax of all `.tf` configuration files
//! - Checks for missing required arguments and invalid attribute names
//! - Validates resource and data source configurations against provider schemas
//! - Ensures internal consistency of variable references and expressions
//!
//! This step should be run after initialization but before planning to catch
//! configuration errors early in the workflow.
use std::sync::Arc;
use tracing::{info, instrument};
use crate::adapters::tofu::client::OpenTofuClient;
use crate::shared::command::CommandError;
/// Simple step that validates `OpenTofu` configuration by executing `tofu validate`
pub struct ValidateInfrastructureStep {
opentofu_client: Arc<OpenTofuClient>,
}
impl ValidateInfrastructureStep {
#[must_use]
pub fn new(opentofu_client: Arc<OpenTofuClient>) -> Self {
Self { opentofu_client }
}
/// Execute the `OpenTofu` validation step
///
/// # Errors
///
/// Returns an error if:
/// * The `OpenTofu` validation fails due to syntax or consistency errors
/// * The working directory does not exist or is not accessible
/// * The `OpenTofu` command execution fails
/// * The configuration is not initialized (providers not installed)
#[instrument(
name = "validate_infrastructure",
skip_all,
fields(step_type = "infrastructure", operation = "validate")
)]
pub fn execute(&self) -> Result<(), CommandError> {
info!(
step = "validate_infrastructure",
"Validating OpenTofu configuration"
);
// Execute tofu validate command
let output = self.opentofu_client.validate()?;
info!(
step = "validate_infrastructure",
status = "success",
"OpenTofu configuration validated successfully"
);
// Log output for debugging if needed
tracing::debug!(output = %output, "OpenTofu validate output");
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::adapters::tofu::client::OpenTofuClient;
use super::*;
#[test]
fn it_should_create_validate_infrastructure_step() {
let opentofu_client = Arc::new(OpenTofuClient::new("/tmp"));
let _step = ValidateInfrastructureStep::new(opentofu_client);
// If we reach this point, the step was created successfully
}
}