Skip to content

Commit b93affb

Browse files
committed
refactor: [#174] remove OpenTofuClient from ProvisionCommandHandler constructor
Move OpenTofuClient from constructor dependency to local creation within execute_provisioning_with_tracking method. This continues the pattern of separating global dependencies from runtime/environment-specific ones. Changes: - Remove opentofu_client field from ProvisionCommandHandler struct - Create OpenTofuClient locally with environment.tofu_build_dir() - Convert create_instance() and get_instance_info() to static methods that accept opentofu_client as parameter - Update test builders and E2E tests to remove opentofu_client injection - Add OpenTofuClient import to use type directly Benefits: - OpenTofuClient now created with environment-specific working directory - Clearer separation between global and runtime dependencies - Reduced constructor complexity (one less parameter) - Static helper methods are more testable independently
1 parent b79d7cf commit b93affb

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

src/application/command_handlers/provision/handler.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::errors::ProvisionCommandHandlerError;
99
use crate::adapters::ansible::AnsibleClient;
1010
use crate::adapters::ssh::{SshConfig, SshCredentials};
1111
use crate::adapters::tofu::client::InstanceInfo;
12+
use crate::adapters::OpenTofuClient;
1213
use crate::application::command_handlers::common::StepResult;
1314
use crate::application::steps::{
1415
ApplyInfrastructureStep, GetInstanceInfoStep, InitializeInfrastructureStep,
@@ -50,7 +51,6 @@ use crate::shared::error::Traceable;
5051
pub struct ProvisionCommandHandler {
5152
tofu_template_renderer: Arc<TofuTemplateRenderer>,
5253
ansible_template_renderer: Arc<AnsibleTemplateRenderer>,
53-
opentofu_client: Arc<crate::adapters::tofu::client::OpenTofuClient>,
5454
clock: Arc<dyn crate::shared::Clock>,
5555
repository: TypedEnvironmentRepository,
5656
}
@@ -61,14 +61,12 @@ impl ProvisionCommandHandler {
6161
pub fn new(
6262
tofu_template_renderer: Arc<TofuTemplateRenderer>,
6363
ansible_template_renderer: Arc<AnsibleTemplateRenderer>,
64-
opentofu_client: Arc<crate::adapters::tofu::client::OpenTofuClient>,
6564
clock: Arc<dyn crate::shared::Clock>,
6665
repository: Arc<dyn EnvironmentRepository>,
6766
) -> Self {
6867
Self {
6968
tofu_template_renderer,
7069
ansible_template_renderer,
71-
opentofu_client,
7270
clock,
7371
repository: TypedEnvironmentRepository::new(repository),
7472
}
@@ -178,6 +176,7 @@ impl ProvisionCommandHandler {
178176
let ssh_credentials = environment.ssh_credentials();
179177

180178
let ansible_client = Arc::new(AnsibleClient::new(environment.ansible_build_dir()));
179+
let opentofu_client = Arc::new(OpenTofuClient::new(environment.tofu_build_dir()));
181180

182181
// Track current step and execute each step
183182
// If an error occurs, we return it along with the current step
@@ -188,10 +187,11 @@ impl ProvisionCommandHandler {
188187
.map_err(|e| (e, current_step))?;
189188

190189
let current_step = ProvisionStep::OpenTofuInit;
191-
self.create_instance().map_err(|e| (e, current_step))?;
190+
Self::create_instance(&opentofu_client).map_err(|e| (e, current_step))?;
192191

193192
let current_step = ProvisionStep::GetInstanceInfo;
194-
let instance_info = self.get_instance_info().map_err(|e| (e, current_step))?;
193+
let instance_info =
194+
Self::get_instance_info(&opentofu_client).map_err(|e| (e, current_step))?;
195195
let instance_ip = instance_info.ip_address;
196196

197197
let current_step = ProvisionStep::RenderAnsibleTemplates;
@@ -238,11 +238,14 @@ impl ProvisionCommandHandler {
238238
/// # Errors
239239
///
240240
/// Returns an error if any `OpenTofu` operation fails
241-
fn create_instance(&self) -> Result<(), ProvisionCommandHandlerError> {
242-
InitializeInfrastructureStep::new(Arc::clone(&self.opentofu_client)).execute()?;
243-
ValidateInfrastructureStep::new(Arc::clone(&self.opentofu_client)).execute()?;
244-
PlanInfrastructureStep::new(Arc::clone(&self.opentofu_client)).execute()?;
245-
ApplyInfrastructureStep::new(Arc::clone(&self.opentofu_client)).execute()?;
241+
fn create_instance(
242+
opentofu_client: &Arc<OpenTofuClient>,
243+
) -> Result<(), ProvisionCommandHandlerError> {
244+
InitializeInfrastructureStep::new(Arc::clone(opentofu_client)).execute()?;
245+
ValidateInfrastructureStep::new(Arc::clone(opentofu_client)).execute()?;
246+
PlanInfrastructureStep::new(Arc::clone(opentofu_client)).execute()?;
247+
ApplyInfrastructureStep::new(Arc::clone(opentofu_client)).execute()?;
248+
246249
Ok(())
247250
}
248251

@@ -253,9 +256,10 @@ impl ProvisionCommandHandler {
253256
/// # Errors
254257
///
255258
/// Returns an error if instance information cannot be retrieved
256-
fn get_instance_info(&self) -> Result<InstanceInfo, ProvisionCommandHandlerError> {
257-
let instance_info =
258-
GetInstanceInfoStep::new(Arc::clone(&self.opentofu_client)).execute()?;
259+
fn get_instance_info(
260+
opentofu_client: &Arc<OpenTofuClient>,
261+
) -> Result<InstanceInfo, ProvisionCommandHandlerError> {
262+
let instance_info = GetInstanceInfoStep::new(Arc::clone(opentofu_client)).execute()?;
259263
Ok(instance_info)
260264
}
261265

@@ -278,13 +282,15 @@ impl ProvisionCommandHandler {
278282
ssh_port: u16,
279283
) -> Result<(), ProvisionCommandHandlerError> {
280284
let socket_addr = std::net::SocketAddr::new(instance_ip, ssh_port);
285+
281286
RenderAnsibleTemplatesStep::new(
282287
Arc::clone(&self.ansible_template_renderer),
283288
ssh_credentials.clone(),
284289
socket_addr,
285290
)
286291
.execute()
287292
.await?;
293+
288294
Ok(())
289295
}
290296

src/application/command_handlers/provision/tests/builders.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ impl ProvisionCommandHandlerTestBuilder {
8080
template_manager,
8181
));
8282

83-
let opentofu_client = Arc::new(crate::adapters::tofu::client::OpenTofuClient::new(
84-
self.temp_dir.path(),
85-
));
86-
8783
let clock: Arc<dyn crate::shared::Clock> = Arc::new(crate::shared::SystemClock);
8884

8985
let repository_factory =
@@ -92,13 +88,8 @@ impl ProvisionCommandHandlerTestBuilder {
9288
);
9389
let repository = repository_factory.create(self.temp_dir.path().to_path_buf());
9490

95-
let command_handler = ProvisionCommandHandler::new(
96-
tofu_renderer,
97-
ansible_renderer,
98-
opentofu_client,
99-
clock,
100-
repository,
101-
);
91+
let command_handler =
92+
ProvisionCommandHandler::new(tofu_renderer, ansible_renderer, clock, repository);
10293

10394
(command_handler, self.temp_dir, ssh_credentials)
10495
}

src/testing/e2e/tasks/virtual_machine/run_provision_command.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ pub async fn run_provision_command(
149149
let provision_command_handler = ProvisionCommandHandler::new(
150150
Arc::clone(&test_context.services.tofu_template_renderer),
151151
Arc::clone(&test_context.services.ansible_template_renderer),
152-
Arc::clone(&test_context.services.opentofu_client),
153152
Arc::clone(&test_context.services.clock),
154153
repository,
155154
);

0 commit comments

Comments
 (0)