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

fix(userconfig): plan doesn't show zero values for new resources #1941

Merged
merged 1 commit into from
Dec 16, 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
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
Loading