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 helper functions for ipcidr #102

Closed
wants to merge 0 commits into from

Conversation

Issacwww
Copy link
Contributor

Description of changes:
Add new helper function for EKS to config the template

Testing done:
Added unit test and cargo test passed

Terms of contribution:

By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.

@cbgbt cbgbt self-requested a review August 22, 2024 00:27
@Issacwww Issacwww changed the title Add convert_cidr_to_dns_ip helper function Add helper functions for ipcidr Aug 23, 2024
Copy link
Contributor

@cbgbt cbgbt left a comment

Choose a reason for hiding this comment

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

Thanks so much for your contribution! The test cases are great.

I have a few style nits around error handling and such. I think the big change I'd really like to see is to move to using the cidr crate for CIDR range parsing, then for your helper functions, internally lean on the types exposed by that and the standard library before finally converting to Strings at the end of the helper functions.

sources/api/schnauzer/src/helpers.rs Outdated Show resolved Hide resolved
@@ -907,6 +910,128 @@ pub fn goarch(
Ok(())
}

Copy link
Contributor

Choose a reason for hiding this comment

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

For the helpers that you created, you will need to add them to the template registries that are used when templates are rendered. There are currently two codepaths that set up registries, and adding the functions to both is fine:

sources/api/schnauzer/src/helpers.rs Outdated Show resolved Hide resolved
sources/api/schnauzer/src/helpers.rs Outdated Show resolved Hide resolved
Ok(())
}

fn validate_and_parse_cidr(cidr: &str) -> Result<&str, RenderError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than using String return types, could we add a dependency on the cidr crate and then use the AnyIpCidr enum for parsing?

AnyIpCidr implements FromStr, so you should be able to do something like:

    let parsed_cidr: AnyIpCidr = cidr.parse()
        .context(error::InvalidCidrSnafu { cidr: cidr.to_string() })?;

For this to work, you would also need to modify the InvalidCidr error to have a source of type cidr::errors::NetworkParseError.

I believe you could then just call first_address to get the IP part of the CIDR range, and return that as an IpAddr.

use serde::Serialize;
use serde_json::json;

const TEMPLATE: &str = r#"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice test!

@Issacwww
Copy link
Contributor Author

Issacwww commented Aug 30, 2024

Open a new one #116 that resolved merge conflict and comments

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