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
30impl ArtifactRegistry {
31    /// The resource type identifier for ArtifactRegistry
32    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("artifact-registry");
33
34    /// Returns the artifact registry's unique identifier.
35    pub fn id(&self) -> &str {
36        &self.id
37    }
38}
39
40// Implementation of ResourceDefinition trait for ArtifactRegistry
41#[typetag::serde(name = "artifact-registry")]
42impl ResourceDefinition for ArtifactRegistry {
43    fn resource_type() -> ResourceType {
44        Self::RESOURCE_TYPE.clone()
45    }
46
47    fn get_resource_type(&self) -> ResourceType {
48        Self::resource_type()
49    }
50
51    fn id(&self) -> &str {
52        &self.id
53    }
54
55    fn get_dependencies(&self) -> Vec<ResourceRef> {
56        Vec::new()
57    }
58
59    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
60        // Downcast to ArtifactRegistry type to use the existing validate_update method
61        let new_registry = new_config
62            .as_any()
63            .downcast_ref::<ArtifactRegistry>()
64            .ok_or_else(|| {
65                AlienError::new(ErrorData::UnexpectedResourceType {
66                    resource_id: self.id.clone(),
67                    expected: Self::RESOURCE_TYPE,
68                    actual: new_config.get_resource_type(),
69                })
70            })?;
71
72        if self.id != new_registry.id {
73            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
74                resource_id: self.id.clone(),
75                reason: "the 'id' field is immutable".to_string(),
76            }));
77        }
78        Ok(())
79    }
80
81    fn as_any(&self) -> &dyn Any {
82        self
83    }
84
85    fn as_any_mut(&mut self) -> &mut dyn Any {
86        self
87    }
88
89    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
90        Box::new(self.clone())
91    }
92
93    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
94        other.as_any().downcast_ref::<ArtifactRegistry>() == Some(self)
95    }
96}
97
98/// Outputs generated by a successfully provisioned ArtifactRegistry.
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
101#[serde(rename_all = "camelCase")]
102pub struct ArtifactRegistryOutputs {
103    /// The platform-specific registry identifier.
104    /// - AWS: Account and region (e.g., "123456789012:us-west-2")
105    /// - GCP: Full registry name (e.g., "projects/my-project/locations/us-central1")
106    /// - Azure: Registry resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ContainerRegistry/registries/myregistry")
107    pub registry_id: String,
108
109    /// The registry endpoint for docker operations.
110    /// - AWS: ECR registry URL (e.g., "123456789012.dkr.ecr.us-west-2.amazonaws.com")
111    /// - GCP: Artifact Registry URL (e.g., "us-central1-docker.pkg.dev/my-project")
112    /// - Azure: Container registry login server (e.g., "myregistry.azurecr.io")
113    pub registry_endpoint: String,
114
115    /// Role/principal identifier for pull-only access.
116    /// - AWS: IAM role ARN (e.g., "arn:aws:iam::123456789012:role/my-stack-my-registry-pull")
117    /// - GCP: Service account email (e.g., "my-stack-my-registry-pull@my-project.iam.gserviceaccount.com")
118    /// - Azure: Managed identity resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-registry-pull")
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub pull_role: Option<String>,
121
122    /// Role/principal identifier for push and pull access.
123    /// - AWS: IAM role ARN (e.g., "arn:aws:iam::123456789012:role/my-stack-my-registry-push")
124    /// - GCP: Service account email (e.g., "my-stack-my-registry-push@my-project.iam.gserviceaccount.com")
125    /// - Azure: Managed identity resource ID (e.g., "/subscriptions/.../resourceGroups/.../providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-registry-push")
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub push_role: Option<String>,
128}
129
130#[typetag::serde(name = "artifact-registry")]
131impl ResourceOutputsDefinition for ArtifactRegistryOutputs {
132    fn resource_type() -> ResourceType {
133        ArtifactRegistry::RESOURCE_TYPE.clone()
134    }
135
136    fn as_any(&self) -> &dyn Any {
137        self
138    }
139
140    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
141        Box::new(self.clone())
142    }
143
144    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
145        other.as_any().downcast_ref::<ArtifactRegistryOutputs>() == Some(self)
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn test_artifact_registry_creation() {
155        let registry = ArtifactRegistry::new("my-registry".to_string()).build();
156        assert_eq!(registry.id, "my-registry");
157    }
158
159    #[test]
160    fn test_artifact_registry_dependencies() {
161        let registry = ArtifactRegistry::new("my-registry".to_string()).build();
162        let dependencies = registry.get_dependencies();
163        assert!(dependencies.is_empty());
164    }
165}