Skip to main content

stackless_integrations/providers/clickhouse/
cluster.rs

1//! `clickhouse/clickhouse` integration.
2
3use 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] = &[
48        "host",
49        "username",
50        "password",
51        "https_port",
52        "native_secure_port",
53        "tls",
54        "cloud_api_key",
55        "cloud_api_secret",
56    ];
57}
58
59impl FamilyResource for ClickHouseClickhouse {
60    type Config = ClickHouseClickhouseConfig;
61    const PROVIDER_PREFIX: &'static str = "CLICKHOUSE";
62    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
63        ("HOST", "host", true),
64        ("USERNAME", "username", true),
65        ("PASSWORD", "password", true),
66        ("HTTPS_PORT", "https_port", false),
67        ("NATIVE_SECURE_PORT", "native_secure_port", false),
68        ("TLS", "tls", false),
69        ("CLOUD_API_KEY", "cloud_api_key", true),
70        ("CLOUD_API_SECRET", "cloud_api_secret", true),
71    ];
72
73    fn build_config(
74        ctx: &ProvisionContext<'_>,
75    ) -> Result<ClickHouseClickhouseConfig, IntegrationError> {
76        let config = super::integration_config(ctx)?;
77        Ok(ClickHouseClickhouseConfig {
78            max_replica_memory_gb: super::int_required(ctx, &config, "maxReplicaMemoryGb")?,
79            min_replica_memory_gb: super::int_required(ctx, &config, "minReplicaMemoryGb")?,
80            name: super::interp_required(ctx, &config, "name")?,
81            region: super::interp_required(ctx, &config, "region")?,
82            idle_scaling: super::bool_optional(ctx, &config, "idleScaling")?,
83            idle_timeout_minutes: super::int_optional(ctx, &config, "idleTimeoutMinutes")?,
84            num_replicas: super::int_optional(ctx, &config, "numReplicas")?,
85        })
86    }
87}
88
89pub fn validate_config(
90    name: &str,
91    config: &BTreeMap<String, toml::Value>,
92) -> Result<(), IntegrationError> {
93    if config
94        .get("maxReplicaMemoryGb")
95        .and_then(toml::Value::as_integer)
96        .is_none()
97    {
98        return Err(IntegrationError::ConfigInvalid {
99            location: format!("integrations.{name}.maxReplicaMemoryGb"),
100            detail: "maxReplicaMemoryGb is required and must be an integer".into(),
101        });
102    }
103    if config
104        .get("minReplicaMemoryGb")
105        .and_then(toml::Value::as_integer)
106        .is_none()
107    {
108        return Err(IntegrationError::ConfigInvalid {
109            location: format!("integrations.{name}.minReplicaMemoryGb"),
110            detail: "minReplicaMemoryGb is required and must be an integer".into(),
111        });
112    }
113    registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
114        location: format!("integrations.{name}.name"),
115        detail: err.to_string(),
116    })?;
117    registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
118        location: format!("integrations.{name}.region"),
119        detail: err.to_string(),
120    })?;
121    let _ = (name, config);
122    Ok(())
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128    use crate::ProviderOps;
129    use crate::resource::ResourcePayload;
130    use stackless_core::def::StackDef;
131    use stackless_stripe_projects::stripe::StripeProjects;
132    use stackless_stripe_projects::test_support;
133
134    #[test]
135    fn config_matches_catalog() {
136        const FIXTURE: &str = include_str!(concat!(
137            env!("CARGO_MANIFEST_DIR"),
138            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
139        ));
140        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
141        let failures = stackless_stripe_projects::verify_service(
142            &catalog,
143            &ClickHouseClickhouseConfig {
144                max_replica_memory_gb: 12,
145                min_replica_memory_gb: 12,
146                name: "test-name".into(),
147                region: "aws-us-west-2".into(),
148                idle_scaling: None,
149                idle_timeout_minutes: None,
150                num_replicas: None,
151            },
152        );
153        assert!(
154            failures.is_empty(),
155            "clickhouse/clickhouse catalog gaps:\n{}",
156            failures.join("\n")
157        );
158    }
159
160    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"}}]}}"##;
161
162    fn test_def() -> StackDef {
163        StackDef::parse(
164            r#"
165[stack]
166name = "atto"
167[stack.projects.stripe]
168project = "project_1"
169[integrations.res]
170provider = "clickhouse"
171maxReplicaMemoryGb = 12
172minReplicaMemoryGb = 12
173name = "test-name"
174region = "aws-us-west-2"
175[services.api]
176source = { repo = "r", ref = "main" }
177env = { OUT = "${integrations.res.host}" }
178health = { path = "/health" }
179[services.api.local]
180run = "true"
181"#,
182        )
183        .unwrap()
184    }
185
186    #[tokio::test]
187    async fn provision_records_outputs() {
188        let runner = test_support::provision_script(
189            CATALOG_ENVELOPE,
190            serde_json::json!({"CLICKHOUSE_HOST": "val_host", "CLICKHOUSE_USERNAME": "val_username", "CLICKHOUSE_PASSWORD": "val_password", "CLICKHOUSE_HTTPS_PORT": "val_https_port", "CLICKHOUSE_NATIVE_SECURE_PORT": "val_native_secure_port", "CLICKHOUSE_TLS": "val_tls", "CLICKHOUSE_CLOUD_API_KEY": "val_cloud_api_key", "CLICKHOUSE_CLOUD_API_SECRET": "val_cloud_api_secret"}),
191            0,
192        );
193        let dir = tempfile::tempdir().unwrap();
194        std::fs::write(
195            dir.path().join("stackless.toml"),
196            "[stack]\nname=\"atto\"\n",
197        )
198        .unwrap();
199        let stripe = StripeProjects::new(&runner, dir.path());
200
201        let resource = ClickHouseClickhouse
202            .provision(
203                &stripe.as_dyn(),
204                &test_def(),
205                dir.path(),
206                "demo",
207                "res",
208                "local",
209                false,
210            )
211            .await
212            .unwrap();
213        assert_eq!(resource.resource_kind, "integration-clickhouse");
214        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
215        assert_eq!(payload.outputs["host"], "val_host");
216    }
217}