Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fn::Cidr support #82

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions src/ir/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum ResourceIr {
ImportValue(Box<ResourceIr>),
GetAZs(Box<ResourceIr>),
Select(i64, Box<ResourceIr>),
Cidr(Box<ResourceIr>, Box<ResourceIr>, Box<ResourceIr>),
}

/// ResourceTranslationInputs is a place to store all the intermediate recursion
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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),
))
}
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/parser/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum ResourceValue {
Base64(Box<ResourceValue>),
ImportValue(Box<ResourceValue>),
Select(Box<ResourceValue>, Box<ResourceValue>),
Cidr(Box<ResourceValue>, Box<ResourceValue>, Box<ResourceValue>),
}

impl ResourceValue {}
Expand Down Expand Up @@ -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 => {
Expand Down
8 changes: 8 additions & 0 deletions src/synthesizer/typescript_synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ pub fn to_string_ir(resource_value: &ResourceIr) -> Option<String> {
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})"
))
}
}
}

Expand Down