Skip to content

Commit

Permalink
Fix absolute name bug and better handling of rrsets
Browse files Browse the repository at this point in the history
  • Loading branch information
obynio committed Jan 25, 2024
1 parent da32615 commit 5a26022
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
29 changes: 19 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.
return err
}

// might contain request crafting error
var requestErr error

if len(rec.RRSetValues) > 1 {
// if it contains multiple values, the best is to update the record instead of deleting all the values
newRRSetValues := []string{}
Expand All @@ -99,26 +102,26 @@ func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.
return err
}

req, err = http.NewRequestWithContext(ctx, "PUT", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, record.Name, record.Type), bytes.NewReader(raw))
req, requestErr = http.NewRequestWithContext(ctx, "PUT", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, record.Name, record.Type), bytes.NewReader(raw))
} else {
// if there is only one entry, we make sure that the value to delete is matching the one we found
// otherwise we may delete the wrong record
if strings.Trim(rec.RRSetValues[0], "\"") != record.Value {
return fmt.Errorf("LiveDNS returned a %v (%v)", http.StatusNotFound, "Can't find such a DNS value")
}

req, err = http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, record.Name, record.Type), nil)
req, requestErr = http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", domain.DomainRecordsHref, record.Name, record.Type), nil)
}

// we check if NewRequestWithContext threw an error
if err != nil {
return err
if requestErr != nil {
return requestErr
}

req.Header.Set("Content-Type", "application/json")

_, err = p.doRequest(req, nil)
return err
_, requestErr = p.doRequest(req, nil)
return requestErr
}

func (p *Provider) getDomain(ctx context.Context, zone string) (gandiDomain, error) {
Expand All @@ -136,12 +139,18 @@ func (p *Provider) getDomain(ctx context.Context, zone string) (gandiDomain, err
return domain, nil
}

req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.gandi.net/v5/livedns/domains/%s", fqdn), nil)
// might contain request crafting error
var requestErr error

req, requestErr := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.gandi.net/v5/livedns/domains/%s", fqdn), nil)
if requestErr != nil {
return gandiDomain{}, requestErr
}

var domain gandiDomain

if _, err = p.doRequest(req, &domain); err != nil {
return gandiDomain{}, err
if _, requestErr = p.doRequest(req, &domain); requestErr != nil {
return gandiDomain{}, requestErr
}

p.domains[fqdn] = domain
Expand All @@ -150,7 +159,7 @@ func (p *Provider) getDomain(ctx context.Context, zone string) (gandiDomain, err
}

func (p *Provider) doRequest(req *http.Request, result interface{}) (gandiStatus, error) {
req.Header.Set("Authorization", fmt.Sprintf("Apikey %s", p.APIToken))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", p.BearerToken))
req.Header.Set("Accept", "application/json")

resp, err := http.DefaultClient.Do(req)
Expand Down
16 changes: 0 additions & 16 deletions gandi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ type gandiStatus struct {
Errors []gandiErrors `json:"errors"`
}

type gandiZone struct {
Retry int `json:"retry"`
UUID string `json:"uuid"`
ZoneHref string `json:"zone_href"`
Minimum int `json:"minimum"`
DomainsHref string `json:"domains_href"`
Refresh int `json:"refresh"`
ZoneRecordsHref string `json:"zone_records_href"`
Expire int `json:"expire"`
SharingID string `json:"sharing_id"`
Serial int `json:"serial"`
Email string `json:"email"`
PrimaryNS string `json:"primary_ns"`
Name string `json:"name"`
}

type gandiDomain struct {
Fqdn string `json:"fqdn"`
AutomaticSnapshots bool `json:"automatic_snapshots"`
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/libdns/gandi

go 1.14
go 1.20

require github.com/libdns/libdns v0.1.0
require github.com/libdns/libdns v0.2.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/libdns/libdns v0.1.0 h1:0ctCOrVJsVzj53mop1angHp/pE3hmAhP7KiHvR0HD04=
github.com/libdns/libdns v0.1.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
2 changes: 1 addition & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Provider implements the libdns interfaces for Gandi.
type Provider struct {
APIToken string `json:"api_token,omitempty"`
BearerToken string `json:"bearer_token,omitempty"`

domains map[string]gandiDomain
mutex sync.Mutex
Expand Down

0 comments on commit 5a26022

Please sign in to comment.