rusty_cdk_core/lambda/
dto.rs

1use crate::{dto_methods, ref_struct};
2use crate::shared::Id;
3use crate::stack::Asset;
4use serde::Serialize;
5use serde_json::Value;
6use std::collections::HashMap;
7use crate::intrinsic::{get_arn, get_att, get_ref};
8
9// this one also needs the `id` field for some custom ids used by API Gateway and subscriptions
10pub struct FunctionRef {
11    id: Id,
12    resource_id: String,
13}
14
15impl FunctionRef {
16    pub fn new(id: Id, resource_id: String) -> Self {
17        Self { id, resource_id }
18    }
19    
20    pub fn get_id(&self) -> &Id {
21        &self.id
22    }
23
24    pub fn get_resource_id(&self) -> &str {
25        self.resource_id.as_str()
26    }
27
28    pub fn get_ref(&self) -> Value {
29        crate::intrinsic::get_ref(self.get_resource_id())
30    }
31
32    pub fn get_arn(&self) -> Value {
33        crate::intrinsic::get_arn(self.get_resource_id())
34    }
35
36    pub fn get_att(&self, id: &str) -> Value {
37        crate::intrinsic::get_att(self.get_resource_id(), id)
38    }
39}
40
41#[derive(Debug, Serialize)]
42pub struct Function {
43    #[serde(skip)]
44    pub(super) id: Id,
45    #[serde(skip)]
46    pub(super) resource_id: String,
47    #[serde(skip)]
48    pub(crate) asset: Option<Asset>,
49    #[serde(rename = "Type")]
50    pub(super) r#type: String,
51    #[serde(rename = "Properties")]
52    pub(super) properties: LambdaFunctionProperties,
53}
54
55impl Function {
56    pub fn get_id(&self) -> &Id {
57        &self.id
58    }
59
60    pub fn get_resource_id(&self) -> &str {
61        self.resource_id.as_str()
62    }
63}
64
65#[derive(Debug, Serialize)]
66pub struct LambdaFunctionProperties {
67    #[serde(rename = "Code")]
68    pub(super) code: LambdaCode,
69    #[serde(rename = "MemorySize")]
70    pub(super) memory_size: u16,
71    #[serde(rename = "Timeout")]
72    pub(super) timeout: u16,
73    #[serde(rename = "Architectures")]
74    pub(super) architectures: Vec<String>,
75    #[serde(rename = "Role")]
76    pub(super) role: Value,
77    #[serde(rename = "Runtime", skip_serializing_if = "Option::is_none")]
78    pub(super) runtime: Option<String>,
79    #[serde(rename = "Handler", skip_serializing_if = "Option::is_none")]
80    pub(super) handler: Option<String>,
81    #[serde(rename = "FunctionName", skip_serializing_if = "Option::is_none")]
82    pub(super) function_name: Option<String>,
83    #[serde(rename = "Environment", skip_serializing_if = "Option::is_none")]
84    pub(super) environment: Option<Environment>,
85    #[serde(rename = "ReservedConcurrentExecutions", skip_serializing_if = "Option::is_none")]
86    pub(super) reserved_concurrent_executions: Option<u32>,
87    #[serde(rename = "LoggingConfig")]
88    pub(super) logging_info: LoggingInfo, 
89    // package_type: Option<String>,
90    // "VpcConfig": VpcConfig
91}
92
93#[derive(Debug, Serialize)]
94pub struct LambdaCode {
95    #[serde(rename = "S3Bucket", skip_serializing_if = "Option::is_none")]
96    pub(super) s3_bucket: Option<String>,
97    #[serde(rename = "S3Key", skip_serializing_if = "Option::is_none")]
98    pub(super) s3_key: Option<String>,
99    #[serde(rename = "ZipFile", skip_serializing_if = "Option::is_none")]
100    pub(super) zipfile: Option<String>,
101    // s3_object_version: Option<String>,
102    // image_uri: Option<String>,
103    // source_kmskey_arn: String
104}
105
106#[derive(Debug, Serialize)]
107pub struct Environment {
108    #[serde(rename = "Variables")]
109    pub(super) variables: HashMap<String, Value>,
110}
111
112#[derive(Debug, Serialize)]
113pub struct LoggingInfo {
114    #[serde(rename = "LogGroup")]
115    pub(super) log_group: Option<Value>,
116    // "ApplicationLogLevel" : String,
117    // "LogFormat" : String,
118    // "SystemLogLevel" : String
119}
120
121ref_struct!(EventSourceMappingRef);
122
123#[derive(Debug, Serialize)]
124pub struct EventSourceMapping {
125    #[serde(skip)]
126    pub(super) id: Id,
127    #[serde(skip)]
128    pub(super) resource_id: String,
129    #[serde(rename = "Type")]
130    pub(super) r#type: String,
131    #[serde(rename = "Properties")]
132    pub(super) properties: EventSourceProperties,
133}
134dto_methods!(EventSourceMapping);
135
136#[derive(Debug, Serialize)]
137pub struct EventSourceProperties {
138    #[serde(rename = "EventSourceArn", skip_serializing_if = "Option::is_none")]
139    pub(super) event_source_arn: Option<Value>,
140    #[serde(rename = "FunctionName", skip_serializing_if = "Option::is_none")]
141    pub(super) function_name: Option<Value>,
142    #[serde(rename = "ScalingConfig", skip_serializing_if = "Option::is_none")]
143    pub(super) scaling_config: Option<ScalingConfig>,
144}
145
146#[derive(Debug, Serialize)]
147pub struct ScalingConfig {
148    #[serde(rename = "MaximumConcurrency")]
149    pub(super) max_concurrency: u16,
150}
151
152pub struct PermissionRef {
153    id: Id,
154    resource_id: String,
155}
156
157impl PermissionRef {
158    pub fn new(id: Id, resource_id: String) -> Self {
159        Self {
160            id,
161            resource_id
162        }
163    }
164    
165    pub fn get_id(&self) -> Id {
166        self.id.clone()
167    }
168
169    pub fn get_resource_id(&self) -> &str {
170        self.resource_id.as_str()
171    }
172    
173    pub fn get_ref(&self) -> Value {
174        get_ref(self.get_resource_id())
175    }
176    
177    pub fn get_arn(&self) -> Value {
178        get_arn(self.get_resource_id())
179    }
180    
181    pub fn get_att(&self, id: &str) -> Value {
182        get_att(self.get_resource_id(), id)
183    }
184}
185
186#[derive(Debug, Serialize)]
187pub struct Permission {
188    #[serde(skip)]
189    pub(super) id: Id,
190    #[serde(skip)]
191    pub(super) resource_id: String,
192    #[serde(rename = "Type")]
193    pub(super) r#type: String,
194    #[serde(rename = "Properties")]
195    pub(super) properties: LambdaPermissionProperties,
196}
197dto_methods!(Permission);
198
199#[derive(Debug, Serialize)]
200pub struct LambdaPermissionProperties {
201    #[serde(rename = "Action")]
202    pub(super) action: String,
203    #[serde(rename = "FunctionName")]
204    pub(super) function_name: Value,
205    #[serde(rename = "Principal")]
206    pub(super) principal: String,
207    #[serde(rename = "SourceArn", skip_serializing_if = "Option::is_none")]
208    pub(super) source_arn: Option<Value>,
209    #[serde(rename = "SourceAccount", skip_serializing_if = "Option::is_none")]
210    pub(super) source_account: Option<Value>,
211}