Fetch remaining zones when response is truncated.

Route53 API won’t return more than 100 zones per request.
This commit is contained in:
Jan Broer 2016-01-22 18:50:18 +01:00
parent 50be32a69e
commit 323bb88640

View file

@ -2,8 +2,8 @@ package acme
import ( import (
"fmt" "fmt"
"math"
"strings" "strings"
"time"
"github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/route53" "github.com/mitchellh/goamz/route53"
@ -64,13 +64,28 @@ func (r *DNSProviderRoute53) changeRecord(action, fqdn, value string, ttl int) e
} }
func (r *DNSProviderRoute53) getHostedZoneID(fqdn string) (string, error) { func (r *DNSProviderRoute53) getHostedZoneID(fqdn string) (string, error) {
zoneResp, err := r.client.ListHostedZones("", math.MaxInt32) zones := []route53.HostedZone{}
zoneResp, err := r.client.ListHostedZones("", 0)
if err != nil { if err != nil {
return "", err return "", err
} }
zones = append(zones, zoneResp.HostedZones...)
for zoneResp.IsTruncated {
resp, err := r.client.ListHostedZones(zoneResp.Marker, 0)
if err != nil {
if rateExceeded(err) {
time.Sleep(time.Second)
continue
}
return "", err
}
zoneResp = resp
zones = append(zones, zoneResp.HostedZones...)
}
var hostedZone route53.HostedZone var hostedZone route53.HostedZone
for _, zone := range zoneResp.HostedZones { for _, zone := range zones {
//if strings.HasSuffix(domain, strings.Trim(zone.Name, ".")) {
if strings.HasSuffix(fqdn, zone.Name) { if strings.HasSuffix(fqdn, zone.Name) {
if len(zone.Name) > len(hostedZone.Name) { if len(zone.Name) > len(hostedZone.Name) {
hostedZone = zone hostedZone = zone
@ -78,7 +93,7 @@ func (r *DNSProviderRoute53) getHostedZoneID(fqdn string) (string, error) {
} }
} }
if hostedZone.ID == "" { if hostedZone.ID == "" {
return "", fmt.Errorf("No Route53 zone found for domain %s", fqdn) return "", fmt.Errorf("No Route53 hosted zone found for domain %s", fqdn)
} }
return hostedZone.ID, nil return hostedZone.ID, nil
@ -92,3 +107,12 @@ func newTXTRecordSet(fqdn, value string, ttl int) route53.ResourceRecordSet {
TTL: ttl, TTL: ttl,
} }
} }
// Route53 API has pretty strict rate limits (5req/s globally per account)
// Hence we check if we are being throttled to maybe retry the request
func rateExceeded (err error) bool {
if strings.Contains(err.Error(), "Throttling") {
return true
}
return false
}