changed validation for MX record corner case

This commit is contained in:
Lourenço Vales
2025-10-05 21:34:37 +02:00
parent 1787e44503
commit ce4d2bfe50

View File

@@ -161,8 +161,10 @@ func (obj *CloudflareDNSRes) Validate() error {
return fmt.Errorf("priority is required for MX records") return fmt.Errorf("priority is required for MX records")
} }
if obj.MetaParams().Poll == 0 || obj.MetaParams().Poll < 1 { // CF accepts ~4req/s so this is good enough // cloudflare accepts ~4req/s so this is safe enough even when managing lots
return fmt.Errorf("cloudflare:dns requires polling, set Meta:poll param (e.g., 60 seconds), min. 1s") // of records
if obj.MetaParams().Poll == 0 || obj.MetaParams().Poll < 60 {
return fmt.Errorf("cloudflare:dns requires polling, set Meta:poll param (e.g., 300s), min. 60s")
} }
return nil return nil
@@ -216,21 +218,6 @@ func (obj *CloudflareDNSRes) Watch(context.Context) error {
// if necessary. If apply is false, it only checks if changes are needed. If // if necessary. If apply is false, it only checks if changes are needed. If
// Purge is enabled, it will first delete any unmanaged records in the zone. // Purge is enabled, it will first delete any unmanaged records in the zone.
func (obj *CloudflareDNSRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *CloudflareDNSRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
zone, err := obj.client.Zones.List(ctx, zones.ZoneListParams{
Name: cloudflare.F(obj.Zone),
})
if err != nil {
return false, err
}
if len(zone.Result) == 0 {
return false, fmt.Errorf("there's no zone registered with name %s", obj.Zone)
}
if len(zone.Result) > 1 {
return false, fmt.Errorf("there's more than one zone with name %s", obj.Zone)
}
// We start by checking the need for purging // We start by checking the need for purging
if obj.Purge { if obj.Purge {
checkOK, err := obj.purgeCheckApply(ctx, apply) checkOK, err := obj.purgeCheckApply(ctx, apply)
@@ -374,10 +361,18 @@ func (obj *CloudflareDNSRes) Cmp(r engine.Res) error {
return fmt.Errorf("content param differs") return fmt.Errorf("content param differs")
} }
if obj.Priority != res.Priority { if (obj.Priority == nil) != (res.Priority == nil) {
return fmt.Errorf("the priority param differs") return fmt.Errorf("the priority param differs")
} }
if obj.Priority != nil && *obj.Priority != *obj.Priority {
return fmt.Errorf("the priority param differs")
}
if obj.Data != res.Data {
return fmt.Errorf("the data param differs")
}
return nil return nil
} }
@@ -471,7 +466,7 @@ func (obj *CloudflareDNSRes) buildRecordParam() (any, error) {
param := dns.NSRecordParam{ param := dns.NSRecordParam{
Name: cloudflare.F(obj.RecordName), Name: cloudflare.F(obj.RecordName),
Type: cloudflare.F(dns.NSRecordTypeNS), Type: cloudflare.F(dns.NSRecordTypeNS),
Content: cloudflare.F(obj.Content), Data: cloudflare.F(obj.Data),
TTL: cloudflare.F(ttl), TTL: cloudflare.F(ttl),
} }
if obj.Proxied != nil { if obj.Proxied != nil {
@@ -659,6 +654,10 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
if cfRes.Zone == obj.Zone { if cfRes.Zone == obj.Zone {
recordKey := fmt.Sprintf("%s:%s:%s", cfRes.RecordName, cfRes.Type, recordKey := fmt.Sprintf("%s:%s:%s", cfRes.RecordName, cfRes.Type,
cfRes.Content) cfRes.Content)
if cfRes.Priority != nil {
// corner case for MX records which require priority set
recordKey = fmt.Sprintf("%s:%d", recordKey, *cfRes.Priority)
}
excludes[recordKey] = true excludes[recordKey] = true
} }
} }