Skip to content

Commit 0008dcd

Browse files
abner-douAbner Garcia
andauthored
INTMDB-17: Resource/Data Source Atlas Search (#488)
* implemented search feature in provider * changes to search index resource * added tests to indexes and analyzers * added documentation * fixed atlas conn statement * fix lint issues * changed atlas version in go.mod * fixes to documentation * fix lint issues * fix lint issues * delete analyzer resource * fix documentation * improving datasource search documentation Co-authored-by: Abner Garcia <[email protected]>
1 parent 2a7e9b7 commit 0008dcd

13 files changed

+1970
-41
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ require (
66
github.com/client9/misspell v0.3.4
77
github.com/go-test/deep v1.0.7
88
github.com/gruntwork-io/terratest v0.32.20
9-
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
109
github.com/hashicorp/terraform-plugin-sdk v1.14.0
1110
github.com/mongodb-forks/digest v1.0.1
1211
github.com/mwielbut/pointy v1.1.0
1312
github.com/spf13/cast v1.3.1
1413
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20200518153306-40099de47e37
1514
github.com/terraform-providers/terraform-provider-google v1.20.1-0.20200518165017-1dd21651c496
16-
go.mongodb.org/atlas v0.8.1-0.20210604212331-3f489d7a4eed
15+
go.mongodb.org/atlas v0.10.0
1716
go.mongodb.org/realm v0.0.0-20210618220639-e70c919266f2
1817
)

go.sum

Lines changed: 6 additions & 39 deletions
Large diffs are not rendered by default.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func dataSourceMongoDBAtlasSearchIndex() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceMongoDBAtlasSearchIndexRead,
14+
Schema: returnSearchIndexDSSchema(),
15+
}
16+
}
17+
18+
func returnSearchIndexDSSchema() map[string]*schema.Schema {
19+
return map[string]*schema.Schema{
20+
"project_id": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"cluster_name": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"index_id": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
"analyzer": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
},
36+
"analyzers": {
37+
Type: schema.TypeSet,
38+
Optional: true,
39+
Elem: customAnalyzersSchema(),
40+
},
41+
"collection_name": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
},
45+
"database": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
},
49+
"name": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
},
53+
"search_analyzer": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
},
57+
"mappings_dynamic": {
58+
Type: schema.TypeBool,
59+
Optional: true,
60+
},
61+
"mappings_fields": {
62+
Type: schema.TypeString,
63+
Optional: true,
64+
DiffSuppressFunc: validateSearchIndexMappingDiff,
65+
},
66+
"status": {
67+
Type: schema.TypeString,
68+
Optional: true,
69+
Computed: true,
70+
},
71+
}
72+
}
73+
74+
func dataSourceMongoDBAtlasSearchIndexRead(d *schema.ResourceData, meta interface{}) error {
75+
// Get client connection.
76+
conn := meta.(*MongoDBClient).Atlas
77+
78+
projectID, projectIDOk := d.GetOk("project_id")
79+
clusterName, clusterNameOK := d.GetOk("cluster_name")
80+
indexID, indexIDOk := d.GetOk("index_id")
81+
82+
if !(projectIDOk && clusterNameOK && indexIDOk) {
83+
return errors.New("project_id, cluster_name and index_id must be configured")
84+
}
85+
86+
searchIndex, _, err := conn.Search.GetIndex(context.Background(), projectID.(string), clusterName.(string), indexID.(string))
87+
if err != nil {
88+
return fmt.Errorf("error getting search index information: %s", err)
89+
}
90+
91+
if err := d.Set("index_id", indexID); err != nil {
92+
return fmt.Errorf("error setting `index_id` for search index (%s): %s", d.Id(), err)
93+
}
94+
95+
if err := d.Set("analyzer", searchIndex.Analyzer); err != nil {
96+
return fmt.Errorf("error setting `analyzer` for search index (%s): %s", d.Id(), err)
97+
}
98+
99+
searchIndexCustomAnalyzers, err := flattenSearchIndexCustomAnalyzers(searchIndex.Analyzers)
100+
if err != nil {
101+
return nil
102+
}
103+
104+
if err := d.Set("analyzers", searchIndexCustomAnalyzers); err != nil {
105+
return fmt.Errorf("error setting `analyzer` for search index (%s): %s", d.Id(), err)
106+
}
107+
108+
if err := d.Set("collection_name", searchIndex.CollectionName); err != nil {
109+
return fmt.Errorf("error setting `collectionName` for search index (%s): %s", d.Id(), err)
110+
}
111+
112+
if err := d.Set("database", searchIndex.Database); err != nil {
113+
return fmt.Errorf("error setting `database` for search index (%s): %s", d.Id(), err)
114+
}
115+
116+
if err := d.Set("name", searchIndex.Name); err != nil {
117+
return fmt.Errorf("error setting `name` for search index (%s): %s", d.Id(), err)
118+
}
119+
120+
if err := d.Set("search_analyzer", searchIndex.SearchAnalyzer); err != nil {
121+
return fmt.Errorf("error setting `searchAnalyzer` for search index (%s): %s", d.Id(), err)
122+
}
123+
124+
if err := d.Set("mappings_dynamic", searchIndex.Mappings.Dynamic); err != nil {
125+
return fmt.Errorf("error setting `mappings_dynamic` for search index (%s): %s", d.Id(), err)
126+
}
127+
128+
if searchIndex.Mappings.Fields != nil {
129+
searchIndexMappingFields, err := marshallSearchIndexMappingFields(*searchIndex.Mappings.Fields)
130+
if err != nil {
131+
return err
132+
}
133+
if err := d.Set("mappings_fields", searchIndexMappingFields); err != nil {
134+
return fmt.Errorf("error setting `mappings_fields` for for search index (%s): %s", d.Id(), err)
135+
}
136+
}
137+
138+
d.SetId(encodeStateID(map[string]string{
139+
"project_id": projectID.(string),
140+
"cluster_name": clusterName.(string),
141+
"index_id": indexID.(string),
142+
}))
143+
144+
return nil
145+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package mongodbatlas
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
)
11+
12+
func TestAccDataSourceMongoDBAtlasSearchIndexes_byID(t *testing.T) {
13+
var (
14+
clusterName = acctest.RandomWithPrefix("test-acc-global")
15+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
16+
datasourceName = "data.mongodbatlas_search_index.test_two"
17+
)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
21+
Providers: testAccProviders,
22+
CheckDestroy: testAccCheckMongoDBAtlasSearchIndexDestroy,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccMongoDBAtlasSearchIndexDSConfig(projectID, clusterName),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttrSet(datasourceName, "name"),
28+
resource.TestCheckResourceAttrSet(datasourceName, "project_id"),
29+
resource.TestCheckResourceAttrSet(datasourceName, "name"),
30+
resource.TestCheckResourceAttrSet(datasourceName, "collection_name"),
31+
resource.TestCheckResourceAttrSet(datasourceName, "database"),
32+
resource.TestCheckResourceAttrSet(datasourceName, "search_analyzer"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccMongoDBAtlasSearchIndexDSConfig(projectID, clusterName string) string {
40+
return fmt.Sprintf(`
41+
%s
42+
43+
data "mongodbatlas_search_index" "test_two" {
44+
cluster_name = mongodbatlas_search_index.test.cluster_name
45+
project_id = mongodbatlas_search_index.test.project_id
46+
index_id = mongodbatlas_search_index.test.index_id
47+
}
48+
`, testAccMongoDBAtlasSearchIndexConfig(projectID, clusterName))
49+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
matlas "go.mongodb.org/atlas/mongodbatlas"
11+
)
12+
13+
func dataSourceMongoDBAtlasSearchIndexes() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceMongoDBAtlasSearchIndexesRead,
16+
Schema: map[string]*schema.Schema{
17+
"project_id": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
},
21+
"cluster_name": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
},
25+
"database": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
},
29+
"collection_name": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
},
33+
"page_num": {
34+
Type: schema.TypeInt,
35+
Optional: true,
36+
},
37+
"items_per_page": {
38+
Type: schema.TypeInt,
39+
Optional: true,
40+
},
41+
"results": {
42+
Type: schema.TypeList,
43+
Computed: true,
44+
Elem: &schema.Resource{
45+
Schema: returnSearchIndexSchema(),
46+
},
47+
},
48+
"total_count": {
49+
Type: schema.TypeInt,
50+
Computed: true,
51+
},
52+
},
53+
}
54+
}
55+
56+
func dataSourceMongoDBAtlasSearchIndexesRead(d *schema.ResourceData, meta interface{}) error {
57+
// Get client connection.
58+
conn := meta.(*MongoDBClient).Atlas
59+
60+
projectID, projectIDOK := d.GetOk("project_id")
61+
clusterName, clusterNameOk := d.GetOk("cluster_name")
62+
databaseName, databaseNameOK := d.GetOk("database")
63+
collectionName, collectionNameOK := d.GetOk("collection_name")
64+
65+
if !(projectIDOK && clusterNameOk && databaseNameOK && collectionNameOK) {
66+
return errors.New("project_id, cluster_name, database and collection_name must be configured")
67+
}
68+
69+
options := &matlas.ListOptions{
70+
PageNum: d.Get("page_num").(int),
71+
ItemsPerPage: d.Get("items_per_page").(int),
72+
}
73+
74+
searchIndexes, _, err := conn.Search.ListIndexes(context.Background(), projectID.(string), clusterName.(string), databaseName.(string), collectionName.(string), options)
75+
if err != nil {
76+
return fmt.Errorf("error getting search indexes information: %s", err)
77+
}
78+
79+
flattedSearchIndexes, err := flattenSearchIndexes(searchIndexes)
80+
if err != nil {
81+
return err
82+
}
83+
84+
if err := d.Set("results", flattedSearchIndexes); err != nil {
85+
return fmt.Errorf("error setting `result` for search indexes: %s", err)
86+
}
87+
88+
if err := d.Set("total_count", len(searchIndexes)); err != nil {
89+
return fmt.Errorf("error setting `name`: %s", err)
90+
}
91+
92+
d.SetId(resource.UniqueId())
93+
94+
return nil
95+
}
96+
97+
func flattenSearchIndexes(searchIndexes []*matlas.SearchIndex) ([]map[string]interface{}, error) {
98+
var searchIndexesMap []map[string]interface{}
99+
100+
if len(searchIndexes) == 0 {
101+
return nil, nil
102+
}
103+
searchIndexesMap = make([]map[string]interface{}, len(searchIndexes))
104+
105+
for i := range searchIndexes {
106+
searchIndexCustomAnalyzers, err := flattenSearchIndexCustomAnalyzers(searchIndexes[i].Analyzers)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
searchIndexesMap[i] = map[string]interface{}{
112+
"analyzer": searchIndexes[i].Analyzer,
113+
"analyzers": searchIndexCustomAnalyzers,
114+
"collection_name": searchIndexes[i].CollectionName,
115+
"database": searchIndexes[i].Database,
116+
"index_id": searchIndexes[i].IndexID,
117+
"mappings_dynamic": searchIndexes[i].Mappings.Dynamic,
118+
"name": searchIndexes[i].Name,
119+
"search_analyzer": searchIndexes[i].SearchAnalyzer,
120+
"status": searchIndexes[i].Status,
121+
}
122+
123+
if searchIndexes[i].Mappings.Fields != nil {
124+
searchIndexMappingFields, err := marshallSearchIndexMappingFields(*searchIndexes[i].Mappings.Fields)
125+
if err != nil {
126+
return nil, err
127+
}
128+
searchIndexesMap[i]["mappings_fields"] = searchIndexMappingFields
129+
}
130+
}
131+
132+
return searchIndexesMap, nil
133+
}

0 commit comments

Comments
 (0)