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
30impl ArtifactRegistry {
31 pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("artifact-registry");
33
34 pub fn id(&self) -> &str {
36 &self.id
37 }
38}
39
40#[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 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#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
101#[serde(rename_all = "camelCase")]
102pub struct ArtifactRegistryOutputs {
103 pub registry_id: String,
108
109 pub registry_endpoint: String,
114
115 #[serde(skip_serializing_if = "Option::is_none")]
120 pub pull_role: Option<String>,
121
122 #[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}