Skip to content

Conversation

@filipcirtog
Copy link
Collaborator

@filipcirtog filipcirtog commented Dec 12, 2025

Description

The numPartitions field for MongoDB Atlas Search Index exists in the API, but Terraform can't manage it yet. The API overwrites nested fields like definition.numPartitions entirely (similar to PUT), making partial updates via PATCH unsupported. For now, customers need to use the MongoDB Atlas UI to manage this field until Terraform adds support.

This PR is for the addition of the num_partitions field in mongodbatlas_search_index

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

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

@filipcirtog filipcirtog requested review from a team as code owners December 12, 2025 19:30
@github-actions
Copy link
Contributor

github-actions bot commented Dec 12, 2025

APIx bot: a message has been sent to Docs Slack channel
APIx bot: a message has been sent to Docs Slack channel

@filipcirtog filipcirtog changed the title chore: add num_partitions field in mongodbatlas_search_index chore: Add num_partitions field in mongodbatlas_search_index Dec 12, 2025
Copy link
Contributor

@QuiqueSanMongo QuiqueSanMongo left a comment

Choose a reason for hiding this comment

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

LGTM, just a small question

* `type_sets` - Set of type set definitions (when present). Each item includes:
* `name` - Type set name.
* `types` - JSON array string describing the types for the set.
* `num_partitions` - (Optional) Number of index partitions. Allowed values are [1, 2, 4].
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the feature just allow 1, 2, and 4, or is this a typo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! yess, link to the api definition in here. additionally, I've updated the definition with the default value:

Number of index partitions. Allowed values are [1, 2, 4]. Default value is 1.

@filipcirtog filipcirtog marked this pull request as draft December 15, 2025 09:40
@marcabreracast
Copy link
Collaborator

