Skip to main content

vantage_aws/models/logs/
group.rs

1use serde::{Deserialize, Serialize};
2use vantage_table::table::Table;
3
4use crate::{AwsAccount, eq};
5
6use super::event::{LogEvent, events_table};
7use super::stream::{LogStream, streams_table};
8
9/// One CloudWatch Logs group. Field names match the wire shape —
10/// these are exactly what `DescribeLogGroups` returns.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct LogGroup {
13    #[serde(rename = "logGroupName")]
14    pub log_group_name: String,
15    #[serde(rename = "creationTime", default)]
16    pub creation_time: i64,
17    #[serde(rename = "storedBytes", default)]
18    pub stored_bytes: i64,
19}
20
21/// `DescribeLogGroups` table — every log group in the account. Filter
22/// by adding `eq("logGroupNamePrefix", "...")`.
23///
24/// Two relations:
25///   - `events` → `FilterLogEvents` for this group
26///   - `streams` → `DescribeLogStreams` for this group
27///
28/// AWS doesn't accept multi-value filters, so the source has to narrow
29/// to a single group before traversal — otherwise the call errors at
30/// execute time.
31///
32/// ```no_run
33/// # use vantage_aws::{AwsAccount, eq};
34/// # use vantage_aws::models::logs::groups_table;
35/// # async fn run() -> vantage_core::Result<()> {
36/// # let aws = AwsAccount::from_default()?;
37/// let mut groups = groups_table(aws);
38/// groups.add_condition(eq("logGroupNamePrefix", "/aws/lambda/"));
39/// # Ok(()) }
40/// ```
41pub fn groups_table(aws: AwsAccount) -> Table<AwsAccount, LogGroup> {
42    Table::new(
43        "json1/logGroups@nextToken:logs/Logs_20140328.DescribeLogGroups",
44        aws,
45    )
46    .with_id_column("logGroupName")
47    .with_column_of::<i64>("creationTime")
48    .with_title_column_of::<i64>("storedBytes")
49    .with_many("events", "logGroupName", events_table)
50    .with_many("streams", "logGroupName", streams_table)
51}
52
53impl LogGroup {
54    /// Build a [`groups_table`] narrowed to the group named in `arn`.
55    ///
56    /// Accepts ARNs of the shape
57    /// `arn:aws:logs:<region>:<account>:log-group:<name>` with an
58    /// optional trailing `:*` (the form returned by IAM policy
59    /// resources). Returns `None` if the ARN isn't a log-group ARN.
60    pub fn from_arn(arn: &str, aws: AwsAccount) -> Option<Table<AwsAccount, LogGroup>> {
61        let after = arn.split(":log-group:").nth(1)?;
62        // Strip the optional :log-stream:... or :* suffix.
63        let name = after
64            .split(":log-stream:")
65            .next()
66            .unwrap_or(after)
67            .trim_end_matches(":*");
68        if name.is_empty() {
69            return None;
70        }
71        let mut t = groups_table(aws);
72        t.add_condition(eq("logGroupNamePrefix", name.to_string()));
73        Some(t)
74    }
75
76    /// Events table pre-filtered to *this* group's name.
77    pub fn ref_events(&self, aws: AwsAccount) -> Table<AwsAccount, LogEvent> {
78        let mut t = events_table(aws);
79        t.add_condition(eq("logGroupName", self.log_group_name.clone()));
80        t
81    }
82
83    /// Streams table pre-filtered to *this* group's name.
84    pub fn ref_streams(&self, aws: AwsAccount) -> Table<AwsAccount, LogStream> {
85        let mut t = streams_table(aws);
86        t.add_condition(eq("logGroupName", self.log_group_name.clone()));
87        t
88    }
89}