Skip to main content

alien_bindings/
lib.rs

1use alien_error::AlienError;
2
3// Re-export core traits and types
4pub use alien_context::AlienContext;
5pub use alien_core::{
6    BindingsMode, Platform, ENV_ALIEN_BINDINGS_MODE, ENV_ALIEN_DEPLOYMENT_TYPE,
7    ENV_OPERATOR_BASE_PLATFORM,
8};
9pub use error::{ErrorData, Result};
10pub use provider::BindingsProvider;
11pub use traits::{
12    ArtifactRegistry, ArtifactRegistryCredentials, ArtifactRegistryPermissions,
13    AwsServiceAccountInfo, AzureServiceAccountInfo, Binding, BindingsProviderApi, Build, Container,
14    GcpServiceAccountInfo, ImpersonationRequest, Kv, Postgres, PostgresConnectionParams, Queue,
15    RegistryAuthMethod, RepositoryResponse, ServiceAccount, ServiceAccountInfo, SslMode, Storage,
16    Vault, Worker,
17};
18pub use wait_until::{DrainConfig, DrainResponse, WaitUntil, WaitUntilContext};
19
20pub mod error;
21#[cfg(feature = "grpc")]
22pub mod grpc;
23pub mod providers;
24pub mod traits;
25// Re-export presigned types from alien-core
26pub mod presigned {
27    pub use alien_core::presigned::*;
28}
29pub mod alien_context;
30pub mod http_client;
31pub mod provider;
32
33mod wait_until;
34
35#[cfg(feature = "grpc")]
36pub use grpc::control;
37#[cfg(feature = "grpc")]
38pub use grpc::control_service::ControlGrpcServer;
39#[cfg(feature = "grpc")]
40pub use grpc::GrpcServerHandles;
41
42/// Gets the current platform from the ALIEN_DEPLOYMENT_TYPE environment variable.
43/// This is used by the runtime to determine which platform-specific implementations to use.
44pub fn get_current_platform() -> Result<Platform> {
45    let env_vars: std::collections::HashMap<String, String> = std::env::vars().collect();
46    get_platform_from_env(&env_vars)
47}
48
49/// Gets the platform from a HashMap of environment variables.
50pub fn get_platform_from_env(env: &std::collections::HashMap<String, String>) -> Result<Platform> {
51    let deployment_type = env.get(ENV_ALIEN_DEPLOYMENT_TYPE).ok_or_else(|| {
52        AlienError::new(ErrorData::EnvironmentVariableMissing {
53            variable_name: ENV_ALIEN_DEPLOYMENT_TYPE.to_string(),
54        })
55    })?;
56
57    deployment_type.parse().map_err(|_| {
58        AlienError::new(ErrorData::InvalidEnvironmentVariable {
59            variable_name: ENV_ALIEN_DEPLOYMENT_TYPE.to_string(),
60            value: deployment_type.clone(),
61            reason: "Cannot parse the ALIEN_DEPLOYMENT_TYPE environment variable".to_string(),
62        })
63    })
64}
65
66/// Parse ALIEN_BINDINGS_MODE from environment variables.
67/// Defaults to Direct if not specified.
68pub fn get_bindings_mode_from_env(
69    env: &std::collections::HashMap<String, String>,
70) -> Result<BindingsMode> {
71    let mode_str = env
72        .get(ENV_ALIEN_BINDINGS_MODE)
73        .map(|s| s.as_str())
74        .unwrap_or("direct");
75
76    mode_str.parse().map_err(|reason: String| {
77        AlienError::new(ErrorData::InvalidEnvironmentVariable {
78            variable_name: ENV_ALIEN_BINDINGS_MODE.to_string(),
79            value: mode_str.to_string(),
80            reason,
81        })
82    })
83}