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
49impl ResourceOutputsDefinition for ServiceActivationOutputs {
50    fn get_resource_type(&self) -> ResourceType {
51        ServiceActivation::RESOURCE_TYPE.clone()
52    }
53
54    fn as_any(&self) -> &dyn Any {
55        self
56    }
57
58    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
59        Box::new(self.clone())
60    }
61
62    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
63        other.as_any().downcast_ref::<ServiceActivationOutputs>() == Some(self)
64    }
65
66    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
67        serde_json::to_value(self)
68    }
69}
70
71// Implementation of ResourceDefinition trait for ServiceActivation
72impl ResourceDefinition for ServiceActivation {
73    fn get_resource_type(&self) -> ResourceType {
74        Self::RESOURCE_TYPE
75    }
76
77    fn id(&self) -> &str {
78        &self.id
79    }
80
81    fn get_dependencies(&self) -> Vec<ResourceRef> {
82        Vec::new()
83    }
84
85    fn validate_update(&self, _new_config: &dyn ResourceDefinition) -> Result<()> {
86        Err(AlienError::new(ErrorData::InvalidResourceUpdate {
87            resource_id: self.id.clone(),
88            reason: "Service activations cannot be updated once created".to_string(),
89        }))
90    }
91
92    fn as_any(&self) -> &dyn Any {
93        self
94    }
95
96    fn as_any_mut(&mut self) -> &mut dyn Any {
97        self
98    }
99
100    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
101        Box::new(self.clone())
102    }
103
104    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
105        other.as_any().downcast_ref::<ServiceActivation>() == Some(self)
106    }
107
108    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
109        serde_json::to_value(self)
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_service_activation_creation() {
119        let service = ServiceActivation::new("enable-iam".to_string())
120            .service_name("iam.googleapis.com".to_string())
121            .build();
122
123        assert_eq!(service.id, "enable-iam");
124        assert_eq!(service.service_name, "iam.googleapis.com");
125    }
126}