Skip to content

Commit cdc2b4d

Browse files
committed
refactor: remove with_host and with_host_and_port methods from SshCredentials
- Removed SshCredentials::with_host() method - Removed SshCredentials::with_host_and_port() method - Updated all callers to use SshConnection constructors directly: - Use SshConnection::with_default_port() for default port 22 - Use SshConnection::new() with SocketAddr for custom ports - Cleaned up unused imports in credentials.rs - Updated imports in affected files to include SshConnection - All tests pass and linting is clean This simplifies the API by removing duplicate functionality and makes socket address creation explicit in the calling code.
1 parent cff50ad commit cdc2b4d

File tree

10 files changed

+26
-62
lines changed

10 files changed

+26
-62
lines changed

src/application/commands/provision.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::infrastructure::adapters::opentofu::client::{InstanceInfo, OpenTofuEr
3030
use crate::infrastructure::ansible::AnsibleTemplateRenderer;
3131
use crate::infrastructure::tofu::{ProvisionTemplateError, TofuTemplateRenderer};
3232
use crate::shared::executor::CommandError;
33-
use crate::shared::ssh::{credentials::SshCredentials, SshError};
33+
use crate::shared::ssh::{credentials::SshCredentials, SshConnection, SshError};
3434

3535
/// Comprehensive error type for the `ProvisionCommand`
3636
#[derive(Debug, thiserror::Error)]
@@ -155,7 +155,8 @@ impl ProvisionCommand {
155155
.execute()
156156
.await?;
157157

158-
let ssh_connection = self.ssh_credentials.clone().with_host(instance_ip);
158+
let ssh_connection =
159+
SshConnection::with_default_port(self.ssh_credentials.clone(), instance_ip);
159160
WaitForSSHConnectivityStep::new(ssh_connection)
160161
.execute()
161162
.await?;

src/application/commands/test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::application::steps::{
2222
};
2323
use crate::infrastructure::remote_actions::RemoteActionError;
2424
use crate::shared::executor::CommandError;
25-
use crate::shared::ssh::credentials::SshCredentials;
25+
use crate::shared::ssh::{credentials::SshCredentials, SshConnection};
2626

2727
/// Comprehensive error type for the `TestCommand`
2828
#[derive(Debug, thiserror::Error)]
@@ -73,7 +73,8 @@ impl TestCommand {
7373
"Starting complete infrastructure testing workflow"
7474
);
7575

76-
let ssh_connection = self.ssh_credentials.clone().with_host(self.instance_ip);
76+
let ssh_connection =
77+
SshConnection::with_default_port(self.ssh_credentials.clone(), self.instance_ip);
7778

7879
// TODO: Cloud-init validation disabled for container testing
7980
// This step fails when testing with Docker containers since they don't have cloud-init installed.

src/application/steps/connectivity/wait_ssh_connectivity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mod tests {
9090
"/tmp/test_key.pub".into(),
9191
"testuser".to_string(),
9292
);
93-
let ssh_connection = credentials.with_host(instance_ip);
93+
let ssh_connection = SshConnection::with_default_port(credentials, instance_ip);
9494

9595
let step = WaitForSSHConnectivityStep::new(ssh_connection);
9696

@@ -110,7 +110,7 @@ mod tests {
110110
"/home/user/.ssh/id_rsa.pub".into(),
111111
"torrust".to_string(),
112112
);
113-
let ssh_connection = credentials.with_host(instance_ip);
113+
let ssh_connection = SshConnection::with_default_port(credentials, instance_ip);
114114

115115
let step = WaitForSSHConnectivityStep::new(ssh_connection);
116116

@@ -130,7 +130,7 @@ mod tests {
130130
"/path/to/ssh/key.pub".into(),
131131
"admin".to_string(),
132132
);
133-
let ssh_connection = credentials.with_host(instance_ip);
133+
let ssh_connection = SshConnection::with_default_port(credentials, instance_ip);
134134

135135
let step = WaitForSSHConnectivityStep::new(ssh_connection);
136136

src/application/steps/validation/cloud_init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mod tests {
8686
"test_user".to_string(),
8787
);
8888
let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
89-
let ssh_connection = ssh_credentials.with_host(host_ip);
89+
let ssh_connection = SshConnection::with_default_port(ssh_credentials, host_ip);
9090

9191
let step = ValidateCloudInitCompletionStep::new(ssh_connection);
9292

src/application/steps/validation/docker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mod tests {
8585
"test_user".to_string(),
8686
);
8787
let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
88-
let ssh_connection = ssh_credentials.with_host(host_ip);
88+
let ssh_connection = SshConnection::with_default_port(ssh_credentials, host_ip);
8989

9090
let step = ValidateDockerInstallationStep::new(ssh_connection);
9191

src/application/steps/validation/docker_compose.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ mod tests {
9191
"test_user".to_string(),
9292
);
9393
let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
94-
let ssh_connection = ssh_credentials.with_host(host_ip);
94+
let ssh_connection = SshConnection::with_default_port(ssh_credentials, host_ip);
9595

9696
let step = ValidateDockerComposeInstallationStep::new(ssh_connection);
9797

src/e2e/tasks/run_configuration_validation.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::config::SshCredentials;
2828
use crate::infrastructure::remote_actions::{
2929
DockerComposeValidator, DockerValidator, RemoteAction,
3030
};
31+
use crate::shared::ssh::SshConnection;
3132

3233
/// Run configuration validation tests on a configured instance
3334
///
@@ -127,7 +128,8 @@ async fn validate_docker_installation(
127128
) -> Result<()> {
128129
info!("Validating Docker installation");
129130

130-
let ssh_connection = ssh_credentials.clone().with_host_and_port(ip_addr, port);
131+
let ssh_connection =
132+
SshConnection::new(ssh_credentials.clone(), SocketAddr::new(ip_addr, port));
131133

132134
let docker_validator = DockerValidator::new(ssh_connection);
133135
docker_validator
@@ -165,7 +167,8 @@ async fn validate_docker_compose_installation(
165167
) -> Result<()> {
166168
info!("Validating Docker Compose installation");
167169

168-
let ssh_connection = ssh_credentials.clone().with_host_and_port(ip_addr, port);
170+
let ssh_connection =
171+
SshConnection::new(ssh_credentials.clone(), SocketAddr::new(ip_addr, port));
169172

170173
let compose_validator = DockerComposeValidator::new(ssh_connection);
171174
compose_validator

src/shared/ssh/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl SshClient {
245245

246246
#[cfg(test)]
247247
mod tests {
248-
use super::super::SshCredentials;
248+
use super::super::{SshConnection, SshCredentials};
249249
use super::*;
250250
use std::net::{IpAddr, Ipv4Addr};
251251
use std::path::PathBuf;
@@ -258,7 +258,7 @@ mod tests {
258258
PathBuf::from("/path/to/key.pub"),
259259
"testuser".to_string(),
260260
);
261-
let ssh_connection = credentials.with_host(host_ip);
261+
let ssh_connection = SshConnection::with_default_port(credentials, host_ip);
262262
let ssh_client = SshClient::new(ssh_connection);
263263

264264
assert_eq!(
@@ -281,7 +281,7 @@ mod tests {
281281
PathBuf::from("/path/to/key.pub"),
282282
"testuser".to_string(),
283283
);
284-
let ssh_connection = credentials.with_host(host_ip);
284+
let ssh_connection = SshConnection::with_default_port(credentials, host_ip);
285285
let ssh_client = SshClient::new(ssh_connection);
286286

287287
assert_eq!(

src/shared/ssh/connection.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use std::path::PathBuf;
1818

1919
use super::SshCredentials;
2020

21+
/// Default SSH port number.
22+
const DEFAULT_SSH_PORT: u16 = 22;
23+
2124
/// SSH connection configuration for a specific remote instance.
2225
///
2326
/// Contains both the SSH credentials and the target host socket address,
@@ -53,10 +56,10 @@ impl SshConnection {
5356
/// );
5457
/// ```
5558
#[must_use]
56-
pub fn new(credentials: SshCredentials, socket_addr: SocketAddr) -> Self {
59+
pub fn new(credentials: SshCredentials, ssh_socket_addr: SocketAddr) -> Self {
5760
Self {
5861
credentials,
59-
socket_addr,
62+
socket_addr: ssh_socket_addr,
6063
}
6164
}
6265

@@ -80,7 +83,7 @@ impl SshConnection {
8083
/// ```
8184
#[must_use]
8285
pub fn with_default_port(credentials: SshCredentials, host_ip: IpAddr) -> Self {
83-
let socket_addr = SocketAddr::new(host_ip, 22);
86+
let socket_addr = SocketAddr::new(host_ip, DEFAULT_SSH_PORT);
8487
Self::new(credentials, socket_addr)
8588
}
8689

@@ -119,30 +122,4 @@ impl SshConnection {
119122
pub fn socket_addr(&self) -> SocketAddr {
120123
self.socket_addr
121124
}
122-
123-
/// Creates a new SSH connection configuration with IP address and port.
124-
///
125-
/// This is a convenience method for creating a connection when you have
126-
/// separate IP address and port values.
127-
///
128-
/// ```rust
129-
/// # use std::net::{IpAddr, Ipv4Addr};
130-
/// # use std::path::PathBuf;
131-
/// # use torrust_tracker_deploy::shared::ssh::{SshCredentials, SshConnection};
132-
/// let credentials = SshCredentials::new(
133-
/// PathBuf::from("/home/user/.ssh/deploy_key"),
134-
/// PathBuf::from("/home/user/.ssh/deploy_key.pub"),
135-
/// "ubuntu".to_string(),
136-
/// );
137-
/// let connection = SshConnection::with_ip_and_port(
138-
/// credentials,
139-
/// IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)),
140-
/// 2222,
141-
/// );
142-
/// ```
143-
#[must_use]
144-
pub fn with_ip_and_port(credentials: SshCredentials, host_ip: IpAddr, port: u16) -> Self {
145-
let socket_addr = SocketAddr::new(host_ip, port);
146-
Self::new(credentials, socket_addr)
147-
}
148125
}

src/shared/ssh/credentials.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
//! The credentials are typically configured at startup and used throughout
1414
//! the deployment process for secure remote access to provisioned instances.
1515
16-
use std::net::IpAddr;
1716
use std::path::PathBuf;
1817

19-
use super::SshConnection;
20-
2118
/// SSH credentials for remote instance authentication.
2219
///
2320
/// Contains the static SSH authentication information that is known
@@ -69,19 +66,4 @@ impl SshCredentials {
6966
ssh_username,
7067
}
7168
}
72-
73-
/// Promote these credentials to a full SSH connection configuration.
74-
///
75-
/// This method creates an `SshConnection` by combining these credentials
76-
/// with a specific host IP address.
77-
#[must_use]
78-
pub fn with_host(self, host_ip: IpAddr) -> SshConnection {
79-
SshConnection::with_default_port(self, host_ip)
80-
}
81-
82-
/// Create an SSH connection with a custom port
83-
#[must_use]
84-
pub fn with_host_and_port(self, host_ip: IpAddr, port: u16) -> SshConnection {
85-
SshConnection::with_ip_and_port(self, host_ip, port)
86-
}
8769
}

0 commit comments

Comments
 (0)