Skip to content

Commit 58c2e5c

Browse files
authored
fixed import state method in search index resource (#500)
1 parent 74349f5 commit 58c2e5c

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

mongodbatlas/resource_mongodbatlas_search_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func customAnalyzersSchema() *schema.Resource {
230230
func resourceMongoDBAtlasSearchIndexImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
231231
conn := meta.(*MongoDBClient).Atlas
232232

233-
parts := strings.SplitN(d.Id(), "-", 3)
233+
parts := strings.SplitN(d.Id(), "--", 3)
234234
if len(parts) != 3 {
235235
return nil, errors.New("import format error: to import a search index, use the format {project_id}-{cluster_name}-{index_id}")
236236
}

mongodbatlas/resource_mongodbatlas_search_index_test.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ import (
1414

1515
func TestAccResourceMongoDBAtlasSearchIndex_basic(t *testing.T) {
1616
var (
17-
index matlas.SearchIndex
18-
resourceName = "mongodbatlas_search_index.test"
19-
clusterName = acctest.RandomWithPrefix("test-acc-index")
20-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
21-
name = "name_test"
22-
updatedAnalyzer = "lucene.simple"
17+
index matlas.SearchIndex
18+
resourceName = "mongodbatlas_search_index.test"
19+
clusterName = acctest.RandomWithPrefix("test-acc-index")
20+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
21+
name = "name_test"
2322
)
2423
resource.ParallelTest(t, resource.TestCase{
2524
PreCheck: func() { testAccPreCheck(t) },
@@ -35,6 +34,24 @@ func TestAccResourceMongoDBAtlasSearchIndex_basic(t *testing.T) {
3534
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
3635
),
3736
},
37+
},
38+
})
39+
}
40+
41+
func TestAccResourceMongoDBAtlasSearchIndex_withMapping(t *testing.T) {
42+
var (
43+
index matlas.SearchIndex
44+
resourceName = "mongodbatlas_search_index.test"
45+
clusterName = acctest.RandomWithPrefix("test-acc-index")
46+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
47+
name = "name_test"
48+
updatedAnalyzer = "lucene.simple"
49+
)
50+
resource.ParallelTest(t, resource.TestCase{
51+
PreCheck: func() { testAccPreCheck(t) },
52+
Providers: testAccProviders,
53+
CheckDestroy: testAccCheckMongoDBAtlasSearchIndexDestroy,
54+
Steps: []resource.TestStep{
3855
{
3956
Config: testAccMongoDBAtlasSearchIndexConfigAdvanced(projectID, clusterName),
4057
Check: resource.ComposeTestCheckFunc(
@@ -52,25 +69,28 @@ func TestAccResourceMongoDBAtlasSearchIndex_basic(t *testing.T) {
5269

5370
func TestAccResourceMongoDBAtlasSearchIndex_importBasic(t *testing.T) {
5471
var (
72+
index matlas.SearchIndex
5573
resourceName = "mongodbatlas_search_index.test"
56-
clusterName = acctest.RandomWithPrefix("test-acc-global")
74+
clusterName = acctest.RandomWithPrefix("test-acc-index")
5775
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
76+
name = "name_test"
5877
)
59-
6078
resource.ParallelTest(t, resource.TestCase{
6179
PreCheck: func() { testAccPreCheck(t) },
6280
Providers: testAccProviders,
6381
CheckDestroy: testAccCheckMongoDBAtlasSearchIndexDestroy,
6482
Steps: []resource.TestStep{
6583
{
66-
Config: testAccMongoDBAtlasSearchIndexConfig(projectID, clusterName),
67-
ResourceName: resourceName,
68-
ImportStateIdFunc: testAccCheckMongoDBAtlasSearchIndexImportStateIDFunc(resourceName),
69-
ImportState: true,
70-
ImportStateVerify: true,
84+
Config: testAccMongoDBAtlasSearchIndexConfig(projectID, clusterName),
85+
Check: resource.ComposeTestCheckFunc(
86+
testAccCheckMongoDBAtlasSearchIndexExists(resourceName, &index),
87+
resource.TestCheckResourceAttr(resourceName, "name", name),
88+
resource.TestCheckResourceAttr(resourceName, "project_id", projectID),
89+
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
90+
),
7191
},
7292
{
73-
Config: testAccMongoDBAtlasSearchIndexConfigAdvanced(projectID, clusterName),
93+
Config: testAccMongoDBAtlasSearchIndexConfig(projectID, clusterName),
7494
ResourceName: resourceName,
7595
ImportStateIdFunc: testAccCheckMongoDBAtlasSearchIndexImportStateIDFunc(resourceName),
7696
ImportState: true,
@@ -247,6 +267,8 @@ func testAccCheckMongoDBAtlasSearchIndexImportStateIDFunc(resourceName string) r
247267
return "", fmt.Errorf("not found: %s", resourceName)
248268
}
249269

250-
return rs.Primary.ID, nil
270+
ids := decodeStateID(rs.Primary.ID)
271+
272+
return fmt.Sprintf("%s--%s--%s", ids["project_id"], ids["cluster_name"], ids["index_id"]), nil
251273
}
252274
}

0 commit comments

Comments
 (0)