use serde::{Deserialize, Serialize};
use vantage_table::table::Table;
use crate::{AwsAccount, eq};
use super::service::{Service, services_table};
use super::task::{Task, tasks_table};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cluster {
#[serde(rename = "clusterArn")]
pub cluster_arn: String,
}
pub fn clusters_table(aws: AwsAccount) -> Table<AwsAccount, Cluster> {
Table::new(
"json1/clusterArns@nextToken:ecs/AmazonEC2ContainerServiceV20141113.ListClusters",
aws,
)
.with_id_column("clusterArn")
.with_many("services", "cluster", services_table)
.with_many("tasks", "cluster", tasks_table)
}
impl Cluster {
pub fn from_arn(arn: &str, aws: AwsAccount) -> Option<Table<AwsAccount, Cluster>> {
if !arn.contains(":cluster/") {
return None;
}
let mut t = clusters_table(aws);
t.add_condition(eq("clusterArn", arn.to_string()));
Some(t)
}
pub fn name(&self) -> Option<&str> {
self.cluster_arn.rsplit('/').next()
}
pub fn ref_services(&self, aws: AwsAccount) -> Table<AwsAccount, Service> {
let mut t = services_table(aws);
t.add_condition(eq("cluster", self.cluster_arn.clone()));
t
}
pub fn ref_tasks(&self, aws: AwsAccount) -> Table<AwsAccount, Task> {
let mut t = tasks_table(aws);
t.add_condition(eq("cluster", self.cluster_arn.clone()));
t
}
}