Skip to content

Commit 55695cb

Browse files
authored
Merge pull request #559 from Boavizta/558-expose-the-json-schema-of-the-inventory-format
feat: add CLI option to get the json schema of inventory file.
2 parents 6a7336f + 3e427b0 commit 55695cb

File tree

7 files changed

+367
-8
lines changed

7 files changed

+367
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
_This paragraph may describe WIP/unreleased features. They are merged to main branch but not tagged._
1111

1212
- [Doc: reference Boavizta methodology paper. · Issue #552 · Boavizta/cloud-scanner](https://github.com/Boavizta/cloud-scanner/issues/552)
13+
- [Expose the json schema of the inventory format · Issue #558 · Boavizta/cloud-scanner](https://github.com/Boavizta/cloud-scanner/issues/558). Use `cargo run inventory --print-json-schema` with CLI to get the schema on stdout.
1314

1415
## [3.0.1]-2024-06-19
1516

cloud-scanner-cli/src/inventory_exporter.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Context;
2+
use schemars::schema_for;
23

34
use crate::model::Inventory;
45

@@ -13,3 +14,31 @@ pub async fn print_inventory(inventory: &Inventory) -> anyhow::Result<()> {
1314
println!("{}", json_inventory);
1415
Ok(())
1516
}
17+
18+
/// Returns the json schema of an inventory as String
19+
pub fn get_inventory_schema() -> anyhow::Result<String> {
20+
let schema = schema_for!(Inventory);
21+
let st = serde_json::to_string_pretty(&schema)?;
22+
Ok(st)
23+
}
24+
25+
/// Print inventory schema on stdout
26+
pub fn print_inventory_schema() -> anyhow::Result<()> {
27+
let s = get_inventory_schema()?;
28+
println!("{}", s);
29+
Ok(())
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use crate::inventory_exporter::get_inventory_schema;
35+
const INVENTORY_JSON_SCHEMA: &str = include_str!("../test-data/INVENTORY_JSON_SCHEMA.json");
36+
37+
#[test]
38+
pub fn generate_inventory_schema() {
39+
let s = get_inventory_schema().unwrap();
40+
println!("{}", s);
41+
42+
assert_eq!(s, INVENTORY_JSON_SCHEMA, "schema do not match");
43+
}
44+
}

cloud-scanner-cli/src/main.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ enum SubCommand {
7272
#[arg(long, short = 'b', action)]
7373
/// Experimental feature: include block storage in the inventory
7474
include_block_storage: bool,
75+
76+
/// Print the json schema of the inventory (instead of performing inventory)
77+
#[arg(short = 's', long)]
78+
print_json_schema: bool,
7579
},
7680
/// Run as a standalone server.
7781
/// Access metrics (e.g. http://localhost:8000/metrics?aws_region=eu-west-3), inventory or impacts (see http://localhost:8000/swagger-ui)
@@ -168,12 +172,20 @@ async fn main() -> Result<()> {
168172
}
169173
SubCommand::Inventory {
170174
include_block_storage,
175+
print_json_schema,
171176
} => {
172-
info!("Using filter tags {:?}", &args.filter_tags);
173-
let inventory =
174-
cloud_scanner_cli::get_inventory(&args.filter_tags, &region, include_block_storage)
175-
.await?;
176-
print_inventory(&inventory).await?;
177+
if print_json_schema {
178+
cloud_scanner_cli::inventory_exporter::print_inventory_schema()?;
179+
} else {
180+
info!("Using filter tags {:?}", &args.filter_tags);
181+
let inventory = cloud_scanner_cli::get_inventory(
182+
&args.filter_tags,
183+
&region,
184+
include_block_storage,
185+
)
186+
.await?;
187+
print_inventory(&inventory).await?;
188+
}
177189
}
178190
SubCommand::Serve {} => cloud_scanner_cli::serve_metrics(&api_url).await?,
179191
}

cloud-scanner-cli/src/model.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{fmt, fs};
1212
use crate::impact_provider::CloudResourceWithImpacts;
1313
use crate::usage_location::UsageLocation;
1414

15-
/// Statistics about program execution
15+
/// Statistics about program execution
1616
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
1717
#[serde(rename_all = "snake_case")]
1818
pub struct ExecutionStatistics {
@@ -27,7 +27,7 @@ impl fmt::Display for ExecutionStatistics {
2727
}
2828
}
2929

30-
/// Inventory: a list of resources
30+
/// A list of cloud resources and metadata that describes the inventory itself
3131
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
3232
#[serde(rename_all = "snake_case")]
3333
pub struct Inventory {
@@ -39,10 +39,13 @@ pub struct Inventory {
3939
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
4040
#[serde(rename_all = "snake_case")]
4141
pub struct InventoryMetadata {
42+
/// The date when the inventory was generated
4243
pub inventory_date: Option<DateTime<Utc>>,
44+
/// A free text description of the inventory
4345
pub description: Option<String>,
4446
/// The version of the cloud scanner that generated the inventory
4547
pub cloud_scanner_version: Option<String>,
48+
/// Statistics about program execution
4649
pub execution_statistics: Option<ExecutionStatistics>,
4750
}
4851

@@ -67,11 +70,12 @@ pub struct EstimatedInventory {
6770
pub execution_statistics: Option<ExecutionStatistics>,
6871
}
6972

70-
/// A cloud resource (could be an instance, function or any other resource)
73+
/// A cloud resource (could be an instance, block storage or any other resource)
7174
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
7275
pub struct CloudResource {
7376
pub provider: CloudProvider,
7477
pub id: String,
78+
/// The location where cloud resources are running.
7579
pub location: UsageLocation,
7680
pub resource_details: ResourceDetails,
7781
pub tags: Vec<CloudResourceTag>,

0 commit comments

Comments
 (0)