torrust_tracker_deployer_lib/application/steps/infrastructure/
destroy.rs1use std::sync::Arc;
25
26use tracing::{info, instrument};
27
28use crate::adapters::tofu::client::OpenTofuClient;
29use crate::shared::command::CommandError;
30
31pub struct DestroyInfrastructureStep {
33 opentofu_client: Arc<OpenTofuClient>,
34 auto_approve: bool,
35}
36
37impl DestroyInfrastructureStep {
38 #[must_use]
39 pub fn new(opentofu_client: Arc<OpenTofuClient>) -> Self {
40 Self {
41 opentofu_client,
42 auto_approve: true, }
44 }
45
46 #[must_use]
47 pub fn with_auto_approve(mut self, auto_approve: bool) -> Self {
48 self.auto_approve = auto_approve;
49 self
50 }
51
52 #[instrument(
61 name = "destroy_infrastructure",
62 skip_all,
63 fields(
64 step_type = "infrastructure",
65 operation = "destroy",
66 auto_approve = %self.auto_approve
67 )
68 )]
69 pub fn execute(&self) -> Result<(), CommandError> {
70 info!(
71 step = "destroy_infrastructure",
72 auto_approve = self.auto_approve,
73 "Destroying OpenTofu infrastructure"
74 );
75
76 let output = self
78 .opentofu_client
79 .destroy(self.auto_approve, &["-var-file=variables.tfvars"])?;
80
81 info!(
82 step = "destroy_infrastructure",
83 status = "success",
84 "OpenTofu infrastructure destroyed successfully"
85 );
86
87 tracing::debug!(output = %output, "OpenTofu destroy output");
89
90 Ok(())
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use std::sync::Arc;
97
98 use crate::adapters::tofu::client::OpenTofuClient;
99
100 use super::*;
101
102 #[test]
103 fn it_should_create_destroy_infrastructure_step() {
104 let opentofu_client = Arc::new(OpenTofuClient::new("/tmp"));
105
106 let _step = DestroyInfrastructureStep::new(opentofu_client);
107
108 }
110
111 #[test]
112 fn it_should_create_step_with_custom_auto_approve() {
113 let opentofu_client = Arc::new(OpenTofuClient::new("/tmp"));
114
115 let step = DestroyInfrastructureStep::new(opentofu_client).with_auto_approve(false);
116
117 assert!(!step.auto_approve);
118 }
119
120 #[test]
121 fn it_should_default_to_auto_approve() {
122 let opentofu_client = Arc::new(OpenTofuClient::new("/tmp"));
123
124 let step = DestroyInfrastructureStep::new(opentofu_client);
125
126 assert!(step.auto_approve);
127 }
128}