dnscontrol/providers/gandi/gandiProvider.go

129 lines
3.1 KiB
Go
Raw Normal View History

2016-08-23 08:31:50 +08:00
package gandi
import (
"encoding/json"
"fmt"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/providers"
"github.com/StackExchange/dnscontrol/providers/diff"
gandidomain "github.com/prasmussen/gandi-api/domain"
2016-08-23 08:31:50 +08:00
gandirecord "github.com/prasmussen/gandi-api/domain/zone/record"
)
/*
Gandi API DNS provider:
Info required in `creds.json`:
- apikey
*/
type GandiApi struct {
ApiKey string
domainIndex map[string]int64 // Map of domainname to index
nameservers map[string][]*models.Nameserver
ZoneId int64
}
type gandiRecord struct {
2016-08-23 08:31:50 +08:00
gandirecord.RecordInfo
}
func (c *GandiApi) getDomainInfo(domain string) (*gandidomain.DomainInfo, error) {
if err := c.fetchDomainList(); err != nil {
return nil, err
2016-08-23 08:31:50 +08:00
}
_, ok := c.domainIndex[domain]
2016-08-23 08:31:50 +08:00
if !ok {
return nil, fmt.Errorf("%s not listed in zones for gandi account", domain)
2016-08-23 08:31:50 +08:00
}
return c.fetchDomainInfo(domain)
}
2016-08-23 08:31:50 +08:00
func (c *GandiApi) GetNameservers(domain string) ([]*models.Nameserver, error) {
domaininfo, err := c.getDomainInfo(domain)
2016-08-23 08:31:50 +08:00
if err != nil {
return nil, err
}
ns := []*models.Nameserver{}
2016-08-23 08:31:50 +08:00
for _, nsname := range domaininfo.Nameservers {
ns = append(ns, &models.Nameserver{Name: nsname})
}
return ns, nil
}
func (c *GandiApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
domaininfo, err := c.getDomainInfo(dc.Name)
if err != nil {
return nil, err
2016-08-23 08:31:50 +08:00
}
foundRecords, err := c.getZoneRecords(domaininfo.ZoneId)
if err != nil {
return nil, err
}
2016-08-23 08:31:50 +08:00
expectedRecordSets := make([]gandirecord.RecordSet, len(dc.Records))
for i, rec := range dc.Records {
if rec.Type == "MX" {
rec.Target = fmt.Sprintf("%d %s", rec.Priority, rec.Target)
2016-08-23 08:31:50 +08:00
}
if rec.Type == "TXT" {
rec.Target = "\"" + rec.Target + "\"" // FIXME(tlim): Should do proper quoting.
2016-08-23 08:31:50 +08:00
}
expectedRecordSets[i] = gandirecord.RecordSet{}
expectedRecordSets[i]["type"] = rec.Type
expectedRecordSets[i]["name"] = rec.Name
expectedRecordSets[i]["value"] = rec.Target
expectedRecordSets[i]["ttl"] = rec.TTL
2016-08-23 08:31:50 +08:00
}
differ := diff.New(dc)
_, create, del, mod := differ.IncrementalDiff(foundRecords)
2016-08-23 08:31:50 +08:00
// Print a list of changes. Generate an actual change that is the zone
changes := false
for _, i := range create {
changes = true
fmt.Println(i)
}
for _, i := range del {
changes = true
fmt.Println(i)
}
for _, i := range mod {
changes = true
fmt.Println(i)
}
msg := fmt.Sprintf("GENERATE_ZONE: %s (%d records)", dc.Name, len(dc.Records))
2016-08-23 08:31:50 +08:00
corrections := []*models.Correction{}
if changes {
corrections = append(corrections,
&models.Correction{
Msg: msg,
F: func() error {
fmt.Printf("CREATING ZONE: %v\n", dc.Name)
return c.createGandiZone(dc.Name, domaininfo.ZoneId, expectedRecordSets)
},
})
}
return corrections, nil
}
func newGandi(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
api := &GandiApi{}
api.ApiKey = m["apikey"]
if api.ApiKey == "" {
return nil, fmt.Errorf("Gandi apikey must be provided.")
}
return api, nil
}
func init() {
providers.RegisterDomainServiceProviderType("GANDI", newGandi)
}