Skip to content
Open
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
21 changes: 21 additions & 0 deletions internal/provider/resource_kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ func kafkaTopicResource() *schema.Resource {
Optional: true,
Computed: true,
Description: "The custom topic settings to set (e.g., `\"cleanup.policy\" = \"compact\"`).",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "" && new == "" {
return true
}

// when old value exists but new value is empty, check if key is in new config and suppress diff if not
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

The comment has inconsistent spacing and should end with a period for consistency: "// When old value exists but new value is empty, check if key is in new config and suppress diff if not."

Suggested change
// when old value exists but new value is empty, check if key is in new config and suppress diff if not
// When old value exists but new value is empty, check if the key is in the new config and suppress the diff if not.

Copilot uses AI. Check for mistakes.
if new == "" && old != "" {
configKey := k
if strings.HasPrefix(k, "config.") {
configKey = strings.TrimPrefix(k, "config.")
}

if newConfigs, ok := d.GetOk(paramConfigs); ok {
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

The logic should handle the case where d.GetOk(paramConfigs) returns false. Consider adding an explicit return statement or handling the else case to make the intention clearer.

Copilot uses AI. Check for mistakes.
newConfigMap := newConfigs.(map[string]interface{})
_, keyExists := newConfigMap[configKey]

return !keyExists
}
}
return false
},
},
paramCredentials: credentialsSchema(),
},
Expand Down
31 changes: 31 additions & 0 deletions internal/provider/resource_kafka_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
Expand Down Expand Up @@ -200,7 +201,7 @@
resource.TestCheckResourceAttr(fullTopicResourceLabel, "rest_endpoint", mockTopicTestServerUrl),
resource.TestCheckResourceAttr(fullTopicResourceLabel, "config.%", "2"),
resource.TestCheckResourceAttr(fullTopicResourceLabel, "config.max.message.bytes", "12345"),
resource.TestCheckResourceAttr(fullTopicResourceLabel, "config.retention.ms", "6789"),

Check failure on line 204 in internal/provider/resource_kafka_topic_test.go

View check run for this annotation

SonarQube-Confluent / terraform-provider-confluent Sonarqube Results

internal/provider/resource_kafka_topic_test.go#L204

Define a constant instead of duplicating this literal "config.retention.ms" 6 times.
resource.TestCheckNoResourceAttr(fullTopicResourceLabel, fmt.Sprintf("config.%s", fifthConfigName)),
resource.TestCheckResourceAttr(fullTopicResourceLabel, "credentials.#", "1"),
resource.TestCheckResourceAttr(fullTopicResourceLabel, "credentials.0.%", "2"),
Expand Down Expand Up @@ -503,6 +504,36 @@
checkStubCount(t, wiremockClient, deleteTopicStub, fmt.Sprintf("DELETE %s", kafkaTopicPath), expectedCountTwo)
}

func TestAccTopicConfigDiffSuppression(t *testing.T) {
d := schema.TestResourceDataRaw(t, kafkaTopicResource().Schema, map[string]interface{}{
"config": map[string]interface{}{
"retention.ms": "604800000",
},
})

diffSuppressFunc := kafkaTopicResource().Schema["config"].DiffSuppressFunc

result1 := diffSuppressFunc("config.cleanup.policy", "", "", d)
if !result1 {
t.Error("Expected diff to be suppressed when both old and new values are empty")
}

result2 := diffSuppressFunc("config.cleanup.policy", "delete", "", d)
if !result2 {
t.Error("Expected diff to be suppressed when old value exists but key is not in new config")
}

result3 := diffSuppressFunc("config.retention.ms", "123456", "", d)
if result3 {
t.Error("Expected diff NOT to be suppressed when key exists in new config but values differ")
}

result4 := diffSuppressFunc("config.retention.ms", "123456", "604800000", d)
if result4 {
t.Error("Expected diff NOT to be suppressed when both values exist but are different")
}
}

func testAccCheckTopicDestroy(s *terraform.State, url string) error {
testClient := testAccProvider.Meta().(*Client)
c := testClient.kafkaRestClientFactory.CreateKafkaRestClient(url, clusterId, kafkaApiKey, kafkaApiSecret, false, false, testClient.oauthToken)
Expand Down