stackless-integrations 0.3.0

Hosted integration routing for stackless
Documentation
//! `neon/postgres` integration.

use std::collections::BTreeMap;

use serde::Serialize;
use stackless_stripe_projects::catalog::verify::CatalogService;
use stackless_stripe_projects::provision::ProvisionContext;

use super::FamilyResource;
use crate::error::IntegrationError;
use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};

pub const RESOURCE_KIND: &str = "integration-neon";

#[derive(Debug, Serialize)]
pub struct NeonPostgresConfig {}

impl CatalogService for NeonPostgresConfig {
    const REFERENCE: &'static str = "neon/postgres";
}

#[derive(Debug)]
pub struct NeonPostgres;

impl Hostable for NeonPostgres {
    const PROVIDER: &'static str = "neon";
    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
    const OUTPUTS: &'static [&'static str] = &[
        "connection_string",
        "project_id",
        "branch_id",
        "branch_name",
        "database_name",
        "org_id",
    ];
}

impl FamilyResource for NeonPostgres {
    type Config = NeonPostgresConfig;
    const PROVIDER_PREFIX: &'static str = "NEON";
    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
        ("CONNECTION_STRING", "connection_string", true),
        ("PROJECT_ID", "project_id", true),
        ("BRANCH_ID", "branch_id", false),
        ("BRANCH_NAME", "branch_name", false),
        ("DATABASE_NAME", "database_name", false),
        ("ORG_ID", "org_id", false),
    ];

    fn build_config(ctx: &ProvisionContext<'_>) -> Result<NeonPostgresConfig, IntegrationError> {
        let _ = super::integration_config(ctx)?;
        Ok(NeonPostgresConfig {})
    }
}

pub fn validate_config(
    name: &str,
    config: &BTreeMap<String, toml::Value>,
) -> Result<(), IntegrationError> {
    let _ = (name, config);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ProviderOps;
    use crate::resource::ResourcePayload;
    use stackless_core::def::StackDef;
    use stackless_stripe_projects::stripe::StripeProjects;
    use stackless_stripe_projects::test_support;

    #[test]
    fn config_matches_catalog() {
        const FIXTURE: &str = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
        ));
        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
        let failures = stackless_stripe_projects::verify_service(&catalog, &NeonPostgresConfig {});
        assert!(
            failures.is_empty(),
            "neon/postgres catalog gaps:\n{}",
            failures.join("\n")
        );
    }

    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":{}}}]}}"##;

    fn test_def() -> StackDef {
        StackDef::parse(
            r#"
[stack]
name = "atto"
[stack.projects.stripe]
project = "project_1"
[integrations.res]
provider = "neon"
[services.api]
source = { repo = "r", ref = "main" }
env = { OUT = "${integrations.res.connection_string}" }
health = { path = "/health" }
[services.api.local]
run = "true"
"#,
        )
        .unwrap()
    }

    #[tokio::test]
    async fn provision_records_outputs() {
        let runner = test_support::provision_script(
            CATALOG_ENVELOPE,
            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"}),
            0,
        );
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("stackless.toml"),
            "[stack]\nname=\"atto\"\n",
        )
        .unwrap();
        let stripe = StripeProjects::new(&runner, dir.path());

        let resource = NeonPostgres
            .provision(
                &stripe.as_dyn(),
                &test_def(),
                dir.path(),
                "demo",
                "res",
                "local",
                false,
            )
            .await
            .unwrap();
        assert_eq!(resource.resource_kind, "integration-neon");
        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
        assert_eq!(
            payload.outputs["connection_string"],
            "val_connection_string"
        );
    }
}