alien_core/resources/
remote_bindings.rs1use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
2use alien_error::AlienError;
3use bon::Builder;
4use serde::{Deserialize, Serialize};
5use std::any::Any;
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
10#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
11#[serde(rename_all = "camelCase", deny_unknown_fields)]
12#[builder(start_fn = new)]
13pub struct RemoteBindings {
14 #[builder(start_fn)]
15 pub id: String,
16
17 #[serde(default, skip_serializing_if = "Vec::is_empty")]
20 #[builder(default)]
21 pub grants: Vec<RemoteBindingGrant>,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
26#[serde(rename_all = "camelCase", deny_unknown_fields)]
27pub struct RemoteBindingGrant {
28 pub resource_id: String,
29 pub permission_set: String,
30 pub revision: u32,
31}
32
33impl RemoteBindings {
34 pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("resource-access");
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
39#[serde(rename_all = "camelCase", deny_unknown_fields)]
40pub struct RemoteBindingsOutputs {
41 pub resource_id: String,
43 pub access_configuration: String,
45}
46
47impl ResourceDefinition for RemoteBindings {
48 fn get_resource_type(&self) -> ResourceType {
49 Self::RESOURCE_TYPE
50 }
51 fn id(&self) -> &str {
52 &self.id
53 }
54 fn get_dependencies(&self) -> Vec<ResourceRef> {
55 Vec::new()
56 }
57
58 fn validate_update(&self, new_config: &dyn ResourceDefinition) -> crate::error::Result<()> {
59 let Some(new) = new_config.as_any().downcast_ref::<Self>() else {
60 return Err(AlienError::new(
61 crate::error::ErrorData::UnexpectedResourceType {
62 resource_id: self.id.clone(),
63 expected: Self::RESOURCE_TYPE,
64 actual: new_config.get_resource_type(),
65 },
66 ));
67 };
68 if self.id != new.id {
69 return Err(AlienError::new(
70 crate::error::ErrorData::InvalidResourceUpdate {
71 resource_id: self.id.clone(),
72 reason: "the 'id' field is immutable".to_string(),
73 },
74 ));
75 }
76 Ok(())
77 }
78 fn as_any(&self) -> &dyn Any {
79 self
80 }
81 fn as_any_mut(&mut self) -> &mut dyn Any {
82 self
83 }
84 fn box_clone(&self) -> Box<dyn ResourceDefinition> {
85 Box::new(self.clone())
86 }
87 fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
88 other.as_any().downcast_ref::<Self>() == Some(self)
89 }
90 fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
91 serde_json::to_value(self)
92 }
93}
94
95impl ResourceOutputsDefinition for RemoteBindingsOutputs {
96 fn get_resource_type(&self) -> ResourceType {
97 RemoteBindings::RESOURCE_TYPE
98 }
99 fn as_any(&self) -> &dyn Any {
100 self
101 }
102 fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
103 Box::new(self.clone())
104 }
105 fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
106 other.as_any().downcast_ref::<Self>() == Some(self)
107 }
108 fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
109 serde_json::to_value(self)
110 }
111}