Skip to content

Commit

Permalink
fix: make cassandra configuration optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dracarys18 committed Dec 24, 2024
1 parent c1ebb78 commit e0c1fa7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
7 changes: 0 additions & 7 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ pool_size = 5
min_idle = 2
enable_ssl = false

[cassandra]
known_nodes = ["localhost:9042"]
pool_size = 16
keyspace = "encryption_db"
timeout = 60
cache_size = 120

[log]
log_level = "debug"
log_format = "console"
Expand Down
63 changes: 51 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ pub struct Config {
pub metrics_server: Server,
pub database: Database,
pub secrets: Secrets,
#[serde(default)]
pub cassandra: Cassandra,
pub log: LogConfig,
pub pool_config: PoolConfig,
#[cfg(feature = "mtls")]
pub certs: Certs,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Eq, PartialEq)]
pub struct Cassandra {
pub known_nodes: Vec<String>,
pub keyspace: String,
Expand Down Expand Up @@ -192,19 +193,53 @@ pub struct Server {

impl Secrets {
fn validate(&self) -> CustomResult<(), errors::ParsingError> {
if cfg!(feature = "aws") && (self.kms_config == AwsKmsConfig::default()) {
Err(error_stack::report!(errors::ParsingError::DecodingFailed(
"AWS config is not provided".to_string()
)))
} else if cfg!(feature = "vault") && (self.vault_config == VaultSettings::default()) {
Err(error_stack::report!(errors::ParsingError::DecodingFailed(
"Vault config is not provided".to_string()
)))
} else {
Ok(())
if cfg!(feature = "aws") {
error_stack::ensure!(
!self.kms_config.eq(&AwsKmsConfig::default()),
errors::ParsingError::DecodingFailed("AWS config is not provided".to_string())
)
} else if cfg!(feature = "vault") {
error_stack::ensure!(
!self.vault_config.eq(&VaultSettings::default()),
errors::ParsingError::DecodingFailed("Vault config is not provided".to_string())
)
}
Ok(())
}
}

/// # Panics
///
/// Panics if the provided pool_size is not a non zero number
#[allow(clippy::expect_used)]
impl Default for Cassandra {
fn default() -> Self {
Self {
known_nodes: Vec::new(),
keyspace: String::new(),
timeout: 0,
cache_size: 0,
pool_size: NonZeroUsize::new(1).expect("The provided number is non zero"),
}
}
}

impl Cassandra {
fn validate(&self) -> CustomResult<(), errors::ParsingError> {
if cfg!(feature = "cassandra") {
error_stack::ensure!(
!self.eq(&Self::default()),
errors::ParsingError::DecodingFailed(
"Failed to validate Cassandra configuration, missing configuration found"
.to_string()
)
)
}

Ok(())
}
}

impl Config {
pub fn config_path(environment: Environment, explicit_config_path: Option<PathBuf>) -> PathBuf {
let mut config_path = PathBuf::new();
Expand Down Expand Up @@ -247,7 +282,11 @@ impl Config {
pub fn validate(&self) {
self.secrets
.validate()
.expect("Failed to valdiate secrets some missing configuration found")
.expect("Failed to valdiate secrets some missing configuration found");

self.cassandra
.validate()
.expect("Failed to valdiate cassandra some missing configuration found");
}
}

Expand Down

0 comments on commit e0c1fa7

Please sign in to comment.