From b1bd29885f0595a6b4f4e210b542ca52fe858d92 Mon Sep 17 00:00:00 2001 From: olivier de Meringo Date: Thu, 31 Aug 2023 12:26:12 +0200 Subject: [PATCH] feat: list volumes (WIP) --- cloud-scanner-cli/src/aws_inventory.rs | 36 +++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/cloud-scanner-cli/src/aws_inventory.rs b/cloud-scanner-cli/src/aws_inventory.rs index 0779181..c942d35 100644 --- a/cloud-scanner-cli/src/aws_inventory.rs +++ b/cloud-scanner-cli/src/aws_inventory.rs @@ -11,6 +11,7 @@ use aws_sdk_cloudwatch::operation::get_metric_statistics::GetMetricStatisticsOut use aws_sdk_cloudwatch::types::{Dimension, StandardUnit, Statistic}; use aws_sdk_ec2::config::Region; use aws_sdk_ec2::types::Instance; +use aws_sdk_ec2::types::Volume; use chrono::Duration; use chrono::Utc; @@ -45,7 +46,7 @@ impl AwsInventory { // Use default region (from env) let sdk_config = aws_config::from_env().load().await; warn!( - "Cannot initialize from empty region, falling back to using default region from environement [{}]", + "Cannot initialize from empty region, falling back to using default region from environment [{}]", sdk_config.region().unwrap() ); sdk_config @@ -154,6 +155,30 @@ impl AwsInventory { Ok(resp) } + + + /// List all Volumes of current account. + /// + /// ⚠ Filtering on tags is not yet implemented. + pub async fn list_volumes(self, tags: &[String]) -> Result> { + warn!("Warning: filtering on tags is not implemented {:?}", tags); + + let client = &self.ec2_client; + let mut volumes: Vec = Vec::new(); + // Filter: AND on name, OR on values + //let filters :std::vec::Vec; + + let resp = client + .describe_volumes() + //set_filters() // Use filters for tags + .send() + .await?; + + for v in resp.volumes().unwrap_or_default() { + volumes.push(v.clone()); + } + Ok(volumes) + } } #[async_trait] @@ -356,4 +381,13 @@ mod tests { let res = inventory.get_average_cpu(instance_id).await.unwrap(); assert_eq!(0 as f64, res); } + + #[tokio::test] + #[ignore] + async fn returns_the_right_number_of_volumes() { + let inventory: AwsInventory = AwsInventory::new("eu-west-1").await; + let filtertags: Vec = Vec::new(); + let res = inventory.list_volumes(&filtertags).await.unwrap(); + assert_eq!(4 , res.len()); + } }