Skip to content

Commit

Permalink
fix: soft error on cloudflare rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
ebachle committed May 6, 2024
1 parent fa17f9d commit 4ee03cd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
15 changes: 15 additions & 0 deletions provider/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cloudflare

import (
"context"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -223,6 +224,13 @@ func (p *CloudFlareProvider) Zones(ctx context.Context) ([]cloudflare.Zone, erro

zonesResponse, err := p.Client.ListZonesContext(ctx)
if err != nil {
var apiErr *cloudflare.Error
if errors.As(err, &apiErr) {
if apiErr.ClientRateLimited() {
// Handle rate limit error as a soft error
return nil, provider.NewSoftError(err)
}
}
return nil, err
}

Expand Down Expand Up @@ -456,6 +464,13 @@ func (p *CloudFlareProvider) listDNSRecordsWithAutoPagination(ctx context.Contex
for {
pageRecords, resultInfo, err := p.Client.ListDNSRecords(ctx, cloudflare.ZoneIdentifier(zoneID), params)
if err != nil {
var apiErr *cloudflare.Error
if errors.As(err, &apiErr) {
if apiErr.ClientRateLimited() {
// Handle rate limit error as a soft error
return nil, provider.NewSoftError(err)
}
}
return nil, err
}

Expand Down
42 changes: 34 additions & 8 deletions provider/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ type MockAction struct {
}

type mockCloudFlareClient struct {
User cloudflare.User
Zones map[string]string
Records map[string]map[string]cloudflare.DNSRecord
Actions []MockAction
listZonesError error
dnsRecordsError error
User cloudflare.User
Zones map[string]string
Records map[string]map[string]cloudflare.DNSRecord
Actions []MockAction
listZonesError error
listZonesContextError error
dnsRecordsError error
}

var ExampleDomain = []cloudflare.DNSRecord{
Expand Down Expand Up @@ -253,8 +254,8 @@ func (m *mockCloudFlareClient) ListZones(ctx context.Context, zoneID ...string)
}

func (m *mockCloudFlareClient) ListZonesContext(ctx context.Context, opts ...cloudflare.ReqOption) (cloudflare.ZonesResponse, error) {
if m.listZonesError != nil {
return cloudflare.ZonesResponse{}, m.listZonesError
if m.listZonesContextError != nil {
return cloudflare.ZonesResponse{}, m.listZonesContextError
}

result := []cloudflare.Zone{}
Expand Down Expand Up @@ -643,6 +644,31 @@ func TestCloudFlareZonesWithIDFilter(t *testing.T) {
assert.Equal(t, "bar.com", zones[0].Name)
}

func TestCloudflareListZonesRateLimited(t *testing.T) {
// Create a mock client that returns a rate limit error
client := NewMockCloudFlareClient()
baseErr := &cloudflare.Error{
StatusCode: 429,
ErrorCodes: []int{10000},
Type: cloudflare.ErrorTypeRateLimit,
}

client.listZonesContextError = baseErr

// Create a CloudFlareProvider with the mock client
p := &CloudFlareProvider{
Client: client,
}

// Call the Zones function
_, err := p.Zones(context.Background())

// Assert that a soft error was returned
if !errors.Is(err, provider.SoftError) {
t.Error("expected a rate limit error")
}
}

func TestCloudflareRecords(t *testing.T) {
client := NewMockCloudFlareClientWithRecords(map[string][]cloudflare.DNSRecord{
"001": ExampleDomain,
Expand Down

0 comments on commit 4ee03cd

Please sign in to comment.