I see that the PR is marked as Open, but the Required Checklist: section of the PR description is not marked, nor the Type of change section. Since we have some automations that get triggered depending on the change, shouldn't this be considered as a New Feature?
As we're adding a new field to the mongodbatlas_search_index, I'd expect that a new changelog will also be generated, that will be then included in the release notes (example: PR #3978

@filipcirtog
Copy link
Collaborator Author

@marcabreracast thank you! I have converted it back to draft until I address your feedback!

@filipcirtog filipcirtog changed the title chore: Add num_partitions field in mongodbatlas_search_index feat: Add num_partitions field in mongodbatlas_search_index Dec 15, 2025
@filipcirtog
Copy link
Collaborator Author

@marcabreracast Thank you. I have updated the PR title, changelog and Required Checklist.

@filipcirtog filipcirtog marked this pull request as ready for review December 15, 2025 16:51
@filipcirtog filipcirtog requested review from Copilot and removed request for Copilot December 15, 2025 16:51
Copy link
Contributor

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

This PR adds support for the num_partitions field to the MongoDB Atlas search index resource and data sources in the Terraform provider. The field allows users to configure index partitioning (with allowed values of 1, 2, or 4) through Terraform.

Key changes:

  • Added num_partitions field to search index resource, data sources, and their schemas
  • Implemented create, read, and update logic for the new field
  • Added comprehensive test coverage including create and update scenarios

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/service/searchindex/resource_search_index.go Added num_partitions field to schema and implemented create/read/update logic
internal/service/searchindex/data_source_search_index.go Added num_partitions as computed field in data source schema and read logic
internal/service/searchindex/data_source_search_indexes.go Added num_partitions to flattened search indexes output
internal/service/searchindex/resource_search_index_test.go Added test case for num_partitions with create and update scenarios
docs/resources/search_index.md Documented num_partitions parameter with allowed values
docs/data-sources/search_index.md Documented num_partitions attribute in data source
docs/data-sources/search_indexes.md Documented num_partitions attribute in plural data source
.changelog/3982.txt Added changelog entries for the enhancement

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@filipcirtog filipcirtog requested a review from Copilot December 15, 2025 17:08
Copy link
Contributor

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

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* `type_sets` - Set of type set definitions (when present). Each item includes:
* `name` - Type set name.
* `types` - JSON array string describing the types for the set.
* `num_partitions` - Number of index partitions. Allowed values are [1, 2, 4]. Default value is 1.
Copy link
Member

Choose a reason for hiding this comment

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

nit: for both datasource i think just "* num_partitions - Number of index partitions." is enough, allowed values and default value is more insteresting for the resource

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! updated!

EOF
```

* `num_partitions` - (Optional) Number of index partitions. Allowed values are [1, 2, 4]. Default value is 1.
Copy link
Member

@lantoli lantoli Dec 15, 2025

Choose a reason for hiding this comment

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

qq: can it be used in both search and vectorSearch types?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! added the a test for vectorSearch as well.

},
"num_partitions": {
Type: schema.TypeInt,
Computed: true,
Copy link
Member

@lantoli lantoli Dec 15, 2025

Choose a reason for hiding this comment

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

can you confirm that if num_partitions is not used when creating the resource, we'll get null in the data sources (instead of default 1)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We receive an integer pointer as nil. Since SDKv2 does not natively handle nil values, and our num_partitions field is of type integer, the field will default to an undefined integer value, equal to 0.

Copy link
Contributor

@QuiqueSanMongo QuiqueSanMongo left a comment

Choose a reason for hiding this comment

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

LGTM!

resource.ParallelTest(t, *basicVectorTestCase(t))
}
func TestAccSearchIndex_withNumPartitions(t *testing.T) {
resource.ParallelTest(t, *numPartitionsTestCase(t))
Copy link
Member

Choose a reason for hiding this comment

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

in this case that we're not reusing the test (e.g. for acceptance and migration test), you can inline numPartitionsTestCase and just have the code in TestAccSearchIndex_withNumPartitions

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! changed.

Steps: []resource.TestStep{
{
Config: configSearchWithNumPartitions(projectID, indexName, clusterName, 2),
Check: checkSearchWithNumPartitions(projectID, indexName, clusterName, 2),
Copy link
Collaborator

@oarbusi oarbusi Dec 16, 2025

Choose a reason for hiding this comment

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

similar to what Leo mentioned in another comment, it would be nice to test the case of when we don't set the num_partitions, that in the resource the field is not set/nil, and in the data source it has the default value 1 (i assume the API returns the value even if not explitictly set)

Copy link
Member

Choose a reason for hiding this comment

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

would also revise behaviour if attribute is initially defined and then removed

Copy link
Collaborator Author

@filipcirtog filipcirtog Dec 16, 2025

Choose a reason for hiding this comment

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

I have added some more tests as you, Leo and Agustin suggested. However, since we are not able to handle the returned nil value (when not set), we process it as a 0 and that is why I assert based on that. Thank you!

Copy link
Collaborator

@oarbusi oarbusi left a comment

Choose a reason for hiding this comment

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

LGTM, just one test case suggestion

Copy link
Collaborator

@marcabreracast marcabreracast left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for addressing my suggestion. Agreed with Leo and Oriol on the test suggestions, feel free to apply those.

@@ -0,0 +1,11 @@
```release-note:enhancement
resource/mongodbatlas_search_index: Adds the `num_partitions` attribute
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
resource/mongodbatlas_search_index: Adds the `num_partitions` attribute
resource/mongodbatlas_search_index: Adds `num_partitions` attribute

nit: Just to stay consistent with other entries (applies to the 3 entries) https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/contributing/changelog-process.md#resource-and-provider-enhancements

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! changed.

@filipcirtog filipcirtog requested a review from Copilot December 16, 2025 17:27
Copy link
Contributor

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

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 690 to 694
var ret int
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "num_partitions", strconv.Itoa(ret)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", strconv.Itoa(ret)),
)
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

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

The variable ret is declared but never assigned a value, making its purpose unclear. Since it will always be 0 (the zero value for int), it would be clearer to either use '0' directly in the string, use strconv.Itoa(0), or rename the variable to something like defaultNumPartitions with an explicit assignment to 0.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to replicate the actual behavior of the resource/data source. Specifically, the situation where an integer pointer is received as nil. Since SDKv2 does not natively handle nil values, and our num_partitions field is of type integer, in such cases, the field will default to an undefined integer value.

To simulate this behavior in Go, even when we receive a nil, it is processed as zero. Here’s the code snippet that demonstrates this.

if o == nil || IsNil(o.NumPartitions) 
{ var ret int return ret }

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that the point here is that var ret int is not an undefined int value, its always 0.
So we can directly check for "0" which is what we expect.

The go int zero value will never change, but in the hypothetical case it did, we want to make sure there is no regression in the datasource: we expect a 0.

