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`
52/// rollout; wiring it as a cross-service Vista reference (via
53/// `add_raw_condition` on the AWS shell) is deferred — for now,
54/// reach the function's log group through the inherent helper.
55///
56/// ```no_run
57/// # use vantage_aws::AwsAccount;
58/// # use vantage_aws::models::lambda::functions_table;
59/// # async fn run() -> vantage_core::Result<()> {
60/// # let aws = AwsAccount::from_default()?;
61/// let functions = functions_table(aws);
62/// # Ok(()) }
63/// ```
64pub fn functions_table(aws: AwsAccount) -> Table<AwsAccount, Function> {
65    Table::new("restjson/Functions:lambda/GET /2015-03-31/functions/", aws)
66        .with_id_column("FunctionName")
67        .with_column_of::<Arn>("FunctionArn")
68        .with_title_column_of::<String>("Runtime")
69        .with_title_column_of::<String>("Handler")
70        .with_column_of::<String>("Description")
71        .with_column_of::<Arn>("Role")
72        .with_column_of::<i64>("Timeout")
73        .with_column_of::<i64>("MemorySize")
74        .with_column_of::<AwsDateTime>("LastModified")
75        .with_column_of::<String>("Version")
76        .with_column_of::<String>("PackageType")
77        .with_many("aliases", "FunctionName", aliases_table)
78        .with_many("versions", "FunctionName", versions_table)
79}
80
81impl Function {
82    /// Build a [`functions_table`] narrowed to the function named in
83    /// `arn`. Accepts ARNs of the shape
84    /// `arn:aws:lambda:<region>:<account>:function:<name>` (with or
85    /// without a trailing `:<qualifier>`).
86    pub fn from_arn(arn: &str, aws: AwsAccount) -> Option<Table<AwsAccount, Function>> {
87        let after = arn.split(":function:").nth(1)?;
88        // Strip optional version/alias qualifier.
89        let name = after.split(':').next().unwrap_or(after);
90        if name.is_empty() {
91            return None;
92        }
93        let mut t = functions_table(aws);
94        t.add_condition(eq("FunctionName", name.to_string()));
95        Some(t)
96    }
97
98    /// Aliases for *this* function.
99    pub fn ref_aliases(&self, aws: AwsAccount) -> Table<AwsAccount, Alias> {
100        let mut t = aliases_table(aws);
101        t.add_condition(eq("FunctionName", self.function_name.clone()));
102        t
103    }
104
105    /// Published versions for *this* function (always includes
106    /// `$LATEST`).
107    pub fn ref_versions(&self, aws: AwsAccount) -> Table<AwsAccount, Version> {
108        let mut t = versions_table(aws);
109        t.add_condition(eq("FunctionName", self.function_name.clone()));
110        t
111    }
112
113    /// CloudWatch Logs group for *this* function — `/aws/lambda/<name>`
114    /// by default. Returns a [`crate::models::logs::group::LogGroup`]
115    /// table pre-narrowed via `logGroupNamePrefix`.
116    pub fn ref_log_group(&self, aws: AwsAccount) -> Table<AwsAccount, LogGroup> {
117        let mut t = log_groups_table(aws);
118        t.add_condition(eq(
119            "logGroupNamePrefix",
120            format!("/aws/lambda/{}", self.function_name),
121        ));
122        t
123    }
124}