Skip to content

Commit bee85fc

Browse files
committed
Use %q log fmt for cloudflare provider code
1 parent 9fe3f08 commit bee85fc

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

provider/cloudflare/cloudflare.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func NewCloudFlareProvider(domainFilter endpoint.DomainFilter, zoneIDFilter prov
229229
config, err = cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
230230
}
231231
if err != nil {
232-
return nil, fmt.Errorf("failed to initialize cloudflare provider: %v", err)
232+
return nil, fmt.Errorf("failed to initialize cloudflare provider: %w", err)
233233
}
234234
provider := &CloudFlareProvider{
235235
// Client: config,
@@ -254,10 +254,10 @@ func (p *CloudFlareProvider) Zones(ctx context.Context) ([]cloudflare.Zone, erro
254254
if len(p.zoneIDFilter.ZoneIDs) > 0 && p.zoneIDFilter.ZoneIDs[0] != "" {
255255
log.Debugln("zoneIDFilter configured. only looking up zone IDs defined")
256256
for _, zoneID := range p.zoneIDFilter.ZoneIDs {
257-
log.Debugf("looking up zone %s", zoneID)
257+
log.Debugf("looking up zone %q", zoneID)
258258
detailResponse, err := p.Client.ZoneDetails(ctx, zoneID)
259259
if err != nil {
260-
log.Errorf("zone %s lookup failed, %v", zoneID, err)
260+
log.Errorf("zone %q lookup failed, %v", zoneID, err)
261261
return result, err
262262
}
263263
log.WithFields(log.Fields{
@@ -285,7 +285,7 @@ func (p *CloudFlareProvider) Zones(ctx context.Context) ([]cloudflare.Zone, erro
285285

286286
for _, zone := range zonesResponse.Result {
287287
if !p.domainFilter.Match(zone.Name) {
288-
log.Debugf("zone %s not in domain filter", zone.Name)
288+
log.Debugf("zone %q not in domain filter", zone.Name)
289289
continue
290290
}
291291
result = append(result, zone)
@@ -395,7 +395,7 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
395395
resourceContainer := cloudflare.ZoneIdentifier(zoneID)
396396
records, err := p.listDNSRecordsWithAutoPagination(ctx, zoneID)
397397
if err != nil {
398-
return fmt.Errorf("could not fetch records from zone, %v", err)
398+
return fmt.Errorf("could not fetch records from zone, %w", err)
399399
}
400400
chs, chErr := p.listCustomHostnamesWithPagination(ctx, zoneID)
401401
if chErr != nil {
@@ -408,21 +408,21 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
408408
if prevCh, err := getCustomHostname(chs, prevChName); err == nil {
409409
prevChID := prevCh.ID
410410
if prevChID != "" && prevChName != newChName {
411-
log.WithFields(logFields).Infof("Removing previous custom hostname %v/%v", prevChID, prevChName)
411+
log.WithFields(logFields).Infof("Removing previous custom hostname %q/%q", prevChID, prevChName)
412412
chErr := p.Client.DeleteCustomHostname(ctx, zoneID, prevChID)
413413
if chErr != nil {
414414
failedChange = true
415-
log.WithFields(logFields).Errorf("failed to remove previous custom hostname %v/%v: %v", prevChID, prevChName, chErr)
415+
log.WithFields(logFields).Errorf("failed to remove previous custom hostname %q/%q: %v", prevChID, prevChName, chErr)
416416
}
417417
}
418418
}
419419
if newChName != "" {
420420
if prevChName != newChName {
421-
log.WithFields(logFields).Infof("Adding custom hostname %v", newChName)
421+
log.WithFields(logFields).Infof("Adding custom hostname %q", newChName)
422422
_, chErr := p.Client.CreateCustomHostname(ctx, zoneID, change.CustomHostname)
423423
if chErr != nil {
424424
failedChange = true
425-
log.WithFields(logFields).Errorf("failed to add custom hostname %v: %v", newChName, chErr)
425+
log.WithFields(logFields).Errorf("failed to add custom hostname %q: %v", newChName, chErr)
426426
}
427427
}
428428
}
@@ -458,16 +458,16 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
458458
log.WithFields(logFields).Errorf("failed to delete record: %v", err)
459459
}
460460
if recordTypeCustomHostnameSupported[change.ResourceRecord.Type] && change.CustomHostname.Hostname != "" {
461-
log.WithFields(logFields).Infof("Deleting custom hostname %v", change.CustomHostname.Hostname)
461+
log.WithFields(logFields).Infof("Deleting custom hostname %q", change.CustomHostname.Hostname)
462462
if ch, err := getCustomHostname(chs, change.CustomHostname.Hostname); err == nil {
463463
chID := ch.ID
464464
chErr := p.Client.DeleteCustomHostname(ctx, zoneID, chID)
465465
if chErr != nil {
466466
failedChange = true
467-
log.WithFields(logFields).Errorf("failed to delete custom hostname %v/%v: %v", chID, change.CustomHostname.Hostname, chErr)
467+
log.WithFields(logFields).Errorf("failed to delete custom hostname %q/%q: %v", chID, change.CustomHostname.Hostname, chErr)
468468
}
469469
} else {
470-
log.WithFields(logFields).Warnf("failed to delete custom hostname %v: %v", change.CustomHostname.Hostname, err)
470+
log.WithFields(logFields).Warnf("failed to delete custom hostname %q: %v", change.CustomHostname.Hostname, err)
471471
}
472472
}
473473
} else if change.Action == cloudFlareCreate {
@@ -478,19 +478,19 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
478478
log.WithFields(logFields).Errorf("failed to create record: %v", err)
479479
}
480480
if recordTypeCustomHostnameSupported[change.ResourceRecord.Type] && change.CustomHostname.Hostname != "" {
481-
log.WithFields(logFields).Infof("Creating custom hostname %v", change.CustomHostname.Hostname)
481+
log.WithFields(logFields).Infof("Creating custom hostname %q", change.CustomHostname.Hostname)
482482
if ch, err := getCustomHostname(chs, change.CustomHostname.Hostname); err == nil {
483483
if change.CustomHostname.CustomOriginServer == ch.CustomOriginServer {
484-
log.WithFields(logFields).Warnf("custom hostname %v already exists with the same origin %v, continue", change.CustomHostname.Hostname, ch.CustomOriginServer)
484+
log.WithFields(logFields).Warnf("custom hostname %q already exists with the same origin %q, continue", change.CustomHostname.Hostname, ch.CustomOriginServer)
485485
} else {
486486
failedChange = true
487-
log.WithFields(logFields).Errorf("failed to create custom hostname, %v already exists with origin %v", change.CustomHostname.Hostname, ch.CustomOriginServer)
487+
log.WithFields(logFields).Errorf("failed to create custom hostname, %q already exists with origin %q", change.CustomHostname.Hostname, ch.CustomOriginServer)
488488
}
489489
} else {
490490
_, chErr := p.Client.CreateCustomHostname(ctx, zoneID, change.CustomHostname)
491491
if chErr != nil {
492492
failedChange = true
493-
log.WithFields(logFields).Errorf("failed to create custom hostname %v: %v", change.CustomHostname.Hostname, chErr)
493+
log.WithFields(logFields).Errorf("failed to create custom hostname %q: %v", change.CustomHostname.Hostname, chErr)
494494
}
495495
}
496496
}
@@ -502,7 +502,7 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
502502
}
503503

504504
if len(failedZones) > 0 {
505-
return fmt.Errorf("failed to submit all changes for the following zones: %v", failedZones)
505+
return fmt.Errorf("failed to submit all changes for the following zones: %q", failedZones)
506506
}
507507

508508
return nil
@@ -536,7 +536,7 @@ func (p *CloudFlareProvider) changesByZone(zones []cloudflare.Zone, changeSet []
536536
for _, c := range changeSet {
537537
zoneID, _ := zoneNameIDMapper.FindZone(c.ResourceRecord.Name)
538538
if zoneID == "" {
539-
log.Debugf("Skipping record %s because no hosted zone matching record DNS Name was detected", c.ResourceRecord.Name)
539+
log.Debugf("Skipping record %q because no hosted zone matching record DNS Name was detected", c.ResourceRecord.Name)
540540
continue
541541
}
542542
changes[zoneID] = append(changes[zoneID], c)
@@ -655,7 +655,7 @@ func (p *CloudFlareProvider) listCustomHostnamesWithPagination(ctx context.Conte
655655
return nil, provider.NewSoftError(err)
656656
}
657657
}
658-
log.Errorf("zone %s failed to fetch custom hostnames. Please check if \"Cloudflare for SaaS\" is enabled and API key permissions, %v", zoneID, err)
658+
log.Errorf("zone %q failed to fetch custom hostnames. Please check if \"Cloudflare for SaaS\" is enabled and API key permissions, %v", zoneID, err)
659659
return nil, err
660660
}
661661

@@ -687,7 +687,7 @@ func shouldBeProxied(endpoint *endpoint.Endpoint, proxiedByDefault bool) bool {
687687
if v.Name == source.CloudflareProxiedKey {
688688
b, err := strconv.ParseBool(v.Value)
689689
if err != nil {
690-
log.Errorf("Failed to parse annotation [%s]: %v", source.CloudflareProxiedKey, err)
690+
log.Errorf("Failed to parse annotation [%q]: %v", source.CloudflareProxiedKey, err)
691691
} else {
692692
proxied = b
693693
}

provider/cloudflare/cloudflare_test.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -1866,9 +1866,9 @@ func TestCloudflareDNSRecordsOperationsFail(t *testing.T) {
18661866

18671867
err = provider.ApplyChanges(context.Background(), planned.Changes)
18681868
if err == nil && tc.shouldFail {
1869-
t.Errorf("should fail - %s, %s", tc.Name, err)
1869+
t.Errorf("should fail - %q, %v", tc.Name, err)
18701870
} else if err != nil && !tc.shouldFail {
1871-
t.Errorf("should not fail - %s, %s", tc.Name, err)
1871+
t.Errorf("should not fail - %q, %v", tc.Name, err)
18721872
}
18731873
}
18741874
}
@@ -2216,7 +2216,7 @@ func TestCloudflareCustomHostnameOperations(t *testing.T) {
22162216
for _, tc := range testFailCases {
22172217
records, err := provider.Records(ctx)
22182218
if err != nil {
2219-
t.Errorf("should not fail, %s", err)
2219+
t.Errorf("should not fail, %v", err)
22202220
}
22212221

22222222
endpoints, err := provider.AdjustEndpoints(tc.Endpoints)
@@ -2233,16 +2233,16 @@ func TestCloudflareCustomHostnameOperations(t *testing.T) {
22332233

22342234
err = provider.ApplyChanges(context.Background(), planned.Changes)
22352235
if err == nil && tc.shouldFail {
2236-
t.Errorf("should fail - %s, %s", tc.Name, err)
2236+
t.Errorf("should fail - %q, %v", tc.Name, err)
22372237
} else if err != nil && !tc.shouldFail {
2238-
t.Errorf("should not fail - %s, %s", tc.Name, err)
2238+
t.Errorf("should not fail - %q, %v", tc.Name, err)
22392239
}
22402240
}
22412241

22422242
for _, tc := range testCases {
22432243
records, err := provider.Records(ctx)
22442244
if err != nil {
2245-
t.Errorf("should not fail, %s", err)
2245+
t.Errorf("should not fail, %v", err)
22462246
}
22472247

22482248
endpoints, err := provider.AdjustEndpoints(tc.Endpoints)
@@ -2259,12 +2259,12 @@ func TestCloudflareCustomHostnameOperations(t *testing.T) {
22592259

22602260
err = provider.ApplyChanges(context.Background(), planned.Changes)
22612261
if err != nil {
2262-
t.Errorf("should not fail - %s, %s", tc.Name, err)
2262+
t.Errorf("should not fail - %q, %v", tc.Name, err)
22632263
}
22642264

22652265
chs, chErr := provider.listCustomHostnamesWithPagination(ctx, "001")
22662266
if chErr != nil {
2267-
t.Errorf("should not fail - %s, %s", tc.Name, chErr)
2267+
t.Errorf("should not fail - %q, %v", tc.Name, chErr)
22682268
}
22692269

22702270
for expectedOrigin, expectedCustomHostname := range tc.ExpectedCustomHostnames {
@@ -2315,7 +2315,7 @@ func TestCloudflareCustomHostnameNotFoundOnRecordDeletion(t *testing.T) {
23152315
Name: "remove DNS record with unexpectedly missing custom hostname",
23162316
Endpoints: []*endpoint.Endpoint{},
23172317
preApplyHook: "corrupt",
2318-
logOutput: "level=warning msg=\"failed to delete custom hostname newerror-getCustomHostnameOrigin.foo.fancybar.com: not found\" action=DELETE record=create.foo.bar.com",
2318+
logOutput: "level=warning msg=\"failed to delete custom hostname \\\"newerror-getCustomHostnameOrigin.foo.fancybar.com\\\": not found\" action=DELETE record=create.foo.bar.com",
23192319
},
23202320
{
23212321
Name: "duplicate custom hostname",
@@ -2341,7 +2341,7 @@ func TestCloudflareCustomHostnameNotFoundOnRecordDeletion(t *testing.T) {
23412341
},
23422342
},
23432343
preApplyHook: "",
2344-
logOutput: "custom hostname a.foo.fancybar.com already exists with the same origin a.foo.bar.com, continue",
2344+
logOutput: "custom hostname \\\"a.foo.fancybar.com\\\" already exists with the same origin \\\"a.foo.bar.com\\\", continue",
23452345
},
23462346
}
23472347

@@ -2350,7 +2350,7 @@ func TestCloudflareCustomHostnameNotFoundOnRecordDeletion(t *testing.T) {
23502350

23512351
records, err := provider.Records(ctx)
23522352
if err != nil {
2353-
t.Errorf("should not fail, %s", err)
2353+
t.Errorf("should not fail, %v", err)
23542354
}
23552355

23562356
endpoints, err := provider.AdjustEndpoints(tc.Endpoints)
@@ -2369,12 +2369,12 @@ func TestCloudflareCustomHostnameNotFoundOnRecordDeletion(t *testing.T) {
23692369
// the purpose is to cause getCustomHostnameOrigin() to fail on change.Action == cloudFlareDelete
23702370
chs, chErr := provider.listCustomHostnamesWithPagination(ctx, zoneID)
23712371
if chErr != nil {
2372-
t.Errorf("should not fail - %s, %s", tc.Name, chErr)
2372+
t.Errorf("should not fail - %q, %v", tc.Name, chErr)
23732373
}
23742374
if tc.preApplyHook == "corrupt" {
23752375
if ch, err := getCustomHostname(chs, "newerror-getCustomHostnameOrigin.foo.fancybar.com"); err == nil {
23762376
chID := ch.ID
2377-
t.Logf("corrupting custom hostname %v", chID)
2377+
t.Logf("corrupting custom hostname %q", chID)
23782378
oldIdx := getCustomHostnameIdxByID(client.customHostnames[zoneID], chID)
23792379
oldCh := client.customHostnames[zoneID][oldIdx]
23802380
ch := cloudflare.CustomHostname{
@@ -2395,7 +2395,7 @@ func TestCloudflareCustomHostnameNotFoundOnRecordDeletion(t *testing.T) {
23952395
}
23962396
err = provider.ApplyChanges(context.Background(), planned.Changes)
23972397
if err != nil {
2398-
t.Errorf("should not fail - %s, %s", tc.Name, err)
2398+
t.Errorf("should not fail - %q, %v", tc.Name, err)
23992399
}
24002400
assert.Contains(t, b.String(), tc.logOutput)
24012401
}
@@ -2433,7 +2433,7 @@ func TestCloudflareListCustomHostnamesWithPagionation(t *testing.T) {
24332433

24342434
records, err := provider.Records(ctx)
24352435
if err != nil {
2436-
t.Errorf("should not fail, %s", err)
2436+
t.Errorf("should not fail, %v", err)
24372437
}
24382438

24392439
endpoints, err := provider.AdjustEndpoints(generatedEndpoints)
@@ -2450,12 +2450,12 @@ func TestCloudflareListCustomHostnamesWithPagionation(t *testing.T) {
24502450

24512451
err = provider.ApplyChanges(context.Background(), planned.Changes)
24522452
if err != nil {
2453-
t.Errorf("should not fail - %s", err)
2453+
t.Errorf("should not fail - %v", err)
24542454
}
24552455

24562456
chs, chErr := provider.listCustomHostnamesWithPagination(ctx, "001")
24572457
if chErr != nil {
2458-
t.Errorf("should not fail - %s", chErr)
2458+
t.Errorf("should not fail - %v", chErr)
24592459
}
24602460
assert.Equal(t, len(chs), CustomHostnamesNumber)
24612461
}

0 commit comments

Comments
 (0)