diff --git a/client.go b/client.go index 6e63c01..10a06fd 100644 --- a/client.go +++ b/client.go @@ -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{} @@ -99,7 +102,7 @@ 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 @@ -107,18 +110,18 @@ func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns. 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) { @@ -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 @@ -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) diff --git a/gandi.go b/gandi.go index cb572d0..bdcb167 100644 --- a/gandi.go +++ b/gandi.go @@ -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"` diff --git a/go.mod b/go.mod index ae29abb..3f9850d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ea4b1a2..ba9d0cf 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/provider.go b/provider.go index ee09e75..e01bf98 100644 --- a/provider.go +++ b/provider.go @@ -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