stackless_integrations/providers/render_db/
postgres.rs1use 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-render-postgres";
15
16#[derive(Debug, Serialize)]
17pub struct RenderPostgresConfig {
18 pub name: String,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub disk_size: Option<i64>,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 pub owner_id: Option<String>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub region: Option<String>,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 pub version: Option<String>,
27}
28
29impl CatalogService for RenderPostgresConfig {
30 const REFERENCE: &'static str = "render/postgres";
31}
32
33#[derive(Debug)]
34pub struct RenderPostgres;
35
36impl Hostable for RenderPostgres {
37 const PROVIDER: &'static str = "render-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] = &["url"];
42}
43
44impl FamilyResource for RenderPostgres {
45 type Config = RenderPostgresConfig;
46 const PROVIDER_PREFIX: &'static str = "RENDER";
47 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[("URL", "url", true)];
48
49 fn build_config(ctx: &ProvisionContext<'_>) -> Result<RenderPostgresConfig, IntegrationError> {
50 let config = super::integration_config(ctx)?;
51 Ok(RenderPostgresConfig {
52 name: super::interp_required(ctx, &config, "name")?,
53 disk_size: super::int_optional(ctx, &config, "disk_size")?,
54 owner_id: super::interp_optional(ctx, &config, "owner_id")?,
55 region: super::interp_optional(ctx, &config, "region")?,
56 version: super::interp_optional(ctx, &config, "version")?,
57 })
58 }
59}
60
61pub fn validate_config(
62 name: &str,
63 config: &BTreeMap<String, toml::Value>,
64) -> Result<(), IntegrationError> {
65 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
66 location: format!("integrations.{name}.name"),
67 detail: err.to_string(),
68 })?;
69 let _ = (name, config);
70 Ok(())
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use crate::ProviderOps;
77 use crate::resource::ResourcePayload;
78 use stackless_core::def::StackDef;
79 use stackless_stripe_projects::stripe::StripeProjects;
80 use stackless_stripe_projects::test_support;
81
82 #[test]
83 fn config_matches_catalog() {
84 const FIXTURE: &str = include_str!(concat!(
85 env!("CARGO_MANIFEST_DIR"),
86 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
87 ));
88 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
89 let failures = stackless_stripe_projects::verify_service(
90 &catalog,
91 &RenderPostgresConfig {
92 name: "test-name".into(),
93 disk_size: None,
94 owner_id: None,
95 region: None,
96 version: None,
97 },
98 );
99 assert!(
100 failures.is_empty(),
101 "render/postgres catalog gaps:\n{}",
102 failures.join("\n")
103 );
104 }
105
106 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_render_db","provider_name":"Render","service_id":"postgres","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid"},"configuration_schema":{"properties":{"disk_size":{"description":"Storage capacity for the database instance, in GB. Defaults to 15 GB. Must be in multiples of 5 GB.","type":"integer"},"name":{"description":"Name of the PostgreSQL database instance","type":"string"},"owner_id":{"description":"Workspace ID to deploy into. If omitted, uses the default workspace.","type":"string"},"region":{"description":"Region to deploy the database in (e.g. oregon, frankfurt, ohio, singapore, virginia). Defaults to 'oregon'.","type":"string"},"version":{"description":"PostgreSQL major version (e.g. 16, 17, 18). Defaults to the latest supported version.","type":"string"}},"required":["name"],"type":"object"}}]}}"##;
107
108 fn test_def() -> StackDef {
109 StackDef::parse(
110 r#"
111[stack]
112name = "atto"
113[stack.projects.stripe]
114project = "project_1"
115[integrations.res]
116provider = "render-postgres"
117name = "test-name"
118[services.api]
119source = { repo = "r", ref = "main" }
120env = { OUT = "${integrations.res.url}" }
121health = { path = "/health" }
122[services.api.local]
123run = "true"
124"#,
125 )
126 .unwrap()
127 }
128
129 #[tokio::test]
130 async fn provision_records_outputs() {
131 let runner = test_support::provision_script(
132 CATALOG_ENVELOPE,
133 serde_json::json!({"RENDER_URL": "val_url"}),
134 0,
135 );
136 let dir = tempfile::tempdir().unwrap();
137 std::fs::write(
138 dir.path().join("stackless.toml"),
139 "[stack]\nname=\"atto\"\n",
140 )
141 .unwrap();
142 let stripe = StripeProjects::new(&runner, dir.path());
143
144 let resource = RenderPostgres
145 .provision(
146 &stripe.as_dyn(),
147 &test_def(),
148 dir.path(),
149 "demo",
150 "res",
151 "local",
152 false,
153 )
154 .await
155 .unwrap();
156 assert_eq!(resource.resource_kind, "integration-render-postgres");
157 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
158 assert_eq!(payload.outputs["url"], "val_url");
159 }
160}