/
alidns_handler.go
42 lines (34 loc) · 1.07 KB
/
alidns_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package alidns
import (
"fmt"
"github.com/TimothyYe/godns/internal/settings"
log "github.com/sirupsen/logrus"
)
// DNSProvider struct.
type DNSProvider struct {
aliDNS *AliDNS
}
func (provider *DNSProvider) Init(conf *settings.Settings) {
provider.aliDNS = NewAliDNS(
conf.Email,
conf.Password,
conf.IPType)
}
func (provider *DNSProvider) UpdateIP(domainName, subdomainName, ip string) error {
log.Infof("%s.%s - Start to update record IP...", subdomainName, domainName)
records := provider.aliDNS.GetDomainRecords(domainName, subdomainName)
if len(records) == 0 {
log.Errorf("Cannot get subdomain [%s] from AliDNS.", subdomainName)
return fmt.Errorf("cannot get subdomain [%s] from AliDNS", subdomainName)
}
if records[0].Value != ip {
records[0].Value = ip
if err := provider.aliDNS.UpdateDomainRecord(records[0]); err != nil {
return fmt.Errorf("failed to update IP for subdomain: %s", subdomainName)
}
log.Infof("IP updated for subdomain: %s", subdomainName)
} else {
log.Debugf("IP not changed for subdomain: %s", subdomainName)
}
return nil
}