Skip to content

Commit 3eb59af

Browse files
committed
refactor: convert ansible and opentofu subfolder fields to constants
Move ansible_subfolder and opentofu_subfolder from Config struct to dedicated constants in their respective infrastructure modules: - Add ANSIBLE_SUBFOLDER constant to src/infrastructure/ansible/mod.rs - Add OPENTOFU_SUBFOLDER constant to src/infrastructure/tofu/mod.rs - Remove ansible_subfolder and opentofu_subfolder fields from Config struct - Update all usage sites to reference new constants instead of config fields This improves code organization by placing constants in their logical infrastructure modules and reduces Config struct complexity.
1 parent 6dae2a1 commit 3eb59af

File tree

7 files changed

+22
-27
lines changed

7 files changed

+22
-27
lines changed

src/config/mod.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ pub struct Config {
3030
/// will be left running for manual inspection or reuse.
3131
pub keep_env: bool,
3232

33-
/// Subdirectory name for Ansible-related files within the build directory.
34-
///
35-
/// Ansible playbooks, inventory files, and configuration templates
36-
/// will be rendered to `build_dir/{ansible_subfolder}/`.
37-
pub ansible_subfolder: String,
38-
39-
/// Subdirectory name for OpenTofu-related files within the build directory.
40-
///
41-
/// OpenTofu/Terraform configuration files and state will be managed
42-
/// in `build_dir/{opentofu_subfolder}/`. Example: "tofu/lxd".
43-
pub opentofu_subfolder: String,
44-
4533
/// Directory containing template files for rendering configurations.
4634
///
4735
/// This directory should contain subdirectories for different template
@@ -66,8 +54,6 @@ pub struct Config {
6654
impl Config {
6755
/// Creates a new configuration with the provided parameters.
6856
///
69-
/// The `ansible_subfolder` is set to "ansible" and `opentofu_subfolder` is set to "tofu/lxd" internally.
70-
///
7157
/// ```rust
7258
/// # use std::path::PathBuf;
7359
/// # use torrust_tracker_deploy::config::Config;
@@ -87,8 +73,6 @@ impl Config {
8773
) -> Self {
8874
Self {
8975
keep_env,
90-
ansible_subfolder: "ansible".to_string(),
91-
opentofu_subfolder: "tofu/lxd".to_string(),
9276
templates_dir,
9377
project_root,
9478
build_dir,

src/container.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use crate::infrastructure::adapters::ansible::AnsibleClient;
1919
use crate::infrastructure::adapters::lxd::LxdClient;
2020
use crate::infrastructure::adapters::opentofu::OpenTofuClient;
2121
use crate::infrastructure::ansible::AnsibleTemplateRenderer;
22+
use crate::infrastructure::ansible::ANSIBLE_SUBFOLDER;
2223
use crate::infrastructure::tofu::TofuTemplateRenderer;
24+
use crate::infrastructure::tofu::OPENTOFU_SUBFOLDER;
2325
use crate::shared::ssh::SshCredentials;
2426

2527
/// Service clients and renderers for performing actions
@@ -49,14 +51,13 @@ impl Services {
4951
let template_manager = Arc::new(template_manager);
5052

5153
// Create OpenTofu client pointing to build/opentofu_subfolder directory
52-
let opentofu_client =
53-
OpenTofuClient::new(config.build_dir.join(&config.opentofu_subfolder));
54+
let opentofu_client = OpenTofuClient::new(config.build_dir.join(OPENTOFU_SUBFOLDER));
5455

5556
// Create LXD client for instance management
5657
let lxd_client = LxdClient::new();
5758

5859
// Create Ansible client pointing to build/ansible_subfolder directory
59-
let ansible_client = AnsibleClient::new(config.build_dir.join(&config.ansible_subfolder));
60+
let ansible_client = AnsibleClient::new(config.build_dir.join(ANSIBLE_SUBFOLDER));
6061

6162
// Create provision template renderer
6263
let tofu_template_renderer = TofuTemplateRenderer::new(

src/e2e/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use tracing::{info, warn};
2626
use crate::config::Config;
2727
use crate::container::Services;
2828
use crate::domain::Environment;
29+
use crate::infrastructure::tofu::OPENTOFU_SUBFOLDER;
2930

3031
/// Errors that can occur during test context creation and initialization
3132
#[derive(Debug, thiserror::Error)]
@@ -317,7 +318,7 @@ impl Drop for TestContext {
317318
TestContextType::VirtualMachine => {
318319
// Try basic cleanup in case async cleanup failed
319320
// Using emergency_destroy for consistent OpenTofu handling
320-
let tofu_dir = self.config.build_dir.join(&self.config.opentofu_subfolder);
321+
let tofu_dir = self.config.build_dir.join(OPENTOFU_SUBFOLDER);
321322

322323
if let Err(e) =
323324
crate::infrastructure::adapters::opentofu::emergency_destroy(&tofu_dir)

src/e2e/environment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use tracing::{info, warn};
2727
use crate::config::Config;
2828
use crate::container::Services;
2929
use crate::domain::{InstanceName, ProfileName};
30+
use crate::infrastructure::tofu::OPENTOFU_SUBFOLDER;
3031
use crate::shared::{ssh::SshCredentials, Username};
3132

3233
/// Errors that can occur during test context creation and initialization
@@ -339,7 +340,7 @@ impl Drop for TestContext {
339340
TestContextType::VirtualMachine => {
340341
// Try basic cleanup in case async cleanup failed
341342
// Using emergency_destroy for consistent OpenTofu handling
342-
let tofu_dir = self.config.build_dir.join(&self.config.opentofu_subfolder);
343+
let tofu_dir = self.config.build_dir.join(OPENTOFU_SUBFOLDER);
343344

344345
if let Err(e) =
345346
crate::infrastructure::adapters::opentofu::emergency_destroy(&tofu_dir)

src/e2e/tasks/virtual_machine/preflight_cleanup.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::e2e::tasks::preflight_cleanup::{
1010
};
1111
use crate::infrastructure::adapters::lxd::client::LxdClient;
1212
use crate::infrastructure::adapters::opentofu::{self};
13+
use crate::infrastructure::tofu::OPENTOFU_SUBFOLDER;
1314
use tracing::{info, warn};
1415

1516
/// Performs comprehensive pre-flight cleanup for VM-based E2E tests
@@ -84,10 +85,7 @@ pub fn cleanup_lingering_resources(
8485
fn cleanup_opentofu_infrastructure(
8586
test_context: &TestContext,
8687
) -> Result<(), PreflightCleanupError> {
87-
let tofu_dir = test_context
88-
.config
89-
.build_dir
90-
.join(&test_context.config.opentofu_subfolder);
88+
let tofu_dir = test_context.config.build_dir.join(OPENTOFU_SUBFOLDER);
9189

9290
if !tofu_dir.exists() {
9391
info!(

src/infrastructure/ansible/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
//!
66
//! The main component is `AnsibleTemplateRenderer` which handles the generation
77
//! of Ansible configuration files with dynamic content like VM IP addresses and SSH keys.
8-
98
pub mod template;
109

1110
pub use template::{AnsibleTemplateRenderer, InventoryTemplateRenderer};
11+
12+
/// Subdirectory name for Ansible-related files within the build directory.
13+
///
14+
/// Ansible playbooks, inventory files, and configuration templates
15+
/// will be rendered to `build_dir/{ANSIBLE_SUBFOLDER}/`.
16+
pub const ANSIBLE_SUBFOLDER: &str = "ansible";

src/infrastructure/tofu/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
//! - Template processing for infrastructure definitions
1212
//!
1313
//! The module complements the `OpenTofu` command wrapper by providing the template
14-
1514
pub mod template;
1615

1716
pub use template::{CloudInitTemplateRenderer, ProvisionTemplateError, TofuTemplateRenderer};
17+
18+
/// Subdirectory name for OpenTofu-related files within the build directory.
19+
///
20+
/// OpenTofu/Terraform configuration files and state will be managed
21+
/// in `build_dir/{OPENTOFU_SUBFOLDER}/`. Example: "tofu/lxd".
22+
pub const OPENTOFU_SUBFOLDER: &str = "tofu/lxd";

0 commit comments

Comments
 (0)