92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package updateip
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/transip/gotransip/v6"
|
|
transipdomain "github.com/transip/gotransip/v6/domain"
|
|
)
|
|
|
|
type transipProvider struct {
|
|
repo transipdomain.Repository
|
|
}
|
|
|
|
func NewTransIP(config *ProviderConfig) (Provider, error) {
|
|
client, err := gotransip.NewClient(gotransip.ClientConfiguration{
|
|
AccountName: config.Username,
|
|
PrivateKeyPath: config.PrivateKey,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &transipProvider{
|
|
repo: transipdomain.Repository{Client: client},
|
|
}, nil
|
|
}
|
|
|
|
func (p *transipProvider) UpdateIP(domain, name string, ip net.IP) error {
|
|
domainName := UnFqdn(domain)
|
|
entries, err := p.repo.GetDNSEntries(domainName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
entryName := CompressLabel(domainName, UnFqdn(name))
|
|
for _, entry := range entries {
|
|
log := log.WithFields(log.Fields{
|
|
"domain": domainName,
|
|
"name": entry.Name,
|
|
"type": entry.Type,
|
|
"ttl": entry.Expire,
|
|
"content": entry.Content,
|
|
})
|
|
log.Trace("checking DNS record")
|
|
|
|
if strings.EqualFold(ExapandLabel(domainName, entryName), ExapandLabel(domainName, entry.Name)) {
|
|
switch {
|
|
case IsLabel(entry.Type):
|
|
if entry.Content != ip.String() {
|
|
log.WithField("content", ip.String()).Info("updating DNS record content")
|
|
return p.repo.UpdateDNSEntry(domainName, entry)
|
|
}
|
|
log.Debug("not updating DNS record; already up-to-date!")
|
|
return nil
|
|
|
|
case IsCanonicalName(entry.Type):
|
|
log.Info("removing DNS record")
|
|
if err = p.repo.RemoveDNSEntry(domainName, entry); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// No entry found, create one
|
|
entry := transipdomain.DNSEntry{
|
|
Name: entryName,
|
|
Type: "A",
|
|
Content: ip.String(),
|
|
Expire: int(DefaultTTL.Seconds()),
|
|
}
|
|
log.WithFields(log.Fields{
|
|
"name": entry.Name,
|
|
"type": "A",
|
|
"new": ip.String(),
|
|
"expire": entry.Expire,
|
|
}).Info("creating DNS record")
|
|
return p.repo.AddDNSEntry(domainName, entry)
|
|
}
|
|
|
|
// IsLabel check if the DNS record type is a DNS label (A-record).
|
|
func IsLabel(recordType string) bool {
|
|
return strings.EqualFold(recordType, "A")
|
|
}
|
|
|
|
// IsCanonicalName checks if the DNS record type is a DNS canonical name (CNAME record).
|
|
func IsCanonicalName(recordType string) bool {
|
|
return strings.EqualFold(recordType, "CNAME")
|
|
}
|