Skip to content

Commit

Permalink
refactor(RecordConfig): Use ChangeType() instead of assignment (#3441)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlimoncelli authored Feb 18, 2025
1 parent e0e32ca commit 3f8f9e7
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 9 deletions.
14 changes: 14 additions & 0 deletions models/recordtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package models

// ChangeType converts rc to an rc of type newType. This is only needed when
// converting from one type to another. Do not use this when initializing a new
// record.
//
// Typically this is used to convert an ALIAS to a CNAME, or SPF to TXT. Using
// this function future-proofs the code since eventually such changes will
// require extra steps.
func (rc *RecordConfig) ChangeType(newType string, _ string) {

rc.Type = newType

}
2 changes: 1 addition & 1 deletion providers/bunnydns/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (b *bunnydnsProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, ex
}

if rc.Type == "ALIAS" {
rc.Type = "CNAME"
rc.ChangeType("CNAME", dc.Name)
}
}

Expand Down
9 changes: 7 additions & 2 deletions providers/cloudflare/cloudflareProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (c *cloudflareProvider) getDomainID(name string) (string, error) {
func (c *cloudflareProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, records models.Records) ([]*models.Correction, int, error) {
for _, rec := range dc.Records {
if rec.Type == "ALIAS" {
rec.Type = "CNAME"
rec.ChangeType("CNAME", dc.Name)
}
}

Expand Down Expand Up @@ -446,7 +446,7 @@ func (c *cloudflareProvider) preprocessConfig(dc *models.DomainConfig) error {

for _, rec := range dc.Records {
if rec.Type == "ALIAS" {
rec.Type = "CNAME"
rec.ChangeType("CNAME", dc.Name)
}
}

Expand Down Expand Up @@ -819,6 +819,11 @@ func stringDefault(value interface{}, def string) string {

func (c *cloudflareProvider) nativeToRecord(domain string, cr cloudflare.DNSRecord) (*models.RecordConfig, error) {

// ALIAS in Cloudflare works like CNAME.
if cr.Type == "ALIAS" {
cr.Type = "CNAME"
}

// workaround for https://github.com/StackExchange/dnscontrol/issues/446
if cr.Type == "SPF" {
cr.Type = "TXT"
Expand Down
2 changes: 1 addition & 1 deletion providers/dnsmadeeasy/dnsMadeEasyProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (api *dnsMadeEasyProvider) GetZoneRecordsCorrections(dc *models.DomainConfi
for _, rec := range dc.Records {
if rec.Type == "ALIAS" {
// ALIAS is called ANAME on DNS Made Easy
rec.Type = "ANAME"
rec.ChangeType("ANAME", dc.Name)
} else if rec.Type == "NS" {
// NS records have fixed TTL on DNS Made Easy and it cannot be changed
rec.TTL = fixedNameServerRecordTTL
Expand Down
2 changes: 1 addition & 1 deletion providers/gandiv5/gandi_v5Provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func PrepDesiredRecords(dc *models.DomainConfig) {
if rec.Type == "ALIAS" && rec.Name != "@" {
// GANDI only permits aliases on a naked domain.
// Therefore, we change this to a CNAME.
rec.Type = "CNAME"
rec.ChangeType("CNAME", dc.Name)
}
if rec.TTL < 300 {
printer.Warnf("Gandi does not support ttls < 300. Setting %s from %d to 300\n", rec.GetLabelFQDN(), rec.TTL)
Expand Down
2 changes: 1 addition & 1 deletion providers/gcore/gcoreProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *gcoreProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, exist
// Gcore auto uses ALIAS for apex zone CNAME records, just like CloudFlare
for _, rec := range dc.Records {
if rec.Type == "ALIAS" {
rec.Type = "CNAME"
rec.ChangeType("CNAME", dc.Name)
}
}

Expand Down
2 changes: 1 addition & 1 deletion providers/hedns/hednsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (c *hednsProvider) GetZoneRecords(domain string, meta map[string]string) (m
err = rc.SetTargetSRVPriorityString(priority, data)
case "SPF":
// Convert to TXT record as SPF is deprecated
rc.Type = "TXT"
rc.ChangeType("TXT", domain)
fallthrough
default:
err = rc.PopulateFromStringFunc(rc.Type, data, domain, txtutil.ParseQuoted)
Expand Down
2 changes: 1 addition & 1 deletion providers/loopia/loopiaProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func PrepDesiredRecords(dc *models.DomainConfig) {
if rec.Type == "ALIAS" {
// Loopia does not support ALIAS.
// Therefore, we change this to a CNAME.
rec.Type = "CNAME"
rec.ChangeType("CNAME", dc.Name)
}
if rec.TTL < 300 {
/* you can submit TTL lower than 300 but the dig results are normalized to 300 */
Expand Down
2 changes: 1 addition & 1 deletion providers/namedotcom/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (n *namedotcomProvider) GetZoneRecordsCorrections(dc *models.DomainConfig,

for _, rec := range dc.Records {
if rec.Type == "ALIAS" {
rec.Type = "ANAME"
rec.ChangeType("ANAME", dc.Name)
}
}

Expand Down

0 comments on commit 3f8f9e7

Please sign in to comment.