Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opensearch_config: only send PATCH on create if values set. #1268

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions digitalocean/database/resource_database_opensearch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ func resourceDigitalOceanDatabaseOpensearchConfigCreate(ctx context.Context, d *
client := meta.(*config.CombinedConfig).GodoClient()
clusterID := d.Get("cluster_id").(string)

if err := updateOpensearchConfig(ctx, d, client); err != nil {
return diag.Errorf("Error updating Opensearch configuration: %s", err)
if d.HasChangeExcept("cluster_id") {
if err := updateOpensearchConfig(ctx, d, client); err != nil {
return diag.Errorf("Error updating Opensearch configuration: %s", err)
}
}

d.SetId(makeDatabaseOpensearchConfigID(clusterID))
Expand All @@ -278,11 +280,11 @@ func updateOpensearchConfig(ctx context.Context, d *schema.ResourceData, client

opts := &godo.OpensearchConfig{}

if v, ok := d.GetOk("ism_enabled"); ok {
if v, ok := d.GetOkExists("ism_enabled"); ok {
opts.IsmEnabled = godo.PtrTo(v.(bool))
}

if v, ok := d.GetOk("ism_history_enabled"); ok {
if v, ok := d.GetOkExists("ism_history_enabled"); ok {
opts.IsmHistoryEnabled = godo.PtrTo(v.(bool))
}

Expand Down Expand Up @@ -350,15 +352,15 @@ func updateOpensearchConfig(ctx context.Context, d *schema.ResourceData, client
opts.IndicesRecoveryMaxConcurrentFileChunks = godo.PtrTo(v.(int))
}

if v, ok := d.GetOk("action_auto_create_index_enabled"); ok {
if v, ok := d.GetOkExists("action_auto_create_index_enabled"); ok {
opts.ActionAutoCreateIndexEnabled = godo.PtrTo(v.(bool))
}

if v, ok := d.GetOk("action_destructive_requires_name"); ok {
if v, ok := d.GetOkExists("action_destructive_requires_name"); ok {
opts.ActionDestructiveRequiresName = godo.PtrTo(v.(bool))
}

if v, ok := d.GetOk("enable_security_audit"); ok {
if v, ok := d.GetOkExists("enable_security_audit"); ok {
opts.EnableSecurityAudit = godo.PtrTo(v.(bool))
}

Expand Down Expand Up @@ -406,7 +408,7 @@ func updateOpensearchConfig(ctx context.Context, d *schema.ResourceData, client
opts.ThreadPoolForceMergeSize = godo.PtrTo(v.(int))
}

if v, ok := d.GetOk("override_main_response_version"); ok {
if v, ok := d.GetOkExists("override_main_response_version"); ok {
opts.OverrideMainResponseVersion = godo.PtrTo(v.(bool))
}

Expand All @@ -422,7 +424,7 @@ func updateOpensearchConfig(ctx context.Context, d *schema.ResourceData, client
opts.ClusterRoutingAllocationNodeConcurrentRecoveries = godo.PtrTo(v.(int))
}

if v, ok := d.GetOk("plugins_alerting_filter_by_backend_roles_enabled"); ok {
if v, ok := d.GetOkExists("plugins_alerting_filter_by_backend_roles_enabled"); ok {
opts.PluginsAlertingFilterByBackendRolesEnabled = godo.PtrTo(v.(bool))
}

Expand Down
14 changes: 10 additions & 4 deletions digitalocean/database/resource_database_opensearch_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ func TestAccDigitalOceanDatabaseOpensearchConfig_Basic(t *testing.T) {
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseOpensearchConfigConfigBasic, dbConfig, true, 10, "1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "enable_security_audit", "true"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_enabled", "true"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_enabled", "true"),
Comment on lines +23 to +25
Copy link
Member Author

@andrewsomething andrewsomething Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently both ism_enabled and ism_history_enabled must be true when setting the other ism_history settings or the API returns errors, e.g.

        Error: Error updating Opensearch configuration: PATCH https://api.digitalocean.com/v2/databases/d6b65b4f-954d-4ee0-a29b-29eb2b4d228f/config: 422 (request "7f2461f9-6c49-4291-a304-364f1d5eb740") invalid 'user_config': invalid input for opensearch: 'ism_history_enabled' is a required property

I've added enable_security_audit so that we have another bool value to test changes. This exposed the other bug.

resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_max_age_hours", "10"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_max_docs", "1"),
),
},
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseOpensearchConfigConfigBasic, dbConfig, false, 1, "9223372036854775807"),
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseOpensearchConfigConfigBasic, dbConfig, false, 1, "1000000000000000000"),
Comment on lines -29 to +31
Copy link
Member Author

@andrewsomething andrewsomething Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're hitting a bug with upstream Terraform here. I've change this to another value to allow us to test the broader functionality despite this.

hashicorp/terraform-plugin-sdk#1215

Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_enabled", "false"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "enable_security_audit", "false"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_enabled", "true"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_enabled", "true"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_max_age_hours", "1"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_max_docs", "9223372036854775807"),
resource.TestCheckResourceAttr("digitalocean_database_opensearch_config.foobar", "ism_history_max_docs", "1000000000000000000"),
),
},
},
Expand All @@ -42,7 +46,9 @@ const testAccCheckDigitalOceanDatabaseOpensearchConfigConfigBasic = `

resource "digitalocean_database_opensearch_config" "foobar" {
cluster_id = digitalocean_database_cluster.foobar.id
ism_enabled = %t
enable_security_audit = %t
ism_enabled = true
ism_history_enabled = true
ism_history_max_age_hours = %d
ism_history_max_docs = %s
}`
Loading