alien_core/resources/
artifact_registry.rs1use 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#[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 #[builder(start_fn)]
27 pub id: String,
28
29 #[serde(default, skip_serializing_if = "Vec::is_empty")]
34 #[builder(default)]
35 pub replication_regions: Vec<String>,
36
37 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub replication_regions_input: Option<String>,
43}
44
45impl ArtifactRegistry {
46 pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("artifact-registry");
48
49 pub fn validate_aws_replication(&self) -> Result<()> {
52 if !self.replication_regions.is_empty() {
53 tracing::warn!(resource_id = %self.id, "Per-deployment ECR replicationRegions is ignored; configure replication through the registry account's infrastructure. Existing account replication rules remain unchanged.");
54 }
55 Ok(())
56 }
57
58 pub fn id(&self) -> &str {
60 &self.id
61 }
62}
63
64impl ResourceDefinition for ArtifactRegistry {
66 fn get_resource_type(&self) -> ResourceType {
67 Self::RESOURCE_TYPE
68 }
69
70 fn id(&self) -> &str {
71 &self.id
72 }
73
74 fn replace_after_failed_create_is_safe(&self) -> bool {
75 false
78 }
79
80 fn get_dependencies(&self) -> Vec<ResourceRef> {
81 Vec::new()
82 }
83
84 fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
85 let new_registry = new_config
87 .as_any()
88 .downcast_ref::<ArtifactRegistry>()
89 .ok_or_else(|| {
90 AlienError::new(ErrorData::UnexpectedResourceType {
91 resource_id: self.id.clone(),
92 expected: Self::RESOURCE_TYPE,
93 actual: new_config.get_resource_type(),
94 })
95 })?;
96
97 if self.id != new_registry.id {
98 return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
99 resource_id: self.id.clone(),
100 reason: "the 'id' field is immutable".to_string(),
101 }));
102 }
103 Ok(())
104 }
105
106 fn as_any(&self) -> &dyn Any {
107 self
108 }
109
110 fn as_any_mut(&mut self) -> &mut dyn Any {
111 self
112 }
113
114 fn box_clone(&self) -> Box<dyn ResourceDefinition> {
115 Box::new(self.clone())
116 }
117
118 fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
119 other.as_any().downcast_ref::<ArtifactRegistry>() == Some(self)
120 }
121
122 fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
123 serde_json::to_value(self)
124 }
125
126 fn with_empty_input_references_removed(
127 &self,
128 inputs: &crate::InputAnswers<'_>,
129 ) -> Option<Box<dyn ResourceDefinition>> {
130 let input = self.replication_regions_input.as_deref()?;
133 if !inputs.string_list(input)?.is_empty() {
134 return None;
135 }
136 let mut config = self.clone();
137 config.replication_regions_input = None;
138 Some(Box::new(config))
139 }
140
141 fn input_references(&self) -> Vec<(&'static str, String)> {
142 self.replication_regions_input
143 .iter()
144 .map(|input| ("replicationRegionsInput", input.clone()))
145 .collect()
146 }
147}
148
149#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
151#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
152#[serde(rename_all = "camelCase")]
153pub struct ArtifactRegistryOutputs {
154 pub registry_id: String,
159
160 pub registry_endpoint: String,
165
166 #[serde(skip_serializing_if = "Option::is_none")]
171 pub pull_role: Option<String>,
172
173 #[serde(skip_serializing_if = "Option::is_none")]
178 pub push_role: Option<String>,
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
186#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
187#[serde(rename_all = "camelCase")]
188pub enum RegistryReplicationOwner {
189 None,
191 Ours,
193 Foreign,
195}
196
197#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
199#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
200#[serde(rename_all = "camelCase")]
201pub struct RegistryReplicationReport {
202 pub owner: RegistryReplicationOwner,
203 #[serde(default, skip_serializing_if = "Vec::is_empty")]
205 pub foreign_rules: Vec<String>,
206}
207
208impl ResourceOutputsDefinition for ArtifactRegistryOutputs {
209 fn get_resource_type(&self) -> ResourceType {
210 ArtifactRegistry::RESOURCE_TYPE.clone()
211 }
212
213 fn as_any(&self) -> &dyn Any {
214 self
215 }
216
217 fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
218 Box::new(self.clone())
219 }
220
221 fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
222 other.as_any().downcast_ref::<ArtifactRegistryOutputs>() == Some(self)
223 }
224
225 fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
226 serde_json::to_value(self)
227 }
228}
229
230#[cfg(test)]
231mod tests {
232 use super::*;
233
234 #[test]
235 fn test_artifact_registry_creation() {
236 let registry = ArtifactRegistry::new("my-registry".to_string()).build();
237 assert_eq!(registry.id, "my-registry");
238 }
239
240 #[test]
241 fn aws_registry_does_not_promise_per_deployment_replication() {
242 let mut registry = ArtifactRegistry::new("images".to_string()).build();
243 assert!(registry.validate_aws_replication().is_ok());
244 registry.replication_regions = vec!["us-west-2".to_string()];
245 assert!(registry.validate_aws_replication().is_ok());
246 }
247
248 #[test]
249 fn test_artifact_registry_dependencies() {
250 let registry = ArtifactRegistry::new("my-registry".to_string()).build();
251 let dependencies = registry.get_dependencies();
252 assert!(dependencies.is_empty());
253 }
254}