Skip to main content

stackless_integrations/providers/neon/
postgres.rs

1//! `neon/postgres` 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};
12
13pub const RESOURCE_KIND: &str = "integration-neon";
14
15#[derive(Debug, Serialize)]
16pub struct NeonPostgresConfig {}
17
18impl CatalogService for NeonPostgresConfig {
19    const REFERENCE: &'static str = "neon/postgres";
20}
21
22#[derive(Debug)]
23pub struct NeonPostgres;
24
25impl Hostable for NeonPostgres {
26    const PROVIDER: &'static str = "neon";
27    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
28    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
29    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
30    const OUTPUTS: &'static [&'static str] = &[
31        "connection_string",
32        "project_id",
33        "branch_id",
34        "branch_name",
35        "database_name",
36        "org_id",
37    ];
38}
39
40impl FamilyResource for NeonPostgres {
41    type Config = NeonPostgresConfig;
42    const PROVIDER_PREFIX: &'static str = "NEON";
43    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
44        ("CONNECTION_STRING", "connection_string", true),
45        ("PROJECT_ID", "project_id", true),
46        ("BRANCH_ID", "branch_id", false),
47        ("BRANCH_NAME", "branch_name", false),
48        ("DATABASE_NAME", "database_name", false),
49        ("ORG_ID", "org_id", false),
50    ];
51
52    fn build_config(ctx: &ProvisionContext<'_>) -> Result<NeonPostgresConfig, IntegrationError> {
53        let _ = super::integration_config(ctx)?;
54        Ok(NeonPostgresConfig {})
55    }
56}
57
58pub fn validate_config(
59    name: &str,
60    config: &BTreeMap<String, toml::Value>,
61) -> Result<(), IntegrationError> {
62    let _ = (name, config);
63    Ok(())
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::ProviderOps;
70    use crate::resource::ResourcePayload;
71    use stackless_core::def::StackDef;
72    use stackless_stripe_projects::stripe::StripeProjects;
73    use stackless_stripe_projects::test_support;
74
75    #[test]
76    fn config_matches_catalog() {
77        const FIXTURE: &str = include_str!(concat!(
78            env!("CARGO_MANIFEST_DIR"),
79            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
80        ));
81        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
82        let failures = stackless_stripe_projects::verify_service(&catalog, &NeonPostgresConfig {});
83        assert!(
84            failures.is_empty(),
85            "neon/postgres catalog gaps:\n{}",
86            failures.join("\n")
87        );
88    }
89
90    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_neon","provider_name":"Neon","service_id":"postgres","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"type":"object","required":[],"additionalProperties":false,"properties":{}}}]}}"##;
91
92    fn test_def() -> StackDef {
93        StackDef::parse(
94            r#"
95[stack]
96name = "atto"
97[stack.projects.stripe]
98project = "project_1"
99[integrations.res]
100provider = "neon"
101[services.api]
102source = { repo = "r", ref = "main" }
103env = { OUT = "${integrations.res.connection_string}" }
104health = { path = "/health" }
105[services.api.local]
106run = "true"
107"#,
108        )
109        .unwrap()
110    }
111
112    #[tokio::test]
113    async fn provision_records_outputs() {
114        let runner = test_support::provision_script(
115            CATALOG_ENVELOPE,
116            serde_json::json!({"NEON_CONNECTION_STRING": "val_connection_string", "NEON_PROJECT_ID": "val_project_id", "NEON_BRANCH_ID": "val_branch_id", "NEON_BRANCH_NAME": "val_branch_name", "NEON_DATABASE_NAME": "val_database_name", "NEON_ORG_ID": "val_org_id"}),
117            0,
118        );
119        let dir = tempfile::tempdir().unwrap();
120        std::fs::write(
121            dir.path().join("stackless.toml"),
122            "[stack]\nname=\"atto\"\n",
123        )
124        .unwrap();
125        let stripe = StripeProjects::new(&runner, dir.path());
126
127        let resource = NeonPostgres
128            .provision(
129                &stripe.as_dyn(),
130                &test_def(),
131                dir.path(),
132                "demo",
133                "res",
134                "local",
135                false,
136            )
137            .await
138            .unwrap();
139        assert_eq!(resource.resource_kind, "integration-neon");
140        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
141        assert_eq!(
142            payload.outputs["connection_string"],
143            "val_connection_string"
144        );
145    }
146}