Skip to main content

stackless_integrations/providers/schematic/
schematic_environment.rs

1//! `schematic/schematic-environment` 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-schematic";
14
15#[derive(Debug, Serialize)]
16pub struct SchematicEnvironmentConfig {
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub environment_type: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub name: Option<String>,
21}
22
23impl CatalogService for SchematicEnvironmentConfig {
24    const REFERENCE: &'static str = "schematic/schematic-environment";
25}
26
27#[derive(Debug)]
28pub struct SchematicEnvironment;
29
30impl Hostable for SchematicEnvironment {
31    const PROVIDER: &'static str = "schematic";
32    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
33    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
34    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
35    const OUTPUTS: &'static [&'static str] = &["api_key"];
36}
37
38impl FamilyResource for SchematicEnvironment {
39    type Config = SchematicEnvironmentConfig;
40    const PROVIDER_PREFIX: &'static str = "SCHEMATIC";
41    // Provisional until pinned by `mise run discover schematic/schematic-environment`.
42    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
43        &[("API_KEY", "api_key", true)];
44
45    fn build_config(
46        ctx: &ProvisionContext<'_>,
47    ) -> Result<SchematicEnvironmentConfig, IntegrationError> {
48        let config = super::integration_config(ctx)?;
49        Ok(SchematicEnvironmentConfig {
50            environment_type: super::interp_optional(ctx, &config, "environment_type")?,
51            name: super::interp_optional(ctx, &config, "name")?,
52        })
53    }
54}
55
56pub fn validate_config(
57    name: &str,
58    config: &BTreeMap<String, toml::Value>,
59) -> Result<(), IntegrationError> {
60    let _ = (name, config);
61    Ok(())
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::ProviderOps;
68    use crate::resource::ResourcePayload;
69    use stackless_core::def::StackDef;
70    use stackless_stripe_projects::stripe::StripeProjects;
71    use stackless_stripe_projects::test_support;
72
73    #[test]
74    fn config_matches_catalog() {
75        const FIXTURE: &str = include_str!(concat!(
76            env!("CARGO_MANIFEST_DIR"),
77            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
78        ));
79        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
80        let failures = stackless_stripe_projects::verify_service(
81            &catalog,
82            &SchematicEnvironmentConfig {
83                environment_type: None,
84                name: None,
85            },
86        );
87        assert!(
88            failures.is_empty(),
89            "schematic/schematic-environment catalog gaps:\n{}",
90            failures.join("\n")
91        );
92    }
93
94    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_schematic_environment","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_schematic","provider_name":"Schematic","service_id":"schematic-environment","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"environment_type":{"type":"string","enum":["development","production"]},"name":{"type":"string"}},"type":"object"}}]}}"##;
95
96    fn test_def() -> StackDef {
97        StackDef::parse(
98            r#"
99[stack]
100name = "atto"
101[stack.projects.stripe]
102project = "project_1"
103[integrations.res]
104provider = "schematic"
105[services.api]
106source = { repo = "r", ref = "main" }
107env = { OUT = "${integrations.res.api_key}" }
108health = { path = "/health" }
109[services.api.local]
110run = "true"
111"#,
112        )
113        .unwrap()
114    }
115
116    #[tokio::test]
117    async fn provision_records_outputs() {
118        let runner = test_support::provision_script(
119            CATALOG_ENVELOPE,
120            serde_json::json!({"SCHEMATIC_API_KEY": "val_api_key"}),
121            0,
122        );
123        let dir = tempfile::tempdir().unwrap();
124        std::fs::write(
125            dir.path().join("stackless.toml"),
126            "[stack]\nname=\"atto\"\n",
127        )
128        .unwrap();
129        let stripe = StripeProjects::new(&runner, dir.path());
130
131        let resource = SchematicEnvironment
132            .provision(
133                &stripe.as_dyn(),
134                &test_def(),
135                dir.path(),
136                "demo",
137                "res",
138                "local",
139                false,
140            )
141            .await
142            .unwrap();
143        assert_eq!(resource.resource_kind, "integration-schematic");
144        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
145        assert_eq!(payload.outputs["api_key"], "val_api_key");
146    }
147}