stackless_integrations/providers/planetscale/
mysql.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-planetscale-mysql";
15
16#[derive(Debug, Serialize)]
17pub struct PlanetScaleMysqlConfig {
18 pub cluster: String,
19 pub name: String,
20 pub region: String,
21}
22
23impl CatalogService for PlanetScaleMysqlConfig {
24 const REFERENCE: &'static str = "planetscale/mysql";
25}
26
27#[derive(Debug)]
28pub struct PlanetScaleMysql;
29
30impl Hostable for PlanetScaleMysql {
31 const PROVIDER: &'static str = "planetscale-mysql";
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] = &["database_url"];
36}
37
38impl FamilyResource for PlanetScaleMysql {
39 type Config = PlanetScaleMysqlConfig;
40 const PROVIDER_PREFIX: &'static str = "PLANETSCALE";
41 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
43 &[("DATABASE_URL", "database_url", true)];
44
45 fn build_config(
46 ctx: &ProvisionContext<'_>,
47 ) -> Result<PlanetScaleMysqlConfig, IntegrationError> {
48 let config = super::integration_config(ctx)?;
49 Ok(PlanetScaleMysqlConfig {
50 cluster: super::interp_required(ctx, &config, "cluster")?,
51 name: super::interp_required(ctx, &config, "name")?,
52 region: super::interp_required(ctx, &config, "region")?,
53 })
54 }
55}
56
57pub fn validate_config(
58 name: &str,
59 config: &BTreeMap<String, toml::Value>,
60) -> Result<(), IntegrationError> {
61 registry::config_string(config, "cluster").map_err(|err| IntegrationError::ConfigInvalid {
62 location: format!("integrations.{name}.cluster"),
63 detail: err.to_string(),
64 })?;
65 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
66 location: format!("integrations.{name}.name"),
67 detail: err.to_string(),
68 })?;
69 registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
70 location: format!("integrations.{name}.region"),
71 detail: err.to_string(),
72 })?;
73 let _ = (name, config);
74 Ok(())
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use crate::ProviderOps;
81 use crate::resource::ResourcePayload;
82 use stackless_core::def::StackDef;
83 use stackless_stripe_projects::stripe::StripeProjects;
84 use stackless_stripe_projects::test_support;
85
86 #[test]
87 fn config_matches_catalog() {
88 const FIXTURE: &str = include_str!(concat!(
89 env!("CARGO_MANIFEST_DIR"),
90 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
91 ));
92 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
93 let failures = stackless_stripe_projects::verify_service(
94 &catalog,
95 &PlanetScaleMysqlConfig {
96 cluster: "PS-10".into(),
97 name: "test-name".into(),
98 region: "aws-ap-northeast".into(),
99 },
100 );
101 assert!(
102 failures.is_empty(),
103 "planetscale/mysql catalog gaps:\n{}",
104 failures.join("\n")
105 );
106 }
107
108 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_mysql","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_planetscale","provider_name":"PlanetScale","service_id":"mysql","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid"},"configuration_schema":{"properties":{"cluster":{"description":"Cluster size","enum":["PS-10","PS-20","PS-40","PS-80","PS-160","PS-320"],"type":"string"},"name":{"description":"Database name.","maxLength":63,"minLength":2,"pattern":"^[a-z0-9][a-z0-9_-]*[a-z0-9]$","type":"string"},"region":{"description":"Deployment region (prefixed with cloud provider, e.g., aws-us-east, gcp-us-central1)","enum":["aws-ap-northeast","aws-ap-south","aws-ap-southeast","aws-ap-southeast-2","aws-ca-central-1","aws-eu-central","aws-eu-west","aws-eu-west-2","aws-sa-east-1","aws-us-east","aws-us-east-2","aws-us-west","gcp-asia-northeast3","gcp-europe-west1","gcp-europe-west4","gcp-northamerica-northeast1","gcp-us-central1","gcp-us-east1","gcp-us-east4"],"type":"string"}},"required":["name","cluster","region"],"type":"object"}}]}}"##;
109
110 fn test_def() -> StackDef {
111 StackDef::parse(
112 r#"
113[stack]
114name = "atto"
115[stack.projects.stripe]
116project = "project_1"
117[integrations.res]
118provider = "planetscale-mysql"
119cluster = "PS-10"
120name = "test-name"
121region = "aws-ap-northeast"
122[services.api]
123source = { repo = "r", ref = "main" }
124env = { OUT = "${integrations.res.database_url}" }
125health = { path = "/health" }
126[services.api.local]
127run = "true"
128"#,
129 )
130 .unwrap()
131 }
132
133 #[tokio::test]
134 async fn provision_records_outputs() {
135 let runner = test_support::provision_script(
136 CATALOG_ENVELOPE,
137 serde_json::json!({"PLANETSCALE_DATABASE_URL": "val_database_url"}),
138 0,
139 );
140 let dir = tempfile::tempdir().unwrap();
141 std::fs::write(
142 dir.path().join("stackless.toml"),
143 "[stack]\nname=\"atto\"\n",
144 )
145 .unwrap();
146 let stripe = StripeProjects::new(&runner, dir.path());
147
148 let resource = PlanetScaleMysql
149 .provision(
150 &stripe.as_dyn(),
151 &test_def(),
152 dir.path(),
153 "demo",
154 "res",
155 "local",
156 false,
157 )
158 .await
159 .unwrap();
160 assert_eq!(resource.resource_kind, "integration-planetscale-mysql");
161 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
162 assert_eq!(payload.outputs["database_url"], "val_database_url");
163 }
164}