Skip to main content

alien_core/
resource.rs

1use crate::error::Result;
2use serde::{Deserialize, Serialize};
3use std::any::Any;
4use std::borrow::Cow;
5use std::fmt::Debug;
6#[cfg(feature = "openapi")]
7use utoipa::openapi::schema::AdditionalProperties;
8#[cfg(feature = "openapi")]
9use utoipa::openapi::{ObjectBuilder, Ref, RefOr, Schema, Type};
10#[cfg(feature = "openapi")]
11use utoipa::{PartialSchema, ToSchema};
12
13/// Type alias for resource type identifiers
14#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
15#[serde(transparent)]
16pub struct ResourceType(pub Cow<'static, str>);
17
18impl ResourceType {
19    /// Create a new ResourceType from a static string (const-friendly)
20    pub const fn from_static(s: &'static str) -> Self {
21        Self(Cow::Borrowed(s))
22    }
23}
24
25impl From<String> for ResourceType {
26    fn from(s: String) -> Self {
27        Self(Cow::Owned(s))
28    }
29}
30
31impl From<&str> for ResourceType {
32    fn from(s: &str) -> Self {
33        Self(Cow::Owned(s.to_string()))
34    }
35}
36
37impl std::fmt::Display for ResourceType {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(f, "{}", self.0)
40    }
41}
42
43impl From<ResourceType> for String {
44    fn from(val: ResourceType) -> Self {
45        val.0.into_owned()
46    }
47}
48
49impl AsRef<str> for ResourceType {
50    fn as_ref(&self) -> &str {
51        &self.0
52    }
53}
54
55#[cfg(feature = "openapi")]
56impl PartialSchema for ResourceType {
57    fn schema() -> RefOr<Schema> {
58        RefOr::T(Schema::Object(
59            ObjectBuilder::new()
60                .schema_type(Type::String)
61                .description(Some("Resource type identifier that determines the specific kind of resource. This field is used for polymorphic deserialization and resource-specific behavior."))
62                .examples([
63                    "worker",
64                    "storage",
65                    "queue",
66                    "redis",
67                    "postgres"
68                ])
69                .build()
70        ))
71    }
72}
73
74#[cfg(feature = "openapi")]
75impl ToSchema for ResourceType {
76    fn name() -> std::borrow::Cow<'static, str> {
77        std::borrow::Cow::Borrowed("ResourceType")
78    }
79}
80
81/// Trait that defines the interface for all resource types in the Alien system.
82/// This trait enables extensibility by allowing new resource types to be registered
83/// and managed alongside built-in resources.
84pub trait ResourceDefinition: Debug + Send + Sync + 'static {
85    /// Returns the resource type for this instance
86    fn get_resource_type(&self) -> ResourceType;
87
88    /// Returns the unique identifier for this specific resource instance
89    fn id(&self) -> &str;
90
91    /// Returns the list of other resources this resource depends on
92    fn get_dependencies(&self) -> Vec<ResourceRef>;
93
94    /// Returns the permission profile name for this resource, if it has one.
95    ///
96    /// Used by `ServiceAccountDependenciesMutation` to wire the corresponding
97    /// `{profile}-sa` service account as a declared dependency so the executor
98    /// enforces ordering and propagates SA changes automatically.
99    ///
100    /// Override in concrete types that carry a `permissions` field (Container, Worker).
101    fn get_permissions(&self) -> Option<&str> {
102        None
103    }
104
105    /// Validates if an update from the current configuration to a new configuration is allowed
106    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()>;
107
108    /// Provides access to the underlying concrete type for downcasting
109    fn as_any(&self) -> &dyn Any;
110
111    /// Provides mutable access to the underlying concrete type for downcasting
112    fn as_any_mut(&mut self) -> &mut dyn Any;
113
114    /// Creates a boxed clone of this resource definition
115    fn box_clone(&self) -> Box<dyn ResourceDefinition>;
116
117    /// For equality comparison between resource definitions
118    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool;
119
120    /// Serialize this resource to a JSON value (without the "type" tag - that's added by Resource)
121    fn to_json_value(&self) -> serde_json::Result<serde_json::Value>;
122}
123
124/// Clone implementation for boxed ResourceDefinition trait objects
125impl Clone for Box<dyn ResourceDefinition> {
126    fn clone(&self) -> Self {
127        self.box_clone()
128    }
129}
130
131#[derive(Debug, Clone)]
132pub struct Resource {
133    inner: Box<dyn ResourceDefinition>,
134}
135
136impl Serialize for Resource {
137    fn serialize<S: serde::Serializer>(
138        &self,
139        serializer: S,
140    ) -> std::result::Result<S::Ok, S::Error> {
141        let mut v = self
142            .inner
143            .to_json_value()
144            .map_err(serde::ser::Error::custom)?;
145        v.as_object_mut()
146            .ok_or_else(|| serde::ser::Error::custom("resource must serialize as object"))?
147            .insert(
148                "type".into(),
149                serde_json::Value::String(self.inner.get_resource_type().0.into_owned()),
150            );
151        v.serialize(serializer)
152    }
153}
154
155impl<'de> Deserialize<'de> for Resource {
156    fn deserialize<D: serde::Deserializer<'de>>(
157        deserializer: D,
158    ) -> std::result::Result<Self, D::Error> {
159        let mut value = serde_json::Value::deserialize(deserializer)?;
160        let type_tag = value
161            .get("type")
162            .and_then(|v| v.as_str())
163            .ok_or_else(|| serde::de::Error::missing_field("type"))?
164            .to_string();
165
166        // Remove the "type" tag before passing to concrete deserializer
167        // (structs with deny_unknown_fields would reject it)
168        if let Some(obj) = value.as_object_mut() {
169            obj.remove("type");
170        }
171
172        let inner: Box<dyn ResourceDefinition> = match type_tag.as_str() {
173            "vault" => Box::new(
174                serde_json::from_value::<crate::resources::Vault>(value)
175                    .map_err(serde::de::Error::custom)?,
176            ),
177            "worker" => Box::new(
178                serde_json::from_value::<crate::resources::Worker>(value)
179                    .map_err(serde::de::Error::custom)?,
180            ),
181            "daemon" => Box::new(
182                serde_json::from_value::<crate::resources::Daemon>(value)
183                    .map_err(serde::de::Error::custom)?,
184            ),
185            "container" => Box::new(
186                serde_json::from_value::<crate::resources::Container>(value)
187                    .map_err(serde::de::Error::custom)?,
188            ),
189            "compute-cluster" => Box::new(
190                serde_json::from_value::<crate::resources::ComputeCluster>(value)
191                    .map_err(serde::de::Error::custom)?,
192            ),
193            "kubernetes-cluster" => Box::new(
194                serde_json::from_value::<crate::resources::KubernetesCluster>(value)
195                    .map_err(serde::de::Error::custom)?,
196            ),
197            "storage" => Box::new(
198                serde_json::from_value::<crate::resources::Storage>(value)
199                    .map_err(serde::de::Error::custom)?,
200            ),
201            "queue" => Box::new(
202                serde_json::from_value::<crate::resources::Queue>(value)
203                    .map_err(serde::de::Error::custom)?,
204            ),
205            "kv" => Box::new(
206                serde_json::from_value::<crate::resources::Kv>(value)
207                    .map_err(serde::de::Error::custom)?,
208            ),
209            "network" => Box::new(
210                serde_json::from_value::<crate::resources::Network>(value)
211                    .map_err(serde::de::Error::custom)?,
212            ),
213            "build" => Box::new(
214                serde_json::from_value::<crate::resources::Build>(value)
215                    .map_err(serde::de::Error::custom)?,
216            ),
217            "service-account" => Box::new(
218                serde_json::from_value::<crate::resources::ServiceAccount>(value)
219                    .map_err(serde::de::Error::custom)?,
220            ),
221            "artifact-registry" => Box::new(
222                serde_json::from_value::<crate::resources::ArtifactRegistry>(value)
223                    .map_err(serde::de::Error::custom)?,
224            ),
225            "service_activation" => Box::new(
226                serde_json::from_value::<crate::resources::ServiceActivation>(value)
227                    .map_err(serde::de::Error::custom)?,
228            ),
229            "remote-stack-management" => Box::new(
230                serde_json::from_value::<crate::resources::RemoteStackManagement>(value)
231                    .map_err(serde::de::Error::custom)?,
232            ),
233            "azure_resource_group" => Box::new(
234                serde_json::from_value::<crate::resources::AzureResourceGroup>(value)
235                    .map_err(serde::de::Error::custom)?,
236            ),
237            "azure_storage_account" => Box::new(
238                serde_json::from_value::<crate::resources::AzureStorageAccount>(value)
239                    .map_err(serde::de::Error::custom)?,
240            ),
241            "azure_container_apps_environment" => Box::new(
242                serde_json::from_value::<crate::resources::AzureContainerAppsEnvironment>(value)
243                    .map_err(serde::de::Error::custom)?,
244            ),
245            "azure_service_bus_namespace" => Box::new(
246                serde_json::from_value::<crate::resources::AzureServiceBusNamespace>(value)
247                    .map_err(serde::de::Error::custom)?,
248            ),
249            other => {
250                return Err(serde::de::Error::unknown_variant(
251                    other,
252                    &[
253                        "vault",
254                        "worker",
255                        "daemon",
256                        "container",
257                        "compute-cluster",
258                        "kubernetes-cluster",
259                        "storage",
260                        "queue",
261                        "kv",
262                        "network",
263                        "build",
264                        "service-account",
265                        "artifact-registry",
266                        "service_activation",
267                        "remote-stack-management",
268                        "azure_resource_group",
269                        "azure_storage_account",
270                        "azure_container_apps_environment",
271                        "azure_service_bus_namespace",
272                    ],
273                ))
274            }
275        };
276
277        Ok(Resource { inner })
278    }
279}
280
281impl Resource {
282    /// Creates a new Resource from any type that implements ResourceDefinition
283    pub fn new<T: ResourceDefinition>(resource: T) -> Self {
284        Self {
285            inner: Box::new(resource),
286        }
287    }
288
289    /// Creates a new Resource from a boxed ResourceDefinition
290    pub fn from_boxed(boxed_resource: Box<dyn ResourceDefinition>) -> Self {
291        Self {
292            inner: boxed_resource,
293        }
294    }
295
296    /// Returns the resource type identifier
297    pub fn resource_type(&self) -> ResourceType {
298        self.inner.get_resource_type()
299    }
300
301    /// Returns the unique identifier for this resource instance
302    pub fn id(&self) -> &str {
303        self.inner.id()
304    }
305
306    /// Returns the list of other resources this resource depends on
307    pub fn get_dependencies(&self) -> Vec<ResourceRef> {
308        self.inner.get_dependencies()
309    }
310
311    /// Returns the permission profile name for this resource, if it has one.
312    pub fn get_permissions(&self) -> Option<&str> {
313        self.inner.get_permissions()
314    }
315
316    /// Validates if an update from the current configuration to a new configuration is allowed
317    pub fn validate_update(&self, new_config: &Resource) -> Result<()> {
318        self.inner.validate_update(new_config.inner.as_ref())
319    }
320
321    /// Provides access to the underlying ResourceDefinition trait object
322    pub fn as_resource_definition(&self) -> &dyn ResourceDefinition {
323        self.inner.as_ref()
324    }
325
326    /// Generic downcasting for any type
327    pub fn downcast_ref<T: ResourceDefinition + 'static>(&self) -> Option<&T> {
328        self.inner.as_any().downcast_ref::<T>()
329    }
330
331    /// Generic mutable downcasting for any type
332    pub fn downcast_mut<T: ResourceDefinition + 'static>(&mut self) -> Option<&mut T> {
333        self.inner.as_any_mut().downcast_mut::<T>()
334    }
335}
336
337impl PartialEq for Resource {
338    fn eq(&self, other: &Self) -> bool {
339        self.inner.resource_eq(other.inner.as_ref())
340    }
341}
342
343impl Eq for Resource {}
344
345/// OpenAPI schema implementation for Resource.
346///
347/// The schema represents the flattened JSON structure of any resource type in the Alien system.
348/// All resources have a common base structure with `type` and `id` fields, plus type-specific
349/// additional properties that vary depending on the concrete resource implementation.
350///
351/// # Schema Structure
352/// - `type` (required): The resource type identifier (e.g., "worker", "storage", "queue")
353/// - `id` (required): The unique identifier for this specific resource instance
354/// - Additional properties: Type-specific fields that vary by resource type (e.g., Worker has `code`, `memory_mb`, etc.)
355///
356/// # Example JSON
357/// ```json
358/// {
359///   "type": "worker",
360///   "id": "my-function",
361///   "code": { "type": "image", "image": "my-image:latest" },
362///   "memoryMb": 512,
363///   "timeoutSeconds": 30
364/// }
365/// ```
366#[cfg(feature = "openapi")]
367impl PartialSchema for Resource {
368    fn schema() -> RefOr<Schema> {
369        RefOr::T(Schema::Object(
370            ObjectBuilder::new()
371                .schema_type(Type::Object)
372                .property("type", Ref::from_schema_name("ResourceType"))
373                .property("id",
374                    ObjectBuilder::new()
375                        .schema_type(Type::String)
376                        .description(Some("The unique identifier for this specific resource instance. Must contain only alphanumeric characters, hyphens, and underscores ([A-Za-z0-9-_]). Maximum 64 characters."))
377                        .build()
378                )
379                .required("type")
380                .required("id")
381                .additional_properties(Some(AdditionalProperties::FreeForm(true)))
382                .description(Some("Resource that can hold any resource type in the Alien system. All resources share common 'type' and 'id' fields with additional type-specific properties."))
383                .build()
384        ))
385    }
386}
387
388#[cfg(feature = "openapi")]
389impl ToSchema for Resource {
390    fn name() -> std::borrow::Cow<'static, str> {
391        std::borrow::Cow::Borrowed("BaseResource")
392    }
393}
394
395/// New ResourceRef that works with any resource type.
396/// This can eventually replace the enum-based ResourceRef for full extensibility.
397#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
398#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
399#[serde(rename_all = "camelCase")]
400pub struct ResourceRef {
401    #[serde(rename = "type")]
402    pub resource_type: ResourceType,
403    pub id: String,
404}
405
406impl ResourceRef {
407    /// Creates a new ResourceRef
408    pub fn new(resource_type: ResourceType, id: impl Into<String>) -> Self {
409        Self {
410            resource_type,
411            id: id.into(),
412        }
413    }
414
415    /// Returns the resource type
416    pub fn resource_type(&self) -> &ResourceType {
417        &self.resource_type
418    }
419
420    /// Returns the resource id
421    pub fn id(&self) -> &str {
422        &self.id
423    }
424}
425
426impl<T: ResourceDefinition> From<&T> for ResourceRef {
427    fn from(resource: &T) -> Self {
428        Self::new(resource.get_resource_type(), resource.id())
429    }
430}
431
432impl From<&Resource> for ResourceRef {
433    fn from(resource: &Resource) -> Self {
434        Self::new(resource.resource_type(), resource.id())
435    }
436}
437
438/// Trait that defines the interface for all resource output types in the Alien system.
439/// This trait enables extensibility by allowing new resource output types to be registered
440/// and managed alongside built-in resource outputs.
441pub trait ResourceOutputsDefinition: Debug + Send + Sync + 'static {
442    /// Returns the resource type for this instance
443    fn get_resource_type(&self) -> ResourceType;
444
445    /// Provides access to the underlying concrete type for downcasting
446    fn as_any(&self) -> &dyn Any;
447
448    /// Creates a boxed clone of this resource outputs
449    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition>;
450
451    /// For equality comparison between resource outputs
452    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool;
453
454    /// Serialize this resource outputs to a JSON value (without the "type" tag - that's added by ResourceOutputs)
455    fn to_json_value(&self) -> serde_json::Result<serde_json::Value>;
456}
457
458/// Clone implementation for boxed ResourceOutputsDefinition trait objects
459impl Clone for Box<dyn ResourceOutputsDefinition> {
460    fn clone(&self) -> Self {
461        self.box_clone()
462    }
463}
464
465/// New Resource outputs wrapper that can hold any ResourceOutputsDefinition.
466/// This replaces the old ResourceOutputs enum to enable runtime extensibility.
467#[derive(Debug, Clone)]
468pub struct ResourceOutputs {
469    inner: Box<dyn ResourceOutputsDefinition>,
470}
471
472impl Serialize for ResourceOutputs {
473    fn serialize<S: serde::Serializer>(
474        &self,
475        serializer: S,
476    ) -> std::result::Result<S::Ok, S::Error> {
477        let mut v = self
478            .inner
479            .to_json_value()
480            .map_err(serde::ser::Error::custom)?;
481        v.as_object_mut()
482            .ok_or_else(|| serde::ser::Error::custom("resource outputs must serialize as object"))?
483            .insert(
484                "type".into(),
485                serde_json::Value::String(self.inner.get_resource_type().0.into_owned()),
486            );
487        v.serialize(serializer)
488    }
489}
490
491impl<'de> Deserialize<'de> for ResourceOutputs {
492    fn deserialize<D: serde::Deserializer<'de>>(
493        deserializer: D,
494    ) -> std::result::Result<Self, D::Error> {
495        let mut value = serde_json::Value::deserialize(deserializer)?;
496        let type_tag = value
497            .get("type")
498            .and_then(|v| v.as_str())
499            .ok_or_else(|| serde::de::Error::missing_field("type"))?
500            .to_string();
501
502        // Remove the "type" tag before passing to concrete deserializer
503        // (structs with deny_unknown_fields would reject it)
504        if let Some(obj) = value.as_object_mut() {
505            obj.remove("type");
506        }
507
508        let inner: Box<dyn ResourceOutputsDefinition> = match type_tag.as_str() {
509            "vault" => Box::new(
510                serde_json::from_value::<crate::resources::VaultOutputs>(value)
511                    .map_err(serde::de::Error::custom)?,
512            ),
513            "worker" => Box::new(
514                serde_json::from_value::<crate::resources::WorkerOutputs>(value)
515                    .map_err(serde::de::Error::custom)?,
516            ),
517            "daemon" => Box::new(
518                serde_json::from_value::<crate::resources::DaemonOutputs>(value)
519                    .map_err(serde::de::Error::custom)?,
520            ),
521            "container" => Box::new(
522                serde_json::from_value::<crate::resources::ContainerOutputs>(value)
523                    .map_err(serde::de::Error::custom)?,
524            ),
525            "compute-cluster" => Box::new(
526                serde_json::from_value::<crate::resources::ComputeClusterOutputs>(value)
527                    .map_err(serde::de::Error::custom)?,
528            ),
529            "storage" => Box::new(
530                serde_json::from_value::<crate::resources::StorageOutputs>(value)
531                    .map_err(serde::de::Error::custom)?,
532            ),
533            "queue" => Box::new(
534                serde_json::from_value::<crate::resources::QueueOutputs>(value)
535                    .map_err(serde::de::Error::custom)?,
536            ),
537            "kv" => Box::new(
538                serde_json::from_value::<crate::resources::KvOutputs>(value)
539                    .map_err(serde::de::Error::custom)?,
540            ),
541            "network" => Box::new(
542                serde_json::from_value::<crate::resources::NetworkOutputs>(value)
543                    .map_err(serde::de::Error::custom)?,
544            ),
545            "build" => Box::new(
546                serde_json::from_value::<crate::resources::BuildOutputs>(value)
547                    .map_err(serde::de::Error::custom)?,
548            ),
549            "service-account" => Box::new(
550                serde_json::from_value::<crate::resources::ServiceAccountOutputs>(value)
551                    .map_err(serde::de::Error::custom)?,
552            ),
553            "artifact-registry" => Box::new(
554                serde_json::from_value::<crate::resources::ArtifactRegistryOutputs>(value)
555                    .map_err(serde::de::Error::custom)?,
556            ),
557            "service_activation" => Box::new(
558                serde_json::from_value::<crate::resources::ServiceActivationOutputs>(value)
559                    .map_err(serde::de::Error::custom)?,
560            ),
561            "remote-stack-management" => Box::new(
562                serde_json::from_value::<crate::resources::RemoteStackManagementOutputs>(value)
563                    .map_err(serde::de::Error::custom)?,
564            ),
565            "kubernetes-cluster" => Box::new(
566                serde_json::from_value::<crate::resources::KubernetesClusterOutputs>(value)
567                    .map_err(serde::de::Error::custom)?,
568            ),
569            "azure_resource_group" => Box::new(
570                serde_json::from_value::<crate::resources::AzureResourceGroupOutputs>(value)
571                    .map_err(serde::de::Error::custom)?,
572            ),
573            "azure_storage_account" => Box::new(
574                serde_json::from_value::<crate::resources::AzureStorageAccountOutputs>(value)
575                    .map_err(serde::de::Error::custom)?,
576            ),
577            "azure_container_apps_environment" => Box::new(
578                serde_json::from_value::<crate::resources::AzureContainerAppsEnvironmentOutputs>(
579                    value,
580                )
581                .map_err(serde::de::Error::custom)?,
582            ),
583            "azure_service_bus_namespace" => Box::new(
584                serde_json::from_value::<crate::resources::AzureServiceBusNamespaceOutputs>(value)
585                    .map_err(serde::de::Error::custom)?,
586            ),
587            other => {
588                return Err(serde::de::Error::unknown_variant(
589                    other,
590                    &[
591                        "vault",
592                        "worker",
593                        "daemon",
594                        "container",
595                        "compute-cluster",
596                        "storage",
597                        "queue",
598                        "kv",
599                        "network",
600                        "build",
601                        "service-account",
602                        "artifact-registry",
603                        "service_activation",
604                        "remote-stack-management",
605                        "kubernetes-cluster",
606                        "azure_resource_group",
607                        "azure_storage_account",
608                        "azure_container_apps_environment",
609                        "azure_service_bus_namespace",
610                    ],
611                ))
612            }
613        };
614
615        Ok(ResourceOutputs { inner })
616    }
617}
618
619impl ResourceOutputs {
620    /// Creates a new ResourceOutputs from any type that implements ResourceOutputsDefinition
621    pub fn new<T: ResourceOutputsDefinition>(outputs: T) -> Self {
622        Self {
623            inner: Box::new(outputs),
624        }
625    }
626
627    /// Provides access to the underlying ResourceOutputsDefinition trait object
628    pub fn as_resource_outputs(&self) -> &dyn ResourceOutputsDefinition {
629        self.inner.as_ref()
630    }
631
632    /// Generic downcasting for any type
633    pub fn downcast_ref<T: ResourceOutputsDefinition + 'static>(&self) -> Option<&T> {
634        self.inner.as_any().downcast_ref::<T>()
635    }
636}
637
638impl PartialEq for ResourceOutputs {
639    fn eq(&self, other: &Self) -> bool {
640        self.inner.outputs_eq(other.inner.as_ref())
641    }
642}
643
644impl Eq for ResourceOutputs {}
645
646/// OpenAPI schema implementation for ResourceOutputs.
647///
648/// The schema represents the flattened JSON structure of any resource outputs in the Alien system.
649/// All resource outputs have a common base structure with a `type` field, plus type-specific
650/// additional properties that vary depending on the concrete resource implementation.
651///
652/// # Schema Structure
653/// - `type` (required): The resource type identifier (e.g., "worker", "storage", "queue")
654/// - Additional properties: Type-specific output fields that vary by resource type
655///
656/// # Example JSON
657/// ```json
658/// {
659///   "type": "worker",
660///   "functionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-function",
661///   "functionUrl": "https://abc123.lambda-url.us-east-1.on.aws/"
662/// }
663/// ```
664#[cfg(feature = "openapi")]
665impl PartialSchema for ResourceOutputs {
666    fn schema() -> RefOr<Schema> {
667        RefOr::T(Schema::Object(
668            ObjectBuilder::new()
669                .schema_type(Type::Object)
670                .property("type", Ref::from_schema_name("ResourceType"))
671                .required("type")
672                .additional_properties(Some(AdditionalProperties::FreeForm(true)))
673                .description(Some("Resource outputs that can hold output data for any resource type in the Alien system. All resource outputs share a common 'type' field with additional type-specific output properties."))
674                .build()
675        ))
676    }
677}
678
679#[cfg(feature = "openapi")]
680impl ToSchema for ResourceOutputs {
681    fn name() -> std::borrow::Cow<'static, str> {
682        std::borrow::Cow::Borrowed("BaseResourceOutputs")
683    }
684}
685
686/// Represents the high-level status of a resource during its lifecycle.
687#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
688#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
689#[serde(rename_all = "kebab-case")]
690pub enum ResourceStatus {
691    Pending,      // Initial state before any action starts
692    Provisioning, // Resource is being created or updated
693    ProvisionFailed,
694    Running, // Resource is active and configured as desired
695    Updating,
696    UpdateFailed,
697    Deleting, // Resource is being removed
698    DeleteFailed,
699    Deleted,       // Resource has been successfully removed (terminal state)
700    RefreshFailed, // Resource heartbeat/health check failed
701}
702
703impl ResourceStatus {
704    pub fn is_terminal(&self) -> bool {
705        match self {
706            ResourceStatus::Deleted => true,
707            ResourceStatus::ProvisionFailed => true,
708            ResourceStatus::UpdateFailed => true,
709            ResourceStatus::DeleteFailed => true,
710            ResourceStatus::RefreshFailed => true,
711            _ => false, // Pending, Provisioning, Updating, Deleting are not terminal
712        }
713    }
714}
715
716/// Describes the lifecycle of a resource within a stack, determining how it's managed and deployed.
717#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, Deserialize)]
718#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
719#[serde(rename_all = "kebab-case")]
720pub enum ResourceLifecycle {
721    /// Frozen resources are owned by setup. Setup creates, updates, and
722    /// deletes them. Alien may heartbeat them and may run explicit management
723    /// operations when setup granted management permissions.
724    Frozen,
725
726    /// Live resources are owned by Alien. Alien creates, updates, deletes, and
727    /// replaces them after setup, so Live resources require provision
728    /// permissions.
729    Live,
730}