Skip to content

Commit 337f585

Browse files
committed
Allow for indices to be recreated where mappings are not strict supersets
1 parent c57953a commit 337f585

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

es/resource_elasticsearch_index.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ func resourceElasticsearchIndex() *schema.Resource {
419419
Update: resourceElasticsearchIndexUpdate,
420420
Delete: resourceElasticsearchIndexDelete,
421421
Schema: configSchema,
422+
CustomizeDiff: verifyIndexMappingUpdates,
422423
Importer: &schema.ResourceImporter{
423424
StateContext: schema.ImportStatePassthroughContext,
424425
},
@@ -679,25 +680,15 @@ func resourceElasticsearchIndexUpdate(d *schema.ResourceData, meta interface{})
679680
}
680681
}
681682

682-
o, n := d.GetChange("mappings")
683-
difference, _ := jsondiff.Compare([]byte(n.(string)), []byte(o.(string)), &jsondiff.Options{})
684-
685-
// if we're not changing any settings, no-op this function
686-
if len(settings) == 0 && difference == jsondiff.FullMatch {
687-
return resourceElasticsearchIndexRead(d, meta)
688-
}
689-
690683
if len(settings) != 0 {
691684
err = updateIndexSettings(d, meta, settings)
692685
}
693686
if err != nil {
694687
return err
695688
}
696689

697-
if difference == jsondiff.SupersetMatch {
698-
err = updateIndexMappings(d, meta, n.(string))
699-
}
700-
if err != nil {
690+
mappings := d.Get("mappings")
691+
if err = updateIndexMappings(d, meta, mappings.(string)); err != nil {
701692
return err
702693
}
703694

@@ -758,6 +749,15 @@ func updateIndexMappings(d *schema.ResourceData, meta interface{}, mapping strin
758749
return err
759750
}
760751

752+
func verifyIndexMappingUpdates(ctx context.Context, resourceDiff *schema.ResourceDiff, meta interface{}) error {
753+
oldMapping, newMapping := resourceDiff.GetChange("mappings")
754+
difference, _ := jsondiff.Compare([]byte(newMapping.(string)), []byte(oldMapping.(string)), &jsondiff.Options{})
755+
if difference == jsondiff.NoMatch {
756+
return resourceDiff.ForceNew("mappings")
757+
}
758+
return nil
759+
}
760+
761761
func getWriteIndexByAlias(alias string, d *schema.ResourceData, meta interface{}) string {
762762
var (
763763
index = d.Id()

es/resource_elasticsearch_index_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ resource "elasticsearch_index" "test_mapping" {
191191
}
192192
EOF
193193
}
194+
`
195+
testAccElasticsearchRemovedMapping = `
196+
resource "elasticsearch_index" "test_mapping" {
197+
name = "terraform-test"
198+
number_of_replicas = "1"
199+
include_type_name = false
200+
mappings = <<EOF
201+
{
202+
"properties": {
203+
"name": {
204+
"type": "text"
205+
}
206+
}
207+
}
208+
EOF
209+
}
194210
`
195211
testAccElasticsearchIndexUpdateForceDestroy = `
196212
resource "elasticsearch_index" "test" {
@@ -551,6 +567,26 @@ func TestAccElasticsearchIndex_mapping(t *testing.T) {
551567
}),
552568
),
553569
},
570+
{
571+
Config: testAccElasticsearchRemovedMapping,
572+
Check: resource.ComposeTestCheckFunc(
573+
checkElasticsearchIndexRecreated("elasticsearch_index.test_mapping", &initialUUID),
574+
checkElasticsearchIndexMapping("terraform-test", func(s *terraform.State, mapping map[string]interface{}) error {
575+
g.Expect(mapping).To(MatchAllKeys(Keys{
576+
"terraform-test": MatchAllKeys(Keys{
577+
"mappings": MatchAllKeys(Keys{
578+
"properties": MatchAllKeys(Keys{
579+
"name": MatchAllKeys(Keys{
580+
"type": Equal("text"),
581+
}),
582+
}),
583+
}),
584+
}),
585+
}))
586+
return nil
587+
}),
588+
),
589+
},
554590
},
555591
})
556592
}

0 commit comments

Comments
 (0)