Skip to main content

vantage_aws/models/ecs/
task.rs

1use serde::{Deserialize, Serialize};
2use vantage_table::table::Table;
3
4use crate::{AwsAccount, eq};
5
6use crate::models::logs::event::{LogEvent, events_table};
7
8/// One ECS task — `ListTasks` returns just an ARN per row.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Task {
11    #[serde(rename = "taskArn")]
12    pub task_arn: String,
13}
14
15/// `ListTasks` table. AWS requires `cluster` (a name or ARN) before it
16/// will list anything, so add `eq("cluster", "...")` first or traverse
17/// from a [`Cluster`](super::cluster::Cluster).
18///
19/// Also accepts the optional filters AWS supports as conditions:
20/// `serviceName`, `family`, `desiredStatus` (`RUNNING` / `PENDING` /
21/// `STOPPED`), `launchType`, `containerInstance`.
22pub fn tasks_table(aws: AwsAccount) -> Table<AwsAccount, Task> {
23    Table::new(
24        "json1/taskArns@nextToken:ecs/AmazonEC2ContainerServiceV20141113.ListTasks",
25        aws,
26    )
27    .with_id_column("taskArn")
28}
29
30impl Task {
31    /// The task's id, parsed out of [`Self::task_arn`]. ECS task ARNs
32    /// have the shape `arn:aws:ecs:<region>:<account>:task/<cluster>/<id>`.
33    pub fn task_id(&self) -> Option<&str> {
34        self.task_arn.rsplit('/').next()
35    }
36
37    /// Log events from the given log group, with `logStreamNamePrefix`
38    /// set to this task's id.
39    ///
40    /// ECS log streams typically follow the pattern
41    /// `<streamPrefix>/<containerName>/<taskId>` — task id is at the
42    /// END, so this prefix-match doesn't directly find a task's
43    /// streams. Use it instead to point at a known prefix and combine
44    /// with a `filterPattern` that includes the task id.
45    ///
46    /// Most callers will already know the streamPrefix from the task
47    /// definition; pass that as `prefix` instead of the raw task id.
48    pub fn ref_log_events(
49        &self,
50        aws: AwsAccount,
51        log_group_name: &str,
52        prefix: &str,
53    ) -> Table<AwsAccount, LogEvent> {
54        let mut t = events_table(aws);
55        t.add_condition(eq("logGroupName", log_group_name));
56        t.add_condition(eq("logStreamNamePrefix", prefix));
57        t
58    }
59}