From 252d0852aee0f8f122d441c87bee93fbde0013d9 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Mon, 27 May 2024 20:57:15 +0200 Subject: [PATCH] refactor: move crdb to own file Signed-off-by: kjuulh --- crates/cuddle-clusters/src/catalog.rs | 137 +----------------- .../src/catalog/crdb_database.rs | 131 +++++++++++++++++ 2 files changed, 132 insertions(+), 136 deletions(-) create mode 100644 crates/cuddle-clusters/src/catalog/crdb_database.rs diff --git a/crates/cuddle-clusters/src/catalog.rs b/crates/cuddle-clusters/src/catalog.rs index fb78f7b..32b2663 100644 --- a/crates/cuddle-clusters/src/catalog.rs +++ b/crates/cuddle-clusters/src/catalog.rs @@ -1,139 +1,4 @@ pub mod cluster_vars; +pub mod crdb_database; pub mod cuddle_vars; pub mod vault_secret; -pub mod crdb_database { - use std::path::Path; - - use minijinja::{value::Object, Value}; - - use crate::{catalog::cuddle_vars::CuddleVariable, Component}; - - use super::cuddle_vars::{load_cuddle_file, CuddleVariables}; - - pub struct CockroachDB { - variables: CuddleVariables, - } - - impl CockroachDB { - pub async fn new(path: &Path) -> anyhow::Result { - let variables = load_cuddle_file(path).await?; - - Ok(Self { variables }) - } - } - - impl Component for CockroachDB { - fn name(&self) -> String { - "cuddle/crdb".into() - } - - fn render_value( - &self, - _environment: &str, - _value: &serde_yaml::Value, - ) -> Option> { - if let Some(true) = self - .variables - .0 - .get("database") - .and_then(|v| match v { - CuddleVariable::Object(o) => Some(o), - _ => None, - }) - .and_then(|o| o.0.get("crdb")) - .and_then(|o| match o { - CuddleVariable::String(o) => { - if o == "true" { - Some(true) - } else { - None - } - } - _ => None, - }) - { - return Some(Ok(minijinja::Value::from_object(CockroachDBValues { - name: self.name(), - enabled: true, - }))); - } - - Some(Ok(minijinja::Value::from_object(CockroachDBValues { - name: self.name(), - enabled: false, - }))) - } - - fn render( - &self, - _environment: &str, - _value: &serde_yaml::Value, - ) -> Option> { - if let Some(true) = self - .variables - .0 - .get("database") - .and_then(|v| match v { - CuddleVariable::Object(o) => Some(o), - _ => None, - }) - .and_then(|o| o.0.get("crdb")) - .and_then(|o| match o { - CuddleVariable::String(o) => { - if o == "true" { - Some(true) - } else { - None - } - } - _ => None, - }) - { - return Some(Ok(( - format!("{}.yaml", self.name().replace("/", "-")), - r#" -apiVersion: v1 -kind: ConfigMap -metadata: - name: {{ vars.cuddle_crdb.file_name(vars.cuddle_vars.service) }} - namespace: {{ vars.cluster_vars.namespace }} -data: - DATABASE_URL: postgresql://root@{{environment}}-cluster:26257/{{ vars.cuddle_vars.service | replace("-", "_") }} - "# - .into(), - ))); - } - - None - } - } - - #[derive(Debug)] - struct CockroachDBValues { - name: String, - enabled: bool, - } - - impl Object for CockroachDBValues { - fn get_value( - self: &std::sync::Arc, - key: &minijinja::Value, - ) -> Option { - let name = self.name.clone(); - match key.as_str()? { - "has_values" => { - if self.enabled { - Some(minijinja::Value::from_serialize(true)) - } else { - Some(minijinja::Value::from_serialize(false)) - } - } - "file_name" => Some(Value::from_function(move |file_name: String| { - format!("{}-{}", file_name, name.replace("/", "-")) - })), - "env" => Some(Value::from_serialize("DATABASE_URL")), - _ => None, - } - } - } -} diff --git a/crates/cuddle-clusters/src/catalog/crdb_database.rs b/crates/cuddle-clusters/src/catalog/crdb_database.rs new file mode 100644 index 0000000..03a0fe2 --- /dev/null +++ b/crates/cuddle-clusters/src/catalog/crdb_database.rs @@ -0,0 +1,131 @@ +use std::path::Path; + +use minijinja::{value::Object, Value}; + +use crate::{catalog::cuddle_vars::CuddleVariable, Component}; + +use super::cuddle_vars::{load_cuddle_file, CuddleVariables}; + +pub struct CockroachDB { + variables: CuddleVariables, +} + +impl CockroachDB { + pub async fn new(path: &Path) -> anyhow::Result { + let variables = load_cuddle_file(path).await?; + + Ok(Self { variables }) + } +} + +impl Component for CockroachDB { + fn name(&self) -> String { + "cuddle/crdb".into() + } + + fn render_value( + &self, + _environment: &str, + _value: &serde_yaml::Value, + ) -> Option> { + if let Some(true) = self + .variables + .0 + .get("database") + .and_then(|v| match v { + CuddleVariable::Object(o) => Some(o), + _ => None, + }) + .and_then(|o| o.0.get("crdb")) + .and_then(|o| match o { + CuddleVariable::String(o) => { + if o == "true" { + Some(true) + } else { + None + } + } + _ => None, + }) + { + return Some(Ok(minijinja::Value::from_object(CockroachDBValues { + name: self.name(), + enabled: true, + }))); + } + + Some(Ok(minijinja::Value::from_object(CockroachDBValues { + name: self.name(), + enabled: false, + }))) + } + + fn render( + &self, + _environment: &str, + _value: &serde_yaml::Value, + ) -> Option> { + if let Some(true) = self + .variables + .0 + .get("database") + .and_then(|v| match v { + CuddleVariable::Object(o) => Some(o), + _ => None, + }) + .and_then(|o| o.0.get("crdb")) + .and_then(|o| match o { + CuddleVariable::String(o) => { + if o == "true" { + Some(true) + } else { + None + } + } + _ => None, + }) + { + return Some(Ok(( + format!("{}.yaml", self.name().replace("/", "-")), + r#" +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ vars.cuddle_crdb.file_name(vars.cuddle_vars.service) }} + namespace: {{ vars.cluster_vars.namespace }} +data: + DATABASE_URL: postgresql://root@{{environment}}-cluster:26257/{{ vars.cuddle_vars.service | replace("-", "_") }} + "# + .into(), + ))); + } + + None + } +} + +#[derive(Debug)] +struct CockroachDBValues { + name: String, + enabled: bool, +} + +impl Object for CockroachDBValues { + fn get_value(self: &std::sync::Arc, key: &minijinja::Value) -> Option { + let name = self.name.clone(); + match key.as_str()? { + "has_values" => { + if self.enabled { + Some(minijinja::Value::from_serialize(true)) + } else { + Some(minijinja::Value::from_serialize(false)) + } + } + "file_name" => Some(Value::from_function(move |file_name: String| { + format!("{}-{}", file_name, name.replace("/", "-")) + })), + "env" => Some(Value::from_serialize("DATABASE_URL")), + _ => None, + } + } +}