Skip to main content

vantage_aws/models/lambda/
function.rs

1use serde::{Deserialize, Serialize};
2use vantage_table::table::Table;
3
4use crate::types::{Arn, AwsDateTime};
5use crate::{AwsAccount, eq};
6
7use super::alias::{Alias, aliases_table};
8use super::version::{Version, versions_table};
9use crate::models::logs::group::{LogGroup, groups_table as log_groups_table};
10
11/// One Lambda function from `ListFunctions`. Lambda's response
12/// includes nested config blobs (`LoggingConfig`, `TracingConfig`,
13/// `VpcConfig`, …) that v0 leaves nested as wire-shape — we surface
14/// the scalars callers usually want at the top level.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct Function {
17    #[serde(rename = "FunctionName")]
18    pub function_name: String,
19    #[serde(rename = "FunctionArn", default)]
20    pub function_arn: String,
21    #[serde(rename = "Runtime", default)]
22    pub runtime: String,
23    #[serde(rename = "Role", default)]
24    pub role: String,
25    #[serde(rename = "Handler", default)]
26    pub handler: String,
27    #[serde(rename = "Description", default)]
28    pub description: String,
29    #[serde(rename = "Timeout", default)]
30    pub timeout: i64,
31    #[serde(rename = "MemorySize", default)]
32    pub memory_size: i64,
33    #[serde(rename = "LastModified", default)]
34    pub last_modified: String,
35    #[serde(rename = "Version", default)]
36    pub version: String,
37    #[serde(rename = "PackageType", default)]
38    pub package_type: String,
39}
40
41/// `ListFunctions` table — every function in the configured region.
42/// `MasterRegion` and `FunctionVersion` are the only AWS-side filters;
43/// most callers leave them off and rely on post-hoc narrowing.
44///
45/// Relations:
46///   - `aliases` → `ListAliases` for this function
47///   - `versions` → `ListVersionsByFunction` for this function
48///
49/// Cross-service traversal to CloudWatch Logs is available via the
50/// inherent [`Function::ref_log_group`] helper; the typed `with_foreign`
51/// machinery it previously used was removed in the Vista get_ref rollout,
52/// and `log_group` will reappear as a [`vantage_vista::Vista::with_foreign`]
53/// registration once AWS gains a Vista factory.
54///
55/// ```no_run
56/// # use vantage_aws::AwsAccount;
57/// # use vantage_aws::models::lambda::functions_table;
58/// # async fn run() -> vantage_core::Result<()> {
59/// # let aws = AwsAccount::from_default()?;
60/// let functions = functions_table(aws);
61/// # Ok(()) }
62/// ```
63pub fn functions_table(aws: AwsAccount) -> Table<AwsAccount, Function> {
64    Table::new("restjson/Functions:lambda/GET /2015-03-31/functions/", aws)
65        .with_id_column("FunctionName")
66        .with_column_of::<Arn>("FunctionArn")
67        .with_title_column_of::<String>("Runtime")
68        .with_title_column_of::<String>("Handler")
69        .with_column_of::<String>("Description")
70        .with_column_of::<Arn>("Role")
71        .with_column_of::<i64>("Timeout")
72        .with_column_of::<i64>("MemorySize")
73        .with_column_of::<AwsDateTime>("LastModified")
74        .with_column_of::<String>("Version")
75        .with_column_of::<String>("PackageType")
76        .with_many("aliases", "FunctionName", aliases_table)
77        .with_many("versions", "FunctionName", versions_table)
78}
79
80impl Function {
81    /// Build a [`functions_table`] narrowed to the function named in
82    /// `arn`. Accepts ARNs of the shape
83    /// `arn:aws:lambda:<region>:<account>:function:<name>` (with or
84    /// without a trailing `:<qualifier>`).
85    pub fn from_arn(arn: &str, aws: AwsAccount) -> Option<Table<AwsAccount, Function>> {
86        let after = arn.split(":function:").nth(1)?;
87        // Strip optional version/alias qualifier.
88        let name = after.split(':').next().unwrap_or(after);
89        if name.is_empty() {
90            return None;
91        }
92        let mut t = functions_table(aws);
93        t.add_condition(eq("FunctionName", name.to_string()));
94        Some(t)
95    }
96
97    /// Aliases for *this* function.
98    pub fn ref_aliases(&self, aws: AwsAccount) -> Table<AwsAccount, Alias> {
99        let mut t = aliases_table(aws);
100        t.add_condition(eq("FunctionName", self.function_name.clone()));
101        t
102    }
103
104    /// Published versions for *this* function (always includes
105    /// `$LATEST`).
106    pub fn ref_versions(&self, aws: AwsAccount) -> Table<AwsAccount, Version> {
107        let mut t = versions_table(aws);
108        t.add_condition(eq("FunctionName", self.function_name.clone()));
109        t
110    }
111
112    /// CloudWatch Logs group for *this* function — `/aws/lambda/<name>`
113    /// by default. Returns a [`crate::models::logs::group::LogGroup`]
114    /// table pre-narrowed via `logGroupNamePrefix`.
115    pub fn ref_log_group(&self, aws: AwsAccount) -> Table<AwsAccount, LogGroup> {
116        let mut t = log_groups_table(aws);
117        t.add_condition(eq(
118            "logGroupNamePrefix",
119            format!("/aws/lambda/{}", self.function_name),
120        ));
121        t
122    }
123}