Skip to content

Commit a819b24

Browse files
committed
refactor: extract Ansible template wrappers to separate modules
1 parent 5840815 commit a819b24

File tree

6 files changed

+444
-445
lines changed

6 files changed

+444
-445
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use super::{AnsibleHost, AnsiblePort, InventoryContext, InventoryContextError, SshPrivateKeyFile};
2+
3+
/// Builder for `InventoryContext` with fluent interface
4+
#[derive(Debug, Default)]
5+
#[allow(clippy::struct_field_names)] // Field names mirror Ansible inventory variables
6+
pub struct InventoryContextBuilder {
7+
ansible_host: Option<AnsibleHost>,
8+
ansible_ssh_private_key_file: Option<SshPrivateKeyFile>,
9+
ansible_port: Option<AnsiblePort>,
10+
ansible_user: Option<String>,
11+
}
12+
13+
impl InventoryContextBuilder {
14+
/// Creates a new empty builder
15+
#[must_use]
16+
pub fn new() -> Self {
17+
Self::default()
18+
}
19+
20+
/// Sets the Ansible host for the builder.
21+
#[must_use]
22+
pub fn with_host(mut self, ansible_host: AnsibleHost) -> Self {
23+
self.ansible_host = Some(ansible_host);
24+
self
25+
}
26+
27+
/// Sets the SSH port for the builder.
28+
#[must_use]
29+
pub fn with_ssh_port(mut self, ansible_port: AnsiblePort) -> Self {
30+
self.ansible_port = Some(ansible_port);
31+
self
32+
}
33+
34+
/// Sets the SSH private key file path for the builder.
35+
#[must_use]
36+
pub fn with_ssh_priv_key_path(mut self, ssh_private_key_file: SshPrivateKeyFile) -> Self {
37+
self.ansible_ssh_private_key_file = Some(ssh_private_key_file);
38+
self
39+
}
40+
41+
/// Sets the Ansible user for the builder.
42+
#[must_use]
43+
pub fn with_ansible_user(mut self, ansible_user: String) -> Self {
44+
self.ansible_user = Some(ansible_user);
45+
self
46+
}
47+
48+
/// Builds the `InventoryContext`
49+
///
50+
/// # Errors
51+
///
52+
/// Returns an error if any required field is missing
53+
pub fn build(self) -> Result<InventoryContext, InventoryContextError> {
54+
let ansible_host = self
55+
.ansible_host
56+
.ok_or(InventoryContextError::MissingAnsibleHost)?;
57+
58+
let ansible_ssh_private_key_file = self
59+
.ansible_ssh_private_key_file
60+
.ok_or(InventoryContextError::MissingSshPrivateKeyFile)?;
61+
62+
let ansible_port = self
63+
.ansible_port
64+
.ok_or(InventoryContextError::MissingSshPort)?;
65+
66+
let ansible_user = self
67+
.ansible_user
68+
.ok_or(InventoryContextError::MissingAnsibleUser)?;
69+
70+
InventoryContext::new(
71+
ansible_host,
72+
ansible_ssh_private_key_file,
73+
ansible_port,
74+
ansible_user,
75+
)
76+
}
77+
}

src/infrastructure/external_tools/ansible/template/wrappers/inventory/context/mod.rs

Lines changed: 15 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub use ansible_host::{AnsibleHost, AnsibleHostError};
1212
pub use ansible_port::{AnsiblePort, AnsiblePortError};
1313
pub use ssh_private_key_file::{SshPrivateKeyFile, SshPrivateKeyFileError};
1414

15+
pub mod builder;
16+
pub use builder::InventoryContextBuilder;
17+
1518
/// Errors that can occur when creating an `InventoryContext`
1619
#[derive(Debug, Error)]
1720
pub enum InventoryContextError {
@@ -49,82 +52,6 @@ pub struct InventoryContext {
4952
ansible_user: String,
5053
}
5154

52-
/// Builder for `InventoryContext` with fluent interface
53-
#[derive(Debug, Default)]
54-
#[allow(clippy::struct_field_names)] // Field names mirror Ansible inventory variables
55-
pub struct InventoryContextBuilder {
56-
ansible_host: Option<AnsibleHost>,
57-
ansible_ssh_private_key_file: Option<SshPrivateKeyFile>,
58-
ansible_port: Option<AnsiblePort>,
59-
ansible_user: Option<String>,
60-
}
61-
62-
impl InventoryContextBuilder {
63-
/// Creates a new empty builder
64-
#[must_use]
65-
pub fn new() -> Self {
66-
Self::default()
67-
}
68-
69-
/// Sets the Ansible host for the builder.
70-
#[must_use]
71-
pub fn with_host(mut self, ansible_host: AnsibleHost) -> Self {
72-
self.ansible_host = Some(ansible_host);
73-
self
74-
}
75-
76-
/// Sets the SSH port for the builder.
77-
#[must_use]
78-
pub fn with_ssh_port(mut self, ansible_port: AnsiblePort) -> Self {
79-
self.ansible_port = Some(ansible_port);
80-
self
81-
}
82-
83-
/// Sets the SSH private key file path for the builder.
84-
#[must_use]
85-
pub fn with_ssh_priv_key_path(mut self, ssh_private_key_file: SshPrivateKeyFile) -> Self {
86-
self.ansible_ssh_private_key_file = Some(ssh_private_key_file);
87-
self
88-
}
89-
90-
/// Sets the Ansible user for the builder.
91-
#[must_use]
92-
pub fn with_ansible_user(mut self, ansible_user: String) -> Self {
93-
self.ansible_user = Some(ansible_user);
94-
self
95-
}
96-
97-
/// Builds the `InventoryContext`
98-
///
99-
/// # Errors
100-
///
101-
/// Returns an error if any required field is missing
102-
pub fn build(self) -> Result<InventoryContext, InventoryContextError> {
103-
let ansible_host = self
104-
.ansible_host
105-
.ok_or(InventoryContextError::MissingAnsibleHost)?;
106-
107-
let ansible_ssh_private_key_file = self
108-
.ansible_ssh_private_key_file
109-
.ok_or(InventoryContextError::MissingSshPrivateKeyFile)?;
110-
111-
let ansible_port = self
112-
.ansible_port
113-
.ok_or(InventoryContextError::MissingSshPort)?;
114-
115-
let ansible_user = self
116-
.ansible_user
117-
.ok_or(InventoryContextError::MissingAnsibleUser)?;
118-
119-
Ok(InventoryContext {
120-
ansible_host,
121-
ansible_ssh_private_key_file,
122-
ansible_port,
123-
ansible_user,
124-
})
125-
}
126-
}
127-
12855
impl InventoryContext {
12956
/// Creates a new `InventoryContext` using typed parameters
13057
///
@@ -334,4 +261,16 @@ mod tests {
334261
assert_eq!(inventory_context.ansible_port(), 22);
335262
assert_eq!(inventory_context.ansible_user(), "ubuntu");
336263
}
264+
265+
#[test]
266+
fn it_should_create_inventory_context_with_ipv6() {
267+
let host = AnsibleHost::from_str("2001:db8::1").unwrap();
268+
let ssh_key = SshPrivateKeyFile::new("/path/to/key").unwrap();
269+
let ssh_port = AnsiblePort::new(22).unwrap();
270+
let user = "ubuntu".to_string();
271+
272+
let inventory_context = InventoryContext::new(host, ssh_key, ssh_port, user).unwrap();
273+
274+
assert_eq!(inventory_context.ansible_host(), "2001:db8::1");
275+
}
337276
}

0 commit comments

Comments
 (0)