alien_bindings/traits.rs
1use crate::error::Result;
2use crate::presigned::PresignedRequest;
3use alien_core::{BuildConfig, BuildExecution};
4use async_trait::async_trait;
5use object_store::path::Path;
6use object_store::ObjectStore;
7use serde::{Deserialize, Serialize};
8use std::collections::BTreeMap;
9use std::sync::Arc;
10use std::time::Duration;
11use url::Url;
12
13#[cfg(feature = "openapi")]
14use utoipa::ToSchema;
15
16/// Marker trait for all binding types.
17pub trait Binding: Send + Sync + std::fmt::Debug {}
18
19/// A storage binding that provides object store capabilities.
20#[async_trait]
21pub trait Storage: Binding + ObjectStore {
22 /// Gets the base directory path configured for this storage binding.
23 fn get_base_dir(&self) -> Path;
24 /// Gets the underlying URL configured for this storage binding.
25 fn get_url(&self) -> Url;
26
27 /// Creates a presigned request for uploading data to the specified path.
28 /// The request can be serialized, stored, and executed later.
29 async fn presigned_put(&self, path: &Path, expires_in: Duration) -> Result<PresignedRequest>;
30
31 /// Creates a presigned request for downloading data from the specified path.
32 /// The request can be serialized, stored, and executed later.
33 async fn presigned_get(&self, path: &Path, expires_in: Duration) -> Result<PresignedRequest>;
34
35 /// Creates a presigned request for deleting the object at the specified path.
36 /// The request can be serialized, stored, and executed later.
37 async fn presigned_delete(&self, path: &Path, expires_in: Duration)
38 -> Result<PresignedRequest>;
39}
40
41/// A build binding that provides build execution capabilities.
42#[async_trait]
43pub trait Build: Binding {
44 /// Starts a new build with the given configuration.
45 /// Returns the build execution information.
46 async fn start_build(&self, config: BuildConfig) -> Result<BuildExecution>;
47
48 /// Gets the status of a specific build execution.
49 async fn get_build_status(&self, build_id: &str) -> Result<BuildExecution>;
50
51 /// Stops or cancels a running build.
52 async fn stop_build(&self, build_id: &str) -> Result<()>;
53}
54
55/// AWS IAM Role service account information
56#[derive(Debug, Clone, Serialize, Deserialize)]
57#[serde(rename_all = "camelCase")]
58#[cfg_attr(feature = "openapi", derive(ToSchema))]
59pub struct AwsServiceAccountInfo {
60 /// The IAM role name
61 pub role_name: String,
62 /// The IAM role ARN (for AssumeRole)
63 pub role_arn: String,
64}
65
66/// GCP Service Account information
67#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(rename_all = "camelCase")]
69#[cfg_attr(feature = "openapi", derive(ToSchema))]
70pub struct GcpServiceAccountInfo {
71 /// The service account email (for impersonation)
72 pub email: String,
73 /// The service account unique ID
74 pub unique_id: String,
75}
76
77/// Azure User-Assigned Managed Identity information
78#[derive(Debug, Clone, Serialize, Deserialize)]
79#[serde(rename_all = "camelCase")]
80#[cfg_attr(feature = "openapi", derive(ToSchema))]
81pub struct AzureServiceAccountInfo {
82 /// The managed identity client ID (for authentication)
83 pub client_id: String,
84 /// The managed identity resource ID (ARM ID)
85 pub resource_id: String,
86 /// The managed identity principal ID
87 pub principal_id: String,
88}
89
90/// Platform-specific service account information
91#[derive(Debug, Clone, Serialize, Deserialize)]
92#[serde(tag = "platform", rename_all = "camelCase")]
93#[cfg_attr(feature = "openapi", derive(ToSchema))]
94pub enum ServiceAccountInfo {
95 /// AWS IAM Role
96 Aws(AwsServiceAccountInfo),
97 /// GCP Service Account
98 Gcp(GcpServiceAccountInfo),
99 /// Azure User-Assigned Managed Identity
100 Azure(AzureServiceAccountInfo),
101}
102
103/// Configuration for impersonation
104#[derive(Debug, Clone)]
105pub struct ImpersonationRequest {
106 /// Optional session name (AWS only)
107 pub session_name: Option<String>,
108 /// Optional session duration in seconds
109 pub duration_seconds: Option<i32>,
110 /// Optional scopes (GCP only)
111 pub scopes: Option<Vec<String>>,
112}
113
114impl Default for ImpersonationRequest {
115 fn default() -> Self {
116 Self {
117 session_name: None,
118 duration_seconds: Some(3600), // 1 hour default
119 scopes: None,
120 }
121 }
122}
123
124/// A service account binding that provides identity and impersonation capabilities.
125#[async_trait]
126pub trait ServiceAccount: Binding {
127 /// Gets information about the service account
128 async fn get_info(&self) -> Result<ServiceAccountInfo>;
129
130 /// Impersonates the service account and returns credentials as a ClientConfig.
131 ///
132 /// This performs the cloud-specific impersonation:
133 /// - AWS: STS AssumeRole to get temporary credentials
134 /// - GCP: IAM Credentials API generateAccessToken
135 /// - Azure: Uses the attached managed identity (no API call needed)
136 async fn impersonate(&self, request: ImpersonationRequest) -> Result<alien_core::ClientConfig>;
137
138 /// Helper for downcasting trait object
139 fn as_any(&self) -> &dyn std::any::Any;
140}
141
142/// Response from repository operations.
143#[derive(Debug, Clone, Serialize, Deserialize)]
144#[serde(rename_all = "camelCase")]
145#[cfg_attr(feature = "openapi", derive(ToSchema))]
146pub struct RepositoryResponse {
147 /// The **routable name** of the repository — the full, platform-specific
148 /// path used for subsequent calls (`get_repository`, `delete_repository`,
149 /// `generate_credentials`, `*_cross_account_access`).
150 ///
151 /// Per-platform format (matches
152 /// `alien.dev/content/docs/infrastructure/artifact-registry/behavior.mdx`):
153 ///
154 /// | Platform | Format |
155 /// |---|---|
156 /// | AWS (ECR) | `{registry_prefix}-{logical}` (e.g. `alien-artifacts-my-app`) |
157 /// | GCP (GAR) | `{project_id}/{gar_repo}/{logical}` |
158 /// | Azure (ACR) | `{logical}` (used directly) |
159 /// | Local | `{binding_name}/{logical}` |
160 ///
161 /// **Round-trip invariant:** callers MUST be able to pass this value back
162 /// to any other method on the trait without further transformation.
163 /// Implementations MUST NOT re-apply prefixing in receivers — assume
164 /// `repo_id` arguments are already routable.
165 pub name: String,
166 /// Repository URI for pushing/pulling images. None if repository is not ready yet.
167 pub uri: Option<String>,
168 /// Optional creation timestamp in ISO8601 format.
169 pub created_at: Option<String>,
170}
171
172/// Permissions level for artifact registry access.
173#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
174#[serde(rename_all = "kebab-case")]
175#[cfg_attr(feature = "openapi", derive(ToSchema))]
176pub enum ArtifactRegistryPermissions {
177 /// Pull-only access (download artifacts).
178 Pull,
179 /// Push and pull access (upload and download artifacts).
180 PushPull,
181}
182
183/// How the registry expects credentials to be presented.
184#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
185#[serde(rename_all = "lowercase")]
186#[cfg_attr(feature = "openapi", derive(ToSchema))]
187pub enum RegistryAuthMethod {
188 /// HTTP Basic auth (username:password). Used by ECR, GAR, Local.
189 Basic,
190 /// HTTP Bearer token. Used by ACR (Azure).
191 Bearer,
192}
193
194/// Credentials for accessing a repository.
195#[derive(Debug, Clone, Serialize, Deserialize)]
196#[serde(rename_all = "camelCase")]
197#[cfg_attr(feature = "openapi", derive(ToSchema))]
198pub struct ArtifactRegistryCredentials {
199 /// How to present these credentials to the registry.
200 pub auth_method: RegistryAuthMethod,
201 /// Username for authentication (empty for Bearer auth).
202 pub username: String,
203 /// Password or token for authentication.
204 pub password: String,
205 /// Optional expiration time in ISO8601 format.
206 pub expires_at: Option<String>,
207}
208
209/// Types of compute services that can access artifact registries.
210#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
211#[serde(rename_all = "kebab-case")]
212#[cfg_attr(feature = "openapi", derive(ToSchema))]
213pub enum ComputeServiceType {
214 /// Serverless functions
215 Worker,
216 // In the future, we could add Container, VirtualMachine, Kubernetes, etc.
217}
218
219/// Cross-account access configuration for AWS artifact registries.
220#[derive(Debug, Clone, Serialize, Deserialize)]
221#[serde(rename_all = "camelCase")]
222#[cfg_attr(feature = "openapi", derive(ToSchema))]
223pub struct AwsCrossAccountAccess {
224 /// AWS account IDs that should have cross-account access.
225 pub account_ids: Vec<String>,
226 /// AWS regions where the target Lambda functions run.
227 /// Used to construct `aws:sourceArn` patterns for the Lambda service principal condition.
228 pub regions: Vec<String>,
229 /// Types of compute services that should have access.
230 pub allowed_service_types: Vec<ComputeServiceType>,
231 /// Specific IAM role ARNs to grant access to.
232 /// These are typically deployment/management roles or service-specific roles.
233 pub role_arns: Vec<String>,
234}
235
236/// Cross-account access configuration for GCP artifact registries.
237#[derive(Debug, Clone, Serialize, Deserialize)]
238#[serde(rename_all = "camelCase")]
239#[cfg_attr(feature = "openapi", derive(ToSchema))]
240pub struct GcpCrossAccountAccess {
241 /// GCP project numbers that should have access.
242 pub project_numbers: Vec<String>,
243 /// Types of compute services that should have access.
244 pub allowed_service_types: Vec<ComputeServiceType>,
245 /// Additional service account emails to grant access to.
246 /// These are typically deployment/management service accounts.
247 pub service_account_emails: Vec<String>,
248}
249
250/// Platform-specific cross-account access configuration.
251#[derive(Debug, Clone, Serialize, Deserialize)]
252#[serde(tag = "platform", rename_all = "lowercase")]
253#[cfg_attr(feature = "openapi", derive(ToSchema))]
254pub enum CrossAccountAccess {
255 /// AWS-specific cross-account access configuration.
256 Aws(AwsCrossAccountAccess),
257 /// GCP-specific cross-account access configuration.
258 Gcp(GcpCrossAccountAccess),
259}
260
261/// Current cross-account access permissions for a repository.
262#[derive(Debug, Clone, Serialize, Deserialize)]
263#[serde(rename_all = "camelCase")]
264#[cfg_attr(feature = "openapi", derive(ToSchema))]
265pub struct CrossAccountPermissions {
266 /// Platform-specific access configuration currently applied.
267 pub access: CrossAccountAccess,
268 /// Timestamp when permissions were last updated.
269 pub last_updated: Option<String>,
270}
271
272/// A trait for artifact registry bindings that provide container image repository management.
273#[async_trait]
274pub trait ArtifactRegistry: Binding {
275 /// Returns the raw registry endpoint URL (e.g., "https://123456.dkr.ecr.us-east-1.amazonaws.com"
276 /// or "http://localhost:5000"). Used by the push proxy to forward requests transparently.
277 ///
278 /// Default returns empty string — cloud provider implementations should override.
279 fn registry_endpoint(&self) -> String {
280 String::new()
281 }
282
283 /// Returns the OCI repository path prefix used for upstream operations.
284 ///
285 /// This identifier serves two related roles, both pointing at the same
286 /// upstream location:
287 ///
288 /// 1. **Proxy routing.** When the push proxy forwards push/pull requests
289 /// to the upstream registry, this prefix is prepended to the image
290 /// name portion of the OCI path.
291 /// 2. **Shared deployment-image repository name.** `alien release` pushes
292 /// every function image as `{prefix}:{logical}-{hash}` into one shared
293 /// repository whose routable name is exactly this prefix. Pass it as
294 /// `repo_id` when calling `add_cross_account_access` /
295 /// `remove_cross_account_access` for the deployment cross-account
296 /// flow.
297 ///
298 /// Examples:
299 /// - ECR: `"alien-e2e"` — flat repo prefix; also the routable repo name
300 /// for the shared deployment-image repository
301 /// - GAR: `"my-project/alien-e2e"` — project/repo structure
302 /// - ACR: `"alien-e2e"` — images pushed into this repository prefix;
303 /// principal pull access is granted on the parent registry
304 /// - Local: `"artifacts"` or similar — cross-account not supported
305 ///
306 /// An empty return value indicates the platform has no shared
307 /// deployment-image repo at the binding level.
308 ///
309 /// Default returns empty string.
310 fn upstream_repository_prefix(&self) -> String {
311 String::new()
312 }
313
314 /// Creates a repository within the artifact registry.
315 ///
316 /// `repo_name` is the **logical** identifier the caller chose (e.g.
317 /// `"my-app"`). The implementation transforms it to the routable
318 /// platform-specific form before calling any backend API; what's
319 /// returned in [`RepositoryResponse::name`] is the routable form.
320 ///
321 /// On platforms where image paths are implicit (GAR, ACR, Local),
322 /// this may not call any backend API — but it still returns a valid
323 /// routable name.
324 async fn create_repository(&self, repo_name: &str) -> Result<RepositoryResponse>;
325
326 /// Gets repository details. `repo_id` is the routable name returned by
327 /// [`Self::create_repository`]; implementations MUST NOT re-apply
328 /// prefixing.
329 async fn get_repository(&self, repo_id: &str) -> Result<RepositoryResponse>;
330
331 /// Adds cross-account access permissions for a repository.
332 /// This adds the specified permissions to any existing cross-account permissions.
333 ///
334 /// `repo_id` is the routable name from [`Self::create_repository`].
335 ///
336 /// For AWS: grants access to specified account IDs with configurable principals and compute service types (ECR repository policy).
337 /// For GCP: grants access to serverless robots and service accounts on the parent GAR registry (image-path-level IAM is not supported).
338 /// For Azure: not supported — returns `OperationNotSupported`.
339 async fn add_cross_account_access(
340 &self,
341 repo_id: &str,
342 access: CrossAccountAccess,
343 ) -> Result<()>;
344
345 /// Removes cross-account access permissions for a repository.
346 ///
347 /// `repo_id` is the routable name from [`Self::create_repository`].
348 ///
349 /// For AWS: removes access from the ECR repository policy.
350 /// For GCP: removes IAM bindings on the parent GAR registry.
351 /// For Azure: not supported — returns `OperationNotSupported`.
352 async fn remove_cross_account_access(
353 &self,
354 repo_id: &str,
355 access: CrossAccountAccess,
356 ) -> Result<()>;
357
358 /// Gets the current cross-account access permissions for a repository.
359 ///
360 /// `repo_id` is the routable name from [`Self::create_repository`].
361 /// For Azure: not supported — returns `OperationNotSupported`.
362 async fn get_cross_account_access(&self, repo_id: &str) -> Result<CrossAccountPermissions>;
363
364 /// Generates credentials for accessing a repository with the specified
365 /// permissions.
366 ///
367 /// `repo_id` is the routable name from [`Self::create_repository`].
368 ///
369 /// Most platforms produce registry-scoped (not repo-scoped) credentials,
370 /// so `repo_id` typically only affects logging — not the credentials
371 /// themselves.
372 async fn generate_credentials(
373 &self,
374 repo_id: &str,
375 permissions: ArtifactRegistryPermissions,
376 ttl_seconds: Option<u32>,
377 ) -> Result<ArtifactRegistryCredentials>;
378
379 /// Deletes a repository and all contained images.
380 ///
381 /// `repo_id` is the routable name from [`Self::create_repository`].
382 /// Implementations MUST NOT delete the *parent* registry (which is owned
383 /// by `alien-infra`); on platforms with implicit image paths (GAR, ACR,
384 /// Local) this is a no-op.
385 async fn delete_repository(&self, repo_id: &str) -> Result<()>;
386}
387
388/// A trait for vault bindings that provide secure secret management.
389#[async_trait]
390pub trait Vault: Binding {
391 /// Gets a secret value by name.
392 async fn get_secret(&self, secret_name: &str) -> Result<String>;
393
394 /// Sets a secret value, creating it if it doesn't exist or updating it if it does.
395 async fn set_secret(&self, secret_name: &str, value: &str) -> Result<()>;
396
397 /// Deletes a secret by name.
398 async fn delete_secret(&self, secret_name: &str) -> Result<()>;
399}
400
401/// Represents options for put operations in KV stores.
402#[derive(Debug, Clone, Default)]
403pub struct PutOptions {
404 /// Optional TTL for automatic expiration (soft hint - items MAY be deleted after expiry)
405 pub ttl: Option<Duration>,
406 /// Only put if the key does not exist
407 pub if_not_exists: bool,
408}
409
410/// Represents the result of a scan operation.
411#[derive(Debug)]
412pub struct ScanResult {
413 /// Key-value pairs found (may be ≤ limit, no guarantee to fill)
414 pub items: Vec<(String, Vec<u8>)>,
415 /// Opaque cursor for pagination. None if no more results.
416 /// **Warning**: Cursor may become invalid if data changes. No TTL guarantees.
417 pub next_cursor: Option<String>,
418}
419
420/// A trait for key-value store bindings that provide minimal, platform-agnostic KV operations.
421/// This API is designed to work consistently across DynamoDB, Firestore, Redis, and Azure Table Storage.
422#[async_trait]
423pub trait Kv: Binding {
424 /// Get a value by key. Returns None if key doesn't exist or has expired.
425 ///
426 /// **TTL Behavior**: TTL is a soft hint for automatic cleanup. If `now >= expires_at`,
427 /// implementations SHOULD behave as if the key is absent, even if the item still exists
428 /// physically in the backend. Physical deletion is eventual and not guaranteed.
429 ///
430 /// **Validation**: Keys are validated against MAX_KEY_BYTES and portable charset.
431 /// Invalid keys return `KvError::InvalidKey` immediately.
432 async fn get(&self, key: &str) -> Result<Option<Vec<u8>>>;
433
434 /// Put a value with optional options. When options.if_not_exists is true, returns true if created,
435 /// false if already exists. When options.if_not_exists is false or options is None, always returns true.
436 ///
437 /// **Size Limits**:
438 /// - Keys: ≤ MAX_KEY_BYTES (512 bytes) with portable ASCII charset
439 /// - Values: ≤ MAX_VALUE_BYTES (24,576 bytes = 24 KiB)
440 ///
441 /// **Validation**: Size and charset constraints are enforced before backend calls.
442 /// Invalid inputs return `KvError::InvalidKey` or `KvError::InvalidValue` immediately.
443 ///
444 /// **TTL Behavior**: TTL is a soft hint for automatic cleanup. If TTL is specified,
445 /// item expires at `put_time + ttl`. Expired items SHOULD appear absent on subsequent
446 /// reads, but physical deletion is eventual and not guaranteed.
447 ///
448 /// **Conditional Logic**: The if_not_exists operation maps to backend primitives:
449 /// - Redis: SETNX
450 /// - DynamoDB: PutItem with condition_expression="attribute_not_exists(pk)"
451 /// - Firestore: create() with Precondition::DoesNotExist
452 /// - Azure Table Storage: InsertEntity (409 on conflict)
453 async fn put(&self, key: &str, value: Vec<u8>, options: Option<PutOptions>) -> Result<bool>;
454
455 /// Delete a key. No error if key doesn't exist.
456 ///
457 /// **Validation**: Keys are validated against MAX_KEY_BYTES and portable charset.
458 /// Invalid keys return `KvError::InvalidKey` immediately.
459 async fn delete(&self, key: &str) -> Result<()>;
460
461 /// Check if a key exists without retrieving the value.
462 ///
463 /// **TTL Behavior**: TTL is a soft hint for automatic cleanup. If `now >= expires_at`,
464 /// SHOULD return false even if physically present. Physical deletion is eventual and not guaranteed.
465 ///
466 /// **Validation**: Keys are validated against MAX_KEY_BYTES and portable charset.
467 /// Invalid keys return `KvError::InvalidKey` immediately.
468 async fn exists(&self, key: &str) -> Result<bool>;
469
470 /// Scan keys with a prefix, with pagination support.
471 ///
472 /// **Scan Contract**:
473 /// - Returns an **arbitrary, unordered subset** in backend-natural order
474 /// - **No ordering guarantees** across backends (Redis SCAN, Azure fan-out, etc.)
475 /// - **May return ≤ limit items** (not guaranteed to fill even if more data exists)
476 /// - **Clients MUST de-duplicate** keys across pages (backends may return duplicates)
477 /// - **No completeness guarantee** under concurrent writes (may miss or duplicate)
478 ///
479 /// **Cursor Behavior**:
480 /// - Opaque string, implementation-specific format
481 /// - **May become invalid** anytime after backend state changes
482 /// - **No TTL guarantees** - can expire without notice
483 /// - Passing invalid cursor should return error, not partial results
484 ///
485 /// **TTL Behavior**: TTL is a soft hint for automatic cleanup. Expired items SHOULD
486 /// be filtered out from results, but physical deletion is eventual and not guaranteed.
487 ///
488 /// **Validation**: Prefix follows same key validation rules.
489 /// Invalid prefix returns `KvError::InvalidKey` immediately.
490 async fn scan_prefix(
491 &self,
492 prefix: &str,
493 limit: Option<usize>,
494 cursor: Option<String>,
495 ) -> Result<ScanResult>;
496}
497
498/// JSON/Text message payload for Queue
499#[derive(Debug, Clone, Serialize, Deserialize)]
500#[serde(tag = "type", rename_all = "lowercase")]
501#[cfg_attr(feature = "openapi", derive(ToSchema))]
502pub enum MessagePayload {
503 /// JSON-serializable value
504 Json(serde_json::Value),
505 /// UTF-8 text payload
506 Text(String),
507}
508
509/// A queue message with payload and receipt handle for acknowledgment
510#[derive(Debug, Clone, Serialize, Deserialize)]
511#[serde(rename_all = "camelCase")]
512#[cfg_attr(feature = "openapi", derive(ToSchema))]
513pub struct QueueMessage {
514 /// JSON-first message payload
515 pub payload: MessagePayload,
516 /// Opaque receipt handle for acknowledgment (backend-specific, short-lived)
517 pub receipt_handle: String,
518}
519
520/// Maximum message size in bytes (64 KiB = 65,536 bytes)
521///
522/// This limit ensures compatibility across all queue backends:
523/// - **AWS SQS**: 256KB message limit (much higher, not constraining)
524/// - **Azure Service Bus**: 1MB message limit (much higher, not constraining)
525/// - **GCP Pub/Sub**: 10MB message limit (much higher, not constraining)
526///
527/// The 64KB limit provides:
528/// - Reasonable message sizes for most use cases
529/// - Fast network transfer and low latency
530/// - Consistent behavior across all cloud providers
531/// - Efficient memory usage during batch processing
532pub const MAX_MESSAGE_BYTES: usize = 65_536; // 64 KiB
533
534/// Maximum number of messages per receive call
535///
536/// This limit balances throughput with processing simplicity:
537/// - **AWS SQS**: Supports up to 10 messages per ReceiveMessage call
538/// - **Azure Service Bus**: Can receive multiple messages via prefetch/batching
539/// - **GCP Pub/Sub**: Supports configurable max_messages per Pull request
540///
541/// The 10-message limit ensures:
542/// - Portable batch sizes across all backends
543/// - Manageable memory usage
544/// - Reasonable processing latency per batch
545pub const MAX_BATCH_SIZE: usize = 10;
546
547/// Fixed lease duration in seconds
548///
549/// Messages are leased for exactly 30 seconds after delivery:
550/// - Long enough for most processing tasks
551/// - Short enough to enable fast retry on failures
552/// - Eliminates complexity of dynamic lease management
553/// - Consistent across all platforms
554pub const LEASE_SECONDS: u64 = 30;
555
556/// A trait for queue bindings providing minimal, portable queue operations.
557#[async_trait]
558pub trait Queue: Binding {
559 /// Send a message to the specified queue
560 async fn send(&self, queue: &str, message: MessagePayload) -> Result<()>;
561
562 /// Receive up to `max_messages` (1..=10) from the specified queue
563 async fn receive(&self, queue: &str, max_messages: usize) -> Result<Vec<QueueMessage>>;
564
565 /// Acknowledge a message using its receipt handle (idempotent)
566 async fn ack(&self, queue: &str, receipt_handle: &str) -> Result<()>;
567}
568
569/// Request for invoking a function directly
570#[derive(Debug, Clone, Serialize, Deserialize)]
571#[serde(rename_all = "camelCase")]
572#[cfg_attr(feature = "openapi", derive(ToSchema))]
573pub struct WorkerInvokeRequest {
574 /// Worker identifier (name, ARN, URL, etc.)
575 pub target_worker: String,
576 /// HTTP method
577 pub method: String,
578 /// Request path
579 pub path: String,
580 /// HTTP headers
581 pub headers: BTreeMap<String, String>,
582 /// Request body bytes
583 pub body: Vec<u8>,
584 /// Optional timeout for the invocation
585 pub timeout: Option<Duration>,
586}
587
588/// Response from worker invocation.
589#[derive(Debug, Clone, Serialize, Deserialize)]
590#[serde(rename_all = "camelCase")]
591#[cfg_attr(feature = "openapi", derive(ToSchema))]
592pub struct WorkerInvokeResponse {
593 /// HTTP status code
594 pub status: u16,
595 /// HTTP response headers
596 pub headers: BTreeMap<String, String>,
597 /// Response body bytes
598 pub body: Vec<u8>,
599}
600
601/// A trait for worker bindings that enable direct worker-to-worker calls.
602#[async_trait]
603pub trait Worker: Binding {
604 /// Invoke a worker with HTTP request data.
605 ///
606 /// This enables direct, low-latency worker-to-worker communication within
607 /// the same cloud environment, bypassing Commands for internal calls.
608 ///
609 /// Platform implementations:
610 /// - AWS: Uses InvokeWorker API directly
611 /// - GCP: Calls private service URL directly
612 /// - Azure: Calls private container app URL directly
613 /// - Kubernetes: HTTP call to internal service
614 async fn invoke(&self, request: WorkerInvokeRequest) -> Result<WorkerInvokeResponse>;
615
616 /// Get the public URL of the worker, if available.
617 ///
618 /// Returns the worker's public URL if it exists and is accessible.
619 /// This is useful for exposing public endpoints or getting URLs for
620 /// external integration.
621 ///
622 /// Platform implementations:
623 /// - AWS: Uses GetWorkerUrlConfig API or returns URL from binding
624 /// - GCP: Returns Cloud Run service URL or calls get_service API
625 /// - Azure: Returns Container App URL or calls get_container_app API
626 async fn get_worker_url(&self) -> Result<Option<String>>;
627
628 /// Get a reference to this object as `Any` for dynamic casting
629 fn as_any(&self) -> &dyn std::any::Any;
630}
631
632/// A trait for container bindings that enable container-to-container communication
633#[async_trait]
634pub trait Container: Binding {
635 /// Get the internal URL for container-to-container communication.
636 ///
637 /// This returns the internal service discovery URL that other containers
638 /// in the same network can use to communicate with this container.
639 ///
640 /// Platform implementations:
641 /// - Managed cloud (AWS/GCP/Azure): Returns internal DNS URL (e.g., "http://api.svc:8080")
642 /// - Local (Docker): Returns Docker network DNS URL (e.g., "http://api.svc:3000")
643 fn get_internal_url(&self) -> &str;
644
645 /// Get the public URL of the container, if available.
646 ///
647 /// Returns the container's public URL if it exists and is accessible
648 /// from outside the cluster/network.
649 ///
650 /// Platform implementations:
651 /// - Managed cloud: Returns load balancer URL if exposed publicly
652 /// - Local: Returns localhost URL with mapped port (e.g., "http://localhost:62844")
653 fn get_public_url(&self) -> Option<&str>;
654
655 /// Get the container name/ID.
656 fn get_container_name(&self) -> &str;
657
658 /// Get a reference to this object as `Any` for dynamic casting
659 fn as_any(&self) -> &dyn std::any::Any;
660}
661
662/// A provider must implement methods to load the various types of bindings
663/// based on environment variables or other configuration sources.
664#[async_trait]
665pub trait BindingsProviderApi: Send + Sync + std::fmt::Debug {
666 /// Given a binding identifier, builds a Storage implementation.
667 async fn load_storage(&self, binding_name: &str) -> Result<Arc<dyn Storage>>;
668
669 /// Given a binding identifier, builds a Build implementation.
670 async fn load_build(&self, binding_name: &str) -> Result<Arc<dyn Build>>;
671
672 /// Given a binding identifier, builds an ArtifactRegistry implementation.
673 async fn load_artifact_registry(&self, binding_name: &str)
674 -> Result<Arc<dyn ArtifactRegistry>>;
675
676 /// Given a binding identifier, builds a Vault implementation.
677 async fn load_vault(&self, binding_name: &str) -> Result<Arc<dyn Vault>>;
678
679 /// Given a binding identifier, builds a KV implementation.
680 async fn load_kv(&self, binding_name: &str) -> Result<Arc<dyn Kv>>;
681
682 /// Given a binding identifier, builds a Queue implementation.
683 async fn load_queue(&self, binding_name: &str) -> Result<Arc<dyn Queue>>;
684
685 /// Given a binding identifier, builds a Worker implementation.
686 async fn load_worker(&self, binding_name: &str) -> Result<Arc<dyn Worker>>;
687
688 /// Given a binding identifier, builds a Container implementation.
689 async fn load_container(&self, binding_name: &str) -> Result<Arc<dyn Container>>;
690
691 /// Given a binding identifier, builds a ServiceAccount implementation.
692 async fn load_service_account(&self, binding_name: &str) -> Result<Arc<dyn ServiceAccount>>;
693}