Skip to main content

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