Skip to content

Conversation

lantoli
Copy link
Collaborator

@lantoli lantoli commented Aug 21, 2025

Description

Support dynamic blocks in adv2v2 command

  • Remove limitation of using variable num_shards out of dynamic blocks in both clu2adv and adv2v2 commands. (This makes the code more complex as there are multiple use cases depending on the number of replication_specs and if they use a mix of variable and numerical num_shards, see Terraform in and out test files for more info).
  • Total PR line changes are +1,923 −354, but changes in Go files only are: +436 -299, other changes are for Terraform test files.
  • Documentation will be updated in a follow-up PRs

Link to any related issue(s): CLOUDP-339840

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@github-actions github-actions bot added the enhancement New feature or request label Aug 21, 2025
@lantoli lantoli marked this pull request as ready for review August 25, 2025 10:12
@Copilot Copilot AI review requested due to automatic review settings August 25, 2025 10:12
@lantoli lantoli requested a review from a team as a code owner August 25, 2025 10:12
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds support for dynamic blocks in both clu2adv and adv2v2 commands by removing the limitation that num_shards must be a numeric literal.

  • Enables processing of variable num_shards values using for expressions and dynamic block conversion
  • Introduces new test cases for dynamic blocks and variable num_shards scenarios
  • Refactors shared logic into shared.go for better code organization

Reviewed Changes

Copilot reviewed 24 out of 28 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/hcl/hcl.go Removes error prefix from GetAttrString function and adds TokensFuncConcat utility
internal/convert/shared.go New file containing shared helper functions for dynamic block processing
internal/convert/clu2adv.go Updates to use shared functions and support variable num_shards
internal/convert/adv2v2.go Adds dynamic block validation and variable num_shards support
test files Adds comprehensive test cases for dynamic blocks and variable num_shards scenarios

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@@ -120,7 +120,6 @@ dynamic "replication_specs" {

### Limitations

- [`num_shards`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster#num_shards-2) in `replication_specs` must be a numeric [literal expression](https://developer.hashicorp.com/nomad/docs/job-specification/hcl2/expressions#literal-expressions), e.g. `var.num_shards` is not supported. This is to allow creating a `replication_specs` element per shard in `mongodbatlas_advanced_cluster`. This limitation doesn't apply if you're using `dynamic` blocks in `regions_config` or `replication_specs`.

Choose a reason for hiding this comment

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

nice!


// processNumShards handles num_shards for a block, returning tokens for the expanded specs.
// processedBody is the body with num_shards removed and other processing done.
func processNumShards(shardsAttr *hclwrite.Attribute, processedBody *hclwrite.Body) hclwrite.Tokens {

Choose a reason for hiding this comment

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

q: This functions is already assuming numShards is defined dynamically right? Potentially we can make explicit by renaming to processNumShardsWithForEach

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you're right, changing better to processNumShardsWhenSomeIsVariable here: 308d00b

project_id = var.project_id
name = "geo"
cluster_type = "GEOSHARDED"
replication_specs = concat(
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow!

Copy link
Contributor

@EspenAlbert EspenAlbert left a comment

Choose a reason for hiding this comment

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

Wow. Well done 👏

@lantoli lantoli merged commit 8fec80a into main Aug 25, 2025
8 of 9 checks passed
@lantoli lantoli deleted the CLOUDP-339840_dynamic_blocks branch August 25, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants