From cfa782ec535d0d2c2488a4ffa7c9fc7688b0273d Mon Sep 17 00:00:00 2001 From: Sean Tyler Myers Date: Thu, 9 Feb 2023 15:28:00 -0800 Subject: [PATCH] Add Fn::Cidr support --- README.md | 6 +-- src/ir/resources.rs | 18 ++++++++ src/parser/resource.rs | 50 +++++++++++++++++++++++ src/synthesizer/typescript_synthesizer.rs | 8 ++++ 4 files changed, 78 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6a779573..60e4675c 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,12 @@ cargo build --release - [x] Fn::GetAZs support - [x] Adding depends-on, and ordering based on it too. - [x] Deletion policy - +- [x] Fn::Cidr support + ## Remaining There are known unsupported features. Working on them in priority order: - - -- [ ] Fn::Cidr support - [ ] Create policy - [ ] ssm metadata references - [ ] secretsmanager references diff --git a/src/ir/resources.rs b/src/ir/resources.rs index 1afbbb1d..6713bbd3 100644 --- a/src/ir/resources.rs +++ b/src/ir/resources.rs @@ -35,6 +35,7 @@ pub enum ResourceIr { ImportValue(Box), GetAZs(Box), Select(i64, Box), + Cidr(Box, Box, Box), } /// ResourceTranslationInputs is a place to store all the intermediate recursion @@ -224,6 +225,11 @@ fn find_dependencies( ResourceIr::GetAZs(x) => { find_dependencies(resource_name, x.deref(), topo); } + ResourceIr::Cidr(x, y, z) => { + find_dependencies(resource_name, x.deref(), topo); + find_dependencies(resource_name, y.deref(), topo); + find_dependencies(resource_name, z.deref(), topo); + } } } @@ -447,6 +453,18 @@ pub fn translate_resource( let ir = translate_resource(x, resource_translator)?; Ok(ResourceIr::GetAZs(Box::new(ir))) } + ResourceValue::Cidr(ip_block, count, cidr_bits) => { + let mut rt = resource_translator.clone(); + rt.complexity = Structure::Simple(CfnType::String); + let ip_block_str = translate_resource(ip_block, &rt)?; + let count_str = translate_resource(count, &rt)?; + let cidr_bits_str = translate_resource(cidr_bits, &rt)?; + Ok(ResourceIr::Cidr( + Box::new(ip_block_str), + Box::new(count_str), + Box::new(cidr_bits_str), + )) + } } } diff --git a/src/parser/resource.rs b/src/parser/resource.rs index d139f913..194061b7 100644 --- a/src/parser/resource.rs +++ b/src/parser/resource.rs @@ -27,6 +27,7 @@ pub enum ResourceValue { Base64(Box), ImportValue(Box), Select(Box, Box), + Cidr(Box, Box, Box), } impl ResourceValue {} @@ -420,6 +421,55 @@ pub fn build_resources_recursively( ResourceValue::Join(v) } + "Fn::Cidr" => { + let v = match resource_object.as_array() { + None => { + return Err(TransmuteError { + details: format!( + "Fn::Cidr is supposed to be an array entry {name}" + ), + }) + } + Some(x) => x, + }; + + let first_obj = match v.get(0) { + None => { + return Err(TransmuteError { + details: format!( + "Fn::Cidr is supposed to have 3 values in array, has 0 {name}" + ), + }) + } + Some(x) => build_resources_recursively(name, x), + }?; + let second_obj = match v.get(1) { + None => { + return Err(TransmuteError { + details: format!( + "Fn::Cidr is supposed to have 3 values in array, has 1 {name}" + ), + }) + } + Some(x) => build_resources_recursively(name, x), + }?; + let third_obj = match v.get(2) { + None => { + return Err(TransmuteError { + details: format!( + "Fn::Cidr is supposed to have 3 values in array, has 2 {name}" + ), + }) + } + Some(x) => build_resources_recursively(name, x), + }?; + + ResourceValue::Cidr( + Box::new(first_obj), + Box::new(second_obj), + Box::new(third_obj), + ) + } "Ref" => { let ref_name = match resource_object.as_str() { None => { diff --git a/src/synthesizer/typescript_synthesizer.rs b/src/synthesizer/typescript_synthesizer.rs index 171e5eb0..9a8436e0 100644 --- a/src/synthesizer/typescript_synthesizer.rs +++ b/src/synthesizer/typescript_synthesizer.rs @@ -388,6 +388,14 @@ pub fn to_string_ir(resource_value: &ResourceIr) -> Option { let str = to_string_ir(obj.as_ref()).unwrap(); Option::Some(format!("{}[{}]", str, *index)) } + ResourceIr::Cidr(ip_block, count, cidr_bits) => { + let ip_block_str = to_string_ir(ip_block.as_ref()).unwrap(); + let count_str = to_string_ir(count.as_ref()).unwrap(); + let cidr_bits_str = to_string_ir(cidr_bits.as_ref()).unwrap(); + Option::Some(format!( + "cdk.Fn.cidr({ip_block_str}, {count_str}, {cidr_bits_str})" + )) + } } }