From de8e4feece3382e16aafff74e0ded241ee255199 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Thu, 12 Dec 2024 22:53:56 +0100 Subject: [PATCH] fix(userconfig): plan doesn't show zero values for new resources --- CHANGELOG.md | 1 + .../service/opensearch/opensearch_test.go | 76 +++++++++++++++++++ internal/sdkprovider/userconfig/diff/diff.go | 5 ++ 3 files changed, 82 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea7c71c9c..2d56bb723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ nav_order: 1 - Add `alloydbomni` BETA resource and datasource - Add `aiven_alloydbomni_user` BETA resource and datasource - Add `aiven_alloydbomni_database` BETA resource and datasource +- Fix `terraform plan`: new resources don't display zero values for user configuration options - Add `aiven_service_integration` resource field `destination_service_project`: Destination service project name - Add `aiven_service_integration` resource field `source_service_project`: Source service project name - Add `aiven_service_integration` datasource field `destination_service_project`: Destination service project name diff --git a/internal/sdkprovider/service/opensearch/opensearch_test.go b/internal/sdkprovider/service/opensearch/opensearch_test.go index a6966483d..11f823980 100644 --- a/internal/sdkprovider/service/opensearch/opensearch_test.go +++ b/internal/sdkprovider/service/opensearch/opensearch_test.go @@ -202,3 +202,79 @@ func testAccCheckAivenServiceOSAttributes(n string) resource.TestCheckFunc { return nil } } + +// TestAccAivenOpenSearchUser_user_config_zero_values +// Tests that user config diff suppress doesn't suppress zero values for new resources, and they appear in the plan. +func TestAccAivenOpenSearchUser_user_config_zero_values(t *testing.T) { + resourceName := "aiven_opensearch.foo" + resource.ParallelTest(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestProtoV6ProviderFactories, + CheckDestroy: testAccCheckAivenOpenSearchUserResourceDestroy, + Steps: []resource.TestStep{ + { + // 1. No user config at all + Config: testAccAivenOpenSearchUserUserConfigZeroValues(), + PlanOnly: true, + ExpectNonEmptyPlan: true, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.#", "0"), + ), + }, + { + // 2. All values are non-zero + Config: testAccAivenOpenSearchUserUserConfigZeroValues( + "action_destructive_requires_name", "true", + "override_main_response_version", "true", + "knn_memory_circuit_breaker_limit", "1", + "email_sender_username", "test@aiven.io", + ), + PlanOnly: true, + ExpectNonEmptyPlan: true, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.#", "1"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.action_destructive_requires_name", "true"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.override_main_response_version", "true"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.knn_memory_circuit_breaker_limit", "1"), + ), + }, + { + // 2. All values are zero + Config: testAccAivenOpenSearchUserUserConfigZeroValues( + "action_destructive_requires_name", "false", + "override_main_response_version", "true", + "knn_memory_circuit_breaker_limit", "0", + "email_sender_username", "", + ), + PlanOnly: true, + ExpectNonEmptyPlan: true, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.#", "1"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.action_destructive_requires_name", "false"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.override_main_response_version", "true"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.knn_memory_circuit_breaker_limit", "0"), + resource.TestCheckResourceAttr(resourceName, "opensearch_user_config.0.opensearch.0.email_sender_username", ""), + ), + }, + }, + }) +} + +func testAccAivenOpenSearchUserUserConfigZeroValues(kv ...string) string { + options := make([]string, 0) + for i := 0; i < len(kv); i += 2 { + options = append(options, fmt.Sprintf(`%s = "%s"`, kv[i], kv[i+1])) + } + return fmt.Sprintf(` +resource "aiven_opensearch" "os2" { + project = "foo" + cloud_name = "google-europe-west1" + plan = "business-4" + service_name = "bar" + + opensearch_user_config { + opensearch { + %s + } + } +}`, strings.Join(options, "\n")) +} diff --git a/internal/sdkprovider/userconfig/diff/diff.go b/internal/sdkprovider/userconfig/diff/diff.go index 132ef6bd3..1bb1c2dc4 100644 --- a/internal/sdkprovider/userconfig/diff/diff.go +++ b/internal/sdkprovider/userconfig/diff/diff.go @@ -16,6 +16,11 @@ var ( // SuppressUnchanged suppresses diff for unchanged fields. // Applied for all nested values: both for objects and arrays. func SuppressUnchanged(k, oldValue, newValue string, d *schema.ResourceData) bool { + if d.Id() == "" { + // Do not suppress diff for new resources, must show the whole diff. + return false + } + // schema.TypeMap if strings.HasSuffix(k, ".%") { return oldValue == newValue