From c8c04dfcf7cc3365d109774dac3c485a69ec3f0b Mon Sep 17 00:00:00 2001
From: Alexander Puzynia <werwolf.by@gmail.com>
Date: Wed, 25 Jun 2025 23:49:27 -0700
Subject: [PATCH 1/2] fix: prevent drift in netlify_site_domain_settings
 resource

Fix constant drift detection in netlify_site_domain_settings by properly
handling null values from the API. The Read function now correctly sets
null values for empty optional fields instead of converting them to
empty strings, preventing Terraform from detecting false differences.
---
 .../provider/site_domain_settings_resource.go | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/internal/provider/site_domain_settings_resource.go b/internal/provider/site_domain_settings_resource.go
index 7cedc50..a13c187 100644
--- a/internal/provider/site_domain_settings_resource.go
+++ b/internal/provider/site_domain_settings_resource.go
@@ -133,13 +133,26 @@ func (r *siteDomainSettingsResource) Read(ctx context.Context, req resource.Read
 		return
 	}
 
-	state.CustomDomain = types.StringValue(site.CustomDomain)
+	if site.CustomDomain != "" {
+		state.CustomDomain = types.StringValue(site.CustomDomain)
+	} else {
+		state.CustomDomain = types.StringNull()
+	}
 	state.DomainAliases = make([]types.String, len(site.DomainAliases))
 	for i, domainAlias := range site.DomainAliases {
 		state.DomainAliases[i] = types.StringValue(domainAlias)
 	}
-	state.BranchDeployCustomDomain = types.StringValue(site.BranchDeployCustomDomain)
-	state.DeployPreviewCustomDomain = types.StringValue(site.DeployPreviewCustomDomain)
+	if site.BranchDeployCustomDomain != "" {
+		state.BranchDeployCustomDomain = types.StringValue(site.BranchDeployCustomDomain)
+	} else {
+		state.BranchDeployCustomDomain = types.StringNull()
+	}
+
+	if site.DeployPreviewCustomDomain != "" {
+		state.DeployPreviewCustomDomain = types.StringValue(site.DeployPreviewCustomDomain)
+	} else {
+		state.DeployPreviewCustomDomain = types.StringNull()
+	}
 
 	resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
 	if resp.Diagnostics.HasError() {

From 44a76e0e78f6d9aa87b4f5d98845868d72fc88b1 Mon Sep 17 00:00:00 2001
From: Alexander Puzynia <werwolf.by@gmail.com>
Date: Wed, 25 Jun 2025 23:55:47 -0700
Subject: [PATCH 2/2] test: prevent drift in netlify_site_domain_settings
 resource

---
 .../site_domain_settings_resource_test.go     | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/internal/provider/site_domain_settings_resource_test.go b/internal/provider/site_domain_settings_resource_test.go
index d005aaf..ddedbc1 100644
--- a/internal/provider/site_domain_settings_resource_test.go
+++ b/internal/provider/site_domain_settings_resource_test.go
@@ -54,3 +54,44 @@ func TestAccSiteDomainSettings(t *testing.T) {
 		},
 	}, func(s *terraform.State) error { return nil })
 }
+
+func TestAccSiteDomainSettingsNullFields(t *testing.T) {
+	// Test case for preventing drift when optional fields are null from API
+	accTest(t, []resource.TestStep{
+		{
+			Config: `resource "netlify_site_domain_settings" "null_fields" {
+  site_id       = "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"
+  custom_domain = "tf-test-null-fields.examplepetstore.com"
+}`,
+			Check: resource.ComposeAggregateTestCheckFunc(
+				resource.TestCheckResourceAttr("netlify_site_domain_settings.null_fields", "site_id", "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"),
+				resource.TestCheckResourceAttr("netlify_site_domain_settings.null_fields", "custom_domain", "tf-test-null-fields.examplepetstore.com"),
+				resource.TestCheckResourceAttr("netlify_site_domain_settings.null_fields", "domain_aliases.#", "0"),
+				// These should be null/empty when not set
+				resource.TestCheckNoResourceAttr("netlify_site_domain_settings.null_fields", "branch_deploy_custom_domain"),
+				resource.TestCheckNoResourceAttr("netlify_site_domain_settings.null_fields", "deploy_preview_custom_domain"),
+			),
+		},
+		{
+			// Second step with same config should not detect changes (no drift)
+			Config: `resource "netlify_site_domain_settings" "null_fields" {
+  site_id       = "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"
+  custom_domain = "tf-test-null-fields.examplepetstore.com"
+}`,
+			PlanOnly: true,
+			Check: resource.ComposeAggregateTestCheckFunc(
+				resource.TestCheckResourceAttr("netlify_site_domain_settings.null_fields", "site_id", "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"),
+				resource.TestCheckResourceAttr("netlify_site_domain_settings.null_fields", "custom_domain", "tf-test-null-fields.examplepetstore.com"),
+				resource.TestCheckResourceAttr("netlify_site_domain_settings.null_fields", "domain_aliases.#", "0"),
+			),
+		},
+		{
+			ResourceName:                         "netlify_site_domain_settings.null_fields",
+			ImportState:                          true,
+			ImportStateId:                        "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26",
+			ImportStateVerifyIdentifierAttribute: "site_id",
+			ImportStateVerify:                    true,
+			ImportStateVerifyIgnore:              []string{"last_updated"},
+		},
+	}, func(s *terraform.State) error { return nil })
+}
