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] = &[
42 "connection_string",
43 "hostname",
44 "username",
45 "password",
46 "tls",
47 "cloud_api_key",
48 "cloud_api_secret",
49 ];
50}
51
52impl FamilyResource for ClickHousePostgres {
53 type Config = ClickHousePostgresConfig;
54 const PROVIDER_PREFIX: &'static str = "CLICKHOUSE";
55 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
56 ("CONNECTION_STRING", "connection_string", true),
57 ("HOSTNAME", "hostname", false),
58 ("USERNAME", "username", true),
59 ("PASSWORD", "password", true),
60 ("TLS", "tls", false),
61 ("CLOUD_API_KEY", "cloud_api_key", true),
62 ("CLOUD_API_SECRET", "cloud_api_secret", true),
63 ];
64
65 fn build_config(
66 ctx: &ProvisionContext<'_>,
67 ) -> Result<ClickHousePostgresConfig, IntegrationError> {
68 let config = super::integration_config(ctx)?;
69 Ok(ClickHousePostgresConfig {
70 name: super::interp_required(ctx, &config, "name")?,
71 region: super::interp_required(ctx, &config, "region")?,
72 size: super::interp_required(ctx, &config, "size")?,
73 ha_type: super::interp_optional(ctx, &config, "haType")?,
74 postgres_version: super::interp_optional(ctx, &config, "postgresVersion")?,
75 })
76 }
77}
78
79pub fn validate_config(
80 name: &str,
81 config: &BTreeMap<String, toml::Value>,
82) -> Result<(), IntegrationError> {
83 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
84 location: format!("integrations.{name}.name"),
85 detail: err.to_string(),
86 })?;
87 registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
88 location: format!("integrations.{name}.region"),
89 detail: err.to_string(),
90 })?;
91 registry::config_string(config, "size").map_err(|err| IntegrationError::ConfigInvalid {
92 location: format!("integrations.{name}.size"),
93 detail: err.to_string(),
94 })?;
95 let _ = (name, config);
96 Ok(())
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102 use crate::ProviderOps;
103 use crate::resource::ResourcePayload;
104 use stackless_core::def::StackDef;
105 use stackless_stripe_projects::stripe::StripeProjects;
106 use stackless_stripe_projects::test_support;
107
108 #[test]
109 fn config_matches_catalog() {
110 const FIXTURE: &str = include_str!(concat!(
111 env!("CARGO_MANIFEST_DIR"),
112 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
113 ));
114 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
115 let failures = stackless_stripe_projects::verify_service(
116 &catalog,
117 &ClickHousePostgresConfig {
118 name: "test-name".into(),
119 region: "aws-ap-northeast-1".into(),
120 size: "c6gd.large".into(),
121 ha_type: None,
122 postgres_version: None,
123 },
124 );
125 assert!(
126 failures.is_empty(),
127 "clickhouse/postgres catalog gaps:\n{}",
128 failures.join("\n")
129 );
130 }
131
132 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"}}]}}"##;
133
134 fn test_def() -> StackDef {
135 StackDef::parse(
136 r#"
137[stack]
138name = "atto"
139[stack.projects.stripe]
140project = "project_1"
141[integrations.res]
142provider = "clickhouse-postgres"
143name = "test-name"
144region = "aws-ap-northeast-1"
145size = "c6gd.large"
146[services.api]
147source = { repo = "r", ref = "main" }
148env = { OUT = "${integrations.res.connection_string}" }
149health = { path = "/health" }
150[services.api.local]
151run = "true"
152"#,
153 )
154 .unwrap()
155 }
156
157 #[tokio::test]
158 async fn provision_records_outputs() {
159 let runner = test_support::provision_script(
160 CATALOG_ENVELOPE,
161 serde_json::json!({"CLICKHOUSE_CONNECTION_STRING": "val_connection_string", "CLICKHOUSE_HOSTNAME": "val_hostname", "CLICKHOUSE_USERNAME": "val_username", "CLICKHOUSE_PASSWORD": "val_password", "CLICKHOUSE_TLS": "val_tls", "CLICKHOUSE_CLOUD_API_KEY": "val_cloud_api_key", "CLICKHOUSE_CLOUD_API_SECRET": "val_cloud_api_secret"}),
162 0,
163 );
164 let dir = tempfile::tempdir().unwrap();
165 std::fs::write(
166 dir.path().join("stackless.toml"),
167 "[stack]\nname=\"atto\"\n",
168 )
169 .unwrap();
170 let stripe = StripeProjects::new(&runner, dir.path());
171
172 let resource = ClickHousePostgres
173 .provision(
174 &stripe.as_dyn(),
175 &test_def(),
176 dir.path(),
177 "demo",
178 "res",
179 "local",
180 false,
181 )
182 .await
183 .unwrap();
184 assert_eq!(resource.resource_kind, "integration-clickhouse-postgres");
185 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
186 assert_eq!(
187 payload.outputs["connection_string"],
188 "val_connection_string"
189 );
190 }
191}