Skip to content

Commit

Permalink
Merge pull request #1941 from aiven/byashimov-fix-zero-values-plan
Browse files Browse the repository at this point in the history
fix(userconfig): plan doesn't show zero values for new resources
  • Loading branch information
vmyroslav authored Dec 16, 2024
2 parents 8d6208f + de8e4fe commit 8b80997
Show file tree
Hide file tree
Showing 3 changed files with 82 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
- 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
Expand Down
76 changes: 76 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,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", "[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 = "foo"
cloud_name = "google-europe-west1"
plan = "business-4"
service_name = "bar"
opensearch_user_config {
opensearch {
%s
}
}
}`, 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 8b80997

Please sign in to comment.