use crate::error::{ErrorData, Result};
use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
use alien_error::AlienError;
use bon::Builder;
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::fmt::Debug;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[builder(start_fn = new)]
pub struct ArtifactRegistry {
#[builder(start_fn)]
pub id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[builder(default)]
pub replication_regions: Vec<String>,
}
impl ArtifactRegistry {
pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("artifact-registry");
pub fn id(&self) -> &str {
&self.id
}
}
impl ResourceDefinition for ArtifactRegistry {
fn get_resource_type(&self) -> ResourceType {
Self::RESOURCE_TYPE
}
fn id(&self) -> &str {
&self.id
}
fn get_dependencies(&self) -> Vec<ResourceRef> {
Vec::new()
}
fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
let new_registry = new_config
.as_any()
.downcast_ref::<ArtifactRegistry>()
.ok_or_else(|| {
AlienError::new(ErrorData::UnexpectedResourceType {
resource_id: self.id.clone(),
expected: Self::RESOURCE_TYPE,
actual: new_config.get_resource_type(),
})
})?;
if self.id != new_registry.id {
return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
resource_id: self.id.clone(),
reason: "the 'id' field is immutable".to_string(),
}));
}
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn box_clone(&self) -> Box<dyn ResourceDefinition> {
Box::new(self.clone())
}
fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
other.as_any().downcast_ref::<ArtifactRegistry>() == Some(self)
}
fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
serde_json::to_value(self)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ArtifactRegistryOutputs {
pub registry_id: String,
pub registry_endpoint: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub pull_role: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub push_role: Option<String>,
}
impl ResourceOutputsDefinition for ArtifactRegistryOutputs {
fn get_resource_type(&self) -> ResourceType {
ArtifactRegistry::RESOURCE_TYPE.clone()
}
fn as_any(&self) -> &dyn Any {
self
}
fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
Box::new(self.clone())
}
fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
other.as_any().downcast_ref::<ArtifactRegistryOutputs>() == Some(self)
}
fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
serde_json::to_value(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_artifact_registry_creation() {
let registry = ArtifactRegistry::new("my-registry".to_string()).build();
assert_eq!(registry.id, "my-registry");
}
#[test]
fn test_artifact_registry_dependencies() {
let registry = ArtifactRegistry::new("my-registry".to_string()).build();
let dependencies = registry.get_dependencies();
assert!(dependencies.is_empty());
}
}