From 9aab57466e7bd9bb2b5b29d07eb513b28e181a48 Mon Sep 17 00:00:00 2001 From: Andrey Inishev Date: Tue, 27 Jun 2023 06:19:19 +0300 Subject: [PATCH 1/6] Support for OpenSearch SoftwareUpdateOptions (closes #30103) --- internal/service/opensearch/domain.go | 51 +++++++++++++++++++ .../service/opensearch/domain_data_source.go | 20 ++++++++ internal/service/opensearch/flex.go | 10 ++++ 3 files changed, 81 insertions(+) diff --git a/internal/service/opensearch/domain.go b/internal/service/opensearch/domain.go index 5901d21885e..8a188200c24 100644 --- a/internal/service/opensearch/domain.go +++ b/internal/service/opensearch/domain.go @@ -552,6 +552,21 @@ func ResourceDomain() *schema.Resource { }, }, }, + "software_update_options": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "auto_software_update_enabled": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, + }, + }, names.AttrTags: tftags.TagsSchema(), names.AttrTagsAll: tftags.TagsSchemaComputed(), "vpc_options": { @@ -699,6 +714,24 @@ func resourceDomainCreate(ctx context.Context, d *schema.ResourceData, meta inte } } + if v, ok := d.GetOk("software_update_options"); ok { + options := v.([]interface{}) + + if len(options) == 1 { + if options[0] == nil { + return sdkdiag.AppendErrorf(diags, "At least one field is expected inside software_update_options") + } + + o := options[0].(map[string]interface{}) + + softwareUpdateOptions := opensearchservice.SoftwareUpdateOptions{ + AutoSoftwareUpdateEnabled: aws.Bool(o["auto_software_update_enabled"].(bool)), + } + + input.SoftwareUpdateOptions = &softwareUpdateOptions + } + } + if v, ok := d.GetOk("vpc_options"); ok { options := v.([]interface{}) if options[0] == nil { @@ -892,6 +925,10 @@ func resourceDomainRead(ctx context.Context, d *schema.ResourceData, meta interf return sdkdiag.AppendErrorf(diags, "setting snapshot_options: %s", err) } + if err := d.Set("software_update_options", flattenSoftwareUpdateOptions(ds.SoftwareUpdateOptions)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting software_update_options: %s", err) + } + if ds.VPCOptions != nil { if err := d.Set("vpc_options", flattenVPCDerivedInfo(ds.VPCOptions)); err != nil { return sdkdiag.AppendErrorf(diags, "setting vpc_options: %s", err) @@ -1050,6 +1087,20 @@ func resourceDomainUpdate(ctx context.Context, d *schema.ResourceData, meta inte } } + if d.HasChange("software_update_options") { + options := d.Get("software_update_options").([]interface{}) + + if len(options) == 1 { + o := options[0].(map[string]interface{}) + + softwareUpdateOptions := opensearchservice.SoftwareUpdateOptions{ + AutoSoftwareUpdateEnabled: aws.Bool(o["auto_software_update_enabled"].(bool)), + } + + input.SoftwareUpdateOptions = &softwareUpdateOptions + } + } + if d.HasChange("vpc_options") { options := d.Get("vpc_options").([]interface{}) s := options[0].(map[string]interface{}) diff --git a/internal/service/opensearch/domain_data_source.go b/internal/service/opensearch/domain_data_source.go index cb810516e95..e24620d2b91 100644 --- a/internal/service/opensearch/domain_data_source.go +++ b/internal/service/opensearch/domain_data_source.go @@ -2,6 +2,7 @@ package opensearch import ( "context" + "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/opensearchservice" @@ -355,6 +356,21 @@ func DataSourceDomain() *schema.Resource { }, }, }, + "software_update_options": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "auto_software_update_enabled": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, + }, + }, "tags": tftags.TagsSchemaComputed(), "vpc_options": { Type: schema.TypeList, @@ -462,6 +478,10 @@ func dataSourceDomainRead(ctx context.Context, d *schema.ResourceData, meta inte return sdkdiag.AppendErrorf(diags, "setting snapshot_options: %s", err) } + if err := d.Set("software_update_options", flattenSoftwareUpdateOptions(ds.SoftwareUpdateOptions)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting software_update_options: %s", err) + } + if ds.VPCOptions != nil { if err := d.Set("vpc_options", flattenVPCDerivedInfo(ds.VPCOptions)); err != nil { return sdkdiag.AppendErrorf(diags, "setting vpc_options: %s", err) diff --git a/internal/service/opensearch/flex.go b/internal/service/opensearch/flex.go index 77db447d765..1181d330e23 100644 --- a/internal/service/opensearch/flex.go +++ b/internal/service/opensearch/flex.go @@ -213,6 +213,16 @@ func flattenSnapshotOptions(snapshotOptions *opensearchservice.SnapshotOptions) return []map[string]interface{}{m} } +func flattenSoftwareUpdateOptions(softwareUpdateOptions *opensearchservice.SoftwareUpdateOptions) map[string]interface{} { + if softwareUpdateOptions == nil { + return map[string]interface{}{} + } + + m := map[string]interface{}{} + + return m +} + func flattenVPCDerivedInfo(o *opensearchservice.VPCDerivedInfo) []map[string]interface{} { m := map[string]interface{}{} From b216486d5f737df4fc81b9e7a45a43c57fd306bd Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 7 Sep 2023 11:44:31 -0500 Subject: [PATCH 2/6] aws_opensearch_domain: update flatterner/expander --- internal/service/opensearch/domain.go | 31 +++---------------- .../service/opensearch/domain_data_source.go | 2 +- internal/service/opensearch/flex.go | 25 ++++++++++++--- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/internal/service/opensearch/domain.go b/internal/service/opensearch/domain.go index 64cd20b8f10..7349d9161d4 100644 --- a/internal/service/opensearch/domain.go +++ b/internal/service/opensearch/domain.go @@ -562,6 +562,7 @@ func ResourceDomain() *schema.Resource { "software_update_options": { Type: schema.TypeList, Optional: true, + Computed: true, MaxItems: 1, DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock, Elem: &schema.Resource{ @@ -569,7 +570,7 @@ func ResourceDomain() *schema.Resource { "auto_software_update_enabled": { Type: schema.TypeBool, Optional: true, - Default: false, + Computed: true, }, }, }, @@ -722,21 +723,7 @@ func resourceDomainCreate(ctx context.Context, d *schema.ResourceData, meta inte } if v, ok := d.GetOk("software_update_options"); ok { - options := v.([]interface{}) - - if len(options) == 1 { - if options[0] == nil { - return sdkdiag.AppendErrorf(diags, "At least one field is expected inside software_update_options") - } - - o := options[0].(map[string]interface{}) - - softwareUpdateOptions := opensearchservice.SoftwareUpdateOptions{ - AutoSoftwareUpdateEnabled: aws.Bool(o["auto_software_update_enabled"].(bool)), - } - - input.SoftwareUpdateOptions = &softwareUpdateOptions - } + input.SoftwareUpdateOptions = expandSoftwareUpdateOptions(v.([]interface{})) } if v, ok := d.GetOk("vpc_options"); ok { @@ -1095,17 +1082,7 @@ func resourceDomainUpdate(ctx context.Context, d *schema.ResourceData, meta inte } if d.HasChange("software_update_options") { - options := d.Get("software_update_options").([]interface{}) - - if len(options) == 1 { - o := options[0].(map[string]interface{}) - - softwareUpdateOptions := opensearchservice.SoftwareUpdateOptions{ - AutoSoftwareUpdateEnabled: aws.Bool(o["auto_software_update_enabled"].(bool)), - } - - input.SoftwareUpdateOptions = &softwareUpdateOptions - } + input.SoftwareUpdateOptions = expandSoftwareUpdateOptions(d.Get("software_update_options").([]interface{})) } if d.HasChange("vpc_options") { diff --git a/internal/service/opensearch/domain_data_source.go b/internal/service/opensearch/domain_data_source.go index a686e95f171..721188bcd4b 100644 --- a/internal/service/opensearch/domain_data_source.go +++ b/internal/service/opensearch/domain_data_source.go @@ -5,7 +5,6 @@ package opensearch import ( "context" - "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/opensearchservice" @@ -16,6 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/verify" ) // @SDKDataSource("aws_opensearch_domain") diff --git a/internal/service/opensearch/flex.go b/internal/service/opensearch/flex.go index ff1507fe9c4..5615066ec13 100644 --- a/internal/service/opensearch/flex.go +++ b/internal/service/opensearch/flex.go @@ -203,14 +203,31 @@ func flattenSnapshotOptions(snapshotOptions *opensearchservice.SnapshotOptions) return []map[string]interface{}{m} } -func flattenSoftwareUpdateOptions(softwareUpdateOptions *opensearchservice.SoftwareUpdateOptions) map[string]interface{} { +func expandSoftwareUpdateOptions(in []interface{}) *opensearchservice.SoftwareUpdateOptions { + if len(in) == 0 { + return nil + } + + m := in[0].(map[string]interface{}) + + var out opensearchservice.SoftwareUpdateOptions + if v, ok := m["auto_software_update_enabled"].(bool); ok { + out.AutoSoftwareUpdateEnabled = aws.Bool(v) + } + + return &out +} + +func flattenSoftwareUpdateOptions(softwareUpdateOptions *opensearchservice.SoftwareUpdateOptions) []interface{} { if softwareUpdateOptions == nil { - return map[string]interface{}{} + return nil } - m := map[string]interface{}{} + m := map[string]interface{}{ + "auto_software_update_enabled": aws.BoolValue(softwareUpdateOptions.AutoSoftwareUpdateEnabled), + } - return m + return []interface{}{m} } func expandVPCOptions(tfMap map[string]interface{}) *opensearchservice.VPCOptions { From 95678389d44b20dbb25e7895e7cf41b392bef36e Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 7 Sep 2023 11:55:06 -0500 Subject: [PATCH 3/6] aws_opensearch_domain: add resoruce test --- internal/service/opensearch/domain_test.go | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/internal/service/opensearch/domain_test.go b/internal/service/opensearch/domain_test.go index dd35eefb52c..d8f3ca23833 100644 --- a/internal/service/opensearch/domain_test.go +++ b/internal/service/opensearch/domain_test.go @@ -1817,6 +1817,39 @@ func TestAccOpenSearchDomain_versionUpdate(t *testing.T) { }}) } +func TestAccOpenSearchDomain_softwareUpdateOptions(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var domain opensearchservice.DomainStatus + rName := testAccRandomDomainName() + resourceName := "aws_opensearch_domain.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckIAMServiceLinkedRole(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, opensearchservice.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckDomainDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccDomainConfig_softwareUpdateOptions(rName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckDomainExists(ctx, resourceName, &domain), + resource.TestCheckResourceAttr(resourceName, "software_update_options.0.auto_software_update_enabled", "false"), + ), + }, + { + Config: testAccDomainConfig_softwareUpdateOptions(rName, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckDomainExists(ctx, resourceName, &domain), + resource.TestCheckResourceAttr(resourceName, "software_update_options.0.auto_software_update_enabled", "true"), + ), + }, + }, + }) +} func TestAccOpenSearchDomain_disappears(t *testing.T) { ctx := acctest.Context(t) if testing.Short() { @@ -3503,3 +3536,20 @@ resource "aws_opensearch_domain" "test" { } `, rName, h, m) } + +func testAccDomainConfig_softwareUpdateOptions(rName string, option bool) string { + return fmt.Sprintf(` +resource "aws_opensearch_domain" "test" { + domain_name = %[1]q + + ebs_options { + ebs_enabled = true + volume_size = 10 + } + + software_update_options { + auto_software_update_enabled = %[2]t + } +} +`, rName, option) +} From a6056a96e5bdd7bece6fa46093b9303e7808a3c7 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 7 Sep 2023 12:00:34 -0500 Subject: [PATCH 4/6] aws_opensearch_domain: add datasource test --- internal/service/opensearch/domain_data_source_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/service/opensearch/domain_data_source_test.go b/internal/service/opensearch/domain_data_source_test.go index e08ac54cc58..513c8193301 100644 --- a/internal/service/opensearch/domain_data_source_test.go +++ b/internal/service/opensearch/domain_data_source_test.go @@ -51,6 +51,8 @@ func TestAccOpenSearchDomainDataSource_Data_basic(t *testing.T) { resource.TestCheckResourceAttrPair(datasourceName, "off_peak_window_options.#", resourceName, "off_peak_window_options.#"), resource.TestCheckResourceAttrPair(datasourceName, "snapshot_options.#", resourceName, "snapshot_options.#"), resource.TestCheckResourceAttrPair(datasourceName, "snapshot_options.0.automated_snapshot_start_hour", resourceName, "snapshot_options.0.automated_snapshot_start_hour"), + resource.TestCheckResourceAttrPair(datasourceName, "software_update_options.#", resourceName, "software_update_options.#"), + resource.TestCheckResourceAttrPair(datasourceName, "software_update_options.0.auto_software_update_enabled", resourceName, "software_update_options.0.auto_software_update_enabled"), ), }, }, @@ -179,6 +181,10 @@ POLICY snapshot_options { automated_snapshot_start_hour = 23 } + + software_update_options { + auto_software_update_enabled = true + } } data "aws_opensearch_domain" "test" { From 99ec1e7837dcb81840f0aff526df6ac3401f5a3c Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 7 Sep 2023 12:47:55 -0500 Subject: [PATCH 5/6] add CHANGELOG entry --- .changelog/32234.txt | 7 +++++++ internal/service/opensearch/domain_data_source.go | 10 +++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 .changelog/32234.txt diff --git a/.changelog/32234.txt b/.changelog/32234.txt new file mode 100644 index 00000000000..8aeaaa9c525 --- /dev/null +++ b/.changelog/32234.txt @@ -0,0 +1,7 @@ +```release-note:enhancement +resource/aws_opensearch_domain: Add `software_update_options` attribute +``` + +```release-note:enhancement +data-source/aws_opensearch_domain: Add `software_update_options` attribute +``` diff --git a/internal/service/opensearch/domain_data_source.go b/internal/service/opensearch/domain_data_source.go index 721188bcd4b..b75684f1c45 100644 --- a/internal/service/opensearch/domain_data_source.go +++ b/internal/service/opensearch/domain_data_source.go @@ -15,7 +15,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" - "github.com/hashicorp/terraform-provider-aws/internal/verify" ) // @SDKDataSource("aws_opensearch_domain") @@ -364,16 +363,13 @@ func DataSourceDomain() *schema.Resource { }, }, "software_update_options": { - Type: schema.TypeList, - Optional: true, - MaxItems: 1, - DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock, + Type: schema.TypeList, + Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "auto_software_update_enabled": { Type: schema.TypeBool, - Optional: true, - Default: false, + Computed: true, }, }, }, From 43d68b6c07c59f2a64c994938a2b1478adc59e23 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 7 Sep 2023 13:03:07 -0500 Subject: [PATCH 6/6] update documentation --- website/docs/d/opensearch_domain.html.markdown | 2 ++ website/docs/r/opensearch_domain.html.markdown | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/website/docs/d/opensearch_domain.html.markdown b/website/docs/d/opensearch_domain.html.markdown index 7502d5e8cc0..daaeca17f44 100644 --- a/website/docs/d/opensearch_domain.html.markdown +++ b/website/docs/d/opensearch_domain.html.markdown @@ -93,6 +93,8 @@ This data source exports the following attributes in addition to the arguments a * `processing` – Status of a configuration change in the domain. * `snapshot_options` – Domain snapshot related options. * `automated_snapshot_start_hour` - Hour during which the service takes an automated daily snapshot of the indices in the domain. +* `software_update_options` - Software update options for the domain + * `auto_software_update_enabled` - Enabled or disabled. * `tags` - Tags assigned to the domain. * `vpc_options` - VPC Options for private OpenSearch domains. * `availability_zones` - Availability zones used by the domain. diff --git a/website/docs/r/opensearch_domain.html.markdown b/website/docs/r/opensearch_domain.html.markdown index 8020e8b8e71..71daeb03f7f 100644 --- a/website/docs/r/opensearch_domain.html.markdown +++ b/website/docs/r/opensearch_domain.html.markdown @@ -335,6 +335,7 @@ The following arguments are optional: * `log_publishing_options` - (Optional) Configuration block for publishing slow and application logs to CloudWatch Logs. This block can be declared multiple times, for each log_type, within the same resource. Detailed below. * `node_to_node_encryption` - (Optional) Configuration block for node-to-node encryption options. Detailed below. * `snapshot_options` - (Optional) Configuration block for snapshot related options. Detailed below. DEPRECATED. For domains running OpenSearch 5.3 and later, Amazon OpenSearch takes hourly automated snapshots, making this setting irrelevant. For domains running earlier versions, OpenSearch takes daily automated snapshots. +* `software_update_options` - (Optional) Software update options for the domain. Detailed below. * `tags` - (Optional) Map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. * `vpc_options` - (Optional) Configuration block for VPC related options. Adding or removing this configuration forces a new resource ([documentation](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/vpc.html)). Detailed below. * `off_peak_window_options` - (Optional) Configuration to add Off Peak update options. ([documentation](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/off-peak.html)). Detailed below. @@ -439,6 +440,10 @@ AWS documentation: [Amazon Cognito Authentication for Dashboard](https://docs.aw * `automated_snapshot_start_hour` - (Required) Hour during which the service takes an automated daily snapshot of the indices in the domain. +### software_update_options + +* `auto_software_update_enabled` - (Optional) Whether automatic service software updates are enabled for the domain. Defaults to `false`. + ### vpc_options AWS documentation: [VPC Support for Amazon OpenSearch Service Domains](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/es-vpc.html)