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] = &["database_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)] =
49 &[("DATABASE_URL", "database_url", true)];
50
51 fn build_config(ctx: &ProvisionContext<'_>) -> Result<RenderPostgresConfig, IntegrationError> {
52 let config = super::integration_config(ctx)?;
53 Ok(RenderPostgresConfig {
54 name: super::interp_required(ctx, &config, "name")?,
55 disk_size: super::int_optional(ctx, &config, "disk_size")?,
56 owner_id: super::interp_optional(ctx, &config, "owner_id")?,
57 region: super::interp_optional(ctx, &config, "region")?,
58 version: super::interp_optional(ctx, &config, "version")?,
59 })
60 }
61}
62
63pub fn validate_config(
64 name: &str,
65 config: &BTreeMap<String, toml::Value>,
66) -> Result<(), IntegrationError> {
67 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
68 location: format!("integrations.{name}.name"),
69 detail: err.to_string(),
70 })?;
71 let _ = (name, config);
72 Ok(())
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78 use crate::ProviderOps;
79 use crate::resource::ResourcePayload;
80 use stackless_core::def::StackDef;
81 use stackless_stripe_projects::stripe::StripeProjects;
82 use stackless_stripe_projects::test_support;
83
84 #[test]
85 fn config_matches_catalog() {
86 const FIXTURE: &str = include_str!(concat!(
87 env!("CARGO_MANIFEST_DIR"),
88 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
89 ));
90 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
91 let failures = stackless_stripe_projects::verify_service(
92 &catalog,
93 &RenderPostgresConfig {
94 name: "test-name".into(),
95 disk_size: None,
96 owner_id: None,
97 region: None,
98 version: None,
99 },
100 );
101 assert!(
102 failures.is_empty(),
103 "render/postgres catalog gaps:\n{}",
104 failures.join("\n")
105 );
106 }
107
108 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"}}]}}"##;
109
110 fn test_def() -> StackDef {
111 StackDef::parse(
112 r#"
113[stack]
114name = "atto"
115[stack.projects.stripe]
116project = "project_1"
117[integrations.res]
118provider = "render-postgres"
119name = "test-name"
120[services.api]
121source = { repo = "r", ref = "main" }
122env = { OUT = "${integrations.res.database_url}" }
123health = { path = "/health" }
124[services.api.local]
125run = "true"
126"#,
127 )
128 .unwrap()
129 }
130
131 #[tokio::test]
132 async fn provision_records_outputs() {
133 let runner = test_support::provision_script(
134 CATALOG_ENVELOPE,
135 serde_json::json!({"RENDER_DATABASE_URL": "val_database_url"}),
136 0,
137 );
138 let dir = tempfile::tempdir().unwrap();
139 std::fs::write(
140 dir.path().join("stackless.toml"),
141 "[stack]\nname=\"atto\"\n",
142 )
143 .unwrap();
144 let stripe = StripeProjects::new(&runner, dir.path());
145
146 let resource = RenderPostgres
147 .provision(
148 &stripe.as_dyn(),
149 &test_def(),
150 dir.path(),
151 "demo",
152 "res",
153 "local",
154 false,
155 )
156 .await
157 .unwrap();
158 assert_eq!(resource.resource_kind, "integration-render-postgres");
159 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
160 assert_eq!(payload.outputs["database_url"], "val_database_url");
161 }
162}