Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions crates/stackless-integrations/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
pub mod clerk;
pub mod cloudflare;
pub mod planetscale;

#[cfg(test)]
mod tests {
use stackless_provider_sdk::CatalogResource;
use stackless_provider_sdk::Hostable;

use crate::providers::cloudflare;
use crate::providers::{cloudflare, planetscale};

/// `Hostable::OUTPUTS` (the names referenceable as `${integrations.*.<out>}`)
/// must stay in lockstep with the names column of `OUTPUT_FIELDS` — the two
/// are co-located but hand-written, so this makes drift a test failure rather
/// than a silent validation bug. Bespoke providers (Clerk) aren't
/// `CatalogResource`, so they're out of scope here by construction.
fn assert_outputs_match<T: CatalogResource>() {
let fields: Vec<&str> = T::OUTPUT_FIELDS.iter().map(|(_, name, _)| *name).collect();
let outputs: Vec<&str> = <T as Hostable>::OUTPUTS.to_vec();
Expand All @@ -34,5 +30,7 @@ mod tests {
assert_outputs_match::<cloudflare::workers::CloudflareWorkers>();
assert_outputs_match::<cloudflare::workers_ai::CloudflareWorkersAi>();
assert_outputs_match::<cloudflare::browser_run::CloudflareBrowserRun>();
assert_outputs_match::<planetscale::mysql::PlanetScaleMysql>();
assert_outputs_match::<planetscale::postgresql::PlanetScalePostgresql>();
}
}
12 changes: 12 additions & 0 deletions crates/stackless-integrations/src/providers/planetscale/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! PlanetScale catalog resources via Stripe Projects.
//!
//! Output envelopes are provisional until pinned by `xtask discover`.

pub mod mysql;
pub mod postgresql;

#[allow(unused_imports)]
pub(crate) use crate::resource::{
CatalogResource as FamilyResource, bool_optional, bool_required, int_optional, int_required,
integration_config, interp_optional, interp_required,
};
164 changes: 164 additions & 0 deletions crates/stackless-integrations/src/providers/planetscale/mysql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//! `planetscale/mysql` integration.

use std::collections::BTreeMap;

use serde::Serialize;
use stackless_stripe_projects::catalog::verify::CatalogService;
use stackless_stripe_projects::provision::ProvisionContext;

use super::FamilyResource;
use crate::error::IntegrationError;
use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
use crate::registry;

pub const RESOURCE_KIND: &str = "integration-planetscale-mysql";

#[derive(Debug, Serialize)]
pub struct PlanetScaleMysqlConfig {
pub cluster: String,
pub name: String,
pub region: String,
}

impl CatalogService for PlanetScaleMysqlConfig {
const REFERENCE: &'static str = "planetscale/mysql";
}

#[derive(Debug)]
pub struct PlanetScaleMysql;

impl Hostable for PlanetScaleMysql {
const PROVIDER: &'static str = "planetscale-mysql";
const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
const RESOURCE_KIND: &'static str = RESOURCE_KIND;
const OUTPUTS: &'static [&'static str] = &["database_url"];
}

impl FamilyResource for PlanetScaleMysql {
type Config = PlanetScaleMysqlConfig;
const PROVIDER_PREFIX: &'static str = "PLANETSCALE";
// Provisional until pinned by `mise run discover planetscale/mysql`.
const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
&[("DATABASE_URL", "database_url", true)];

fn build_config(
ctx: &ProvisionContext<'_>,
) -> Result<PlanetScaleMysqlConfig, IntegrationError> {
let config = super::integration_config(ctx)?;
Ok(PlanetScaleMysqlConfig {
cluster: super::interp_required(ctx, &config, "cluster")?,
name: super::interp_required(ctx, &config, "name")?,
region: super::interp_required(ctx, &config, "region")?,
})
}
}

