mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Refactoring diff package interface (#22)
* initial refactoring of diffing * making cloudflare and others compile * gandi and gcloud. no idea if gandi works anymore. * r53 * namedotcom wasn't working.
This commit is contained in:
@@ -3,8 +3,6 @@ package gandi
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/StackExchange/dnscontrol/models"
|
||||
"github.com/StackExchange/dnscontrol/providers"
|
||||
@@ -30,49 +28,10 @@ type GandiApi struct {
|
||||
ZoneId int64
|
||||
}
|
||||
|
||||
type cfRecord struct {
|
||||
type gandiRecord struct {
|
||||
gandirecord.RecordInfo
|
||||
}
|
||||
|
||||
func (c *cfRecord) GetName() string {
|
||||
return c.Name
|
||||
}
|
||||
|
||||
func (c *cfRecord) GetType() string {
|
||||
return c.Type
|
||||
}
|
||||
|
||||
func (c *cfRecord) GetTtl() int64 {
|
||||
return c.Ttl
|
||||
}
|
||||
|
||||
func (c *cfRecord) GetValue() string {
|
||||
return c.Value
|
||||
}
|
||||
|
||||
func (c *cfRecord) GetContent() string {
|
||||
switch c.Type {
|
||||
case "MX":
|
||||
parts := strings.SplitN(c.Value, " ", 2)
|
||||
// TODO(tlim): This should check for more errors.
|
||||
return strings.Join(parts[1:], " ")
|
||||
default:
|
||||
}
|
||||
return c.Value
|
||||
}
|
||||
|
||||
func (c *cfRecord) GetComparisionData() string {
|
||||
if c.Type == "MX" {
|
||||
parts := strings.SplitN(c.Value, " ", 2)
|
||||
priority, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return fmt.Sprintf("%s %#v", c.Ttl, parts[0])
|
||||
}
|
||||
return fmt.Sprintf("%d %d", c.Ttl, priority)
|
||||
}
|
||||
return fmt.Sprintf("%d", c.Ttl)
|
||||
}
|
||||
|
||||
func (c *GandiApi) getDomainInfo(domain string) (*gandidomain.DomainInfo, error) {
|
||||
if err := c.fetchDomainList(); err != nil {
|
||||
return nil, err
|
||||
@@ -95,6 +54,7 @@ func (c *GandiApi) GetNameservers(domain string) ([]*models.Nameserver, error) {
|
||||
}
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (c *GandiApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
|
||||
domaininfo, err := c.getDomainInfo(dc.Name)
|
||||
if err != nil {
|
||||
@@ -104,46 +64,23 @@ func (c *GandiApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Corr
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Convert to []diff.Records and compare:
|
||||
foundDiffRecords := make([]diff.Record, len(foundRecords))
|
||||
for i, rec := range foundRecords {
|
||||
n := &cfRecord{}
|
||||
n.Id = 0
|
||||
n.Name = rec.Name
|
||||
n.Ttl = int64(rec.Ttl)
|
||||
n.Type = rec.Type
|
||||
n.Value = rec.Value
|
||||
foundDiffRecords[i] = n
|
||||
}
|
||||
expectedDiffRecords := make([]diff.Record, len(dc.Records))
|
||||
|
||||
expectedRecordSets := make([]gandirecord.RecordSet, len(dc.Records))
|
||||
for i, rec := range dc.Records {
|
||||
n := &cfRecord{}
|
||||
n.Id = 0
|
||||
n.Name = rec.Name
|
||||
n.Ttl = int64(rec.TTL)
|
||||
if n.Ttl == 0 {
|
||||
n.Ttl = 3600
|
||||
if rec.Type == "MX" {
|
||||
rec.Target = fmt.Sprintf("%d %s", rec.Priority, rec.Target)
|
||||
}
|
||||
n.Type = rec.Type
|
||||
switch n.Type {
|
||||
case "MX":
|
||||
n.Value = fmt.Sprintf("%d %s", rec.Priority, rec.Target)
|
||||
case "TXT":
|
||||
n.Value = "\"" + rec.Target + "\"" // FIXME(tlim): Should do proper quoting.
|
||||
default:
|
||||
n.Value = rec.Target
|
||||
if rec.Type == "TXT" {
|
||||
rec.Target = "\"" + rec.Target + "\"" // FIXME(tlim): Should do proper quoting.
|
||||
}
|
||||
expectedDiffRecords[i] = n
|
||||
expectedRecordSets[i] = gandirecord.RecordSet{}
|
||||
expectedRecordSets[i]["type"] = n.Type
|
||||
expectedRecordSets[i]["name"] = n.Name
|
||||
expectedRecordSets[i]["value"] = n.Value
|
||||
if n.Ttl != 0 {
|
||||
expectedRecordSets[i]["ttl"] = n.Ttl
|
||||
}
|
||||
expectedRecordSets[i]["type"] = rec.Type
|
||||
expectedRecordSets[i]["name"] = rec.Name
|
||||
expectedRecordSets[i]["value"] = rec.Target
|
||||
expectedRecordSets[i]["ttl"] = rec.TTL
|
||||
}
|
||||
_, create, del, mod := diff.IncrementalDiff(foundDiffRecords, expectedDiffRecords)
|
||||
differ := diff.New(dc)
|
||||
_, create, del, mod := differ.IncrementalDiff(foundRecords)
|
||||
|
||||
// Print a list of changes. Generate an actual change that is the zone
|
||||
changes := false
|
||||
@@ -160,7 +97,7 @@ func (c *GandiApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Corr
|
||||
fmt.Println(i)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("GENERATE_ZONE: %s (%d records)", dc.Name, len(expectedDiffRecords))
|
||||
msg := fmt.Sprintf("GENERATE_ZONE: %s (%d records)", dc.Name, len(dc.Records))
|
||||
corrections := []*models.Correction{}
|
||||
if changes {
|
||||
corrections = append(corrections,
|
||||
|
Reference in New Issue
Block a user