mattolenik / cloudflare-ddns-client

A robust dynamic DNS client for CloudFlare, geared towards home lab usage. Written in Go.
The Unlicense
6 stars 2 forks source link

already exists check doesn't work with bare hostname as record #16

Open anttiryt opened 1 year ago

anttiryt commented 1 year ago
rac@r2d2:~$ bin/updateCF
cloudflare-ddns --domain anttirytsola.fi --record r2d2 --token ...  --verbose
12:15PM INF Found public IP '34.75.222.'
12:15PM DBG Examining DNS record ID '9512c5261a71d4334357bf4c4dd58390' with name 'anttirytsola.fi'
12:15PM DBG Examining DNS record ID '0b2c47be8026ccead6ea06ab7197c5d1' with name 'horizon.anttirytsola.fi'
12:15PM DBG Examining DNS record ID '86588276cf8bce7116a0915aeafa8cd6' with name 'r2d2.anttirytsola.fi'
12:15PM DBG Examining DNS record ID '1e7f2a3a91b9d4dce5e10ba776b1f3a6' with name 'rytsolafi.anttirytsola.fi'
12:15PM DBG Examining DNS record ID 'c746198fe7c0bd772f02cb1018fab1ab' with name 'www.anttirytsola.fi'
12:15PM INF No DNS record 'r2d2' found for domain 'anttirytsola.fi', creating now
Error: failed to create DNS record 'r2d2' on domain 'anttirytsola.fi': HTTP status 400: Record already exists. (81057)
12:15PM ERR HTTP status 400: Record already exists. (81057)
cloudflare/cloudflare.go:75: failed to create DNS record 'r2d2' on domain 'anttirytsola.fi'

Seems to be cloudflare.go 58.. 85

in this case record is hostname r2d2 and when checking r.Name is with domain, r2d2.anttirytsola.fi

    }
    // Find the specific record
    var recordID string
    for _, r := range records {
        log.Debug().Msgf("Examining DNS record ID '%s' with name '%s'", r.ID, r.Name)
        if r.Name == record {         <-  This check fails. 
            recordID = r.ID
            if r.Content == ip {
                log.Info().Msgf("DNS record '%s' is already set to IP '%s'", record, ip)
                return nil
            }
            break
        }
    }
    // Create the record if it's not already there
    if recordID == "" {
        log.Info().Msgf("No DNS record '%s' found for domain '%s', creating now", record, domain)
        resp, err := p.client.CreateDNSRecord(p.ctx, zoneID, cloudflare.DNSRecord{
            Content: ip,
            Type:    "A",
            Name:    record,
        })
        if err != nil {
            return errors.Annotatef(err, "failed to create DNS record '%s' on domain '%s'", record, domain)
        }
        recordID = resp.Result.ID
    }