Skip to main content

alien_core/resources/
artifact_registry.rs

1use crate::error::{ErrorData, Result};
2use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
3use alien_error::AlienError;
4use bon::Builder;
5use serde::{Deserialize, Serialize};
6use std::any::Any;
7use std::fmt::Debug;
8
9/// Represents an artifact registry for storing container images and other build artifacts.
10/// This is a high-level wrapper resource that provides a cloud-agnostic interface over
11/// AWS ECR, GCP Artifact Registry, and Azure Container Registry.
12///
13/// # Platform Mapping
14/// - **AWS**: Implicitly exists as the AWS account and region
15/// - **GCP**: Explicitly configured per project and location (Artifact Registry API enabled)
16/// - **Azure**: Explicitly provisioned Azure Container Registry instance
17///
18/// The actual repository management and permissions are handled through the bindings API.
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
20#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
21#[serde(rename_all = "camelCase", deny_unknown_fields)]
22#[builder(start_fn = new)]
23pub struct ArtifactRegistry {
24    /// Identifier for the artifact registry. Must contain only alphanumeric characters, hyphens, and underscores ([A-Za-z0-9-_]).
25    /// Maximum 64 characters.
26    #[builder(start_fn)]
27    pub id: String,
28
29    /// AWS-only: regions to replicate container images to.
30    /// ECR private image replication ensures images pushed in the registry's home region
31    /// are automatically available in these additional regions (required when Lambda or
32    /// other compute runs in a different region from the registry).
33    #[serde(default, skip_serializing_if = "Vec::is_empty")]
34    #[builder(default)]
35    pub replication_regions: Vec<String>,
36}
37
38impl ArtifactRegistry {
39    /// The resource type identifier for ArtifactRegistry
40    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("artifact-registry");
41
42    /// Returns the artifact registry's unique identifier.
43    pub fn id(&self) -> &str {
44        &self.id
45    }
46}
47
48// Implementation of ResourceDefinition trait for ArtifactRegistry
49impl ResourceDefinition for ArtifactRegistry {
50    fn get_resource_type(&self) -> ResourceType {
51        Self::RESOURCE_TYPE
52    }
53
54    fn id(&self) -> &str {
55        &self.id
56    }
57
58    fn get_dependencies(&self) -> Vec<ResourceRef> {
59        Vec::new()
60    }
61
62    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
63        // Downcast to ArtifactRegistry type to use the existing validate_update method
64        let new_registry = new_config
65            .as_any()
66            .downcast_ref::<ArtifactRegistry>()
67            .ok_or_else(|| {
68                AlienError::new(ErrorData::UnexpectedResourceType {
69                    resource_id: self.id.clone(),
70                    expected: Self::RESOURCE_TYPE,
71                    actual: new_config.get_resource_type(),
72                })
73            })?;
74
75        if self.id != new_registry.id {
76            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
77                resource_id: self.id.clone(),
78                reason: "the 'id' field is immutable".to_string(),
79            }));
80        }
81        Ok(())
82    }
83
84    fn as_any(&self) -> &dyn Any {
85        self
86    }
87
88    fn as_any_mut(&mut self) -> &mut dyn Any {
89        self
90    }
91
92    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
93        Box::new(self.clone())
94    }
95
96    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
97        other.as_any().downcast_ref::<ArtifactRegistry>() == Some(self)
98    }
99
100    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
101        serde_json::to_value(self)
102    }
103}
104
105/// Outputs generated by a successfully provisioned ArtifactRegistry.
106#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
107#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
108#[serde(rename_all = "camelCase")]
109pub struct ArtifactRegistryOutputs {
110    /// The platform-specific registry identifier.
111    /// - AWS: Account and region (e.g., "123456789012:us-west-2")
112    /// - GCP: Full registry name (e.g., "projects/my-project/locations/us-central1")
113    /// - Azure: Registry resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ContainerRegistry/registries/myregistry")
114    pub registry_id: String,
115
116    /// The registry endpoint for docker operations.
117    /// - AWS: ECR registry URL (e.g., "123456789012.dkr.ecr.us-west-2.amazonaws.com")
118    /// - GCP: Artifact Registry URL (e.g., "us-central1-docker.pkg.dev/my-project")
119    /// - Azure: Container registry login server (e.g., "myregistry.azurecr.io")
120    pub registry_endpoint: String,
121
122    /// Role/principal identifier for pull-only access.
123    /// - AWS: IAM role ARN (e.g., "arn:aws:iam::123456789012:role/my-stack-my-registry-pull")
124    /// - GCP: Service account email (e.g., "my-stack-my-registry-pull@my-project.iam.gserviceaccount.com")
125    /// - Azure: Managed identity resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-registry-pull")
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub pull_role: Option<String>,
128
129    /// Role/principal identifier for push and pull access.
130    /// - AWS: IAM role ARN (e.g., "arn:aws:iam::123456789012:role/my-stack-my-registry-push")
131    /// - GCP: Service account email (e.g., "my-stack-my-registry-push@my-project.iam.gserviceaccount.com")
132    /// - Azure: Managed identity resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-registry-push")
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub push_role: Option<String>,
135}
136
137impl ResourceOutputsDefinition for ArtifactRegistryOutputs {
138    fn get_resource_type(&self) -> ResourceType {
139        ArtifactRegistry::RESOURCE_TYPE.clone()
140    }
141
142    fn as_any(&self) -> &dyn Any {
143        self
144    }
145
146    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
147        Box::new(self.clone())
148    }
149
150    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
151        other.as_any().downcast_ref::<ArtifactRegistryOutputs>() == Some(self)
152    }
153
154    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
155        serde_json::to_value(self)
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_artifact_registry_creation() {
165        let registry = ArtifactRegistry::new("my-registry".to_string()).build();
166        assert_eq!(registry.id, "my-registry");
167    }
168
169    #[test]
170    fn test_artifact_registry_dependencies() {
171        let registry = ArtifactRegistry::new("my-registry".to_string()).build();
172        let dependencies = registry.get_dependencies();
173        assert!(dependencies.is_empty());
174    }
175}