Skip to content

netlify_site_domain_settings_changes fix update in place without changes #121

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
19 changes: 16 additions & 3 deletions internal/provider/site_domain_settings_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
41 changes: 41 additions & 0 deletions internal/provider/site_domain_settings_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}