Skip to main content

stackless_integrations/providers/parallel/
api.rs

1//! `parallel/api` 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};
12
13pub const RESOURCE_KIND: &str = "integration-parallel";
14
15#[derive(Debug, Serialize)]
16pub struct ParallelApiConfig {
17    /// Paid-pricing selectors (not in configuration_schema). Default product
18    /// `search` matches the catalog default tier; `processor` / `generator` /
19    /// `tier` select non-default products.
20    pub product: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub processor: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub generator: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub tier: Option<String>,
27}
28
29impl CatalogService for ParallelApiConfig {
30    const REFERENCE: &'static str = "parallel/api";
31}
32
33#[derive(Debug)]
34pub struct ParallelApi;
35
36impl Hostable for ParallelApi {
37    const PROVIDER: &'static str = "parallel";
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] = &["parallel_api_key"];
42}
43
44impl FamilyResource for ParallelApi {
45    type Config = ParallelApiConfig;
46    const PROVIDER_PREFIX: &'static str = "PARALLEL";
47    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
48        &[("PARALLEL_API_KEY", "parallel_api_key", true)];
49
50    fn build_config(ctx: &ProvisionContext<'_>) -> Result<ParallelApiConfig, IntegrationError> {
51        let config = super::integration_config(ctx)?;
52        Ok(ParallelApiConfig {
53            product: super::interp_optional(ctx, &config, "product")?
54                .unwrap_or_else(|| "search".to_owned()),
55            processor: super::interp_optional(ctx, &config, "processor")?,
56            generator: super::interp_optional(ctx, &config, "generator")?,
57            tier: super::interp_optional(ctx, &config, "tier")?,
58        })
59    }
60}
61
62pub fn validate_config(
63    name: &str,
64    config: &BTreeMap<String, toml::Value>,
65) -> Result<(), IntegrationError> {
66    let _ = (name, config);
67    Ok(())
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use crate::ProviderOps;
74    use crate::resource::ResourcePayload;
75    use stackless_core::def::StackDef;
76    use stackless_stripe_projects::stripe::StripeProjects;
77    use stackless_stripe_projects::test_support;
78
79    #[test]
80    fn config_matches_catalog() {
81        const FIXTURE: &str = include_str!(concat!(
82            env!("CARGO_MANIFEST_DIR"),
83            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
84        ));
85        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
86        let failures = stackless_stripe_projects::verify_service(
87            &catalog,
88            &ParallelApiConfig {
89                product: "search".into(),
90                processor: None,
91                generator: None,
92                tier: None,
93            },
94        );
95        assert!(
96            failures.is_empty(),
97            "parallel/api catalog gaps:\n{}",
98            failures.join("\n")
99        );
100    }
101
102    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_api","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_parallel","provider_name":"Parallel","service_id":"api","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid","paid_pricing":[{"type":"freeform","is_default":true,"configuration":{"product":"search"}}]},"configuration_schema":{"type":"object","required":[],"additionalProperties":false,"properties":{}}}]}}"##;
103
104    fn test_def() -> StackDef {
105        StackDef::parse(
106            r#"
107[stack]
108name = "atto"
109[stack.projects.stripe]
110project = "project_1"
111[integrations.res]
112provider = "parallel"
113[services.api]
114source = { repo = "r", ref = "main" }
115env = { OUT = "${integrations.res.parallel_api_key}" }
116health = { path = "/health" }
117[services.api.local]
118run = "true"
119"#,
120        )
121        .unwrap()
122    }
123
124    #[tokio::test]
125    async fn provision_records_outputs() {
126        let runner = test_support::provision_script(
127            CATALOG_ENVELOPE,
128            serde_json::json!({"PARALLEL_PARALLEL_API_KEY": "val_parallel_api_key"}),
129            0,
130        );
131        let dir = tempfile::tempdir().unwrap();
132        std::fs::write(
133            dir.path().join("stackless.toml"),
134            "[stack]\nname=\"atto\"\n",
135        )
136        .unwrap();
137        let stripe = StripeProjects::new(&runner, dir.path());
138
139        let resource = ParallelApi
140            .provision(
141                &stripe.as_dyn(),
142                &test_def(),
143                dir.path(),
144                "demo",
145                "res",
146                "local",
147                false,
148            )
149            .await
150            .unwrap();
151        assert_eq!(resource.resource_kind, "integration-parallel");
152        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
153        assert_eq!(payload.outputs["parallel_api_key"], "val_parallel_api_key");
154    }
155}