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