@@ -75,6 +75,12 @@ var recordTypeCustomHostnameSupported = map[string]bool{
75
75
"CNAME" : true ,
76
76
}
77
77
78
+ var recordTypeRegionalHostnameSupported = map [string ]bool {
79
+ "A" : true ,
80
+ "AAAA" : true ,
81
+ "CNAME" : true ,
82
+ }
83
+
78
84
// cloudFlareDNS is the subset of the CloudFlare API that we actually use. Add methods as required. Signatures must match exactly.
79
85
type cloudFlareDNS interface {
80
86
UserDetails (ctx context.Context ) (cloudflare.User , error )
@@ -86,7 +92,9 @@ type cloudFlareDNS interface {
86
92
CreateDNSRecord (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.CreateDNSRecordParams ) (cloudflare.DNSRecord , error )
87
93
DeleteDNSRecord (ctx context.Context , rc * cloudflare.ResourceContainer , recordID string ) error
88
94
UpdateDNSRecord (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.UpdateDNSRecordParams ) error
95
+ CreateDataLocalizationRegionalHostname (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.CreateDataLocalizationRegionalHostnameParams ) error
89
96
UpdateDataLocalizationRegionalHostname (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.UpdateDataLocalizationRegionalHostnameParams ) error
97
+ DeleteDataLocalizationRegionalHostname (ctx context.Context , rc * cloudflare.ResourceContainer , hostname string ) error
90
98
CustomHostnames (ctx context.Context , zoneID string , page int , filter cloudflare.CustomHostname ) ([]cloudflare.CustomHostname , cloudflare.ResultInfo , error )
91
99
DeleteCustomHostname (ctx context.Context , zoneID string , customHostnameID string ) error
92
100
CreateCustomHostname (ctx context.Context , zoneID string , ch cloudflare.CustomHostname ) (* cloudflare.CustomHostnameResponse , error )
@@ -121,11 +129,20 @@ func (z zoneService) UpdateDNSRecord(ctx context.Context, rc *cloudflare.Resourc
121
129
return err
122
130
}
123
131
132
+ func (z zoneService ) CreateDataLocalizationRegionalHostname (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.CreateDataLocalizationRegionalHostnameParams ) error {
133
+ _ , err := z .service .CreateDataLocalizationRegionalHostname (ctx , rc , rp )
134
+ return err
135
+ }
136
+
124
137
func (z zoneService ) UpdateDataLocalizationRegionalHostname (ctx context.Context , rc * cloudflare.ResourceContainer , rp cloudflare.UpdateDataLocalizationRegionalHostnameParams ) error {
125
138
_ , err := z .service .UpdateDataLocalizationRegionalHostname (ctx , rc , rp )
126
139
return err
127
140
}
128
141
142
+ func (z zoneService ) DeleteDataLocalizationRegionalHostname (ctx context.Context , rc * cloudflare.ResourceContainer , hostname string ) error {
143
+ return z .service .DeleteDataLocalizationRegionalHostname (ctx , rc , hostname )
144
+ }
145
+
129
146
func (z zoneService ) DeleteDNSRecord (ctx context.Context , rc * cloudflare.ResourceContainer , recordID string ) error {
130
147
return z .service .DeleteDNSRecord (ctx , rc , recordID )
131
148
}
@@ -189,6 +206,14 @@ func updateDNSRecordParam(cfc cloudFlareChange) cloudflare.UpdateDNSRecordParams
189
206
}
190
207
}
191
208
209
+ // createDataLocalizationRegionalHostnameParams is a function that returns the appropriate RegionalHostname Param based on the cloudFlareChange passed in
210
+ func createDataLocalizationRegionalHostnameParams (cfc cloudFlareChange ) cloudflare.CreateDataLocalizationRegionalHostnameParams {
211
+ return cloudflare.CreateDataLocalizationRegionalHostnameParams {
212
+ Hostname : cfc .RegionalHostname .Hostname ,
213
+ RegionKey : cfc .RegionalHostname .RegionKey ,
214
+ }
215
+ }
216
+
192
217
// updateDataLocalizationRegionalHostnameParams is a function that returns the appropriate RegionalHostname Param based on the cloudFlareChange passed in
193
218
func updateDataLocalizationRegionalHostnameParams (cfc cloudFlareChange ) cloudflare.UpdateDataLocalizationRegionalHostnameParams {
194
219
return cloudflare.UpdateDataLocalizationRegionalHostnameParams {
@@ -427,6 +452,15 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
427
452
}
428
453
}
429
454
}
455
+ if change .RegionalHostname .Hostname != "" {
456
+ regionalHostnameParam := updateDataLocalizationRegionalHostnameParams (* change )
457
+ log .WithFields (logFields ).Debugf ("Updating regional hostname %s" , change .RegionalHostname .Hostname )
458
+ regionalHostnameErr := p .Client .UpdateDataLocalizationRegionalHostname (ctx , resourceContainer , regionalHostnameParam )
459
+ if regionalHostnameErr != nil {
460
+ failedChange = true
461
+ log .WithFields (logFields ).Errorf ("failed to update regional hostname: %v" , regionalHostnameErr )
462
+ }
463
+ }
430
464
recordID := p .getRecordID (records , change .ResourceRecord )
431
465
if recordID == "" {
432
466
log .WithFields (logFields ).Errorf ("failed to find previous record: %v" , change .ResourceRecord )
@@ -439,13 +473,6 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
439
473
failedChange = true
440
474
log .WithFields (logFields ).Errorf ("failed to update record: %v" , err )
441
475
}
442
- if regionalHostnameParam := updateDataLocalizationRegionalHostnameParams (* change ); regionalHostnameParam .RegionKey != "" {
443
- regionalHostnameErr := p .Client .UpdateDataLocalizationRegionalHostname (ctx , resourceContainer , regionalHostnameParam )
444
- if regionalHostnameErr != nil {
445
- failedChange = true
446
- log .WithFields (logFields ).Errorf ("failed to update record when editing region: %v" , regionalHostnameErr )
447
- }
448
- }
449
476
} else if change .Action == cloudFlareDelete {
450
477
recordID := p .getRecordID (records , change .ResourceRecord )
451
478
if recordID == "" {
@@ -457,19 +484,26 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
457
484
failedChange = true
458
485
log .WithFields (logFields ).Errorf ("failed to delete record: %v" , err )
459
486
}
460
- if change .CustomHostname .Hostname == "" {
461
- continue
462
- }
463
- log .WithFields (logFields ).Infof ("Deleting custom hostname %v" , change .CustomHostname .Hostname )
464
- chID , _ := p .getCustomHostnameOrigin (chs , change .CustomHostname .Hostname )
465
- if chID == "" {
466
- log .WithFields (logFields ).Infof ("Custom hostname %v not found" , change .CustomHostname .Hostname )
467
- continue
487
+ if change .CustomHostname .Hostname != "" {
488
+ log .WithFields (logFields ).Infof ("Deleting custom hostname %v" , change .CustomHostname .Hostname )
489
+ chID , _ := p .getCustomHostnameOrigin (chs , change .CustomHostname .Hostname )
490
+ if chID != "" {
491
+ chErr := p .Client .DeleteCustomHostname (ctx , zoneID , chID )
492
+ if chErr != nil {
493
+ failedChange = true
494
+ log .WithFields (logFields ).Errorf ("failed to delete custom hostname %v/%v: %v" , chID , change .CustomHostname .Hostname , chErr )
495
+ }
496
+ } else {
497
+ log .WithFields (logFields ).Infof ("Custom hostname %v not found" , change .CustomHostname .Hostname )
498
+ }
468
499
}
469
- chErr := p .Client .DeleteCustomHostname (ctx , zoneID , chID )
470
- if chErr != nil {
471
- failedChange = true
472
- log .WithFields (logFields ).Errorf ("failed to delete custom hostname %v/%v: %v" , chID , change .CustomHostname .Hostname , chErr )
500
+ if change .RegionalHostname .Hostname != "" {
501
+ log .WithFields (logFields ).Debugf ("Deleting regional hostname %s" , change .RegionalHostname .Hostname )
502
+ regionalHostnameErr := p .Client .DeleteDataLocalizationRegionalHostname (ctx , resourceContainer , change .RegionalHostname .Hostname )
503
+ if regionalHostnameErr != nil && ! errors .Is (regionalHostnameErr , cloudflare.NotFoundError {}) {
504
+ failedChange = true
505
+ log .WithFields (logFields ).Errorf ("failed to delete regional hostname: %v" , regionalHostnameErr )
506
+ }
473
507
}
474
508
} else if change .Action == cloudFlareCreate {
475
509
recordParam := getCreateDNSRecordParam (* change )
@@ -478,20 +512,28 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
478
512
failedChange = true
479
513
log .WithFields (logFields ).Errorf ("failed to create record: %v" , err )
480
514
}
481
- if change .CustomHostname .Hostname == "" {
482
- continue
483
- }
484
- log .WithFields (logFields ).Infof ("Creating custom hostname %v" , change .CustomHostname .Hostname )
485
- chID , chOrigin := p .getCustomHostnameOrigin (chs , change .CustomHostname .Hostname )
486
- if chID != "" {
487
- failedChange = true
488
- log .WithFields (logFields ).Errorf ("failed to create custom hostname, %v already exists for origin %v" , change .CustomHostname .Hostname , chOrigin )
489
- continue
515
+ if change .CustomHostname .Hostname != "" {
516
+ log .WithFields (logFields ).Infof ("Creating custom hostname %v" , change .CustomHostname .Hostname )
517
+ chID , chOrigin := p .getCustomHostnameOrigin (chs , change .CustomHostname .Hostname )
518
+ if chID == "" {
519
+ _ , chErr := p .Client .CreateCustomHostname (ctx , zoneID , change .CustomHostname )
520
+ if chErr != nil {
521
+ failedChange = true
522
+ log .WithFields (logFields ).Errorf ("failed to create custom hostname %v: %v" , change .CustomHostname .Hostname , chErr )
523
+ }
524
+ } else {
525
+ failedChange = true
526
+ log .WithFields (logFields ).Errorf ("failed to create custom hostname, %v already exists for origin %v" , change .CustomHostname .Hostname , chOrigin )
527
+ }
490
528
}
491
- _ , chErr := p .Client .CreateCustomHostname (ctx , zoneID , change .CustomHostname )
492
- if chErr != nil {
493
- failedChange = true
494
- log .WithFields (logFields ).Errorf ("failed to create custom hostname %v: %v" , change .CustomHostname .Hostname , chErr )
529
+ if change .RegionalHostname .Hostname != "" {
530
+ log .WithFields (logFields ).Debugf ("Creating regional hostname %s" , change .RegionalHostname .Hostname )
531
+ regionalHostnameParam := createDataLocalizationRegionalHostnameParams (* change )
532
+ regionalHostnameErr := p .Client .CreateDataLocalizationRegionalHostname (ctx , resourceContainer , regionalHostnameParam )
533
+ if regionalHostnameErr != nil {
534
+ failedChange = true
535
+ log .WithFields (logFields ).Errorf ("failed to create regional hostname: %v" , regionalHostnameErr )
536
+ }
495
537
}
496
538
}
497
539
}
@@ -583,6 +625,14 @@ func (p *CloudFlareProvider) newCloudFlareChange(action string, endpoint *endpoi
583
625
SSL : getCustomHostnamesSSLOptions (endpoint , p .CustomHostnamesConfig ),
584
626
}
585
627
}
628
+ regionalHostname := cloudflare.RegionalHostname {}
629
+ if recordTypeRegionalHostnameSupported [endpoint .RecordType ] {
630
+ regionalHostname = cloudflare.RegionalHostname {
631
+ Hostname : endpoint .DNSName ,
632
+ RegionKey : p .RegionKey ,
633
+ CreatedOn : & dt ,
634
+ }
635
+ }
586
636
return & cloudFlareChange {
587
637
Action : action ,
588
638
ResourceRecord : cloudflare.DNSRecord {
@@ -597,11 +647,7 @@ func (p *CloudFlareProvider) newCloudFlareChange(action string, endpoint *endpoi
597
647
"region" : p .RegionKey ,
598
648
},
599
649
},
600
- RegionalHostname : cloudflare.RegionalHostname {
601
- Hostname : endpoint .DNSName ,
602
- RegionKey : p .RegionKey ,
603
- CreatedOn : & dt ,
604
- },
650
+ RegionalHostname : regionalHostname ,
605
651
CustomHostnamePrev : customHostnamePrev ,
606
652
CustomHostname : newCustomHostname ,
607
653
}
0 commit comments