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 replace_after_failed_create_is_safe(&self) -> bool {
59        // A create adopts an existing repository with the same name, and deleting it deletes every
60        // image in it, including images this deployment never pushed.
61        false
62    }
63
64    fn get_dependencies(&self) -> Vec<ResourceRef> {
65        Vec::new()
66    }
67
68    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
69        // Downcast to ArtifactRegistry type to use the existing validate_update method
70        let new_registry = new_config
71            .as_any()
72            .downcast_ref::<ArtifactRegistry>()
73            .ok_or_else(|| {
74                AlienError::new(ErrorData::UnexpectedResourceType {
75                    resource_id: self.id.clone(),
76                    expected: Self::RESOURCE_TYPE,
77                    actual: new_config.get_resource_type(),
78                })
79            })?;
80
81        if self.id != new_registry.id {
82            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
83                resource_id: self.id.clone(),
84                reason: "the 'id' field is immutable".to_string(),
85            }));
86        }
87        Ok(())
88    }
89
90    fn as_any(&self) -> &dyn Any {
91        self
92    }
93
94    fn as_any_mut(&mut self) -> &mut dyn Any {
95        self
96    }
97
98    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
99        Box::new(self.clone())
100    }
101
102    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
103        other.as_any().downcast_ref::<ArtifactRegistry>() == Some(self)
104    }
105
106    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
107        serde_json::to_value(self)
108    }
109}
110
111/// Outputs generated by a successfully provisioned ArtifactRegistry.
112#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
113#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
114#[serde(rename_all = "camelCase")]
115pub struct ArtifactRegistryOutputs {
116    /// The platform-specific registry identifier.
117    /// - AWS: Account and region (e.g., "123456789012:us-west-2")
118    /// - GCP: Full registry name (e.g., "projects/my-project/locations/us-central1")
119    /// - Azure: Registry resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ContainerRegistry/registries/myregistry")
120    pub registry_id: String,
121
122    /// The registry endpoint for docker operations.
123    /// - AWS: ECR registry URL (e.g., "123456789012.dkr.ecr.us-west-2.amazonaws.com")
124    /// - GCP: Artifact Registry URL (e.g., "us-central1-docker.pkg.dev/my-project")
125    /// - Azure: Container registry login server (e.g., "myregistry.azurecr.io")
126    pub registry_endpoint: String,
127
128    /// Role/principal identifier for pull-only access.
129    /// - AWS: IAM role ARN (e.g., "arn:aws:iam::123456789012:role/my-stack-my-registry-pull")
130    /// - GCP: Service account email (e.g., "my-stack-my-registry-pull@my-project.iam.gserviceaccount.com")
131    /// - Azure: Managed identity resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-registry-pull")
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub pull_role: Option<String>,
134
135    /// Role/principal identifier for push and pull access.
136    /// - AWS: IAM role ARN (e.g., "arn:aws:iam::123456789012:role/my-stack-my-registry-push")
137    /// - GCP: Service account email (e.g., "my-stack-my-registry-push@my-project.iam.gserviceaccount.com")
138    /// - Azure: Managed identity resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-registry-push")
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub push_role: Option<String>,
141}
142
143impl ResourceOutputsDefinition for ArtifactRegistryOutputs {
144    fn get_resource_type(&self) -> ResourceType {
145        ArtifactRegistry::RESOURCE_TYPE.clone()
146    }
147
148    fn as_any(&self) -> &dyn Any {
149        self
150    }
151
152    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
153        Box::new(self.clone())
154    }
155
156    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
157        other.as_any().downcast_ref::<ArtifactRegistryOutputs>() == Some(self)
158    }
159
160    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
161        serde_json::to_value(self)
162    }
163}
164
165#[cfg(test)]
166mod tests {
167    use super::*;
168
169    #[test]
170    fn test_artifact_registry_creation() {
171        let registry = ArtifactRegistry::new("my-registry".to_string()).build();
172        assert_eq!(registry.id, "my-registry");
173    }
174
175    #[test]
176    fn test_artifact_registry_dependencies() {
177        let registry = ArtifactRegistry::new("my-registry".to_string()).build();
178        let dependencies = registry.get_dependencies();
179        assert!(dependencies.is_empty());
180    }
181}