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 capability to parse and convert floats in CFN templates #63

Merged
merged 1 commit into from
Sep 3, 2022
Merged

Add capability to parse and convert floats in CFN templates #63

merged 1 commit into from
Sep 3, 2022

Conversation

vtanguturi
Copy link
Contributor

What is the problem?

As is noctilucent is unable to convert a CFN template if there exists floats in the template. I ran into this on the Threshold property for a CloudWatch alarm, but looking at the specification, there are a couple different resources that could run into this (any property that allows a Double)

What is the solution?

While parsing, we will check if a string is an integer or not, using https://docs.rs/numberkit/latest/numberkit/fn.is_digit.html. If it is we will continue with today's parsing, otherwise we will use a new ResourceValue::Double. The not straightforward thing that I had to do was create essentially a wrapper around the f64 type as it does not implement Eq.

Example error

when trying to convert the following resource:

    "AlarmResource": {
      "DependsOn": "OtherResource",
      "Properties": {
        "AlarmDescription": "blah",
        "AlarmName": "blah",
        "ComparisonOperator": "GreaterThanOrEqualToThreshold",
        "EvaluationPeriods": 3,
        (... Other Metadata ...)
        "Threshold": 3.5,
        "TreatMissingData": "breaching"
      },
      "Type": "AWS::CloudWatch::Alarm"
    },

I get the following error:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/parser/resource.rs:152:72
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/panicking.rs:143:14
   2: core::panicking::panic
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/panicking.rs:48:5
   3: noctilucent::parser::resource::build_resources_recursively
   4: noctilucent::parser::resource::build_resources
   5: noctilucent::CloudformationParseTree::build
   6: noctilucent::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

After this change

I am able to get the stack converted 💯
(Generated typescript:

const alarmResource = new cloudwatch.CfnAlarm(this, 'AlarmResource', {
evaluationPeriods: 3,
threshold: 3.5,
alarmDescription: 'blah',
comparisonOperator: 'GreaterThanOrEqualToThreshold',
treatMissingData: 'breaching',
alarmName: `blah`
});
alarmResource.addOverride('DependsOn', ['OtherResource']);

(Disclaimer: I've never written rust before so let me know if there is a better way to do this)

Copy link
Collaborator

@iph iph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's beautiful :')

@iph iph merged commit 139cd74 into cdklabs:main Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants