Skip to content

Commit

Permalink
Work on elasticsearch_domain auto-tune
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelding committed Nov 10, 2021
1 parent d9d86cd commit 8d8cab8
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 8 deletions.
107 changes: 107 additions & 0 deletions internal/service/elasticsearch/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,60 @@ func ResourceDomain() *schema.Resource {
},
},
},
"auto_tune_options": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"desired_state": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(elasticsearch.AutoTuneDesiredState_Values(), false),
},
"maintenance_schedule": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_at": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsRFC3339Time,
},
"duration": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: schema.TypeInt,
Required: true,
},
"unit": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(elasticsearch.TimeUnit_Values(), false),
},
},
},
},
"cron_expression_for_recurrence": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"rollback_on_disable": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(elasticsearch.RollbackOnDisable_Values(), false),
},
},
},
},
"domain_name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -484,6 +538,10 @@ func resourceDomainCreate(d *schema.ResourceData, meta interface{}) error {
input.AdvancedSecurityOptions = expandAdvancedSecurityOptions(v.([]interface{}))
}

if v, ok := d.GetOk("auto_tune_options"); ok && len(v.([]interface{})) > 0 {
input.AutoTuneOptions = expandAutoTuneOptionsInput(v.([]interface{})[0].(map[string]interface{}))
}

if v, ok := d.GetOk("ebs_options"); ok {
options := v.([]interface{})

Expand Down Expand Up @@ -639,6 +697,32 @@ func resourceDomainCreate(d *schema.ResourceData, meta interface{}) error {

log.Printf("[DEBUG] Elasticsearch domain %q created", d.Id())

log.Printf("[DEBUG] Modifying config for Elasticsearch domain %q", d.Id())
err = resource.Retry(60*time.Minute, func() *resource.RetryError {

in := &elasticsearch.UpdateElasticsearchDomainConfigInput{
DomainName: aws.String(d.Get("domain_name").(string)),
}

if v, ok := d.GetOk("auto_tune_options"); ok && len(v.([]interface{})) > 0 {
in.AutoTuneOptions = expandAutoTuneOptions(v.([]interface{})[0].(map[string]interface{}))
}

_, err = conn.UpdateElasticsearchDomainConfig(in)

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if err != nil {
return fmt.Errorf("Error modifying config for Elasticsearch domain: %s", err)
}

log.Printf("[DEBUG] Config for Elasticsearch domain %q modified", d.Id())

return resourceDomainRead(d, meta)
}

Expand Down Expand Up @@ -697,6 +781,18 @@ func resourceDomainRead(d *schema.ResourceData, meta interface{}) error {

ds := out.DomainStatus

out2, err := conn.DescribeElasticsearchDomainConfig(&elasticsearch.DescribeElasticsearchDomainConfigInput{
DomainName: aws.String(d.Get("domain_name").(string)),
})

if err != nil {
return err
}

log.Printf("[DEBUG] Received config for Elasticsearch domain: %s", out)

dc := out2.DomainConfig

if ds.AccessPolicies != nil && aws.StringValue(ds.AccessPolicies) != "" {
policies, err := structure.NormalizeJsonString(aws.StringValue(ds.AccessPolicies))
if err != nil {
Expand Down Expand Up @@ -750,6 +846,13 @@ func resourceDomainRead(d *schema.ResourceData, meta interface{}) error {
}
}

if v := dc.AutoTuneOptions; v != nil {
err = d.Set("auto_tune_options", []interface{}{flattenAutoTuneOptions(v.Options)})
if err != nil {
return err
}
}

if err := d.Set("snapshot_options", flattenSnapshotOptions(ds.SnapshotOptions)); err != nil {
return fmt.Errorf("error setting snapshot_options: %s", err)
}
Expand Down Expand Up @@ -845,6 +948,10 @@ func resourceDomainUpdate(d *schema.ResourceData, meta interface{}) error {
input.AdvancedSecurityOptions = expandAdvancedSecurityOptions(d.Get("advanced_security_options").([]interface{}))
}

if d.HasChange("auto_tune_options") {
input.AutoTuneOptions = expandAutoTuneOptions(d.Get("auto_tune_options").([]interface{})[0].(map[string]interface{}))
}

if d.HasChange("domain_endpoint_options") {
input.DomainEndpointOptions = expandDomainEndpointOptions(d.Get("domain_endpoint_options").([]interface{}))
}
Expand Down
69 changes: 69 additions & 0 deletions internal/service/elasticsearch/domain_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,54 @@ func DataSourceDomain() *schema.Resource {
},
},
},
"auto_tune_options": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"desired_state": {
Type: schema.TypeString,
Computed: true,
},
"maintenance_schedule": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_at": {
Type: schema.TypeString,
Computed: true,
},
"duration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: schema.TypeInt,
Computed: true,
},
"unit": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"cron_expression_for_recurrence": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"rollback_on_disable": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"domain_name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -296,6 +344,21 @@ func dataSourceDomainRead(d *schema.ResourceData, meta interface{}) error {

ds := resp.DomainStatus

req2 := &elasticsearchservice.DescribeElasticsearchDomainConfigInput{
DomainName: aws.String(d.Get("domain_name").(string)),
}

resp2, err := conn.DescribeElasticsearchDomainConfig(req2)
if err != nil {
return fmt.Errorf("error querying config for elasticsearch_domain: %w", err)
}

if resp2.DomainConfig == nil {
return fmt.Errorf("your query returned no results")
}

dc := resp2.DomainConfig

d.SetId(aws.StringValue(ds.ARN))

if ds.AccessPolicies != nil && aws.StringValue(ds.AccessPolicies) != "" {
Expand All @@ -319,6 +382,12 @@ func dataSourceDomainRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting advanced_security_options: %w", err)
}

if dc.AutoTuneOptions != nil {
if err := d.Set("auto_tune_options", []interface{}{flattenAutoTuneOptions(dc.AutoTuneOptions.Options)}); err != nil {
return fmt.Errorf("error setting auto_tune_options: %w", err)
}
}

if err := d.Set("ebs_options", flattenEBSOptions(ds.EBSOptions)); err != nil {
return fmt.Errorf("error setting ebs_options: %w", err)
}
Expand Down
58 changes: 50 additions & 8 deletions internal/service/elasticsearch/domain_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

func TestAccElasticsearchDomainDataSource_Data_basic(t *testing.T) {
rInt := sdkacctest.RandInt()
autoTuneStartAtTime := testAccGetValidStartAtTime(t, "24h")
datasourceName := "data.aws_elasticsearch_domain.test"
resourceName := "aws_elasticsearch_domain.test"

Expand All @@ -21,10 +22,14 @@ func TestAccElasticsearchDomainDataSource_Data_basic(t *testing.T) {
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccDomainWithDataSourceConfig(rInt),
Config: testAccDomainWithDataSourceConfig(rInt, autoTuneStartAtTime),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "processing", "false"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch_version", resourceName, "elasticsearch_version"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.#", resourceName, "auto_tune_options.#"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.0.desired_state", resourceName, "auto_tune_options.0.desired_state"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.0.maintenance_schedule", resourceName, "auto_tune_options.0.maintenance_schedule"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.0.rollback_on_disable", resourceName, "auto_tune_options.0.rollback_on_disable"),
resource.TestCheckResourceAttrPair(datasourceName, "cluster_config.#", resourceName, "cluster_config.#"),
resource.TestCheckResourceAttrPair(datasourceName, "cluster_config.0.instance_type", resourceName, "cluster_config.0.instance_type"),
resource.TestCheckResourceAttrPair(datasourceName, "cluster_config.0.instance_count", resourceName, "cluster_config.0.instance_count"),
Expand All @@ -45,6 +50,7 @@ func TestAccElasticsearchDomainDataSource_Data_basic(t *testing.T) {

func TestAccElasticsearchDomainDataSource_Data_advanced(t *testing.T) {
rInt := sdkacctest.RandInt()
autoTuneStartAtTime := testAccGetValidStartAtTime(t, "24h")
datasourceName := "data.aws_elasticsearch_domain.test"
resourceName := "aws_elasticsearch_domain.test"

Expand All @@ -54,9 +60,13 @@ func TestAccElasticsearchDomainDataSource_Data_advanced(t *testing.T) {
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccDomainAdvancedWithDataSourceConfig(rInt),
Config: testAccDomainAdvancedWithDataSourceConfig(rInt, autoTuneStartAtTime),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch_version", resourceName, "elasticsearch_version"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.#", resourceName, "auto_tune_options.#"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.0.desired_state", resourceName, "auto_tune_options.0.desired_state"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.0.maintenance_schedule", resourceName, "auto_tune_options.0.maintenance_schedule"),
resource.TestCheckResourceAttrPair(datasourceName, "auto_tune_options.0.rollback_on_disable", resourceName, "auto_tune_options.0.rollback_on_disable"),
resource.TestCheckResourceAttrPair(datasourceName, "cluster_config.#", resourceName, "cluster_config.#"),
resource.TestCheckResourceAttrPair(datasourceName, "cluster_config.0.instance_type", resourceName, "cluster_config.0.instance_type"),
resource.TestCheckResourceAttrPair(datasourceName, "cluster_config.0.instance_count", resourceName, "cluster_config.0.instance_count"),
Expand All @@ -78,7 +88,7 @@ func TestAccElasticsearchDomainDataSource_Data_advanced(t *testing.T) {
})
}

func testAccDomainWithDataSourceConfig(rInt int) string {
func testAccDomainWithDataSourceConfig(rInt int, autoTuneStartAtTime string) string {
return fmt.Sprintf(`
locals {
random_name = "test-es-%d"
Expand All @@ -92,7 +102,7 @@ data "aws_caller_identity" "current" {}
resource "aws_elasticsearch_domain" "test" {
domain_name = local.random_name
elasticsearch_version = "1.5"
elasticsearch_version = "6.7"
access_policies = <<POLICY
{
Expand All @@ -115,6 +125,22 @@ resource "aws_elasticsearch_domain" "test" {
}
POLICY
auto_tune_options {
desired_state = "ENABLED"
maintenance_schedule {
start_at = "%s"
duration {
value = "2"
unit = "HOURS"
}
cron_expression_for_recurrence = "cron(0 0 ? * 1 *)"
}
rollback_on_disable = "NO_ROLLBACK"
}
cluster_config {
instance_type = "t2.small.elasticsearch"
instance_count = 2
Expand All @@ -141,10 +167,10 @@ POLICY
data "aws_elasticsearch_domain" "test" {
domain_name = aws_elasticsearch_domain.test.domain_name
}
`, rInt)
`, rInt, autoTuneStartAtTime)
}

func testAccDomainAdvancedWithDataSourceConfig(rInt int) string {
func testAccDomainAdvancedWithDataSourceConfig(rInt int, autoTuneStartAtTime string) string {
return acctest.ConfigAvailableAZsNoOptIn() + fmt.Sprintf(`
data "aws_partition" "current" {}
Expand Down Expand Up @@ -217,7 +243,7 @@ resource "aws_security_group_rule" "test" {
resource "aws_elasticsearch_domain" "test" {
domain_name = local.random_name
elasticsearch_version = "1.5"
elasticsearch_version = "6.7"
access_policies = <<POLICY
{
Expand All @@ -233,6 +259,22 @@ resource "aws_elasticsearch_domain" "test" {
}
POLICY
auto_tune_options {
desired_state = "ENABLED"
maintenance_schedule {
start_at = "%s"
duration {
value = "2"
unit = "HOURS"
}
cron_expression_for_recurrence = "cron(0 0 ? * 1 *)"
}
rollback_on_disable = "NO_ROLLBACK"
}
cluster_config {
instance_type = "t2.small.elasticsearch"
instance_count = 2
Expand Down Expand Up @@ -283,5 +325,5 @@ POLICY
data "aws_elasticsearch_domain" "test" {
domain_name = aws_elasticsearch_domain.test.domain_name
}
`, rInt)
`, rInt, autoTuneStartAtTime)
}
Loading

0 comments on commit 8d8cab8

Please sign in to comment.