diff --git a/cloud-scanner-cli/src/aws_cloud_provider.rs b/cloud-scanner-cli/src/aws_cloud_provider.rs index 4891c874..696a6c7b 100644 --- a/cloud-scanner-cli/src/aws_cloud_provider.rs +++ b/cloud-scanner-cli/src/aws_cloud_provider.rs @@ -431,12 +431,12 @@ mod tests { async fn get_cpu_usage_metrics_of_running_instance_should_return_right_number_of_data_points() { let aws: AwsCloudProvider = AwsCloudProvider::new("eu-west-1").await.unwrap(); let res = aws - .get_average_cpu_usage_of_last_10_minutes(&RUNNING_INSTANCE_ID) + .get_average_cpu_usage_of_last_10_minutes(RUNNING_INSTANCE_ID) .await .unwrap(); let datapoints = res.datapoints.unwrap(); assert!( - 0 < datapoints.len() && datapoints.len() < 3, + !datapoints.is_empty() && datapoints.len() < 3, "Strange number of datapoint returned for instance {}, is it really up ?. I was expecting 1 or 2 but got {} .\n {:#?}", &RUNNING_INSTANCE_ID, datapoints.len(), @@ -476,15 +476,15 @@ mod tests { // This instance needs to be running for the test to pass let aws: AwsCloudProvider = AwsCloudProvider::new("eu-west-1").await.unwrap(); - let avg_cpu_load = aws.get_average_cpu(&RUNNING_INSTANCE_ID).await.unwrap(); + let avg_cpu_load = aws.get_average_cpu(RUNNING_INSTANCE_ID).await.unwrap(); assert_ne!( 0 as f64, avg_cpu_load, "CPU load of instance {} is zero, is it really running ?", &RUNNING_INSTANCE_ID ); println!("{:#?}", avg_cpu_load); - assert!((0 as f64) < avg_cpu_load); - assert!((100 as f64) > avg_cpu_load); + assert!(0_f64 < avg_cpu_load); + assert!(100_f64 > avg_cpu_load); } #[tokio::test] diff --git a/cloud-scanner-cli/src/boavizta_api_v1.rs b/cloud-scanner-cli/src/boavizta_api_v1.rs index 32b2610d..b2188412 100644 --- a/cloud-scanner-cli/src/boavizta_api_v1.rs +++ b/cloud-scanner-cli/src/boavizta_api_v1.rs @@ -377,7 +377,7 @@ mod tests { tags: Vec::new(), }; let api: BoaviztaApiV1 = BoaviztaApiV1::new(TEST_API_URL); - let one_hour = 1.0 as f32; + let one_hour = 1.0_f32; let res = api .get_raws_impacts(instance1, &one_hour, false) .await @@ -404,7 +404,7 @@ mod tests { }; let api: BoaviztaApiV1 = BoaviztaApiV1::new(TEST_API_URL); - let one_hour = 1.0 as f32; + let one_hour = 1.0_f32; let res = api.get_raws_impacts(hdd, &one_hour, true).await.unwrap(); let expected: serde_json::Value = serde_json::from_str(DEFAULT_RAW_IMPACTS_OF_HDD).unwrap(); @@ -429,7 +429,7 @@ mod tests { }; let api: BoaviztaApiV1 = BoaviztaApiV1::new(TEST_API_URL); - let one_hour = 1.0 as f32; + let one_hour = 1.0_f32; let res = api.get_raws_impacts(ssd, &one_hour, true).await.unwrap(); let expected: serde_json::Value = @@ -470,11 +470,9 @@ mod tests { }; let api: BoaviztaApiV1 = BoaviztaApiV1::new(TEST_API_URL); - let one_hour = 1.0 as f32; + let one_hour = 1.0_f32; - let mut instances: Vec = Vec::new(); - instances.push(instance1); - instances.push(instance1_1percent); + let instances: Vec = vec![instance1, instance1_1percent]; let inventory = Inventory { metadata: InventoryMetadata { @@ -538,11 +536,8 @@ mod tests { tags: Vec::new(), }; - let mut instances: Vec = Vec::new(); - instances.push(instance1); - instances.push(instance2); - instances.push(instance3); - let one_hour = 1.0 as f32; + let instances: Vec = vec![instance1, instance2, instance3]; + let one_hour = 1.0_f32; let inventory = Inventory { metadata: InventoryMetadata { @@ -590,7 +585,7 @@ mod tests { let raw_impacts = Some(serde_json::from_str(DEFAULT_RAW_IMPACTS_OF_M6GXLARGE_1HRS_FR).unwrap()); - let one_hour: f32 = 1 as f32; + let one_hour: f32 = 1_f32; let cloud_resource_with_impacts: CloudResourceWithImpacts = boa_impacts_to_cloud_resource_with_impacts(&instance1, &raw_impacts, &one_hour); assert!( @@ -636,7 +631,7 @@ mod tests { let raw_impacts = Some(serde_json::from_str(DEFAULT_RAW_IMPACTS_OF_M6GXLARGE_1HRS_FR_VERBOSE).unwrap()); - let one_hour: f32 = 1 as f32; + let one_hour: f32 = 1_f32; let cloud_resource_with_impacts: CloudResourceWithImpacts = boa_impacts_to_cloud_resource_with_impacts(&instance1, &raw_impacts, &one_hour); assert!( diff --git a/cloud-scanner-cli/src/main.rs b/cloud-scanner-cli/src/main.rs index b95a0b02..f8110373 100644 --- a/cloud-scanner-cli/src/main.rs +++ b/cloud-scanner-cli/src/main.rs @@ -21,7 +21,7 @@ struct Arguments { aws_region: Option, #[arg(short, long)] - /// Optional Boavizta API URL if you want to use your own instance (URL without the trailing slash, e.g. https://api.boavizta.org) + /// Optional Boavizta API URL if you want to use your own instance (URL without the trailing slash, e.g. ) boavizta_api_url: Option, #[arg(short = 't', long)] @@ -42,7 +42,7 @@ enum SubCommand { use_duration_hours: f32, #[arg(long, short = 'f', action)] - /// Retrieve and output the details from BoaviztaAPI (equivalent to the verbose flag when querying Boavizta API) + /// Retrieve and output the details from `BoaviztaAPI` (equivalent to the verbose flag when querying Boavizta API) output_verbose_json: bool, #[arg(long, short = 'b', action)] @@ -57,7 +57,7 @@ enum SubCommand { #[arg(short, long)] inventory_file: Option, }, - /// Get estimation of impacts for a given usage duration as OpenMetrics (Prometheus) instead of json + /// Get estimation of impacts for a given usage duration as `OpenMetrics` (Prometheus) instead of json Metrics { #[arg(short = 'u', long)] /// The number of hours of use for which we want to estimate the impacts @@ -78,32 +78,29 @@ enum SubCommand { print_json_schema: bool, }, /// Run as a standalone server. - /// Access metrics (e.g. http://localhost:8000/metrics?aws_region=eu-west-3), inventory or impacts (see http://localhost:8000/swagger-ui) + /// Access metrics (e.g. ), inventory or impacts (seui) Serve {}, } fn set_region(optional_region: Option) -> String { - match optional_region { - Some(region_arg) => { - info!("Using region: {}", region_arg); - region_arg - } - None => "".to_owned(), - } + optional_region.map_or_else(String::new, |region_arg| { + info!("Using region: {}", region_arg); + region_arg + }) } fn set_api_url(optional_url: Option) -> String { - match optional_url { - Some(url_arg) => { - info!("Using API at: {}", url_arg); - url_arg - } - None => { + optional_url.map_or_else( + || { let default_url = "https://api.boavizta.org".to_string(); - warn!("Using default API at: {}", default_url); + warn!("Using default API at: {default_url}"); default_url - } - } + }, + |url_arg| { + info!("Using API at: {}", url_arg); + url_arg + }, + ) } #[tokio::main] @@ -126,8 +123,8 @@ async fn main() -> Result<()> { output_verbose_json, summary_only, inventory_file, - } => match inventory_file { - Some(path) => { + } => { + if let Some(path) = inventory_file { info!("Providing estimation for inventory file"); let i = cloud_scanner_cli::estimate_impacts_of_inventory_file( &use_duration_hours, @@ -137,8 +134,7 @@ async fn main() -> Result<()> { ) .await?; println!("{}", serde_json::to_string(&i)?); - } - None => { + } else { info!("Providing estimation for live inventory"); let i: EstimatedInventory = cloud_scanner_cli::estimate_impacts( &use_duration_hours, @@ -152,9 +148,9 @@ async fn main() -> Result<()> { let result = get_estimated_inventory_as_json(&i, ®ion, &use_duration_hours, summary_only) .await?; - println!("{}", result); + println!("{result}"); } - }, + } SubCommand::Metrics { use_duration_hours, include_block_storage, @@ -168,7 +164,7 @@ async fn main() -> Result<()> { include_block_storage, ) .await?; - println!("{}", metrics); + println!("{metrics}"); } SubCommand::Inventory { include_block_storage, diff --git a/cloud-scanner-cli/src/model.rs b/cloud-scanner-cli/src/model.rs index 8368678e..a0e952d7 100644 --- a/cloud-scanner-cli/src/model.rs +++ b/cloud-scanner-cli/src/model.rs @@ -285,11 +285,10 @@ mod tests { }, ); - let mut instance1tags: Vec = Vec::new(); - instance1tags.push(CloudResourceTag { + let instance1tags: Vec = vec![CloudResourceTag { key: "Name".to_string(), value: Some("App1".to_string()), - }); + }]; let instance1: CloudResource = CloudResource { provider: CloudProvider::AWS, @@ -302,8 +301,7 @@ mod tests { tags: instance1tags, }; - assert_eq!( - true, + assert!( instance1.has_matching_tagmap(&filtertags), "Tags should match" ); @@ -317,9 +315,8 @@ mod tests { value: Some("OtherApp".to_string()), }, ); - assert_eq!( - false, - instance1.has_matching_tagmap(&other_name_tag), + assert!( + !instance1.has_matching_tagmap(&other_name_tag), "Tags should not match" ); @@ -332,9 +329,8 @@ mod tests { value: Some("PROD".to_string()), }, ); - assert_eq!( - false, - instance1.has_matching_tagmap(&more_tags), + assert!( + !instance1.has_matching_tagmap(&more_tags), "Tags should not match" ); @@ -347,16 +343,14 @@ mod tests { value: None, }, ); - assert_eq!( - false, - instance1.has_matching_tagmap(&tag_without_val), + assert!( + !instance1.has_matching_tagmap(&tag_without_val), "Tag without a value should not match" ); // Trying an empty filter let empty_filter = HashMap::new(); - assert_eq!( - true, + assert!( instance1.has_matching_tagmap(&empty_filter), "Tags should match" ); @@ -372,8 +366,7 @@ mod tests { value: Some("whatever".to_string()), }, ); - assert_eq!( - true, + assert!( instance1.has_matching_tagmap(&empty_filter), "Tags should match (i.e. we should ignore this invalid filter" ); diff --git a/cloud-scanner-lambda/src/main.rs b/cloud-scanner-lambda/src/main.rs index ef411770..c126369d 100644 --- a/cloud-scanner-lambda/src/main.rs +++ b/cloud-scanner-lambda/src/main.rs @@ -2,7 +2,7 @@ use lambda_http::{http::StatusCode, IntoResponse, Request, RequestExt, Response} /*use lambda_http::*;*/ use lambda_runtime::Error; -use pkg_version::*; +use pkg_version::{pkg_version_major, pkg_version_minor, pkg_version_patch}; use serde_json::json; extern crate log; @@ -23,7 +23,7 @@ async fn main() -> Result<(), Error> { async fn scan(event: Request) -> Result { let config = match envy::from_env::() { Ok(config) => config, - Err(error) => panic!("{:#?}", error), + Err(error) => panic!("{error:#?}"), }; println!( @@ -31,8 +31,8 @@ async fn scan(event: Request) -> Result { get_version(), cloud_scanner_cli::get_version() ); - println!("Using config {:#?}", config); - println!("Scan account invoked with event : {:?}", event); + println!("Using config {config:#?}"); + println!("Scan account invoked with event : {event:?}"); let query_string_parameters = event.query_string_parameters(); @@ -47,41 +47,41 @@ async fn scan(event: Request) -> Result { } }; - let aws_region = match query_string_parameters.first("aws_region") { - Some(aws_region) => aws_region, - None => { - println!("No 'aws_region' parameter in path, will fallback to default"); - "" - } - }; + let aws_region = query_string_parameters + .first("aws_region") + .unwrap_or_default(); + if aws_region.is_empty() { + println!("No 'aws_region' parameter in path, will fallback to default"); + } - let filter_tags = match query_string_parameters.all("filter_tag") { - Some(filter_tags) => filter_tags.iter().map(|t| t.to_string()).collect(), - None => { - let filter_tags: Vec = Vec::new(); - println!("No 'filter_tag' parameter in path, will fallback to default"); - filter_tags - } - }; + let filter_tags: Vec = query_string_parameters + .all("filter_tag") + .unwrap_or_default() + .iter() + .map(ToString::to_string) + .collect(); + if filter_tags.is_empty() { + println!("No 'filter_tag' parameter in path, will fallback to default"); + } - let verbose_output: bool = match query_string_parameters.first("verbose_output") { - Some(verbose_string) => verbose_string.parse().unwrap_or(false), - None => false, - }; + let verbose_output: bool = query_string_parameters + .first("verbose_output") + .and_then(|s| s.parse().ok()) + .unwrap_or(false); - let include_block_storage: bool = match query_string_parameters.first("include_block_storage") { - Some(include_block_storage_string) => include_block_storage_string.parse().unwrap_or(false), - None => false, - }; + let include_block_storage: bool = query_string_parameters + .first("include_block_storage") + .and_then(|s| s.parse().ok()) + .unwrap_or(false); - let summary_only: bool = match query_string_parameters.first("summary_only") { - Some(summary_only_string) => summary_only_string.parse().unwrap_or(false), - None => false, - }; + let summary_only: bool = query_string_parameters + .first("summary_only") + .and_then(|s| s.parse().ok()) + .unwrap_or(false); - println!("Using use time of {}", use_duration_hours); - println!("Using aws_region {}", aws_region); - println!("Using tag filers {:?}", filter_tags); + println!("Using use time of {use_duration_hours}"); + println!("Using aws_region {aws_region}"); + println!("Using tag filers {filter_tags:?}"); let estimated_inventory = cloud_scanner_cli::estimate_impacts( &use_duration_hours, @@ -95,7 +95,7 @@ async fn scan(event: Request) -> Result { let json_impacts = cloud_scanner_cli::estimated_inventory_exporter::get_estimated_inventory_as_json( &estimated_inventory, - &aws_region, + aws_region, &use_duration_hours, summary_only, )