use crate::error::{ErrorData, Result};
use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef};
use crate::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 AwsOpenSearch {
#[builder(start_fn)]
pub id: String,
#[builder(default)]
#[serde(default)]
pub collection_type: AwsOpenSearchCollectionType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub enum AwsOpenSearchCollectionType {
#[default]
Search,
VectorSearch,
}
impl AwsOpenSearch {
pub const RESOURCE_TYPE: ResourceType =
ResourceType::from_static("experimental/aws-opensearch");
pub fn id(&self) -> &str {
&self.id
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct AwsOpenSearchOutputs {
pub endpoint: String,
pub collection_arn: String,
}
impl ResourceOutputsDefinition for AwsOpenSearchOutputs {
fn get_resource_type(&self) -> ResourceType {
AwsOpenSearch::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::<AwsOpenSearchOutputs>() == Some(self)
}
fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
serde_json::to_value(self)
}
}
impl ResourceDefinition for AwsOpenSearch {
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_search = new_config
.as_any()
.downcast_ref::<AwsOpenSearch>()
.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_search.id {
return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
resource_id: self.id.clone(),
reason: "the 'id' field is immutable".to_string(),
}));
}
if self.collection_type != new_search.collection_type {
return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
resource_id: self.id.clone(),
reason: "the 'collectionType' field is immutable once the resource exists"
.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::<AwsOpenSearch>() == 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 builder_defaults_to_search() {
let search = AwsOpenSearch::new("search".to_string()).build();
assert_eq!(search.id, "search");
assert_eq!(search.collection_type, AwsOpenSearchCollectionType::Search);
}
#[test]
fn resource_type_uses_experimental_namespace() {
assert_eq!(
AwsOpenSearch::RESOURCE_TYPE.as_ref(),
"experimental/aws-opensearch"
);
}
#[test]
fn validate_update_rejects_id_change() {
let original = AwsOpenSearch::new("search".to_string()).build();
let renamed = AwsOpenSearch::new("other".to_string()).build();
let err = original
.validate_update(&renamed)
.expect_err("changing the id must be rejected");
assert!(err.to_string().contains("'id' field is immutable"));
}
#[test]
fn validate_update_rejects_collection_type_change() {
let search = AwsOpenSearch::new("search".to_string()).build();
let vector = AwsOpenSearch::new("search".to_string())
.collection_type(AwsOpenSearchCollectionType::VectorSearch)
.build();
let err = search
.validate_update(&vector)
.expect_err("changing the collection type must be rejected");
assert!(err
.to_string()
.contains("'collectionType' field is immutable"));
assert!(vector.validate_update(&vector).is_ok());
}
#[test]
fn serializes_with_camel_case_and_roundtrips() {
let search = AwsOpenSearch::new("vectors".to_string())
.collection_type(AwsOpenSearchCollectionType::VectorSearch)
.build();
let json = serde_json::to_value(&search).unwrap();
assert_eq!(json["collectionType"], "vectorSearch");
let roundtrip: AwsOpenSearch = serde_json::from_value(json).unwrap();
assert_eq!(search, roundtrip);
}
#[test]
fn outputs_roundtrip() {
let outputs = AwsOpenSearchOutputs {
endpoint: "https://abc123.aoss.us-east-1.on.aws".to_string(),
collection_arn: "arn:aws:aoss:us-east-1:123456789012:collection/abc123".to_string(),
};
let json = serde_json::to_string(&outputs).unwrap();
let deserialized: AwsOpenSearchOutputs = serde_json::from_str(&json).unwrap();
assert_eq!(outputs, deserialized);
}
}