Skip to content
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
14 changes: 9 additions & 5 deletions internal/unifi/unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ 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 {
return DNSPolicy{
Type: RecordTypeAAAA,
Domain: domain,
IPv6Address: r.IP.String(),
TTLSeconds: ttl,
TTLSeconds: &ttl,
Enabled: true,
}, nil
}
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand Down
10 changes: 7 additions & 3 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}

Expand Down Expand Up @@ -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)
}
}
}
Expand Down