stackless_integrations/providers/turso/
database.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};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-turso";
15
16#[derive(Debug, Serialize)]
17pub struct TursoDatabaseConfig {
18 pub location: String,
19 pub name: String,
20}
21
22impl CatalogService for TursoDatabaseConfig {
23 const REFERENCE: &'static str = "turso/database";
24}
25
26#[derive(Debug)]
27pub struct TursoDatabase;
28
29impl Hostable for TursoDatabase {
30 const PROVIDER: &'static str = "turso";
31 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
32 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
33 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
34 const OUTPUTS: &'static [&'static str] = &["database_url", "auth_token"];
35}
36
37impl FamilyResource for TursoDatabase {
38 type Config = TursoDatabaseConfig;
39 const PROVIDER_PREFIX: &'static str = "TURSO";
40 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
42 ("DATABASE_URL", "database_url", true),
43 ("AUTH_TOKEN", "auth_token", true),
44 ];
45
46 fn build_config(ctx: &ProvisionContext<'_>) -> Result<TursoDatabaseConfig, IntegrationError> {
47 let config = super::integration_config(ctx)?;
48 Ok(TursoDatabaseConfig {
49 location: super::interp_required(ctx, &config, "location")?,
50 name: super::interp_required(ctx, &config, "name")?,
51 })
52 }
53}
54
55pub fn validate_config(
56 name: &str,
57 config: &BTreeMap<String, toml::Value>,
58) -> Result<(), IntegrationError> {
59 registry::config_string(config, "location").map_err(|err| IntegrationError::ConfigInvalid {
60 location: format!("integrations.{name}.location"),
61 detail: err.to_string(),
62 })?;
63 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
64 location: format!("integrations.{name}.name"),
65 detail: err.to_string(),
66 })?;
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 &TursoDatabaseConfig {
90 location: "aws-us-east-1".into(),
91 name: "test-name".into(),
92 },
93 );
94 assert!(
95 failures.is_empty(),
96 "turso/database catalog gaps:\n{}",
97 failures.join("\n")
98 );
99 }
100
101 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_database","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_turso","provider_name":"Turso","service_id":"database","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"location":{"description":"Primary location (e.g. aws-us-east-1, aws-eu-west-1)","enum":["aws-us-east-1","aws-us-east-2","aws-us-west-2","aws-eu-west-1","aws-ap-south-1","aws-ap-northeast-1"],"type":"string"},"name":{"description":"Database name","type":"string"}},"required":["name","location"],"type":"object"}}]}}"##;
102
103 fn test_def() -> StackDef {
104 StackDef::parse(
105 r#"
106[stack]
107name = "atto"
108[stack.projects.stripe]
109project = "project_1"
110[integrations.res]
111provider = "turso"
112location = "aws-us-east-1"
113name = "test-name"
114[services.api]
115source = { repo = "r", ref = "main" }
116env = { OUT = "${integrations.res.database_url}" }
117health = { path = "/health" }
118[services.api.local]
119run = "true"
120"#,
121 )
122 .unwrap()
123 }
124
125 #[tokio::test]
126 async fn provision_records_outputs() {
127 let runner = test_support::provision_script(
128 CATALOG_ENVELOPE,
129 serde_json::json!({"TURSO_DATABASE_URL": "val_database_url", "TURSO_AUTH_TOKEN": "val_auth_token"}),
130 0,
131 );
132 let dir = tempfile::tempdir().unwrap();
133 std::fs::write(
134 dir.path().join("stackless.toml"),
135 "[stack]\nname=\"atto\"\n",
136 )
137 .unwrap();
138 let stripe = StripeProjects::new(&runner, dir.path());
139
140 let resource = TursoDatabase
141 .provision(
142 &stripe.as_dyn(),
143 &test_def(),
144 dir.path(),
145 "demo",
146 "res",
147 "local",
148 false,
149 )
150 .await
151 .unwrap();
152 assert_eq!(resource.resource_kind, "integration-turso");
153 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
154 assert_eq!(payload.outputs["database_url"], "val_database_url");
155 }
156}