stackless_integrations/providers/blaxel/
sandbox.rs1use 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-blaxel-sandbox";
14
15#[derive(Debug, Serialize)]
16pub struct BlaxelSandboxConfig {
17 #[serde(skip_serializing_if = "Option::is_none")]
18 pub display_name: Option<String>,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub image: Option<String>,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 pub memory: Option<i64>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub region: Option<String>,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 pub ttl: Option<String>,
27}
28
29impl CatalogService for BlaxelSandboxConfig {
30 const REFERENCE: &'static str = "blaxel/sandbox";
31}
32
33#[derive(Debug)]
34pub struct BlaxelSandbox;
35
36impl Hostable for BlaxelSandbox {
37 const PROVIDER: &'static str = "blaxel-sandbox";
38 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
39 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
40 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
41 const OUTPUTS: &'static [&'static str] = &["api_key"];
42}
43
44impl FamilyResource for BlaxelSandbox {
45 type Config = BlaxelSandboxConfig;
46 const PROVIDER_PREFIX: &'static str = "BLAXEL";
47 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
49 &[("API_KEY", "api_key", true)];
50
51 fn build_config(ctx: &ProvisionContext<'_>) -> Result<BlaxelSandboxConfig, IntegrationError> {
52 let config = super::integration_config(ctx)?;
53 Ok(BlaxelSandboxConfig {
54 display_name: super::interp_optional(ctx, &config, "display_name")?,
55 image: super::interp_optional(ctx, &config, "image")?,
56 memory: super::int_optional(ctx, &config, "memory")?,
57 region: super::interp_optional(ctx, &config, "region")?,
58 ttl: super::interp_optional(ctx, &config, "ttl")?,
59 })
60 }
61}
62
63pub fn validate_config(
64 name: &str,
65 config: &BTreeMap<String, toml::Value>,
66) -> Result<(), IntegrationError> {
67 let _ = (name, config);
68 Ok(())
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use crate::ProviderOps;
75 use crate::resource::ResourcePayload;
76 use stackless_core::def::StackDef;
77 use stackless_stripe_projects::stripe::StripeProjects;
78 use stackless_stripe_projects::test_support;
79
80 #[test]
81 fn config_matches_catalog() {
82 const FIXTURE: &str = include_str!(concat!(
83 env!("CARGO_MANIFEST_DIR"),
84 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
85 ));
86 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
87 let failures = stackless_stripe_projects::verify_service(
88 &catalog,
89 &BlaxelSandboxConfig {
90 display_name: None,
91 image: None,
92 memory: None,
93 region: None,
94 ttl: None,
95 },
96 );
97 assert!(
98 failures.is_empty(),
99 "blaxel/sandbox catalog gaps:\n{}",
100 failures.join("\n")
101 );
102 }
103
104 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_sandbox","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_blaxel","provider_name":"Blaxel","service_id":"sandbox","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"free"},"configuration_schema":{"additionalProperties":false,"properties":{"display_name":{"description":"Human-readable display name shown in the Blaxel dashboard.","type":"string"},"image":{"description":"Base image for the sandbox.","enum":["blaxel/base-image:latest","blaxel/ts-app:latest","blaxel/node:latest","blaxel/py-app:latest","blaxel/nextjs:latest","blaxel/vite:latest","blaxel/expo:latest"],"type":"string"},"memory":{"description":"Memory in MB. CPU is derived automatically.","enum":[1024,2048,4096,8192,16384],"type":"integer"},"region":{"description":"Region where the sandbox runs.","enum":["us-pdx-1","us-was-1","eu-lon-1","eu-fra-1"],"type":"string"},"ttl":{"description":"Max-age TTL from creation. Sandbox is deleted after this duration.","enum":["24h","7d","30d"],"type":"string"}},"required":[],"type":"object"}}]}}"##;
105
106 fn test_def() -> StackDef {
107 StackDef::parse(
108 r#"
109[stack]
110name = "atto"
111[stack.projects.stripe]
112project = "project_1"
113[integrations.res]
114provider = "blaxel-sandbox"
115[services.api]
116source = { repo = "r", ref = "main" }
117env = { OUT = "${integrations.res.api_key}" }
118health = { path = "/health" }
119[services.api.local]
120run = "true"
121"#,
122 )
123 .unwrap()
124 }
125
126 #[tokio::test]
127 async fn provision_records_outputs() {
128 let runner = test_support::provision_script(
129 CATALOG_ENVELOPE,
130 serde_json::json!({"BLAXEL_API_KEY": "val_api_key"}),
131 0,
132 );
133 let dir = tempfile::tempdir().unwrap();
134 std::fs::write(
135 dir.path().join("stackless.toml"),
136 "[stack]\nname=\"atto\"\n",
137 )
138 .unwrap();
139 let stripe = StripeProjects::new(&runner, dir.path());
140
141 let resource = BlaxelSandbox
142 .provision(
143 &stripe.as_dyn(),
144 &test_def(),
145 dir.path(),
146 "demo",
147 "res",
148 "local",
149 false,
150 )
151 .await
152 .unwrap();
153 assert_eq!(resource.resource_kind, "integration-blaxel-sandbox");
154 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
155 assert_eq!(payload.outputs["api_key"], "val_api_key");
156 }
157}