Skip to main content

alien_core/resources/
azure_storage_account.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 Storage Account for blob, file, table, and queue storage.
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 AzureStorageAccount {
15    /// Identifier for the storage account. 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 AzureStorageAccount {
22    /// The resource type identifier for Azure Storage Accounts
23    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("azure_storage_account");
24
25    /// Returns the storage account's unique identifier.
26    pub fn id(&self) -> &str {
27        &self.id
28    }
29}
30
31/// Outputs generated by a successfully provisioned Azure Storage Account.
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
34#[serde(rename_all = "camelCase")]
35pub struct AzureStorageAccountOutputs {
36    /// The name of the storage account.
37    pub account_name: String,
38    /// The resource ID of the storage account.
39    pub resource_id: String,
40    /// The primary endpoint for blob storage.
41    pub primary_blob_endpoint: String,
42    /// The primary endpoint for file storage.
43    pub primary_file_endpoint: String,
44    /// The primary endpoint for queue storage.
45    pub primary_queue_endpoint: String,
46    /// The primary endpoint for table storage.
47    pub primary_table_endpoint: String,
48    /// The primary access key for the storage account.
49    pub primary_access_key: String,
50    /// The connection string for the storage account.
51    pub connection_string: String,
52}
53
54impl ResourceOutputsDefinition for AzureStorageAccountOutputs {
55    fn get_resource_type(&self) -> ResourceType {
56        AzureStorageAccount::RESOURCE_TYPE.clone()
57    }
58
59    fn as_any(&self) -> &dyn Any {
60        self
61    }
62
63    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
64        Box::new(self.clone())
65    }
66
67    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
68        other.as_any().downcast_ref::<AzureStorageAccountOutputs>() == Some(self)
69    }
70
71    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
72        serde_json::to_value(self)
73    }
74}
75
76// Implementation of ResourceDefinition trait for AzureStorageAccount
77impl ResourceDefinition for AzureStorageAccount {
78    fn get_resource_type(&self) -> ResourceType {
79        Self::RESOURCE_TYPE
80    }
81
82    fn id(&self) -> &str {
83        &self.id
84    }
85
86    fn get_dependencies(&self) -> Vec<ResourceRef> {
87        Vec::new()
88    }
89
90    fn validate_update(&self, _new_config: &dyn ResourceDefinition) -> Result<()> {
91        Err(AlienError::new(ErrorData::InvalidResourceUpdate {
92            resource_id: self.id.clone(),
93            reason: "Azure storage accounts cannot be updated once created".to_string(),
94        }))
95    }
96
97    fn as_any(&self) -> &dyn Any {
98        self
99    }
100
101    fn as_any_mut(&mut self) -> &mut dyn Any {
102        self
103    }
104
105    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
106        Box::new(self.clone())
107    }
108
109    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
110        other.as_any().downcast_ref::<AzureStorageAccount>() == Some(self)
111    }
112
113    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
114        serde_json::to_value(self)
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn test_azure_storage_account_creation() {
124        let storage_account = AzureStorageAccount::new("my-storage".to_string()).build();
125        assert_eq!(storage_account.id, "my-storage");
126    }
127}