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-postgres";
15
16#[derive(Debug, Serialize)]
17pub struct ClickHousePostgresConfig {
18 pub name: String,
19 pub region: String,
20 pub size: String,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 #[serde(rename = "haType")]
23 pub ha_type: Option<String>,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 #[serde(rename = "postgresVersion")]
26 pub postgres_version: Option<String>,
27}
28
29impl CatalogService for ClickHousePostgresConfig {
30 const REFERENCE: &'static str = "clickhouse/postgres";
31}
32
33#[derive(Debug)]
34pub struct ClickHousePostgres;
35
36impl Hostable for ClickHousePostgres {
37 const PROVIDER: &'static str = "clickhouse-postgres";
38 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
39 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
40 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
41 const OUTPUTS: &'static [&'static str] = &["connection_string"];
42}
43
44impl FamilyResource for ClickHousePostgres {
45 type Config = ClickHousePostgresConfig;
46 const PROVIDER_PREFIX: &'static str = "CLICKHOUSE";
47 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
49 &[("CONNECTION_STRING", "connection_string", true)];
50
51 fn build_config(
52 ctx: &ProvisionContext<'_>,
53 ) -> Result<ClickHousePostgresConfig, IntegrationError> {
54 let config = super::integration_config(ctx)?;
55 Ok(ClickHousePostgresConfig {
56 name: super::interp_required(ctx, &config, "name")?,
57 region: super::interp_required(ctx, &config, "region")?,
58 size: super::interp_required(ctx, &config, "size")?,
59 ha_type: super::interp_optional(ctx, &config, "haType")?,
60 postgres_version: super::interp_optional(ctx, &config, "postgresVersion")?,
61 })
62 }
63}
64
65pub fn validate_config(
66 name: &str,
67 config: &BTreeMap<String, toml::Value>,
68) -> Result<(), IntegrationError> {
69 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
70 location: format!("integrations.{name}.name"),
71 detail: err.to_string(),
72 })?;
73 registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
74 location: format!("integrations.{name}.region"),
75 detail: err.to_string(),
76 })?;
77 registry::config_string(config, "size").map_err(|err| IntegrationError::ConfigInvalid {
78 location: format!("integrations.{name}.size"),
79 detail: err.to_string(),
80 })?;
81 let _ = (name, config);
82 Ok(())
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88 use crate::ProviderOps;
89 use crate::resource::ResourcePayload;
90 use stackless_core::def::StackDef;
91 use stackless_stripe_projects::stripe::StripeProjects;
92 use stackless_stripe_projects::test_support;
93
94 #[test]
95 fn config_matches_catalog() {
96 const FIXTURE: &str = include_str!(concat!(
97 env!("CARGO_MANIFEST_DIR"),
98 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
99 ));
100 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
101 let failures = stackless_stripe_projects::verify_service(
102 &catalog,
103 &ClickHousePostgresConfig {
104 name: "test-name".into(),
105 region: "aws-ap-northeast-1".into(),
106 size: "c6gd.large".into(),
107 ha_type: None,
108 postgres_version: None,
109 },
110 );
111 assert!(
112 failures.is_empty(),
113 "clickhouse/postgres catalog gaps:\n{}",
114 failures.join("\n")
115 );
116 }
117
118 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_postgres","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_clickhouse","provider_name":"ClickHouse","service_id":"postgres","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"free"},"configuration_schema":{"additionalProperties":false,"properties":{"haType":{"description":"High-availability mode. Defaults to \"none\" when omitted.","enum":["none","async","sync"],"type":"string"},"name":{"description":"Human-readable name for the Postgres instance.","maxLength":50,"minLength":1,"type":"string"},"postgresVersion":{"description":"Major Postgres version. Defaults to the latest available when omitted.","enum":["18","17"],"type":"string"},"region":{"description":"Cloud provider and region to host the service in, as a single cloud-qualified id (e.g. aws-us-east-1). The cloud provider is encoded in the value, so no separate field is needed. Pricing varies per region.","enum":["aws-ap-northeast-1","aws-ap-northeast-2","aws-ap-south-1","aws-ap-southeast-1","aws-ap-southeast-2","aws-eu-central-1","aws-eu-west-1","aws-eu-west-2","aws-us-east-1","aws-us-east-2","aws-us-west-2"],"type":"string"},"size":{"description":"VM instance type determining CPU, memory, and storage.","enum":["c6gd.large","c6gd.xlarge","c6gd.2xlarge","c6gd.4xlarge","c6gd.8xlarge","c6gd.16xlarge","i7i.large","i7i.xlarge","i7i.2xlarge","i7i.4xlarge","i7i.8xlarge","i7i.12xlarge","i7i.16xlarge","i7i.24xlarge","i7ie.large","i7ie.xlarge","i7ie.2xlarge","i7ie.3xlarge","i7ie.6xlarge","i7ie.12xlarge","i7ie.18xlarge","i7ie.24xlarge","i8g.large","i8g.xlarge","i8g.2xlarge","i8g.4xlarge","i8g.8xlarge","i8g.16xlarge","i8g.24xlarge","i8ge.large","i8ge.xlarge","i8ge.2xlarge","i8ge.3xlarge","i8ge.6xlarge","i8ge.12xlarge","i8ge.18xlarge","i8ge.24xlarge","m6gd.large","m6gd.xlarge","m6gd.2xlarge","m6gd.4xlarge","m6gd.8xlarge","m6gd.16xlarge","m6id.large","m6id.xlarge","m6id.2xlarge","m6id.4xlarge","m6id.8xlarge","m6id.16xlarge","m8gd.large","m8gd.xlarge","m8gd.2xlarge","m8gd.4xlarge","m8gd.8xlarge","m8gd.16xlarge","r6gd.medium","r6gd.large","r6gd.xlarge","r6gd.2xlarge","r6gd.4xlarge","r6gd.8xlarge","r6gd.12xlarge","r6gd.16xlarge","r6id.large","r6id.xlarge","r6id.2xlarge","r6id.4xlarge","r6id.8xlarge","r6id.12xlarge","r6id.16xlarge","r6id.24xlarge","r6id.32xlarge","r8gd.medium","r8gd.large","r8gd.xlarge","r8gd.2xlarge","r8gd.4xlarge","r8gd.8xlarge","r8gd.12xlarge","r8gd.16xlarge","r8gd.24xlarge","r8gd.48xlarge"],"type":"string"}},"required":["name","region","size"],"type":"object"}}]}}"##;
119
120 fn test_def() -> StackDef {
121 StackDef::parse(
122 r#"
123[stack]
124name = "atto"
125[stack.projects.stripe]
126project = "project_1"
127[integrations.res]
128provider = "clickhouse-postgres"
129name = "test-name"
130region = "aws-ap-northeast-1"
131size = "c6gd.large"
132[services.api]
133source = { repo = "r", ref = "main" }
134env = { OUT = "${integrations.res.connection_string}" }
135health = { path = "/health" }
136[services.api.local]
137run = "true"
138"#,
139 )
140 .unwrap()
141 }
142
143 #[tokio::test]
144 async fn provision_records_outputs() {
145 let runner = test_support::provision_script(
146 CATALOG_ENVELOPE,
147 serde_json::json!({"CLICKHOUSE_CONNECTION_STRING": "val_connection_string"}),
148 0,
149 );
150 let dir = tempfile::tempdir().unwrap();
151 std::fs::write(
152 dir.path().join("stackless.toml"),
153 "[stack]\nname=\"atto\"\n",
154 )
155 .unwrap();
156 let stripe = StripeProjects::new(&runner, dir.path());
157
158 let resource = ClickHousePostgres
159 .provision(
160 &stripe.as_dyn(),
161 &test_def(),
162 dir.path(),
163 "demo",
164 "res",
165 "local",
166 false,
167 )
168 .await
169 .unwrap();
170 assert_eq!(resource.resource_kind, "integration-clickhouse-postgres");
171 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
172 assert_eq!(
173 payload.outputs["connection_string"],
174 "val_connection_string"
175 );
176 }
177}