Skip to main content

stackless_integrations/providers/depot/
api.rs

1//! `depot/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-depot";
14
15#[derive(Debug, Serialize)]
16pub struct DepotApiConfig {}
17
18impl CatalogService for DepotApiConfig {
19    const REFERENCE: &'static str = "depot/api";
20}
21
22#[derive(Debug)]
23pub struct DepotApi;
24
25impl Hostable for DepotApi {
26    const PROVIDER: &'static str = "depot";
27    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
28    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
29    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
30    const OUTPUTS: &'static [&'static str] = &["api_token", "organization_id", "token_id"];
31}
32
33impl FamilyResource for DepotApi {
34    type Config = DepotApiConfig;
35    const PROVIDER_PREFIX: &'static str = "DEPOT";
36    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
37        ("API_TOKEN", "api_token", true),
38        ("ORGANIZATION_ID", "organization_id", true),
39        ("TOKEN_ID", "token_id", true),
40    ];
41
42    fn build_config(ctx: &ProvisionContext<'_>) -> Result<DepotApiConfig, IntegrationError> {
43        let _ = super::integration_config(ctx)?;
44        Ok(DepotApiConfig {})
45    }
46}
47
48pub fn validate_config(
49    name: &str,
50    config: &BTreeMap<String, toml::Value>,
51) -> Result<(), IntegrationError> {
52    let _ = (name, config);
53    Ok(())
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::ProviderOps;
60    use crate::resource::ResourcePayload;
61    use stackless_core::def::StackDef;
62    use stackless_stripe_projects::stripe::StripeProjects;
63    use stackless_stripe_projects::test_support;
64
65    #[test]
66    fn config_matches_catalog() {
67        const FIXTURE: &str = include_str!(concat!(
68            env!("CARGO_MANIFEST_DIR"),
69            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
70        ));
71        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
72        let failures = stackless_stripe_projects::verify_service(&catalog, &DepotApiConfig {});
73        assert!(
74            failures.is_empty(),
75            "depot/api catalog gaps:\n{}",
76            failures.join("\n")
77        );
78    }
79
80    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_depot","provider_name":"Depot","service_id":"api","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"additionalProperties":false,"properties":{},"type":"object"}}]}}"##;
81
82    fn test_def() -> StackDef {
83        StackDef::parse(
84            r#"
85[stack]
86name = "atto"
87[stack.projects.stripe]
88project = "project_1"
89[integrations.res]
90provider = "depot"
91[services.api]
92source = { repo = "r", ref = "main" }
93env = { OUT = "${integrations.res.api_token}" }
94health = { path = "/health" }
95[services.api.local]
96run = "true"
97"#,
98        )
99        .unwrap()
100    }
101
102    #[tokio::test]
103    async fn provision_records_outputs() {
104        let runner = test_support::provision_script(
105            CATALOG_ENVELOPE,
106            serde_json::json!({"DEPOT_API_TOKEN": "val_api_token", "DEPOT_ORGANIZATION_ID": "val_organization_id", "DEPOT_TOKEN_ID": "val_token_id"}),
107            0,
108        );
109        let dir = tempfile::tempdir().unwrap();
110        std::fs::write(
111            dir.path().join("stackless.toml"),
112            "[stack]\nname=\"atto\"\n",
113        )
114        .unwrap();
115        let stripe = StripeProjects::new(&runner, dir.path());
116
117        let resource = DepotApi
118            .provision(
119                &stripe.as_dyn(),
120                &test_def(),
121                dir.path(),
122                "demo",
123                "res",
124                "local",
125                false,
126            )
127            .await
128            .unwrap();
129        assert_eq!(resource.resource_kind, "integration-depot");
130        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
131        assert_eq!(payload.outputs["api_token"], "val_api_token");
132        assert_eq!(payload.outputs["organization_id"], "val_organization_id");
133        assert_eq!(payload.outputs["token_id"], "val_token_id");
134    }
135}