-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdns.go
44 lines (37 loc) · 961 Bytes
/
dns.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
43
44
package main
import (
"net"
"github.com/Sirupsen/logrus"
"github.com/cloudflare/cloudflare-go"
)
type CFDNSUpdater struct {
log *logrus.Entry
cf *cloudflare.API
zoneId string
}
func NewCFDNSUpdater(zoneId string, apiKey string, apiEmail string, log *logrus.Entry) (*CFDNSUpdater, error) {
api, err := cloudflare.New(apiKey, apiEmail)
if err != nil {
return nil, err
}
return &CFDNSUpdater{
cf: api,
zoneId: zoneId,
log: log,
}, nil
}
func (c *CFDNSUpdater) UpdateRecordA(host string, ip net.IP) error {
// Fetch record IDs for the records we need to update.
records, err := c.cf.DNSRecords(c.zoneId, cloudflare.DNSRecord{Name: host, Type: "A"})
if err != nil {
return err
}
for _, r := range records {
c.log.Infof("Updating record with ID %s to %s", r.ID, ip.String())
err := c.cf.UpdateDNSRecord(c.zoneId, r.ID, cloudflare.DNSRecord{Content: ip.String()})
if err != nil {
return err
}
}
return nil
}