From 7128bedd587b8438ec9e26ebce3e01e96a739e9f Mon Sep 17 00:00:00 2001 From: Rupa Lahiri Date: Thu, 9 Nov 2023 22:36:35 +0530 Subject: [PATCH] Fix error thrown when index configuration has no number_of_replicas specified (#123) * Fix error thrown when index configuration has no number_of_replicas specified Signed-off-by: Rupa Lahiri * Add tests to check index resource with default shards and replicas on re-apply/update Signed-off-by: Rupa Lahiri --------- Signed-off-by: Rupa Lahiri --- provider/resource_opensearch_index.go | 7 ++++-- provider/resource_opensearch_index_test.go | 27 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/provider/resource_opensearch_index.go b/provider/resource_opensearch_index.go index 96000aa..708c9ac 100644 --- a/provider/resource_opensearch_index.go +++ b/provider/resource_opensearch_index.go @@ -152,6 +152,7 @@ var ( Type: schema.TypeString, Description: "Number of shard replicas. A stringified number.", Optional: true, + Computed: true, }, "auto_expand_replicas": { Type: schema.TypeString, @@ -634,8 +635,10 @@ func resourceOpensearchIndexUpdate(d *schema.ResourceData, meta interface{}) err settings := make(map[string]interface{}) for _, key := range settingsKeys { schemaName := strings.Replace(key, ".", "_", -1) - if d.HasChange(schemaName) { - settings[key] = d.Get(schemaName) + if _, ok := d.GetOk(schemaName); ok { + if d.HasChange(schemaName) { + settings[key] = d.Get(schemaName) + } } } diff --git a/provider/resource_opensearch_index_test.go b/provider/resource_opensearch_index_test.go index d353165..9b86231 100644 --- a/provider/resource_opensearch_index_test.go +++ b/provider/resource_opensearch_index_test.go @@ -40,6 +40,11 @@ resource "opensearch_index" "test" { indexing_slowlog_threshold_index_warn = "5s" indexing_slowlog_level = "warn" } +` + testAccOpensearchIndexDefaultShardsReplicas = ` +resource "opensearch_index" "testdefaultshardsreplicas" { + name = "terraform-testdefaultshardsreplicas" +} ` testAccOpensearchIndexAnalysis = ` resource "opensearch_index" "test" { @@ -250,6 +255,28 @@ func TestAccOpensearchIndex(t *testing.T) { }) } +func TestAccOpensearchIndexDefaultShardsReplicas(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: checkOpensearchIndexDestroy, + Steps: []resource.TestStep{ + { + Config: testAccOpensearchIndexDefaultShardsReplicas, + Check: resource.ComposeTestCheckFunc( + checkOpensearchIndexExists("opensearch_index.testdefaultshardsreplicas"), + ), + }, + { + Config: testAccOpensearchIndexDefaultShardsReplicas, + Check: resource.ComposeTestCheckFunc( + checkOpensearchIndexExists("opensearch_index.testdefaultshardsreplicas"), + ), + }, + }, + }) +} + func TestAccOpensearchIndex_rolloverAliasOpendistro(t *testing.T) { provider := Provider() diags := provider.Configure(context.Background(), &terraform.ResourceConfig{})