dnsmadeeasy: log response body on error (#682)

This commit is contained in:
Andrew Savinykh 2018-10-19 22:49:17 +13:00 committed by Ludovic Fernandez
parent 1d1b08ac15
commit 5511373184
2 changed files with 22 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"time" "time"
) )
@ -27,6 +28,10 @@ type Record struct {
SourceID int `json:"sourceId"` SourceID int `json:"sourceId"`
} }
type recordsResponse struct {
Records *[]Record `json:"data"`
}
// Client DNSMadeEasy client // Client DNSMadeEasy client
type Client struct { type Client struct {
apiKey string apiKey string
@ -82,10 +87,6 @@ func (c *Client) GetRecords(domain *Domain, recordName, recordType string) (*[]R
} }
defer resp.Body.Close() defer resp.Body.Close()
type recordsResponse struct {
Records *[]Record `json:"data"`
}
records := &recordsResponse{} records := &recordsResponse{}
err = json.NewDecoder(resp.Body).Decode(&records) err = json.NewDecoder(resp.Body).Decode(&records)
if err != nil { if err != nil {
@ -151,7 +152,11 @@ func (c *Client) sendRequest(method, resource string, payload interface{}) (*htt
} }
if resp.StatusCode > 299 { if resp.StatusCode > 299 {
return nil, fmt.Errorf("DNSMadeEasy API request failed with HTTP status code %d", resp.StatusCode) body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("request failed with HTTP status code %d", resp.StatusCode)
}
return nil, fmt.Errorf("request failed with HTTP status code %d: %s", resp.StatusCode, string(body))
} }
return resp, nil return resp, nil

View file

@ -113,13 +113,13 @@ func (d *DNSProvider) Present(domainName, token, keyAuth string) error {
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
if err != nil { if err != nil {
return err return fmt.Errorf("dnsmadeeasy: unable to find zone for %s: %v", fqdn, err)
} }
// fetch the domain details // fetch the domain details
domain, err := d.client.GetDomain(authZone) domain, err := d.client.GetDomain(authZone)
if err != nil { if err != nil {
return err return fmt.Errorf("dnsmadeeasy: unable to get domain for zone %s: %v", authZone, err)
} }
// create the TXT record // create the TXT record
@ -127,7 +127,10 @@ func (d *DNSProvider) Present(domainName, token, keyAuth string) error {
record := &Record{Type: "TXT", Name: name, Value: value, TTL: d.config.TTL} record := &Record{Type: "TXT", Name: name, Value: value, TTL: d.config.TTL}
err = d.client.CreateRecord(domain, record) err = d.client.CreateRecord(domain, record)
return err if err != nil {
return fmt.Errorf("dnsmadeeasy: unable to create record for %s: %v", name, err)
}
return nil
} }
// CleanUp removes the TXT records matching the specified parameters // CleanUp removes the TXT records matching the specified parameters
@ -136,31 +139,32 @@ func (d *DNSProvider) CleanUp(domainName, token, keyAuth string) error {
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers) authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
if err != nil { if err != nil {
return err return fmt.Errorf("dnsmadeeasy: unable to find zone for %s: %v", fqdn, err)
} }
// fetch the domain details // fetch the domain details
domain, err := d.client.GetDomain(authZone) domain, err := d.client.GetDomain(authZone)
if err != nil { if err != nil {
return err return fmt.Errorf("dnsmadeeasy: unable to get domain for zone %s: %v", authZone, err)
} }
// find matching records // find matching records
name := strings.Replace(fqdn, "."+authZone, "", 1) name := strings.Replace(fqdn, "."+authZone, "", 1)
records, err := d.client.GetRecords(domain, name, "TXT") records, err := d.client.GetRecords(domain, name, "TXT")
if err != nil { if err != nil {
return err return fmt.Errorf("dnsmadeeasy: unable to get records for domain %s: %v", domain.Name, err)
} }
// delete records // delete records
var lastError error
for _, record := range *records { for _, record := range *records {
err = d.client.DeleteRecord(record) err = d.client.DeleteRecord(record)
if err != nil { if err != nil {
return err lastError = fmt.Errorf("dnsmadeeasy: unable to delete record [id=%d, name=%s]: %v", record.ID, record.Name, err)
} }
} }
return nil return lastError
} }
// Timeout returns the timeout and interval to use when checking for DNS propagation. // Timeout returns the timeout and interval to use when checking for DNS propagation.