-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser_inputs.rs
More file actions
159 lines (148 loc) · 5.86 KB
/
Copy pathuser_inputs.rs
File metadata and controls
159 lines (148 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! User Inputs Module
//!
//! This module contains the `UserInputs` struct which holds all user-provided
//! configuration when creating an environment.
//!
//! ## Purpose
//!
//! User inputs represent the immutable configuration choices made by the user
//! when creating an environment. These fields never change throughout the
//! environment's lifecycle.
//!
//! ## Semantic Category
//!
//! **User Inputs** are:
//! - Provided by the user when creating an environment
//! - Immutable throughout environment lifecycle
//! - Examples: name, SSH credentials, port numbers
//!
//! Add new fields here when: User needs to configure something at environment creation time.
use crate::adapters::ssh::SshCredentials;
use crate::domain::environment::EnvironmentName;
use crate::domain::{InstanceName, ProfileName};
use serde::{Deserialize, Serialize};
/// User-provided configuration when creating an environment
///
/// This struct contains all fields that are provided by the user when creating
/// an environment. These fields are immutable throughout the environment lifecycle
/// and represent the user's configuration choices.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{InstanceName, ProfileName, EnvironmentName};
/// use torrust_tracker_deployer_lib::domain::environment::user_inputs::UserInputs;
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let user_inputs = UserInputs {
/// name: EnvironmentName::new("production".to_string())?,
/// instance_name: InstanceName::new("torrust-tracker-vm-production".to_string())?,
/// profile_name: ProfileName::new("torrust-profile-production".to_string())?,
/// ssh_credentials: SshCredentials::new(
/// PathBuf::from("keys/prod_rsa"),
/// PathBuf::from("keys/prod_rsa.pub"),
/// Username::new("torrust".to_string())?,
/// ),
/// ssh_port: 22,
/// };
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInputs {
/// The validated environment name
pub name: crate::domain::environment::EnvironmentName,
/// The instance name for this environment (auto-generated from name)
pub instance_name: InstanceName,
/// The profile name for this environment (auto-generated from name)
pub profile_name: ProfileName,
/// SSH credentials for connecting to instances in this environment
pub ssh_credentials: SshCredentials,
/// SSH port for connecting to instances in this environment
pub ssh_port: u16,
}
impl UserInputs {
/// Creates a new `UserInputs` with auto-generated instance and profile names
///
/// # Arguments
///
/// * `name` - The validated environment name
/// * `ssh_credentials` - SSH credentials for connecting to instances
/// * `ssh_port` - SSH port for connecting to instances
///
/// # Returns
///
/// A new `UserInputs` with:
/// - Auto-generated instance name: `torrust-tracker-vm-{env_name}`
/// - Auto-generated profile name: `torrust-profile-{env_name}`
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::environment::{EnvironmentName, UserInputs};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("production".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/prod_rsa"),
/// PathBuf::from("keys/prod_rsa.pub"),
/// ssh_username,
/// );
///
/// let user_inputs = UserInputs::new(&env_name, ssh_credentials, 22);
///
/// assert_eq!(user_inputs.instance_name.as_str(), "torrust-tracker-vm-production");
/// assert_eq!(user_inputs.profile_name.as_str(), "torrust-profile-production");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Panics
///
/// This function does not panic. All name generation is guaranteed to succeed
/// for valid environment names.
#[must_use]
pub fn new(name: &EnvironmentName, ssh_credentials: SshCredentials, ssh_port: u16) -> Self {
let instance_name = Self::generate_instance_name(name);
let profile_name = Self::generate_profile_name(name);
Self {
name: name.clone(),
instance_name,
profile_name,
ssh_credentials,
ssh_port,
}
}
// ========================================================================
// Private Helper Methods
// ========================================================================
/// Generates an instance name from the environment name
///
/// Format: `torrust-tracker-vm-{env_name}`
///
/// # Panics
///
/// This function does not panic. The generated instance name is guaranteed
/// to be valid for any valid environment name.
fn generate_instance_name(env_name: &EnvironmentName) -> InstanceName {
let instance_name_str = format!("torrust-tracker-vm-{}", env_name.as_str());
InstanceName::new(instance_name_str)
.expect("Generated instance name should always be valid")
}
/// Generates a profile name from the environment name
///
/// Format: `torrust-profile-{env_name}`
///
/// # Panics
///
/// This function does not panic. The generated profile name is guaranteed
/// to be valid for any valid environment name.
fn generate_profile_name(env_name: &EnvironmentName) -> ProfileName {
let profile_name_str = format!("torrust-profile-{}", env_name.as_str());
ProfileName::new(profile_name_str).expect("Generated profile name should always be valid")
}
}