Skip to main content

alien_core/resources/
azure_service_bus_namespace.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 Azure Service Bus Namespace for hosting queues and topics.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
11#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
12#[serde(rename_all = "camelCase", deny_unknown_fields)]
13#[builder(start_fn = new)]
14pub struct AzureServiceBusNamespace {
15    /// Identifier for the Service Bus Namespace. Must contain only alphanumeric characters, hyphens, and underscores ([A-Za-z0-9-_]).
16    /// Maximum 64 characters.
17    #[builder(start_fn)]
18    pub id: String,
19}
20
21impl AzureServiceBusNamespace {
22    /// The resource type identifier for Azure Service Bus Namespaces
23    pub const RESOURCE_TYPE: ResourceType =
24        ResourceType::from_static("azure_service_bus_namespace");
25
26    /// Returns the namespace's unique identifier.
27    pub fn id(&self) -> &str {
28        &self.id
29    }
30}
31
32/// Outputs generated by a successfully provisioned Azure Service Bus Namespace.
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
35#[serde(rename_all = "camelCase")]
36pub struct AzureServiceBusNamespaceOutputs {
37    /// The name of the Service Bus Namespace.
38    pub namespace_name: String,
39    /// The resource ID of the Service Bus Namespace.
40    pub resource_id: String,
41    /// The fully qualified domain name of the namespace.
42    pub fqdn: String,
43    /// The endpoint for Service Bus operations.
44    pub endpoint: String,
45}
46
47#[typetag::serde(name = "azure_service_bus_namespace")]
48impl ResourceOutputsDefinition for AzureServiceBusNamespaceOutputs {
49    fn resource_type() -> ResourceType {
50        AzureServiceBusNamespace::RESOURCE_TYPE.clone()
51    }
52
53    fn as_any(&self) -> &dyn Any {
54        self
55    }
56
57    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
58        Box::new(self.clone())
59    }
60
61    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
62        other
63            .as_any()
64            .downcast_ref::<AzureServiceBusNamespaceOutputs>()
65            == Some(self)
66    }
67}
68
69// Implementation of ResourceDefinition trait for AzureServiceBusNamespace
70#[typetag::serde(name = "azure_service_bus_namespace")]
71impl ResourceDefinition for AzureServiceBusNamespace {
72    fn resource_type() -> ResourceType {
73        Self::RESOURCE_TYPE.clone()
74    }
75
76    fn get_resource_type(&self) -> ResourceType {
77        Self::resource_type()
78    }
79
80    fn id(&self) -> &str {
81        &self.id
82    }
83
84    fn get_dependencies(&self) -> Vec<ResourceRef> {
85        Vec::new()
86    }
87
88    fn validate_update(&self, _new_config: &dyn ResourceDefinition) -> Result<()> {
89        Err(AlienError::new(ErrorData::InvalidResourceUpdate {
90            resource_id: self.id.clone(),
91            reason: "Azure service bus namespaces cannot be updated once created".to_string(),
92        }))
93    }
94
95    fn as_any(&self) -> &dyn Any {
96        self
97    }
98
99    fn as_any_mut(&mut self) -> &mut dyn Any {
100        self
101    }
102
103    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
104        Box::new(self.clone())
105    }
106
107    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
108        other.as_any().downcast_ref::<AzureServiceBusNamespace>() == Some(self)
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn test_azure_service_bus_namespace_creation() {
118        let namespace = AzureServiceBusNamespace::new("my-namespace".to_string()).build();
119        assert_eq!(namespace.id, "my-namespace");
120    }
121}