Skip to content

Commit

Permalink
fix(userconfig): plan doesn't show zero values for new resources
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Dec 13, 2024
1 parent 08c8a97 commit ab45bc6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

## [4.30.0] - 2024-12-05

Expand Down
77 changes: 77 additions & 0 deletions internal/sdkprovider/service/opensearch/opensearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,80 @@ 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{
PreCheck: func() { acc.TestAccPreCheck(t) },
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", "[email protected]",
),
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 = %q
cloud_name = "google-europe-west1"
plan = "business-4"
service_name = "os-19b9f466"
opensearch_user_config {
opensearch {
%s
}
}
}`, os.Getenv("AIVEN_PROJECT_NAME"), strings.Join(options, "\n"))
}
5 changes: 5 additions & 0 deletions internal/sdkprovider/userconfig/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab45bc6

Please sign in to comment.