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