Skip to content

Commit

Permalink
Merge pull request #2 from sspans/patch-1
Browse files Browse the repository at this point in the history
fix domain lookups by removing the trailing dots
  • Loading branch information
omarjatoi authored May 18, 2024
2 parents 506eefc + 857f8a5 commit 37c4712
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -54,7 +55,7 @@ func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record

var records []libdns.Record

resp, err := p.client.Zones.ListRecords(ctx, p.AccountID, zone, &dnsimple.ZoneRecordListOptions{})
resp, err := p.client.Zones.ListRecords(ctx, p.AccountID, unFQDN(zone), &dnsimple.ZoneRecordListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -82,7 +83,7 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
var appendedRecords []libdns.Record

// Get the Zone ID from zone name
resp, err := p.client.Zones.GetZone(ctx, p.AccountID, zone)
resp, err := p.client.Zones.GetZone(ctx, p.AccountID, unFQDN(zone))
if err != nil {
return nil, err
}
Expand All @@ -97,7 +98,7 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
TTL: int(r.TTL),
Priority: int(r.Priority),
}
resp, err := p.client.Zones.CreateRecord(ctx, p.AccountID, zone, attrs)
resp, err := p.client.Zones.CreateRecord(ctx, p.AccountID, unFQDN(zone), attrs)
if err != nil {
return nil, err
}
Expand All @@ -121,7 +122,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns

var setRecords []libdns.Record

existingRecords, err := p.GetRecords(ctx, zone)
existingRecords, err := p.GetRecords(ctx, unFQDN(zone))
if err != nil {
return nil, err
}
Expand All @@ -145,7 +146,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
}

// Create new records and append them to 'setRecords'
createdRecords, err := p.AppendRecords(ctx, zone, records)
createdRecords, err := p.AppendRecords(ctx, unFQDN(zone), records)
if err != nil {
return nil, err
}
Expand All @@ -154,7 +155,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
}

// Get the Zone ID from zone name
resp, err := p.client.Zones.GetZone(ctx, p.AccountID, zone)
resp, err := p.client.Zones.GetZone(ctx, p.AccountID, unFQDN(zone))
if err != nil {
return nil, err
}
Expand All @@ -174,7 +175,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
if err != nil {
return nil, err
}
resp, err := p.client.Zones.UpdateRecord(ctx, p.AccountID, zone, id, attrs)
resp, err := p.client.Zones.UpdateRecord(ctx, p.AccountID, unFQDN(zone), id, attrs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,7 +212,7 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
return deleted, err
}

resp, err := p.client.Zones.DeleteRecord(ctx, p.AccountID, zone, id)
resp, err := p.client.Zones.DeleteRecord(ctx, p.AccountID, unFQDN(zone), id)
if err != nil {
return deleted, err
}
Expand All @@ -231,7 +232,7 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
// If we received records without an ID earlier, we're going to try and figure out the ID by calling
// GetRecords and comparing the record name. If we're able to find it, we'll delete it, otherwise
// we'll append it to our list of failed to delete records.
fetchedRecords, err := p.GetRecords(ctx, zone)
fetchedRecords, err := p.GetRecords(ctx, unFQDN(zone))
if err != nil {
return nil, fmt.Errorf("failed to fetch records: %s", err.Error())
}
Expand All @@ -244,7 +245,7 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
if err != nil {
return nil, err
}
resp, err := p.client.Zones.DeleteRecord(ctx, p.AccountID, zone, id)
resp, err := p.client.Zones.DeleteRecord(ctx, p.AccountID, unFQDN(zone), id)
if err != nil {
return nil, err
}
Expand All @@ -260,6 +261,11 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
return deleted, nil
}

// unFQDN trims any trailing "." from fqdn. dnsimple's API does not use FQDNs.
func unFQDN(fqdn string) string {
return strings.TrimSuffix(fqdn, ".")
}

// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
Expand Down

0 comments on commit 37c4712

Please sign in to comment.