Skip to content

Commit

Permalink
[Docs] Providing an example for the validate command (#487)
Browse files Browse the repository at this point in the history
* providing a getting started example of how to use the validate command from the library

* cleanup
  • Loading branch information
joshfried-aws authored Mar 27, 2024
1 parent 3dc0f22 commit 8fa7615
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
36 changes: 25 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
members = [
"guard",
"guard-lambda",
"guard-ffi"
"guard-ffi",
"guard-examples/library"
]
10 changes: 10 additions & 0 deletions guard-examples/library/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "library"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cfn-guard = { version = "3.1.0-beta", path = "../../guard" }
anyhow = "1.0.81"
43 changes: 43 additions & 0 deletions guard-examples/library/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::io::Cursor;

use anyhow::Context;
use cfn_guard::{
commands::{
validate::{OutputFormatType, ShowSummaryType},
Executable,
},
utils::{
reader::{ReadBuffer, Reader},
writer::{WriteBuffer, Writer},
},
CommandBuilder, ValidateBuilder,
};

#[derive(Debug)]
pub struct Payload {
pub data: String,
pub rules: Vec<String>,
}

fn main() -> anyhow::Result<()> {
let payload = r#"{"data": ["{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2c\"}}},\"Parameters\":{\"InstanceName\":\"TestInstance\"}}","{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2c\"}}},\"Parameters\":{\"InstanceName\":\"TestInstance\"}}"], "rules" : [ "Parameters.InstanceName == \"TestInstance\"","Parameters.InstanceName == \"TestInstance\"" ]}"#;
let mut reader = Reader::new(ReadBuffer::Cursor(Cursor::new(Vec::from(
payload.as_bytes(),
))));
let mut writer = Writer::new_with_err(WriteBuffer::Vec(vec![]), WriteBuffer::Vec(vec![]));

let cmd = ValidateBuilder::default()
.payload(true)
.output_format(OutputFormatType::JSON)
.structured(true)
.show_summary(vec![ShowSummaryType::None])
.try_build()
.context("failed to build validate command")?;

cmd.execute(&mut writer, &mut reader)?;

let content = writer.stripped().context("failed to read from writer")?;
println!("{content}");

Ok(())
}

0 comments on commit 8fa7615

Please sign in to comment.