Skip to content

Commit

Permalink
refactor: Migrates search index resource and data sources to new SDK (#…
Browse files Browse the repository at this point in the history
…1536)

* Read

* Delete

* change interface{} to any

* Create

* Update

* Import

* single data source

* list data source

* fix doc for resource

* change interface{} to any

* apply feedback to move var inside if block

* upgrade test
  • Loading branch information
lantoli authored Oct 19, 2023
1 parent 4ab65ea commit 7a05d81
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 178 deletions.
14 changes: 6 additions & 8 deletions mongodbatlas/data_source_mongodbatlas_search_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ 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

func dataSourceMongoDBAtlasSearchIndexRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
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
32 changes: 14 additions & 18 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 @@ -52,10 +52,7 @@ func dataSourceMongoDBAtlasSearchIndexes() *schema.Resource {
}
}

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

func dataSourceMongoDBAtlasSearchIndexesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
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),
}
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,16 +87,18 @@ func dataSourceMongoDBAtlasSearchIndexesRead(ctx context.Context, d *schema.Reso
return nil
}

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

if len(searchIndexes) == 0 {
return nil, nil
}
searchIndexesMap = make([]map[string]interface{}, len(searchIndexes))
searchIndexesMap = make([]map[string]any, len(searchIndexes))

for i := range searchIndexes {
searchIndexesMap[i] = map[string]interface{}{
searchIndexesMap[i] = map[string]any{
"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

0 comments on commit 7a05d81

Please sign in to comment.