Files
ddns-updater/internal/healthcheck/healthcheck.go
Quentin McGaw c23998bd09 Refactoring (#63)
- Only calls DNS API(s) once the public IP address changes
- Only one ip method per ip version (ipv4, ipv6, ipv4/v6)
- Gets the ip address once every period for all records
- More object oriented coding instead of functional
- Support to update ipv4 and ipv6 records separately, for supported DNS providers
2020-05-29 20:38:01 -04:00

46 lines
1.1 KiB
Go

package healthcheck
import (
"fmt"
"net"
"github.com/qdm12/ddns-updater/internal/constants"
"github.com/qdm12/ddns-updater/internal/data"
"github.com/qdm12/golibs/logging"
)
type lookupIPFunc func(host string) ([]net.IP, error)
// IsHealthy checks all the records were updated successfully and returns an error if not
func IsHealthy(db data.Database, lookupIP lookupIPFunc, logger logging.Logger) (err error) {
defer func() {
if err != nil {
logger.Warn("unhealthy: %s", err)
}
}()
records := db.SelectAll()
for _, record := range records {
if record.Status == constants.FAIL {
return fmt.Errorf("%s", record.String())
} else if !record.Settings.DNSLookup() {
continue
}
lookedUpIPs, err := lookupIP(record.Settings.BuildDomainName())
if err != nil {
return err
}
currentIP := record.History.GetCurrentIP()
if currentIP == nil {
return fmt.Errorf("no set IP address found")
}
for _, lookedUpIP := range lookedUpIPs {
if !lookedUpIP.Equal(currentIP) {
return fmt.Errorf(
"lookup IP address of %s is %s instead of %s",
record.Settings.BuildDomainName(), lookedUpIP, currentIP)
}
}
}
return nil
}