pub fn validate_config(
name: &str,
config: &BTreeMap<String, toml::Value>,
) -> Result<(), IntegrationError> {
registry::config_string(config, "cluster").map_err(|err| IntegrationError::ConfigInvalid {
location: format!("integrations.{name}.cluster"),
detail: err.to_string(),
})?;
registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
location: format!("integrations.{name}.name"),
detail: err.to_string(),
})?;
registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
location: format!("integrations.{name}.region"),
detail: err.to_string(),
})?;
let _ = (name, config);
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ProviderOps;
use crate::resource::ResourcePayload;
use stackless_core::def::StackDef;
use stackless_stripe_projects::stripe::StripeProjects;
use stackless_stripe_projects::test_support;

#[test]
fn config_matches_catalog() {
const FIXTURE: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../stackless-stripe-projects/tests/fixtures/catalog.json"
));
let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
let failures = stackless_stripe_projects::verify_service(
&catalog,
&PlanetScaleMysqlConfig {
cluster: "PS-10".into(),
name: "test-name".into(),
region: "aws-ap-northeast".into(),
},
);
assert!(
failures.is_empty(),
"planetscale/mysql catalog gaps:\n{}",
failures.join("\n")
);
}

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"}}]}}"##;

fn test_def() -> StackDef {
StackDef::parse(
r#"
[stack]
name = "atto"
[stack.projects.stripe]
project = "project_1"
[integrations.res]
provider = "planetscale-mysql"
cluster = "PS-10"
name = "test-name"
region = "aws-ap-northeast"
[services.api]
source = { repo = "r", ref = "main" }
env = { OUT = "${integrations.res.database_url}" }
health = { path = "/health" }
[services.api.local]
run = "true"
"#,
)
.unwrap()
}

#[tokio::test]
async fn provision_records_outputs() {
let runner = test_support::provision_script(
CATALOG_ENVELOPE,
serde_json::json!({"PLANETSCALE_DATABASE_URL": "val_database_url"}),
0,
);
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("stackless.toml"),
"[stack]\nname=\"atto\"\n",
)
.unwrap();
let stripe = StripeProjects::new(&runner, dir.path());

let resource = PlanetScaleMysql
.provision(
&stripe.as_dyn(),
&test_def(),
dir.path(),
"demo",
"res",
"local",
false,
)
.await
.unwrap();
assert_eq!(resource.resource_kind, "integration-planetscale-mysql");
let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
assert_eq!(payload.outputs["database_url"], "val_database_url");
}
}
168 changes: 168 additions & 0 deletions crates/stackless-integrations/src/providers/planetscale/postgresql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
//! `planetscale/postgresql` integration.

use std::collections::BTreeMap;

use serde::Serialize;
use stackless_stripe_projects::catalog::verify::CatalogService;
use stackless_stripe_projects::provision::ProvisionContext;

use super::FamilyResource;
use crate::error::IntegrationError;
use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
use crate::registry;

pub const RESOURCE_KIND: &str = "integration-planetscale-postgresql";

#[derive(Debug, Serialize)]
pub struct PlanetScalePostgresqlConfig {
pub cluster: String,
pub name: String,
pub region: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub replicas: Option<i64>,
}

impl CatalogService for PlanetScalePostgresqlConfig {
const REFERENCE: &'static str = "planetscale/postgresql";
}

#[derive(Debug)]
pub struct PlanetScalePostgresql;

impl Hostable for PlanetScalePostgresql {
const PROVIDER: &'static str = "planetscale-postgresql";
const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
const RESOURCE_KIND: &'static str = RESOURCE_KIND;
const OUTPUTS: &'static [&'static str] = &["database_url"];
}

