Skip to main content

stackless_integrations/providers/laravel_cloud/
application.rs

1//! `laravel_cloud/application` 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};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-laravel-cloud";
15
16#[derive(Debug, Serialize)]
17pub struct LaravelCloudApplicationConfig {
18    pub name: String,
19    pub region: String,
20    pub repository: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub create_cache: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub create_database: Option<String>,
25}
26
27impl CatalogService for LaravelCloudApplicationConfig {
28    const REFERENCE: &'static str = "laravel_cloud/application";
29}
30
31#[derive(Debug)]
32pub struct LaravelCloudApplication;
33
34impl Hostable for LaravelCloudApplication {
35    const PROVIDER: &'static str = "laravel-cloud";
36    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
37    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
38    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
39    const OUTPUTS: &'static [&'static str] = &["app_id"];
40}
41
42impl FamilyResource for LaravelCloudApplication {
43    type Config = LaravelCloudApplicationConfig;
44    const PROVIDER_PREFIX: &'static str = "LARAVEL_CLOUD";
45    // Provisional until pinned by `mise run discover laravel_cloud/application`.
46    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
47        &[("APP_ID", "app_id", true)];
48
49    fn build_config(
50        ctx: &ProvisionContext<'_>,
51    ) -> Result<LaravelCloudApplicationConfig, IntegrationError> {
52        let config = super::integration_config(ctx)?;
53        Ok(LaravelCloudApplicationConfig {
54            name: super::interp_required(ctx, &config, "name")?,
55            region: super::interp_required(ctx, &config, "region")?,
56            repository: super::interp_required(ctx, &config, "repository")?,
57            create_cache: super::interp_optional(ctx, &config, "create_cache")?,
58            create_database: super::interp_optional(ctx, &config, "create_database")?,
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    registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
72        location: format!("integrations.{name}.region"),
73        detail: err.to_string(),
74    })?;
75    registry::config_string(config, "repository").map_err(|err| {
76        IntegrationError::ConfigInvalid {
77            location: format!("integrations.{name}.repository"),
78            detail: err.to_string(),
79        }
80    })?;
81    let _ = (name, config);
82    Ok(())
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use crate::ProviderOps;
89    use crate::resource::ResourcePayload;
90    use stackless_core::def::StackDef;
91    use stackless_stripe_projects::stripe::StripeProjects;
92    use stackless_stripe_projects::test_support;
93
94    #[test]
95    fn config_matches_catalog() {
96        const FIXTURE: &str = include_str!(concat!(
97            env!("CARGO_MANIFEST_DIR"),
98            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
99        ));
100        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
101        let failures = stackless_stripe_projects::verify_service(
102            &catalog,
103            &LaravelCloudApplicationConfig {
104                name: "test-name".into(),
105                region: "us-east-1".into(),
106                repository: "test-repository".into(),
107                create_cache: None,
108                create_database: None,
109            },
110        );
111        assert!(
112            failures.is_empty(),
113            "laravel_cloud/application catalog gaps:\n{}",
114            failures.join("\n")
115        );
116    }
117
118    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_application","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_laravel_cloud","provider_name":"Laravel_Cloud","service_id":"application","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"create_cache":{"default":"no","description":"Provision a managed Valkey cache and connect it to this application.","enum":["yes","no"],"title":"Create a cache","type":"string"},"create_database":{"default":"no","description":"Provision a managed MySQL database and connect it to this application.","enum":["yes","no"],"title":"Create a database","type":"string"},"name":{"description":"A name for your Laravel application.","title":"Application Name","type":"string"},"region":{"enum":["us-east-1","us-east-2","eu-central-1","eu-west-1","eu-west-2","ap-southeast-1","ap-southeast-2","ap-northeast-1","ca-central-1","me-central-1"],"type":"string"},"repository":{"description":"Full repository name (e.g. laravel/cloud).","title":"Repository","type":"string"}},"required":["name","repository","region"],"type":"object"}}]}}"##;
119
120    fn test_def() -> StackDef {
121        StackDef::parse(
122            r#"
123[stack]
124name = "atto"
125[stack.projects.stripe]
126project = "project_1"
127[integrations.res]
128provider = "laravel-cloud"
129name = "test-name"
130region = "us-east-1"
131repository = "test-repository"
132[services.api]
133source = { repo = "r", ref = "main" }
134env = { OUT = "${integrations.res.app_id}" }
135health = { path = "/health" }
136[services.api.local]
137run = "true"
138"#,
139        )
140        .unwrap()
141    }
142
143    #[tokio::test]
144    async fn provision_records_outputs() {
145        let runner = test_support::provision_script(
146            CATALOG_ENVELOPE,
147            serde_json::json!({"LARAVEL_CLOUD_APP_ID": "val_app_id"}),
148            0,
149        );
150        let dir = tempfile::tempdir().unwrap();
151        std::fs::write(
152            dir.path().join("stackless.toml"),
153            "[stack]\nname=\"atto\"\n",
154        )
155        .unwrap();
156        let stripe = StripeProjects::new(&runner, dir.path());
157
158        let resource = LaravelCloudApplication
159            .provision(
160                &stripe.as_dyn(),
161                &test_def(),
162                dir.path(),
163                "demo",
164                "res",
165                "local",
166                false,
167            )
168            .await
169            .unwrap();
170        assert_eq!(resource.resource_kind, "integration-laravel-cloud");
171        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
172        assert_eq!(payload.outputs["app_id"], "val_app_id");
173    }
174}