Skip to main content

alien_core/resources/
azure_resource_group.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 Resource Group that acts as a logical container for Azure resources.
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 AzureResourceGroup {
15    /// Identifier for the resource group. 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 AzureResourceGroup {
22    /// The resource type identifier for Azure Resource Groups
23    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("azure_resource_group");
24
25    /// Returns the resource group's unique identifier.
26    pub fn id(&self) -> &str {
27        &self.id
28    }
29}
30
31/// Outputs generated by a successfully provisioned Azure Resource Group.
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
34#[serde(rename_all = "camelCase")]
35pub struct AzureResourceGroupOutputs {
36    /// The name of the resource group.
37    pub name: String,
38    /// The resource ID of the resource group.
39    pub resource_id: String,
40    /// The location/region of the resource group.
41    pub location: String,
42}
43
44impl ResourceOutputsDefinition for AzureResourceGroupOutputs {
45    fn get_resource_type(&self) -> ResourceType {
46        AzureResourceGroup::RESOURCE_TYPE.clone()
47    }
48
49    fn as_any(&self) -> &dyn Any {
50        self
51    }
52
53    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
54        Box::new(self.clone())
55    }
56
57    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
58        other.as_any().downcast_ref::<AzureResourceGroupOutputs>() == Some(self)
59    }
60
61    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
62        serde_json::to_value(self)
63    }
64}
65
66// Implementation of ResourceDefinition trait for AzureResourceGroup
67impl ResourceDefinition for AzureResourceGroup {
68    fn get_resource_type(&self) -> ResourceType {
69        Self::RESOURCE_TYPE
70    }
71
72    fn id(&self) -> &str {
73        &self.id
74    }
75
76    fn get_dependencies(&self) -> Vec<ResourceRef> {
77        Vec::new()
78    }
79
80    fn validate_update(&self, _new_config: &dyn ResourceDefinition) -> Result<()> {
81        Err(AlienError::new(ErrorData::InvalidResourceUpdate {
82            resource_id: self.id.clone(),
83            reason: "Azure resource groups cannot be updated once created".to_string(),
84        }))
85    }
86
87    fn as_any(&self) -> &dyn Any {
88        self
89    }
90
91    fn as_any_mut(&mut self) -> &mut dyn Any {
92        self
93    }
94
95    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
96        Box::new(self.clone())
97    }
98
99    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
100        other.as_any().downcast_ref::<AzureResourceGroup>() == Some(self)
101    }
102
103    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
104        serde_json::to_value(self)
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_azure_resource_group_creation() {
114        let resource_group = AzureResourceGroup::new("my-rg".to_string()).build();
115        assert_eq!(resource_group.id, "my-rg");
116    }
117}