Skip to main content

aws_lite_rs/api/
lambda.rs

1//! AWS Lambda API client.
2//!
3//! Thin wrapper over generated ops. All URL construction and HTTP methods
4//! are in `ops::lambda::LambdaOps`. This layer adds:
5//! - Ergonomic method signatures
6
7use crate::{
8    AwsHttpClient, Result,
9    ops::lambda::LambdaOps,
10    types::lambda::{
11        FunctionConfiguration, ListFunctionsResponse, UpdateFunctionConfigurationRequest,
12    },
13};
14
15/// Client for the AWS Lambda API
16pub struct LambdaClient<'a> {
17    ops: LambdaOps<'a>,
18}
19
20impl<'a> LambdaClient<'a> {
21    /// Create a new AWS Lambda API client
22    pub(crate) fn new(client: &'a AwsHttpClient) -> Self {
23        Self {
24            ops: LambdaOps::new(client),
25        }
26    }
27
28    /// Returns a list of Lambda functions, with the version-specific configuration of each.
29    pub async fn list_functions(
30        &self,
31        master_region: &str,
32        function_version: &str,
33        marker: &str,
34        max_items: &str,
35    ) -> Result<ListFunctionsResponse> {
36        self.ops
37            .list_functions(master_region, function_version, marker, max_items)
38            .await
39    }
40
41    /// Returns the version-specific settings of a Lambda function or version.
42    pub async fn get_function_configuration(
43        &self,
44        function_name: &str,
45        qualifier: &str,
46    ) -> Result<FunctionConfiguration> {
47        self.ops
48            .get_function_configuration(function_name, qualifier)
49            .await
50    }
51
52    /// Modify the version-specific settings of a Lambda function.
53    pub async fn update_function_configuration(
54        &self,
55        function_name: &str,
56        body: &UpdateFunctionConfigurationRequest,
57    ) -> Result<FunctionConfiguration> {
58        self.ops
59            .update_function_configuration(function_name, body)
60            .await
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::MockClient;
68    use crate::types::lambda::*;
69
70    fn make_function_config(
71        name: &str,
72        arn: &str,
73        runtime: &str,
74        timeout: i32,
75        memory: i32,
76    ) -> FunctionConfiguration {
77        FunctionConfiguration {
78            function_name: name.to_string(),
79            function_arn: arn.to_string(),
80            runtime: Some(runtime.to_string()),
81            timeout: Some(timeout),
82            memory_size: Some(memory),
83            architectures: vec!["x86_64".to_string()],
84            ..Default::default()
85        }
86    }
87
88    #[tokio::test]
89    async fn test_list_functions_returns_empty() {
90        let mut mock = MockClient::new();
91        mock.expect_get("/2015-03-31/functions")
92            .returning_json(serde_json::json!({
93                "Functions": [],
94                "NextMarker": null
95            }));
96
97        let client = AwsHttpClient::from_mock(mock);
98        let result = client
99            .lambda()
100            .list_functions("", "", "", "")
101            .await
102            .unwrap();
103
104        assert_eq!(result.functions.len(), 0);
105        assert!(result.next_marker.is_none());
106    }
107
108    #[tokio::test]
109    async fn test_list_functions_returns_functions_with_correct_fields() {
110        let mut mock = MockClient::new();
111        mock.expect_get("/2015-03-31/functions")
112            .returning_json(serde_json::json!({
113                "Functions": [
114                    {
115                        "FunctionName": "my-function",
116                        "FunctionArn": "arn:aws:lambda:eu-central-1:123456789012:function:my-function",
117                        "Runtime": "python3.12",
118                        "Timeout": 30,
119                        "MemorySize": 256,
120                        "Architectures": ["arm64"]
121                    }
122                ]
123            }));
124
125        let client = AwsHttpClient::from_mock(mock);
126        let result = client
127            .lambda()
128            .list_functions("", "", "", "")
129            .await
130            .unwrap();
131
132        assert_eq!(result.functions.len(), 1);
133        let func = &result.functions[0];
134        assert_eq!(func.function_name, "my-function");
135        assert_eq!(
136            func.function_arn,
137            "arn:aws:lambda:eu-central-1:123456789012:function:my-function"
138        );
139        assert_eq!(func.runtime.as_deref(), Some("python3.12"));
140        assert_eq!(func.timeout, Some(30));
141        assert_eq!(func.memory_size, Some(256));
142        assert_eq!(func.architectures, vec!["arm64"]);
143    }
144
145    #[tokio::test]
146    async fn test_get_function_configuration_returns_config() {
147        let mut mock = MockClient::new();
148        mock.expect_get("/2015-03-31/functions/my-function/configuration")
149            .returning_json(
150                serde_json::to_value(make_function_config(
151                    "my-function",
152                    "arn:aws:lambda:eu-central-1:123456789012:function:my-function",
153                    "python3.12",
154                    30,
155                    256,
156                ))
157                .unwrap(),
158            );
159
160        let client = AwsHttpClient::from_mock(mock);
161        let result = client
162            .lambda()
163            .get_function_configuration("my-function", "")
164            .await
165            .unwrap();
166
167        assert_eq!(result.function_name, "my-function");
168        assert_eq!(result.timeout, Some(30));
169        assert_eq!(result.memory_size, Some(256));
170        assert_eq!(result.architectures, vec!["x86_64"]);
171    }
172
173    #[tokio::test]
174    async fn test_update_function_configuration_returns_updated_config() {
175        let mut mock = MockClient::new();
176        mock.expect_put("/2015-03-31/functions/my-function/configuration")
177            .returning_json(
178                serde_json::to_value(make_function_config(
179                    "my-function",
180                    "arn:aws:lambda:eu-central-1:123456789012:function:my-function",
181                    "python3.12",
182                    60,
183                    512,
184                ))
185                .unwrap(),
186            );
187
188        let client = AwsHttpClient::from_mock(mock);
189        let result = client
190            .lambda()
191            .update_function_configuration(
192                "my-function",
193                &UpdateFunctionConfigurationRequest {
194                    function_name: "my-function".to_string(),
195                    timeout: Some(60),
196                    memory_size: Some(512),
197                    ..Default::default()
198                },
199            )
200            .await
201            .unwrap();
202
203        assert_eq!(result.function_name, "my-function");
204        assert_eq!(result.timeout, Some(60));
205        assert_eq!(result.memory_size, Some(512));
206    }
207}