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

refactor: Migrates search index resource and data sources to new SDK #1536

Merged
merged 12 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions mongodbatlas/data_source_mongodbatlas_search_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ func returnSearchIndexDSSchema() map[string]*schema.Schema {
}

func dataSourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas

projectID, projectIDOk := d.GetOk("project_id")
clusterName, clusterNameOK := d.GetOk("cluster_name")
indexID, indexIDOk := d.GetOk("index_id")
Expand All @@ -102,7 +99,8 @@ func dataSourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resour
return diag.Errorf("project_id, cluster_name and index_id must be configured")
}

searchIndex, _, err := conn.Search.GetIndex(ctx, projectID.(string), clusterName.(string), indexID.(string))
connV2 := meta.(*MongoDBClient).AtlasV2
searchIndex, _, err := connV2.AtlasSearchApi.GetAtlasSearchIndex(ctx, projectID.(string), clusterName.(string), indexID.(string)).Execute()
if err != nil {
return diag.Errorf("error getting search index information: %s", err)
}
Expand All @@ -116,7 +114,7 @@ func dataSourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resour
}

if len(searchIndex.Analyzers) > 0 {
searchIndexMappingFields, err := marshallSearchIndexAnalyzers(searchIndex.Analyzers)
searchIndexMappingFields, err := marshalSearchIndex(searchIndex.Analyzers)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -150,8 +148,8 @@ func dataSourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.Resour
return diag.Errorf("error setting `synonyms` for search index (%s): %s", d.Id(), err)
}

if searchIndex.Mappings.Fields != nil {
searchIndexMappingFields, err := marshallSearchIndexMappingsField(*searchIndex.Mappings.Fields)
if len(searchIndex.Mappings.Fields) > 0 {
searchIndexMappingFields, err := marshalSearchIndex(searchIndex.Mappings.Fields)
if err != nil {
return diag.FromErr(err)
}
Expand Down
24 changes: 10 additions & 14 deletions mongodbatlas/data_source_mongodbatlas_search_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20231001001/admin"
)

func dataSourceMongoDBAtlasSearchIndexes() *schema.Resource {
Expand Down Expand Up @@ -53,9 +53,6 @@ func dataSourceMongoDBAtlasSearchIndexes() *schema.Resource {
}

func dataSourceMongoDBAtlasSearchIndexesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas

projectID, projectIDOK := d.GetOk("project_id")
clusterName, clusterNameOk := d.GetOk("cluster_name")
databaseName, databaseNameOK := d.GetOk("database")
Expand All @@ -65,17 +62,14 @@ func dataSourceMongoDBAtlasSearchIndexesRead(ctx context.Context, d *schema.Reso
return diag.Errorf("project_id, cluster_name, database and collection_name must be configured")
}

options := &matlas.ListOptions{
PageNum: d.Get("page_num").(int),
ItemsPerPage: d.Get("items_per_page").(int),
}
Copy link
Member Author

Choose a reason for hiding this comment

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

still pending what to do with page_num and items_per_page, but please review the rest of the PR

Copy link
Member Author

@lantoli lantoli Oct 19, 2023

Choose a reason for hiding this comment

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

will be done in another PR to better track changelog

connV2 := meta.(*MongoDBClient).AtlasV2
searchIndexes, _, err := connV2.AtlasSearchApi.ListAtlasSearchIndexes(ctx, projectID.(string), clusterName.(string), collectionName.(string), databaseName.(string)).Execute()

searchIndexes, _, err := conn.Search.ListIndexes(ctx, projectID.(string), clusterName.(string), databaseName.(string), collectionName.(string), options)
if err != nil {
return diag.Errorf("error getting search indexes information: %s", err)
}

flattedSearchIndexes, err := flattenSearchIndexes(searchIndexes)
flattedSearchIndexes, err := flattenSearchIndexes(searchIndexes, projectID.(string), clusterName.(string))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -93,7 +87,7 @@ func dataSourceMongoDBAtlasSearchIndexesRead(ctx context.Context, d *schema.Reso
return nil
}

func flattenSearchIndexes(searchIndexes []*matlas.SearchIndex) ([]map[string]interface{}, error) {
func flattenSearchIndexes(searchIndexes []admin.ClusterSearchIndex, projectID, clusterName string) ([]map[string]interface{}, error) {
var searchIndexesMap []map[string]interface{}

if len(searchIndexes) == 0 {
Expand All @@ -103,6 +97,8 @@ func flattenSearchIndexes(searchIndexes []*matlas.SearchIndex) ([]map[string]int

for i := range searchIndexes {
searchIndexesMap[i] = map[string]interface{}{
"project_id": projectID,
"cluster_name": clusterName,
"analyzer": searchIndexes[i].Analyzer,
"collection_name": searchIndexes[i].CollectionName,
"database": searchIndexes[i].Database,
Expand All @@ -114,16 +110,16 @@ func flattenSearchIndexes(searchIndexes []*matlas.SearchIndex) ([]map[string]int
"synonyms": flattenSearchIndexSynonyms(searchIndexes[i].Synonyms),
}

if searchIndexes[i].Mappings.Fields != nil {
searchIndexMappingFields, err := marshallSearchIndexMappingsField(*searchIndexes[i].Mappings.Fields)
if len(searchIndexes[i].Mappings.Fields) > 0 {
searchIndexMappingFields, err := marshalSearchIndex(searchIndexes[i].Mappings.Fields)
if err != nil {
return nil, err
}
searchIndexesMap[i]["mappings_fields"] = searchIndexMappingFields
}

if len(searchIndexes[i].Analyzers) > 0 {
searchIndexAnalyzers, err := marshallSearchIndexAnalyzers(searchIndexes[i].Analyzers)
searchIndexAnalyzers, err := marshalSearchIndex(searchIndexes[i].Analyzers)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading