Skip to content

Commit

Permalink
Merge pull request #574 from jbowes/dnsimple-tolerate-other-zones
Browse files Browse the repository at this point in the history
Make DNSimple tolerant of unknown zones
  • Loading branch information
njuettner authored May 30, 2018
2 parents baa1da6 + 5216b5b commit 7e3363f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 deletions provider/dnsimple.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ func (p *dnsimpleProvider) submitChanges(changes []*dnsimpleChange) error {
}
for _, change := range changes {
zone := dnsimpleSuitableZone(change.ResourceRecordSet.Name, zones)
if zone.ID == 0 {
return fmt.Errorf("No suitable zone name found")
if zone == nil {
log.Debugf("Skipping record %s because no hosted zone matching record DNS Name was detected ", change.ResourceRecordSet.Name)
continue
}

log.Infof("Changing records: %s %v in zone: %s", change.Action, change.ResourceRecordSet, zone.Name)

change.ResourceRecordSet.Name = strings.TrimSuffix(change.ResourceRecordSet.Name, "."+zone.Name)
Expand Down Expand Up @@ -290,12 +292,13 @@ func (p *dnsimpleProvider) GetRecordID(zone string, recordName string) (recordID
}

// dnsimpleSuitableZone returns the most suitable zone for a given hostname and a set of zones.
func dnsimpleSuitableZone(hostname string, zones map[string]dnsimple.Zone) dnsimple.Zone {
var zone dnsimple.Zone
func dnsimpleSuitableZone(hostname string, zones map[string]dnsimple.Zone) *dnsimple.Zone {
var zone *dnsimple.Zone
for _, z := range zones {
if strings.HasSuffix(hostname, z.Name) {
if zone.ID == 0 || len(z.Name) > len(zone.Name) {
zone = z
if zone == nil || len(z.Name) > len(zone.Name) {
newZ := z
zone = &newZ
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions provider/dnsimple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestDnsimpleServices(t *testing.T) {
t.Run("Zones", testDnsimpleProviderZones)
t.Run("Records", testDnsimpleProviderRecords)
t.Run("ApplyChanges", testDnsimpleProviderApplyChanges)
t.Run("ApplyChanges/SkipUnknownZone", testDnsimpleProviderApplyChangesSkipsUnknown)
t.Run("SuitableZone", testDnsimpleSuitableZone)
t.Run("GetRecordID", testDnsimpleGetRecordID)
}
Expand Down Expand Up @@ -165,6 +166,19 @@ func testDnsimpleProviderApplyChanges(t *testing.T) {
}
}

func testDnsimpleProviderApplyChangesSkipsUnknown(t *testing.T) {
changes := &plan.Changes{}
changes.Create = []*endpoint.Endpoint{
{DNSName: "example.not-included.com", Targets: endpoint.Targets{"dasd"}, RecordType: endpoint.RecordTypeCNAME},
}

mockProvider.accountID = "1"
err := mockProvider.ApplyChanges(changes)
if err != nil {
t.Errorf("Failed to ignore unknown zones: %v", err)
}
}

func testDnsimpleSuitableZone(t *testing.T) {
mockProvider.accountID = "1"
zones, err := mockProvider.Zones()
Expand Down

0 comments on commit 7e3363f

Please sign in to comment.