DNSimple: Update to 1.0.0 Client & expose error attributes (#1756)

This commit is contained in:
Amelia Aronsohn 2022-09-23 08:55:09 -07:00 committed by GitHub
parent b821f4914f
commit 1c726a08ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 3 deletions

2
go.mod
View file

@ -24,7 +24,7 @@ require (
github.com/cloudflare/cloudflare-go v0.50.0 github.com/cloudflare/cloudflare-go v0.50.0
github.com/digitalocean/godo v1.84.1 github.com/digitalocean/godo v1.84.1
github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c
github.com/dnsimple/dnsimple-go v0.71.1 github.com/dnsimple/dnsimple-go v1.0.0
github.com/exoscale/egoscale v0.90.2 github.com/exoscale/egoscale v0.90.2
github.com/go-acme/lego v2.7.2+incompatible github.com/go-acme/lego v2.7.2+incompatible
github.com/go-gandi/go-gandi v0.5.0 github.com/go-gandi/go-gandi v0.5.0

4
go.sum
View file

@ -185,8 +185,8 @@ github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c h1:+Zo5Ca9
github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c/go.mod h1:HJGU9ULdREjOcVGZVPB5s6zYmHi1RxzT71l2wQyLmnE= github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c/go.mod h1:HJGU9ULdREjOcVGZVPB5s6zYmHi1RxzT71l2wQyLmnE=
github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE= github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE=
github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c= github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c=
github.com/dnsimple/dnsimple-go v0.71.1 h1:1hGoBA3CIjpjZj5DM3081xfxr4e2jYmYnkO2VuBF8Qc= github.com/dnsimple/dnsimple-go v1.0.0 h1:x9UalQ0tHR68+sQxJYJmq746LdJou4OLTK+cZLR2Z9I=
github.com/dnsimple/dnsimple-go v0.71.1/go.mod h1:F9WHww9cC76hrnwGFfAfrqdW99j3MOYasQcIwTS/aUk= github.com/dnsimple/dnsimple-go v1.0.0/go.mod h1:oaAtPP8bIROK3QXUdc8rMlTN7SyvCBAogw2I31WVNnU=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=

View file

@ -3,6 +3,7 @@ package dnsimple
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"sort" "sort"
"strconv" "strconv"
@ -294,6 +295,10 @@ func (c *dnsimpleProvider) getRecords(domainName string) ([]dnsimpleapi.ZoneReco
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
@ -304,6 +309,10 @@ func (c *dnsimpleProvider) getRecords(domainName string) ([]dnsimpleapi.ZoneReco
opts.Page = &page opts.Page = &page
recordsResponse, err := client.Zones.ListRecords(context.Background(), accountID, domainName, opts) recordsResponse, err := client.Zones.ListRecords(context.Background(), accountID, domainName, opts)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
recs = append(recs, recordsResponse.Data...) recs = append(recs, recordsResponse.Data...)
@ -325,11 +334,19 @@ func (c *dnsimpleProvider) getDnssec(domainName string) (bool, error) {
) )
client = c.getClient() client = c.getClient()
if accountID, err = c.getAccountID(); err != nil { if accountID, err = c.getAccountID(); err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return false, compileAttributeErrors(errorResponse)
}
return false, err return false, err
} }
dnssecResponse, err := client.Domains.GetDnssec(context.Background(), accountID, domainName) dnssecResponse, err := client.Domains.GetDnssec(context.Background(), accountID, domainName)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return false, compileAttributeErrors(errorResponse)
}
return false, err return false, err
} }
if dnssecResponse.Data == nil { if dnssecResponse.Data == nil {
@ -346,11 +363,19 @@ func (c *dnsimpleProvider) enableDnssec(domainName string) (bool, error) {
) )
client = c.getClient() client = c.getClient()
if accountID, err = c.getAccountID(); err != nil { if accountID, err = c.getAccountID(); err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return false, compileAttributeErrors(errorResponse)
}
return false, err return false, err
} }
dnssecResponse, err := client.Domains.EnableDnssec(context.Background(), accountID, domainName) dnssecResponse, err := client.Domains.EnableDnssec(context.Background(), accountID, domainName)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return false, compileAttributeErrors(errorResponse)
}
return false, err return false, err
} }
if dnssecResponse.Data == nil { if dnssecResponse.Data == nil {
@ -367,11 +392,19 @@ func (c *dnsimpleProvider) disableDnssec(domainName string) (bool, error) {
) )
client = c.getClient() client = c.getClient()
if accountID, err = c.getAccountID(); err != nil { if accountID, err = c.getAccountID(); err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return false, compileAttributeErrors(errorResponse)
}
return false, err return false, err
} }
dnssecResponse, err := client.Domains.DisableDnssec(context.Background(), accountID, domainName) dnssecResponse, err := client.Domains.DisableDnssec(context.Background(), accountID, domainName)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return false, compileAttributeErrors(errorResponse)
}
return false, err return false, err
} }
if dnssecResponse.Data == nil { if dnssecResponse.Data == nil {
@ -388,11 +421,19 @@ func (c *dnsimpleProvider) getNameservers(domainName string) ([]string, error) {
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
domainResponse, err := client.Domains.GetDomain(context.Background(), accountID, domainName) domainResponse, err := client.Domains.GetDomain(context.Background(), accountID, domainName)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
@ -400,6 +441,10 @@ func (c *dnsimpleProvider) getNameservers(domainName string) ([]string, error) {
delegationResponse, err := client.Registrar.GetDomainDelegation(context.Background(), accountID, domainName) delegationResponse, err := client.Registrar.GetDomainDelegation(context.Background(), accountID, domainName)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
@ -415,6 +460,10 @@ func (c *dnsimpleProvider) updateNameserversFunc(nameServerNames []string, domai
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
@ -422,6 +471,10 @@ func (c *dnsimpleProvider) updateNameserversFunc(nameServerNames []string, domai
_, err = client.Registrar.ChangeDomainDelegation(context.Background(), accountID, domainName, &nameServers) _, err = client.Registrar.ChangeDomainDelegation(context.Background(), accountID, domainName, &nameServers)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
@ -436,6 +489,10 @@ func (c *dnsimpleProvider) createRecordFunc(rc *models.RecordConfig, domainName
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
record := dnsimpleapi.ZoneRecordAttributes{ record := dnsimpleapi.ZoneRecordAttributes{
@ -447,6 +504,10 @@ func (c *dnsimpleProvider) createRecordFunc(rc *models.RecordConfig, domainName
} }
_, err = client.Zones.CreateRecord(context.Background(), accountID, domainName, record) _, err = client.Zones.CreateRecord(context.Background(), accountID, domainName, record)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
@ -461,11 +522,19 @@ func (c *dnsimpleProvider) deleteRecordFunc(recordID int64, domainName string) f
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
_, err = client.Zones.DeleteRecord(context.Background(), accountID, domainName, recordID) _, err = client.Zones.DeleteRecord(context.Background(), accountID, domainName, recordID)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
@ -481,6 +550,10 @@ func (c *dnsimpleProvider) updateRecordFunc(old *dnsimpleapi.ZoneRecord, rc *mod
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
@ -494,6 +567,10 @@ func (c *dnsimpleProvider) updateRecordFunc(old *dnsimpleapi.ZoneRecord, rc *mod
_, err = client.Zones.UpdateRecord(context.Background(), accountID, domainName, old.ID, record) _, err = client.Zones.UpdateRecord(context.Background(), accountID, domainName, old.ID, record)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return compileAttributeErrors(errorResponse)
}
return err return err
} }
@ -506,6 +583,10 @@ func (c *dnsimpleProvider) ListZones() ([]string, error) {
client := c.getClient() client := c.getClient()
accountID, err := c.getAccountID() accountID, err := c.getAccountID()
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
@ -516,6 +597,10 @@ func (c *dnsimpleProvider) ListZones() ([]string, error) {
opts.Page = &page opts.Page = &page
zonesResponse, err := client.Zones.ListZones(context.Background(), accountID, opts) zonesResponse, err := client.Zones.ListZones(context.Background(), accountID, opts)
if err != nil { if err != nil {
var errorResponse *dnsimpleapi.ErrorResponse
if errors.As(err, &errorResponse) {
return nil, compileAttributeErrors(errorResponse)
}
return nil, err return nil, err
} }
for _, zone := range zonesResponse.Data { for _, zone := range zonesResponse.Data {
@ -612,3 +697,12 @@ func getTargetRecordPriority(rc *models.RecordConfig) int {
return 0 return 0
} }
} }
func compileAttributeErrors(err *dnsimpleapi.ErrorResponse) error {
message := fmt.Sprintf("%d %s", err.HTTPResponse.StatusCode, err.Message)
for field, errors := range err.AttributeErrors {
e := strings.Join(errors, "& ")
message += fmt.Sprintf(": %s %s", field, e)
}
return fmt.Errorf(message)
}