Skip to main content

vantage_aws/models/ecs/
cluster.rs

1use serde::{Deserialize, Serialize};
2use vantage_table::table::Table;
3
4use crate::{AwsAccount, eq};
5
6use super::service::{Service, services_table};
7use super::task::{Task, tasks_table};
8
9/// One ECS cluster — `ListClusters` returns just an ARN per row, so
10/// that's all you get without a follow-up `DescribeClusters` call.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Cluster {
13    #[serde(rename = "clusterArn")]
14    pub cluster_arn: String,
15}
16
17/// `ListClusters` table — every cluster in the account / region.
18///
19/// ```no_run
20/// # use vantage_aws::AwsAccount;
21/// # use vantage_aws::models::ecs::clusters_table;
22/// # async fn run() -> vantage_core::Result<()> {
23/// # let aws = AwsAccount::from_default()?;
24/// let clusters = clusters_table(aws);
25/// # Ok(()) }
26/// ```
27pub fn clusters_table(aws: AwsAccount) -> Table<AwsAccount, Cluster> {
28    Table::new(
29        "json1/clusterArns@nextToken:ecs/AmazonEC2ContainerServiceV20141113.ListClusters",
30        aws,
31    )
32    .with_id_column("clusterArn")
33    .with_many("services", "cluster", services_table)
34    .with_many("tasks", "cluster", tasks_table)
35}
36
37impl Cluster {
38    /// Build a [`clusters_table`] narrowed to the cluster identified
39    /// by `arn`. Accepts ARNs of the shape
40    /// `arn:aws:ecs:<region>:<account>:cluster/<name>`.
41    pub fn from_arn(arn: &str, aws: AwsAccount) -> Option<Table<AwsAccount, Cluster>> {
42        if !arn.contains(":cluster/") {
43            return None;
44        }
45        let mut t = clusters_table(aws);
46        t.add_condition(eq("clusterArn", arn.to_string()));
47        Some(t)
48    }
49
50    /// The cluster's short name, parsed out of [`Self::cluster_arn`].
51    /// Cluster ARNs have the shape
52    /// `arn:aws:ecs:<region>:<account>:cluster/<name>`.
53    pub fn name(&self) -> Option<&str> {
54        self.cluster_arn.rsplit('/').next()
55    }
56
57    /// Services table pre-filtered to *this* cluster.
58    pub fn ref_services(&self, aws: AwsAccount) -> Table<AwsAccount, Service> {
59        let mut t = services_table(aws);
60        t.add_condition(eq("cluster", self.cluster_arn.clone()));
61        t
62    }
63
64    /// Tasks table pre-filtered to *this* cluster.
65    pub fn ref_tasks(&self, aws: AwsAccount) -> Table<AwsAccount, Task> {
66        let mut t = tasks_table(aws);
67        t.add_condition(eq("cluster", self.cluster_arn.clone()));
68        t
69    }
70}