Skip to main content

greentic_distributor_client/
config.rs

1use crate::{DistributorEnvironmentId, TenantCtx};
2use greentic_config_types::GreenticConfig;
3use std::{collections::HashMap, time::Duration};
4
5/// Configuration for distributor clients.
6///
7/// NOTE: `base_url`, `auth_token`, and header fields are used when the
8/// `http-runtime` feature is enabled. Without that feature, the WIT client is
9/// the primary implementation.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct DistributorClientConfig {
12    pub base_url: Option<String>,
13    pub environment_id: DistributorEnvironmentId,
14    pub tenant: TenantCtx,
15    pub auth_token: Option<String>,
16    pub extra_headers: Option<HashMap<String, String>>,
17    pub request_timeout: Option<Duration>,
18}
19
20impl DistributorClientConfig {
21    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
22        self.base_url = Some(base_url.into());
23        self
24    }
25
26    /// Builds a distributor client config from a resolved GreenticConfig and tenant context.
27    ///
28    /// This keeps greentic-config resolution in the host while allowing consumers to
29    /// map the shared schema into the distributor client.
30    pub fn from_greentic(cfg: &GreenticConfig, mut tenant: TenantCtx) -> Self {
31        // Ensure tenant context env matches the shared config.
32        tenant.env = cfg.environment.env_id.clone();
33        tenant.tenant_id = tenant.tenant.clone();
34
35        let environment_id = DistributorEnvironmentId::from(cfg.environment.env_id.as_str());
36        let request_timeout = cfg.network.read_timeout_ms.map(Duration::from_millis);
37
38        Self {
39            base_url: None,
40            environment_id,
41            tenant,
42            auth_token: None,
43            extra_headers: None,
44            request_timeout,
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use greentic_config_types::{
53        ConfigVersion, EnvironmentConfig, NetworkConfig, PathsConfig, RuntimeConfig,
54        SecretsBackendRefConfig, TelemetryConfig,
55    };
56    use greentic_types::{EnvId, TenantCtx, TenantId};
57    use std::path::PathBuf;
58
59    fn greentic_config() -> GreenticConfig {
60        GreenticConfig {
61            schema_version: ConfigVersion::default(),
62            environment: EnvironmentConfig {
63                env_id: EnvId::try_from("dev").unwrap(),
64                deployment: None,
65                connection: None,
66                region: None,
67            },
68            paths: PathsConfig {
69                greentic_root: PathBuf::from("/workspace"),
70                state_dir: PathBuf::from("/workspace/state"),
71                cache_dir: PathBuf::from("/workspace/cache"),
72                logs_dir: PathBuf::from("/workspace/logs"),
73            },
74            packs: None,
75            services: None,
76            events: None,
77            runtime: RuntimeConfig::default(),
78            telemetry: TelemetryConfig::default(),
79            network: NetworkConfig::default(),
80            deployer: None,
81            secrets: SecretsBackendRefConfig::default(),
82            dev: None,
83        }
84    }
85
86    #[test]
87    fn maps_request_timeout_from_network_read_timeout() {
88        let mut cfg = greentic_config();
89        cfg.network.read_timeout_ms = Some(2500);
90
91        let tenant = TenantCtx::new(
92            EnvId::try_from("ignored").unwrap(),
93            TenantId::try_from("t1").unwrap(),
94        );
95        let mapped = DistributorClientConfig::from_greentic(&cfg, tenant);
96
97        assert_eq!(mapped.request_timeout, Some(Duration::from_millis(2500)));
98        assert_eq!(mapped.environment_id.as_str(), "dev");
99    }
100}