1use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-clickhouse";
15
16#[derive(Debug, Serialize)]
17pub struct ClickHouseClickhouseConfig {
18 #[serde(rename = "maxReplicaMemoryGb")]
19 pub max_replica_memory_gb: i64,
20 #[serde(rename = "minReplicaMemoryGb")]
21 pub min_replica_memory_gb: i64,
22 pub name: String,
23 pub region: String,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 #[serde(rename = "idleScaling")]
26 pub idle_scaling: Option<bool>,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 #[serde(rename = "idleTimeoutMinutes")]
29 pub idle_timeout_minutes: Option<i64>,
30 #[serde(skip_serializing_if = "Option::is_none")]
31 #[serde(rename = "numReplicas")]
32 pub num_replicas: Option<i64>,
33}
34
35impl CatalogService for ClickHouseClickhouseConfig {
36 const REFERENCE: &'static str = "clickhouse/clickhouse";
37}
38
39#[derive(Debug)]
40pub struct ClickHouseClickhouse;
41
42impl Hostable for ClickHouseClickhouse {
43 const PROVIDER: &'static str = "clickhouse";
44 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
45 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
46 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
47 const OUTPUTS: &'static [&'static str] = &["connection_string"];
48}
49
50impl FamilyResource for ClickHouseClickhouse {
51 type Config = ClickHouseClickhouseConfig;
52 const PROVIDER_PREFIX: &'static str = "CLICKHOUSE";
53 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
55 &[("CONNECTION_STRING", "connection_string", true)];
56
57 fn build_config(
58 ctx: &ProvisionContext<'_>,
59 ) -> Result<ClickHouseClickhouseConfig, IntegrationError> {
60 let config = super::integration_config(ctx)?;
61 Ok(ClickHouseClickhouseConfig {
62 max_replica_memory_gb: super::int_required(ctx, &config, "maxReplicaMemoryGb")?,
63 min_replica_memory_gb: super::int_required(ctx, &config, "minReplicaMemoryGb")?,
64 name: super::interp_required(ctx, &config, "name")?,
65 region: super::interp_required(ctx, &config, "region")?,
66 idle_scaling: super::bool_optional(ctx, &config, "idleScaling")?,
67 idle_timeout_minutes: super::int_optional(ctx, &config, "idleTimeoutMinutes")?,
68 num_replicas: super::int_optional(ctx, &config, "numReplicas")?,
69 })
70 }
71}
72
73pub fn validate_config(
74 name: &str,
75 config: &BTreeMap<String, toml::Value>,
76) -> Result<(), IntegrationError> {
77 if config
78 .get("maxReplicaMemoryGb")
79 .and_then(toml::Value::as_integer)
80 .is_none()
81 {
82 return Err(IntegrationError::ConfigInvalid {
83 location: format!("integrations.{name}.maxReplicaMemoryGb"),
84 detail: "maxReplicaMemoryGb is required and must be an integer".into(),
85 });
86 }
87 if config
88 .get("minReplicaMemoryGb")
89 .and_then(toml::Value::as_integer)
90 .is_none()
91 {
92 return Err(IntegrationError::ConfigInvalid {
93 location: format!("integrations.{name}.minReplicaMemoryGb"),
94 detail: "minReplicaMemoryGb is required and must be an integer".into(),
95 });
96 }
97 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
98 location: format!("integrations.{name}.name"),
99 detail: err.to_string(),
100 })?;
101 registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
102 location: format!("integrations.{name}.region"),
103 detail: err.to_string(),
104 })?;
105 let _ = (name, config);
106 Ok(())
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use crate::ProviderOps;
113 use crate::resource::ResourcePayload;
114 use stackless_core::def::StackDef;
115 use stackless_stripe_projects::stripe::StripeProjects;
116 use stackless_stripe_projects::test_support;
117
118 #[test]
119 fn config_matches_catalog() {
120 const FIXTURE: &str = include_str!(concat!(
121 env!("CARGO_MANIFEST_DIR"),
122 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
123 ));
124 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
125 let failures = stackless_stripe_projects::verify_service(
126 &catalog,
127 &ClickHouseClickhouseConfig {
128 max_replica_memory_gb: 12,
129 min_replica_memory_gb: 12,
130 name: "test-name".into(),
131 region: "aws-us-west-2".into(),
132 idle_scaling: None,
133 idle_timeout_minutes: None,
134 num_replicas: None,
135 },
136 );
137 assert!(
138 failures.is_empty(),
139 "clickhouse/clickhouse catalog gaps:\n{}",
140 failures.join("\n")
141 );
142 }
143
144 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_clickhouse","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_clickhouse","provider_name":"ClickHouse","service_id":"clickhouse","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"additionalProperties":false,"properties":{"idleScaling":{"description":"When true, compute scales to zero after idleTimeoutMinutes of inactivity and bills $0/hour during idle periods. Defaults to false when omitted.","type":"boolean"},"idleTimeoutMinutes":{"description":"Minutes of inactivity before idle scale-down triggers. Only meaningful when idleScaling=true. Defaults to 60 when omitted.","maximum":1440,"minimum":5,"type":"integer"},"maxReplicaMemoryGb":{"default":12,"description":"Autoscale upper bound: memory per replica, in GiB. Must be \u2265 minReplicaMemoryGb (enforced server-side). Accepts 8\u2013356 GiB per replica in steps of 4; the maximum drops to 236 GiB on AWS regions due to UI caps.","maximum":356,"minimum":8,"multipleOf":4,"type":"integer"},"minReplicaMemoryGb":{"default":12,"description":"Autoscale lower bound: memory per replica, in GiB. Must be \u2264 maxReplicaMemoryGb (enforced server-side). Accepts 8\u2013356 GiB per replica in steps of 4; the maximum drops to 236 GiB on AWS regions due to UI caps.","maximum":356,"minimum":8,"multipleOf":4,"type":"integer"},"name":{"description":"Human-readable name for the ClickHouse service.","maxLength":64,"minLength":1,"type":"string"},"numReplicas":{"default":2,"description":"Number of replicas (HA). Defaults to 2 when omitted. Each replica meters compute independently, so total compute cost scales linearly with numReplicas.","maximum":100,"minimum":1,"type":"integer"},"region":{"description":"Cloud provider and region to host the service in, as a single cloud-qualified id (e.g. aws-us-east-1, gcp-us-east1, azure-eastus). The cloud provider is encoded in the value, so no separate field is needed. Pricing varies per region.","enum":["aws-us-west-2","aws-us-east-2","aws-us-east-1","aws-eu-west-1","aws-eu-west-2","aws-eu-central-1","aws-ap-southeast-1","aws-ap-southeast-2","aws-ap-northeast-1","aws-ap-south-1","aws-ap-northeast-2","aws-il-central-1","gcp-us-east1","gcp-us-central1","gcp-europe-west2","gcp-europe-west4","gcp-asia-southeast1","gcp-asia-northeast1","azure-germanywestcentral","azure-eastus2","azure-westus3"],"type":"string"}},"required":["name","region","minReplicaMemoryGb","maxReplicaMemoryGb"],"type":"object"}}]}}"##;
145
146 fn test_def() -> StackDef {
147 StackDef::parse(
148 r#"
149[stack]
150name = "atto"
151[stack.projects.stripe]
152project = "project_1"
153[integrations.res]
154provider = "clickhouse"
155maxReplicaMemoryGb = 12
156minReplicaMemoryGb = 12
157name = "test-name"
158region = "aws-us-west-2"
159[services.api]
160source = { repo = "r", ref = "main" }
161env = { OUT = "${integrations.res.connection_string}" }
162health = { path = "/health" }
163[services.api.local]
164run = "true"
165"#,
166 )
167 .unwrap()
168 }
169
170 #[tokio::test]
171 async fn provision_records_outputs() {
172 let runner = test_support::provision_script(
173 CATALOG_ENVELOPE,
174 serde_json::json!({"CLICKHOUSE_CONNECTION_STRING": "val_connection_string"}),
175 0,
176 );
177 let dir = tempfile::tempdir().unwrap();
178 std::fs::write(
179 dir.path().join("stackless.toml"),
180 "[stack]\nname=\"atto\"\n",
181 )
182 .unwrap();
183 let stripe = StripeProjects::new(&runner, dir.path());
184
185 let resource = ClickHouseClickhouse
186 .provision(
187 &stripe.as_dyn(),
188 &test_def(),
189 dir.path(),
190 "demo",
191 "res",
192 "local",
193 false,
194 )
195 .await
196 .unwrap();
197 assert_eq!(resource.resource_kind, "integration-clickhouse");
198 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
199 assert_eq!(
200 payload.outputs["connection_string"],
201 "val_connection_string"
202 );
203 }
204}