From 2c60a5d78b55b98d6b747ee6a50415e1e0be2328 Mon Sep 17 00:00:00 2001 From: Joel Cressy Date: Mon, 1 Apr 2019 09:22:14 -0500 Subject: [PATCH 1/6] [Elasticsearch] Implement specifying availability zone count when zone awareness enabled. --- aws/resource_aws_elasticsearch_domain.go | 4 ++++ aws/structure.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/aws/resource_aws_elasticsearch_domain.go b/aws/resource_aws_elasticsearch_domain.go index a989d22531c..0acc238bd33 100644 --- a/aws/resource_aws_elasticsearch_domain.go +++ b/aws/resource_aws_elasticsearch_domain.go @@ -194,6 +194,10 @@ func resourceAwsElasticSearchDomain() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "zone_awareness_count": { + Type: schema.TypeInt, + Optional: true, + }, }, }, }, diff --git a/aws/structure.go b/aws/structure.go index ffc0799e8a3..797f7e48294 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -1205,6 +1205,10 @@ func expandESClusterConfig(m map[string]interface{}) *elasticsearch.Elasticsearc config.ZoneAwarenessEnabled = aws.Bool(v.(bool)) } + if v, ok := m["zone_awareness_count"]; ok { + config.ZoneAwarenessConfig.AvailabilityZoneCount = aws.Int64(v.(int64)) + } + return &config } @@ -1229,6 +1233,9 @@ func flattenESClusterConfig(c *elasticsearch.ElasticsearchClusterConfig) []map[s if c.ZoneAwarenessEnabled != nil { m["zone_awareness_enabled"] = *c.ZoneAwarenessEnabled } + if c.ZoneAwarenessConfig.AvailabilityZoneCount != nil { + m["zone_awareness_count"] = *c.ZoneAwarenessConfig.AvailabilityZoneCount + } return []map[string]interface{}{m} } From bc2ea842669373b9bc733cb254dc90697fd01ffd Mon Sep 17 00:00:00 2001 From: Joel Cressy Date: Thu, 4 Apr 2019 09:17:54 -0500 Subject: [PATCH 2/6] catch a nil c.ZoneAwarenessConfig before checking its attributes --- aws/structure.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/aws/structure.go b/aws/structure.go index 797f7e48294..c2c802c378e 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -1233,10 +1233,9 @@ func flattenESClusterConfig(c *elasticsearch.ElasticsearchClusterConfig) []map[s if c.ZoneAwarenessEnabled != nil { m["zone_awareness_enabled"] = *c.ZoneAwarenessEnabled } - if c.ZoneAwarenessConfig.AvailabilityZoneCount != nil { + if c.ZoneAwarenessConfig != nil && c.ZoneAwarenessConfig.AvailabilityZoneCount != nil { m["zone_awareness_count"] = *c.ZoneAwarenessConfig.AvailabilityZoneCount } - return []map[string]interface{}{m} } From 21f3700b725a5e0d35ac1f7833dc5c028cf08996 Mon Sep 17 00:00:00 2001 From: Joel Cressy Date: Thu, 4 Apr 2019 17:38:32 -0500 Subject: [PATCH 3/6] implement acceptance test for 3AZ zone-aware deployment --- aws/resource_aws_elasticsearch_domain_test.go | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/aws/resource_aws_elasticsearch_domain_test.go b/aws/resource_aws_elasticsearch_domain_test.go index 3ac477b6115..1ac5bd48f28 100644 --- a/aws/resource_aws_elasticsearch_domain_test.go +++ b/aws/resource_aws_elasticsearch_domain_test.go @@ -77,6 +77,28 @@ func TestAccAWSElasticSearchDomain_basic(t *testing.T) { }) } +func TestAccAWSElasticSearchDomain_ZoneAwarenessWith3AZ(t *testing.T) { + var domain elasticsearch.ElasticsearchDomainStatus + ri := acctest.RandInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckESDomainDestroy, + Steps: []resource.TestStep{ + { + Config: testAccESDomainConfig_ZoneAwarenessWith3AZ(ri), + Check: resource.ComposeTestCheckFunc( + testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), + resource.TestCheckResourceAttr( + "aws_elasticsearch_domain.example", "elasticsearch_version", "1.5"), + resource.TestMatchResourceAttr("aws_elasticsearch_domain.example", "kibana_endpoint", regexp.MustCompile(".*es.amazonaws.com/_plugin/kibana/")), + ), + }, + }, + }) +} + func TestAccAWSElasticSearchDomain_withDedicatedMaster(t *testing.T) { var domain elasticsearch.ElasticsearchDomainStatus ri := acctest.RandInt() @@ -749,6 +771,26 @@ resource "aws_elasticsearch_domain" "example" { `, randInt) } +func testAccESDomainConfig_ZoneAwarenessWith3AZ(randInt int) string { + return fmt.Sprintf(` +resource "aws_elasticsearch_domain" "example" { + domain_name = "tf-test-%d" + + ebs_options { + ebs_enabled = true + volume_size = 10 + } + + cluster_config { + instance_type = "t2.micro.elasticsearch" + instance_count = "3" + zone_awareness_enabled = true + zone_awareness_count = "3" + } +} +`, randInt) +} + func testAccESDomainConfig_WithDedicatedClusterMaster(randInt int, enabled bool) string { return fmt.Sprintf(` resource "aws_elasticsearch_domain" "example" { From 0f58958144416fd21f7bf26f0bb1cc5b3715a2f7 Mon Sep 17 00:00:00 2001 From: Joel Cressy Date: Thu, 4 Apr 2019 17:40:02 -0500 Subject: [PATCH 4/6] assign a ZoneAwarenessConfig struct to the ZAC parameter with AZ Count --- aws/structure.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/structure.go b/aws/structure.go index c2c802c378e..ec13b633415 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -1206,7 +1206,7 @@ func expandESClusterConfig(m map[string]interface{}) *elasticsearch.Elasticsearc } if v, ok := m["zone_awareness_count"]; ok { - config.ZoneAwarenessConfig.AvailabilityZoneCount = aws.Int64(v.(int64)) + config.ZoneAwarenessConfig = &elasticsearch.ZoneAwarenessConfig{AvailabilityZoneCount: aws.Int64(int64(v.(int)))} } return &config From a5b7d3596589bec9b134c475c4209444fb55435f Mon Sep 17 00:00:00 2001 From: Joel Cressy Date: Fri, 5 Apr 2019 10:19:24 -0500 Subject: [PATCH 5/6] only set zac when zone awareness is enabled, to mimic the flow of dedicated masters --- aws/structure.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/aws/structure.go b/aws/structure.go index ec13b633415..cf6b4801faa 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -1202,11 +1202,13 @@ func expandESClusterConfig(m map[string]interface{}) *elasticsearch.Elasticsearc } if v, ok := m["zone_awareness_enabled"]; ok { - config.ZoneAwarenessEnabled = aws.Bool(v.(bool)) - } - - if v, ok := m["zone_awareness_count"]; ok { - config.ZoneAwarenessConfig = &elasticsearch.ZoneAwarenessConfig{AvailabilityZoneCount: aws.Int64(int64(v.(int)))} + isEnabled := v.(bool) + config.ZoneAwarenessEnabled = aws.Bool(isEnabled) + if isEnabled { + if v, ok := m["zone_awareness_count"]; ok { + config.ZoneAwarenessConfig = &elasticsearch.ZoneAwarenessConfig{AvailabilityZoneCount: aws.Int64(int64(v.(int)))} + } + } } return &config From 87c864655210b5cd3d5c4fed7db1366d0c6cb00a Mon Sep 17 00:00:00 2001 From: Joel Cressy Date: Fri, 5 Apr 2019 10:38:46 -0500 Subject: [PATCH 6/6] Expand test case to multiple zone aware configurations and update the cluster each time. --- aws/resource_aws_elasticsearch_domain_test.go | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/aws/resource_aws_elasticsearch_domain_test.go b/aws/resource_aws_elasticsearch_domain_test.go index 1ac5bd48f28..027020a52a0 100644 --- a/aws/resource_aws_elasticsearch_domain_test.go +++ b/aws/resource_aws_elasticsearch_domain_test.go @@ -77,7 +77,7 @@ func TestAccAWSElasticSearchDomain_basic(t *testing.T) { }) } -func TestAccAWSElasticSearchDomain_ZoneAwarenessWith3AZ(t *testing.T) { +func TestAccAWSElasticSearchDomain_ZoneAwareness(t *testing.T) { var domain elasticsearch.ElasticsearchDomainStatus ri := acctest.RandInt() @@ -87,12 +87,43 @@ func TestAccAWSElasticSearchDomain_ZoneAwarenessWith3AZ(t *testing.T) { CheckDestroy: testAccCheckESDomainDestroy, Steps: []resource.TestStep{ { - Config: testAccESDomainConfig_ZoneAwarenessWith3AZ(ri), + Config: testAccESDomainConfig_ZoneAwareness(ri, 3, false), Check: resource.ComposeTestCheckFunc( testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), - resource.TestCheckResourceAttr( - "aws_elasticsearch_domain.example", "elasticsearch_version", "1.5"), - resource.TestMatchResourceAttr("aws_elasticsearch_domain.example", "kibana_endpoint", regexp.MustCompile(".*es.amazonaws.com/_plugin/kibana/")), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_enabled", "false"), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_count", "3"), + ), + }, + { + Config: testAccESDomainConfig_ZoneAwareness(ri, 3, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_enabled", "true"), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_count", "3"), + ), + }, + { + Config: testAccESDomainConfig_ZoneAwareness(ri, 3, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_enabled", "false"), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_count", "3"), + ), + }, + { + Config: testAccESDomainConfig_ZoneAwareness(ri, 2, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_enabled", "true"), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_count", "2"), + ), + }, + { + Config: testAccESDomainConfig_ZoneAwareness(ri, 3, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_enabled", "true"), + resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "cluster_config.0.zone_awareness_count", "3"), ), }, }, @@ -771,7 +802,7 @@ resource "aws_elasticsearch_domain" "example" { `, randInt) } -func testAccESDomainConfig_ZoneAwarenessWith3AZ(randInt int) string { +func testAccESDomainConfig_ZoneAwareness(randInt int, azcount int, enabled bool) string { return fmt.Sprintf(` resource "aws_elasticsearch_domain" "example" { domain_name = "tf-test-%d" @@ -783,12 +814,12 @@ resource "aws_elasticsearch_domain" "example" { cluster_config { instance_type = "t2.micro.elasticsearch" - instance_count = "3" - zone_awareness_enabled = true - zone_awareness_count = "3" + instance_count = "6" + zone_awareness_enabled = %t + zone_awareness_count = "%d" } } -`, randInt) +`, randInt, enabled, azcount) } func testAccESDomainConfig_WithDedicatedClusterMaster(randInt int, enabled bool) string {