Skip to main content

stackless_integrations/providers/flyio/
sprite.rs

1//! `flyio/sprite` 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-flyio-sprite";
15
16#[derive(Debug, Serialize)]
17pub struct FlyioSpriteConfig {
18    pub name: String,
19}
20
21impl CatalogService for FlyioSpriteConfig {
22    const REFERENCE: &'static str = "flyio/sprite";
23}
24
25#[derive(Debug)]
26pub struct FlyioSprite;
27
28impl Hostable for FlyioSprite {
29    const PROVIDER: &'static str = "flyio-sprite";
30    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
31    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
32    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
33    const OUTPUTS: &'static [&'static str] = &["auth", "sprite_token", "url"];
34}
35
36impl FamilyResource for FlyioSprite {
37    type Config = FlyioSpriteConfig;
38    const PROVIDER_PREFIX: &'static str = "FLYIO";
39    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
40        ("AUTH", "auth", true),
41        ("SPRITE_TOKEN", "sprite_token", true),
42        ("URL", "url", true),
43    ];
44
45    fn build_config(ctx: &ProvisionContext<'_>) -> Result<FlyioSpriteConfig, IntegrationError> {
46        let config = super::integration_config(ctx)?;
47        Ok(FlyioSpriteConfig {
48            name: super::interp_required(ctx, &config, "name")?,
49        })
50    }
51}
52
53pub fn validate_config(
54    name: &str,
55    config: &BTreeMap<String, toml::Value>,
56) -> Result<(), IntegrationError> {
57    registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
58        location: format!("integrations.{name}.name"),
59        detail: err.to_string(),
60    })?;
61    let _ = (name, config);
62    Ok(())
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::ProviderOps;
69    use crate::resource::ResourcePayload;
70    use stackless_core::def::StackDef;
71    use stackless_stripe_projects::stripe::StripeProjects;
72    use stackless_stripe_projects::test_support;
73
74    #[test]
75    fn config_matches_catalog() {
76        const FIXTURE: &str = include_str!(concat!(
77            env!("CARGO_MANIFEST_DIR"),
78            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
79        ));
80        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
81        let failures = stackless_stripe_projects::verify_service(
82            &catalog,
83            &FlyioSpriteConfig {
84                name: "test-name".into(),
85            },
86        );
87        assert!(
88            failures.is_empty(),
89            "flyio/sprite 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_sprite","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_flyio","provider_name":"Flyio","service_id":"sprite","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid"},"configuration_schema":{"properties":{"name":{"description":"Sprite name","maxLength":63,"minLength":5,"pattern":"^[a-z0-9][a-z0-9-]*[a-z0-9]$","type":"string"}},"required":["name"],"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 = "flyio-sprite"
105name = "test-name"
106[services.api]
107source = { repo = "r", ref = "main" }
108env = { OUT = "${integrations.res.auth}" }
109health = { path = "/health" }
110[services.api.local]
111run = "true"
112"#,
113        )
114        .unwrap()
115    }
116
117    #[tokio::test]
118    async fn provision_records_outputs() {
119        let runner = test_support::provision_script(
120            CATALOG_ENVELOPE,
121            serde_json::json!({"FLYIO_AUTH": "val_auth", "FLYIO_SPRITE_TOKEN": "val_sprite_token", "FLYIO_URL": "val_url"}),
122            0,
123        );
124        let dir = tempfile::tempdir().unwrap();
125        std::fs::write(
126            dir.path().join("stackless.toml"),
127            "[stack]\nname=\"atto\"\n",
128        )
129        .unwrap();
130        let stripe = StripeProjects::new(&runner, dir.path());
131
132        let resource = FlyioSprite
133            .provision(
134                &stripe.as_dyn(),
135                &test_def(),
136                dir.path(),
137                "demo",
138                "res",
139                "local",
140                false,
141            )
142            .await
143            .unwrap();
144        assert_eq!(resource.resource_kind, "integration-flyio-sprite");
145        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
146        assert_eq!(payload.outputs["auth"], "val_auth");
147        assert_eq!(payload.outputs["sprite_token"], "val_sprite_token");
148        assert_eq!(payload.outputs["url"], "val_url");
149    }
150}