terragrunt icon indicating copy to clipboard operation
terragrunt copied to clipboard

[WIP] feat: Support version constraints for Terraform Registry modules

Open juan-vg opened this issue 9 months ago • 8 comments

Description

Fixes #1930 Fixes #2572

TODOs

Read the Gruntwork contribution guidelines.

  • [x] Update the docs.
  • [x] Run the relevant tests successfully, including pre-commit checks.
  • [X] Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
  • [X] Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Added support for version constraints for Terraform Registry modules

Summary by CodeRabbit

  • New Features

    • Enhanced module version management with clearer error messages for various failure scenarios.
    • Improved module version retrieval process, ensuring accurate version selection and increased logging transparency during downloads.
    • New configuration file for Terragrunt to retrieve modules from the public Terraform registry.
    • Added a new test for Terraform registry version resolution to ensure correct functionality.
  • Documentation

    • Updated documentation for the VERSION argument in the terraform block to clarify acceptable formats.
  • Tests

    • Expanded test coverage for the GetTargetVersion function to handle various input scenarios.

juan-vg avatar Feb 24 '25 16:02 juan-vg

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
terragrunt-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 4, 2025 8:25pm

vercel[bot] avatar Feb 24 '25 16:02 vercel[bot]

[!IMPORTANT]

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce several new error types in the tf/errors.go file to improve error handling for module version issues. In tf/getter.go, new data structures and functions are added to facilitate the extraction of module version information from a registry's versions endpoint. The Get method is updated to use the new GetTargetVersion function, which constructs the URL for fetching module versions and handles filtering and parsing. Documentation is enhanced to clarify the acceptable formats for the VERSION argument, and new tests are added to validate the functionality.

Changes

File(s) Change Summary
tf/errors.go Added four new error types (ModuleVersionsFetchErr, ModuleVersionConstraintErr, ModuleVersionConstraintMalformedErr, ModuleVersionMalformedErr) along with their Error() methods for module version handling.
tf/getter.go Introduced three new structs (Modules, Module, and ModuleVersion) for JSON deserialization. Added GetTargetVersion, getHighestVersion, and other functions to enhance version resolution logic and logging.
docs/_docs/04_reference/04-config-blocks-and-attributes.md Updated documentation for the VERSION argument in the terraform block to include details on acceptable formats (specific version number or version constraint).
test/fixtures/tfr/version/terragrunt.hcl Created a new Terragrunt configuration file that retrieves a module from the public Terraform registry with specified version constraints.
test/integration_registry_test.go Added a new constant registryFixtureVersion and a test function TestTerraformRegistryVersionResolution to validate version resolution in the Terraform registry context.
tf/getter_test.go Introduced a new test function TestGetTargetVersion to validate the behavior of tf.GetTargetVersion across various scenarios using a table-driven test structure.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant GTV as "GetTargetVersion()"
    participant HTTP as "HTTP Client"
    participant JSON as "JSON Parser"
    participant Filter as "getHighestVersion()"

    Caller->>GTV: Call with module info and versionQuery
    GTV->>HTTP: Build URL and perform HTTP request
    HTTP-->>GTV: Return JSON response
    GTV->>JSON: Parse JSON into Modules struct
    GTV->>Filter: Filter available versions by constraint
    Filter-->>GTV: Return highest version
    GTV-->>Caller: Return highest version or error

Suggested reviewers

  • levkohimins
  • denis256

Poem

In code we trust, errors now bloom,
With modules dancing in their room.
New paths, functions, logs to see,
A version's flow set error-free.
Cheers to clean code and joyful spree!
✨🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Feb 24 '25 16:02 coderabbitai[bot]

Shame on me I forgot to run the linter... I'm on it!

juan-vg avatar Mar 03 '25 11:03 juan-vg

Thanks for addressing those, @juan-vg .

Upon re-review, I realized that the implementation you've added uses a query string parameter for the source URL:

locals {
  base_source = "tfr:///terraform-aws-modules/iam/aws"
  version     = "~>5.0, <5.51.0, !=5.50.0"
}
terraform {
  source = "${local.base_source}?version=${local.version}"
}

Instead of a new version attribute:

terraform {
  source  = "tfr:///terraform-aws-modules/iam/aws"
  version = "~>5.0, <5.51.0, !=5.50.0"
}

I believe a new version attribute would be more in-line with the UI/UX of OpenTofu/Terraform, so users may find it more convenient to follow that pattern as well. What do you think? Would you be willing to try out an implementation that uses a version attribute?

yhakbar avatar Mar 03 '25 13:03 yhakbar

@yhakbar

I believe a new version attribute would be more in-line with the UI/UX of OpenTofu/Terraform, so users may find it more convenient to follow that pattern as well. What do you think? Would you be willing to try out an implementation that uses a version attribute?

While I agree the new version attribute completely makes sense (and I'm willing to try that implementation), I'm wondering if it belongs to the scope of this PR since the version attribute is already there via query string. I mean, adding that attribute implies a (probably full) refactor of this code and at the end the target is to add the new attribute and not to support version constraints (even if they're related). From my POV the version attribute should be added as an incremental, later change after supporting version constraints. However, that's my take. WDYT?

juan-vg avatar Mar 04 '25 19:03 juan-vg

While I agree the new version attribute completely makes sense (and I'm willing to try that implementation), I'm wondering if it belongs to the scope of this PR since the version attribute is already there via query string. ... However, that's my take. WDYT?

@juan-vg

Normally, I'd be totally onboard with an incremental improvement rather than hold up a PR, but in the case of TG configuration, we want to be more conservative. This introduces configuration for a URL that is not URL encoded, and when we support the attribute, users should switch off using the query string parameter, which would be a breaking change.

If you really don't have time to rework with PR to use an attribute, I'd be open to reviewing it after the changes have been gated behind an experiment flag. That way, we can document the feature as incomplete, and require user opt in to use behavior that might break in the future.

yhakbar avatar Mar 05 '25 12:03 yhakbar

@yhakbar

Normally, I'd be totally onboard with an incremental improvement rather than hold up a PR, but in the case of TG configuration, we want to be more conservative. This introduces configuration for a URL that is not URL encoded, and when we support the attribute, users should switch off using the query string parameter, which would be a breaking change.

I understand

If you really don't have time to rework with PR to use an attribute, I'd be open to reviewing it after the changes have been gated behind an experiment flag. That way, we can document the feature as incomplete, and require user opt in to use behavior that might break in the future.

Nah, let's do it! I have some time and much willing, and any guiding or help will be appreciated :)

juan-vg avatar Mar 06 '25 09:03 juan-vg

Bumping this to see if there were any more updates. We would really like to see this adopted.

stephenbawks avatar May 19 '25 13:05 stephenbawks

What is this PR still missing? Would love to see this implemented.

OpsTalkJordan avatar Jul 01 '25 14:07 OpsTalkJordan