From 15588c59fe4097b78ffe41ac2b99609606d74b1e Mon Sep 17 00:00:00 2001 From: Tom Frenzel Date: Thu, 16 Apr 2026 17:00:25 +0000 Subject: [PATCH] fix: update TTLSeconds to be a pointer in DNSPolicy and adjust related tests --- internal/unifi/unifi.go | 14 +++++++++----- provider_test.go | 10 +++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/unifi/unifi.go b/internal/unifi/unifi.go index 019e4e2..0d63e10 100644 --- a/internal/unifi/unifi.go +++ b/internal/unifi/unifi.go @@ -48,7 +48,7 @@ func LibdnsToPolicy(record libdns.Record, zone string) (DNSPolicy, error) { Type: RecordTypeA, Domain: domain, IPv4Address: r.IP.String(), - TTLSeconds: ttl, + TTLSeconds: &ttl, Enabled: true, }, nil } else { @@ -56,7 +56,7 @@ func LibdnsToPolicy(record libdns.Record, zone string) (DNSPolicy, error) { Type: RecordTypeAAAA, Domain: domain, IPv6Address: r.IP.String(), - TTLSeconds: ttl, + TTLSeconds: &ttl, Enabled: true, }, nil } @@ -66,7 +66,7 @@ func LibdnsToPolicy(record libdns.Record, zone string) (DNSPolicy, error) { Type: RecordTypeCNAME, Domain: domain, TargetDomain: r.Target, - TTLSeconds: ttl, + TTLSeconds: &ttl, Enabled: true, }, nil @@ -110,7 +110,11 @@ func LibdnsToPolicy(record libdns.Record, zone string) (DNSPolicy, error) { // The zone parameter is required to extract the relative record name from the full domain. // For example, with domain "www.example.com" and zone "example.com", the name becomes "www". func PolicyToLibdns(policy DNSPolicy, zone string) (libdns.Record, error) { - ttl := time.Duration(policy.TTLSeconds) * time.Second + ttlSeconds := int32(0) + if policy.TTLSeconds != nil { + ttlSeconds = *policy.TTLSeconds + } + ttl := time.Duration(ttlSeconds) * time.Second // Extract relative name by removing zone suffix from domain name := policy.Domain @@ -213,7 +217,7 @@ type DNSPolicy struct { ID string `json:"id,omitempty"` Enabled bool `json:"enabled"` Domain string `json:"domain"` - TTLSeconds int32 `json:"ttlSeconds"` // This must not be "omitempty" as 0 maps to "auto" for Unifi. + TTLSeconds *int32 `json:"ttlSeconds,omitempty"` // Address record fields (A_RECORD, AAAA_RECORD) IPv4Address string `json:"ipv4Address,omitempty"` diff --git a/provider_test.go b/provider_test.go index f28d86e..3dd0a65 100644 --- a/provider_test.go +++ b/provider_test.go @@ -68,7 +68,11 @@ func TestGetRecords(t *testing.T) { libdns.Address{ Name: "test-b", IP: netip.MustParseAddr("192.0.2.2"), - TTL: 3600 * time.Second, + TTL: 0, + }, + libdns.Address{ + Name: "test-c", + IP: netip.MustParseAddr("192.0.2.3"), }, } @@ -100,13 +104,13 @@ func TestGetRecords(t *testing.T) { addr := record.(libdns.Address) found := false for _, gotRecord := range got { - if gotAddr, ok := gotRecord.(libdns.Address); ok && gotAddr.Name == addr.Name && gotAddr.IP == addr.IP { + if gotAddr, ok := gotRecord.(libdns.Address); ok && gotAddr.Name == addr.Name && gotAddr.IP == addr.IP && gotAddr.TTL == addr.TTL { found = true break } } if !found { - t.Errorf("Expected record %s -> %s not found in GetRecords", addr.Name, addr.IP) + t.Errorf("Expected record %s -> %s with TTL %v not found in GetRecords", addr.Name, addr.IP, addr.TTL) } } }