Copy link
Member

Choose a reason for hiding this comment

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

yep, I think it says that you just do:

checks = append(checks,
			resource.TestCheckResourceAttr(resourceName, "num_partitions", "0"),
			resource.TestCheckResourceAttr(datasourceName, "num_partitions", "0"),
		)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! changed

Comment on lines 724 to 728
var ret int
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "num_partitions", strconv.Itoa(ret)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", strconv.Itoa(ret)),
)
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

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

The variable ret is declared but never assigned a value, making its purpose unclear. Since it will always be 0 (the zero value for int), it would be clearer to either use '0' directly in the string, use strconv.Itoa(0), or rename the variable to something like defaultNumPartitions with an explicit assignment to 0.

Copilot uses AI. Check for mistakes.
Comment on lines 720 to 721
resource.TestCheckResourceAttr(resourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
resource.TestCheckResourceAttr(resourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
resource.TestCheckResourceAttr(resourceName, "num_partitions", strconv.Itoa(*numPartitions)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", strconv.Itoa(*numPartitions)),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! changed

Comment on lines 686 to 687
resource.TestCheckResourceAttr(resourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
resource.TestCheckResourceAttr(resourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", fmt.Sprintf("%d", *numPartitions)),
resource.TestCheckResourceAttr(resourceName, "num_partitions", strconv.Itoa(*numPartitions)),
resource.TestCheckResourceAttr(datasourceName, "num_partitions", strconv.Itoa(*numPartitions)),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you! changed

@lantoli
Copy link
Member

lantoli commented Dec 17, 2025

note that a PR check is failing:

=== NAME  TestAccVectorSearchIndex_withNumPartitions
    resource_search_index_test.go:227: Step 1/3 error: Error running apply: exit status 1
        
        Error: error during search deployment creation
        
          with mongodbatlas_search_deployment.test,
          on terraform_plugin_test.tf line 13, in resource "mongodbatlas_search_deployment" "test":
          13: 		resource "mongodbatlas_search_deployment" "test" {
        
        https://cloud-dev.mongodb.com/api/atlas/v2/groups/694199be3d2621ae3a708994/clusters/test-acc-tf-c-858085136270834754/search/deployment
        POST: HTTP 409 Conflict (Error code:
        "ATLAS_SEARCH_DEPLOYMENT_ALREADY_EXISTS") Detail: Search deployment already
        exists for specified cluster. Reason: Conflict. Params: [], BadRequestDetail:
--- FAIL: TestAccVectorSearchIndex_withNumPartitions (2.65s)

@filipcirtog filipcirtog requested a review from Copilot December 17, 2025 11:04
Copy link
Contributor

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

type = "search"
%[6]s
depends_on = [mongodbatlas_search_deployment.test]
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

Inconsistent indentation in the depends_on line. The line uses a tab character while other lines in the same configuration use spaces. Consider using consistent indentation throughout the configuration string.

Suggested change
depends_on = [mongodbatlas_search_deployment.test]
depends_on = [mongodbatlas_search_deployment.test]

Copilot uses AI. Check for mistakes.
* `type_sets` - Set of type set definitions (when present). Each item includes:
* `name` - Type set name.
* `types` - JSON array string describing the types for the set.
* `num_partitions` - Number of index partitions, returns 0 if not set in the resource
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The documentation states 'returns 0 if not set in the resource', but this phrasing is unclear as this is a data source, not a resource. Consider rephrasing to 'returns 0 if not configured' or 'returns 0 when not set' for clarity.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@oarbusi oarbusi Dec 17, 2025

Choose a reason for hiding this comment

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

if default value is 1(it says so in the API docs), why we return 0? seems like an inconsistency in the API?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@oarbusi We receive an integer pointer as nil. Since SDKv2 does not natively handle nil values, and our num_partitions field is of type integer, the field will default to an undefined integer value, equal to 0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

WDYT?

* `type_sets` - Set of type set definitions (when present). Each item includes:
* `name` - Type set name.
* `types` - JSON array string describing the types for the set.
* `num_partitions` - Number of index partitions, returns 0 if not set in the resource
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The documentation states 'returns 0 if not set in the resource', but this phrasing is unclear as this is a data source, not a resource. Consider rephrasing to 'returns 0 if not configured' or 'returns 0 when not set' for clarity.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants