Skip to content

Commit

Permalink
Merge pull request #397 from Boavizta/396-add-instance-state-to-the-i…
Browse files Browse the repository at this point in the history
…nventory

feat: return instance state with the inventory.
  • Loading branch information
demeringo authored Jan 1, 2024
2 parents 3e741e5 + 469e74b commit 755ca3b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased (DEV branch)
## Unreleased (main branch but not tagged)

_This paragraph may describe WIP/unreleased features_

### Added

- Return instance state (either *Running* or *Stopped*) with the inventory. https://github.com/Boavizta/cloud-scanner/issues/396.

### Changed

- Use API v1.1.0 in docker-compose (support aditional instances): https://github.com/Boavizta/cloud-scanner/issues/386
Expand Down
22 changes: 20 additions & 2 deletions cloud-scanner-cli/src/aws_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use anyhow::{Context, Result};
use aws_sdk_cloudwatch::operation::get_metric_statistics::GetMetricStatisticsOutput;
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 aws_sdk_ec2::types::{Instance, InstanceStateName};
use chrono::Duration;
use chrono::Utc;

Expand Down Expand Up @@ -109,6 +109,7 @@ impl AwsInventory {
let usage: InstanceUsage = InstanceUsage {
average_cpu_load: cpuload,
usage_duration_seconds: 300,
state: Self::aws_state_to_generic(instance.clone()),
};

let cloud_resource_tags = Self::cloud_resource_tags_from_aws_tags(instance.tags());
Expand Down Expand Up @@ -142,9 +143,26 @@ impl AwsInventory {
Ok(inventory)
}

/// We consider that an instance is running unless explicitly stopped or terminated
fn aws_state_to_generic(instance: Instance) -> InstanceState {
if let Some(state) = instance.state {
if let Some(state_name) = state.name {
match state_name {
InstanceStateName::Stopped => InstanceState::Stopped,
InstanceStateName::Terminated => InstanceState::Stopped,
_ => InstanceState::Running,
}
} else {
InstanceState::Running
}
} else {
InstanceState::Running
}
}

/// List all ec2 instances of the current account / region
///
/// ⚠ Filtering instance on tags is not yet implemented. All instances (running or stopped) are returned.
/// ⚠ Filtering instance on tags during query is not yet implemented. All instances (running or stopped) are returned.
async fn list_instances(self, _tags: &[String]) -> Result<Vec<Instance>> {
let client = &self.ec2_client;
let mut instances: Vec<Instance> = Vec::new();
Expand Down
8 changes: 8 additions & 0 deletions cloud-scanner-cli/src/boavizta_api_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running
}),
},
tags: Vec::new(),
Expand Down Expand Up @@ -378,6 +379,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand All @@ -392,6 +394,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 1.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand Down Expand Up @@ -423,6 +426,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand All @@ -437,6 +441,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand All @@ -451,6 +456,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand Down Expand Up @@ -491,6 +497,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand Down Expand Up @@ -537,6 +544,7 @@ mod tests {
usage: Some(InstanceUsage {
average_cpu_load: 100.0,
usage_duration_seconds: 3600,
state: InstanceState::Running,
}),
},
tags: Vec::new(),
Expand Down
8 changes: 8 additions & 0 deletions cloud-scanner-cli/src/cloud_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ pub enum ResourceDetails {
pub struct InstanceUsage {
pub average_cpu_load: f64,
pub usage_duration_seconds: u32,
pub state: InstanceState,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
pub enum InstanceState {
#[default]
Running,
Stopped,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
Expand Down

0 comments on commit 755ca3b

Please sign in to comment.