impl FamilyResource for PlanetScalePostgresql {
type Config = PlanetScalePostgresqlConfig;
const PROVIDER_PREFIX: &'static str = "PLANETSCALE";
// Provisional until pinned by `mise run discover planetscale/postgresql`.
const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
&[("DATABASE_URL", "database_url", true)];

fn build_config(
ctx: &ProvisionContext<'_>,
) -> Result<PlanetScalePostgresqlConfig, IntegrationError> {
let config = super::integration_config(ctx)?;
Ok(PlanetScalePostgresqlConfig {
cluster: super::interp_required(ctx, &config, "cluster")?,
name: super::interp_required(ctx, &config, "name")?,
region: super::interp_required(ctx, &config, "region")?,
replicas: super::int_optional(ctx, &config, "replicas")?,
})
}
}

pub fn validate_config(
name: &str,
config: &BTreeMap<String, toml::Value>,
) -> Result<(), IntegrationError> {
registry::config_string(config, "cluster").map_err(|err| IntegrationError::ConfigInvalid {
location: format!("integrations.{name}.cluster"),
detail: err.to_string(),
})?;
registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
location: format!("integrations.{name}.name"),
detail: err.to_string(),
})?;
registry::config_string(config, "region").map_err(|err| IntegrationError::ConfigInvalid {
location: format!("integrations.{name}.region"),
detail: err.to_string(),
})?;
let _ = (name, config);
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ProviderOps;
use crate::resource::ResourcePayload;
use stackless_core::def::StackDef;
use stackless_stripe_projects::stripe::StripeProjects;
use stackless_stripe_projects::test_support;

#[test]
fn config_matches_catalog() {
const FIXTURE: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../stackless-stripe-projects/tests/fixtures/catalog.json"
));
let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
let failures = stackless_stripe_projects::verify_service(
&catalog,
&PlanetScalePostgresqlConfig {
cluster: "PS-5".into(),
name: "test-name".into(),
region: "aws-ap-northeast".into(),
replicas: None,
},
);
assert!(
failures.is_empty(),
"planetscale/postgresql catalog gaps:\n{}",
failures.join("\n")
);
}

const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_postgresql","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_planetscale","provider_name":"PlanetScale","service_id":"postgresql","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid"},"configuration_schema":{"properties":{"cluster":{"description":"Cluster size","enum":["PS-5","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"},"replicas":{"default":0,"description":"Number of replicas (0 for single node, 2+ for high availability)","maximum":9,"minimum":0,"type":"integer"}},"required":["name","cluster","region"],"type":"object"}}]}}"##;

fn test_def() -> StackDef {
StackDef::parse(
r#"
[stack]
name = "atto"
[stack.projects.stripe]
project = "project_1"
[integrations.res]
provider = "planetscale-postgresql"
cluster = "PS-5"
name = "test-name"
region = "aws-ap-northeast"
[services.api]
source = { repo = "r", ref = "main" }
env = { OUT = "${integrations.res.database_url}" }
health = { path = "/health" }
[services.api.local]
run = "true"
"#,
)
.unwrap()
}

#[tokio::test]
async fn provision_records_outputs() {
let runner = test_support::provision_script(
CATALOG_ENVELOPE,
serde_json::json!({"PLANETSCALE_DATABASE_URL": "val_database_url"}),
0,
);
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("stackless.toml"),
"[stack]\nname=\"atto\"\n",
)
.unwrap();
let stripe = StripeProjects::new(&runner, dir.path());

let resource = PlanetScalePostgresql
.provision(
&stripe.as_dyn(),
&test_def(),
dir.path(),
"demo",
"res",
"local",
false,
)
.await
.unwrap();
assert_eq!(resource.resource_kind, "integration-planetscale-postgresql");
let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
assert_eq!(payload.outputs["database_url"], "val_database_url");
}
}
2 changes: 2 additions & 0 deletions crates/stackless-integrations/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ register_providers! {
(cloudflare::workers, CloudflareWorkers),
(cloudflare::workers_ai, CloudflareWorkersAi),
(cloudflare::browser_run, CloudflareBrowserRun),
(planetscale::mysql, PlanetScaleMysql),
(planetscale::postgresql, PlanetScalePostgresql),
}

fn lookup(provider: &str) -> Option<&'static ProviderEntry> {
Expand Down
Loading
Loading