-
Notifications
You must be signed in to change notification settings - Fork 178
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
chore: Updates search_index resource with new SDK #1848
Conversation
@@ -36,17 +36,21 @@ func IntGreatThan(value int) resource.CheckResourceAttrWithFunc { | |||
} | |||
} | |||
|
|||
func JSONEquals[T any](value T) resource.CheckResourceAttrWithFunc { | |||
func JSONEquals(expected string) resource.CheckResourceAttrWithFunc { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to make it easier to use
@@ -396,38 +391,39 @@ func flattenSearchIndexSynonyms(synonyms []admin.SearchSynonymMappingDefinition) | |||
|
|||
func marshalSearchIndex(fields any) (string, error) { | |||
bytes, err := json.Marshal(fields) | |||
return string(bytes), err | |||
str := string(bytes) | |||
if str == "[]" || str == "{}" { // empty JSON array or object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: why was this check added in these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because now some fields can be empty arrays or objects instead of nil pointer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, seems a little hacky to include in this function. Maybe we can check when calling this function that the input parameter is non empty? That way it is clear when the terraform attribute will be left empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's really the same idea as we're already doing in the unmarshal functions like:
if str == "" {
return fields, nil
}
not sure about checking this in each caller as we really want always this functionality, i.e. empty arrays to be serialized to empty strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed it to only check for empty arrays that it's the real need:
if str == "[]" { // empty JSON array serialized to empty string
there is no need to check for empty objects here as it's a local function, in a generic utiliy function it could make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking into the usages I struggle to understand why this is now needed vs the previous implementation, taking into account that the input of this function are using the .Get of the SDK to pass a slice (not pointer). Just want to make sure we have no change in behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the prev. implementation empty arrays were nil but now they are len 0 arrays so they serialize to "[]" when we want to serialize to "".
if we don't do that we get this error:
Step 1/1 error: After applying this test step, the plan was not empty.
...
# mongodbatlas_search_index.test will be updated in-place
~ resource "mongodbatlas_search_index" "test" {
- analyzers = jsonencode([])
...
resource_search_index_test.go:231: Step 2/2 error: Check failed: Check 2/2 error: mongodbatlas_search_index.test: Attribute 'analyzers' expected "", got "[]"
FAIL
@@ -320,15 +319,39 @@ func resourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resource | |||
return diag.Errorf("error setting `analyzer` for search index (%s): %s", d.Id(), err) | |||
} | |||
|
|||
if analyzers := searchIndex.GetAnalyzers(); len(analyzers) > 0 { | |||
if searchIndex.Type != nil && *searchIndex.Type == vectorSearch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we changing the order and logic of the if statements here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now we want to have empty arrays (instead of nil pointers) so for instance they're updated correctly. but depending on the vector type some arrays can't not be sent even empty. basically here we're doing the same logic as we do in Create.
the order is not important, at the end we need to set all fields in any order, the important part is to do different sets depending on the search type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at the end I've returned to the original order and logic following @AgustinBettati advice and using len > 0 again :-)
@@ -396,38 +391,39 @@ func flattenSearchIndexSynonyms(synonyms []admin.SearchSynonymMappingDefinition) | |||
|
|||
func marshalSearchIndex(fields any) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unit test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a strong opinion but don't know if they add big value here as in other cases because they're local functions that are simple wrappers to json functionality
} | ||
|
||
func testAccSearchIndexConfigSynonyms(projectIDStr, indexName, databaseName, clusterNameStr, clusterTerraformStr string) string { | ||
func configSynonyms(projectIDStr, indexName, databaseName, clusterNameStr, clusterTerraformStr string, has bool) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe a better name for has
? Unless I am missing something obvious here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's fine I'll rename to configWithSynonyms to be consistent with other functions
@@ -159,7 +152,7 @@ func TestAccSearchIndexRS_withSynonyms(t *testing.T) { | |||
CheckDestroy: acc.CheckDestroySearchIndex, | |||
Steps: []resource.TestStep{ | |||
{ | |||
Config: testAccSearchIndexConfigSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr), | |||
Config: configSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, true), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nit: but for a good reading style we (not just you) should avoid passing bool values directtly to methods and instead use bool vars that has meaningful names.
Config: configSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, true), | |
Config: configSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, hasOrIsSomething), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i hear you. here being configWithSynonyms I thought it could be understood. WDYT about defining constansts:
withSynonyms = true
withoutSynonyms = true
and call like:
configWithSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, withSynonyms),
configWithSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, withoutSynonyms),
or being he function already with "synoyms" on it, what about constant to call:
configWithSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, with),
configWithSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, without),
i see it a bit redundant but maybe it's clearer
), | ||
}, | ||
{ | ||
Config: configWithSynonyms(clusterInfo.ProjectIDStr, indexName, databaseName, clusterInfo.ClusterNameStr, clusterInfo.ClusterTerraformStr, without), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marcosuma using with / without instead of true / false
@@ -396,38 +391,39 @@ func flattenSearchIndexSynonyms(synonyms []admin.SearchSynonymMappingDefinition) | |||
|
|||
func marshalSearchIndex(fields any) (string, error) { | |||
bytes, err := json.Marshal(fields) | |||
return string(bytes), err | |||
str := string(bytes) | |||
if str == "[]" || str == "{}" { // empty JSON array or object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, seems a little hacky to include in this function. Maybe we can check when calling this function that the input parameter is non empty? That way it is clear when the terraform attribute will be left empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Updates search_index resource with new SDK.
Link to any related issue(s): https://jira.mongodb.org/browse/CLOUDP-222718
Type of change:
Required Checklist:
Further comments