From d0dfae77dfebacf92a1d35926cf8104bb9446d9c Mon Sep 17 00:00:00 2001 From: Gabriel Hayes Date: Thu, 14 Nov 2024 09:27:28 -0600 Subject: [PATCH 1/9] Normalize zone (remove trailing .) --- client.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client.go b/client.go index 907415c..8c8bc66 100644 --- a/client.go +++ b/client.go @@ -38,6 +38,9 @@ func (p *Provider) getCloudDNSZone(zone string) (string, error) { for _, zone := range response.ManagedZones { if zone.Visibility == "public" { p.zoneMap[zone.DnsName] = zone.Name + if zone.DnsName[len(input)-2:len(input)-1] == "." { + p.zoneMap[zone.DnsName[:len(input)-1] = zone.Name + } } } return nil From b3fbf1b86d585407aae6c08cd88876eeb2d678ea Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 11:39:00 -0600 Subject: [PATCH 2/9] oopss --- client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 8c8bc66..41d8114 100644 --- a/client.go +++ b/client.go @@ -38,8 +38,8 @@ func (p *Provider) getCloudDNSZone(zone string) (string, error) { for _, zone := range response.ManagedZones { if zone.Visibility == "public" { p.zoneMap[zone.DnsName] = zone.Name - if zone.DnsName[len(input)-2:len(input)-1] == "." { - p.zoneMap[zone.DnsName[:len(input)-1] = zone.Name + if zone.DnsName[len(zone.DnsName)-2:len(zone.DnsName)-1] == "." { + p.zoneMap[zone.DnsName[:len(zone.DnsName)-1]] = zone.Name } } } From 2785115db55bc24a3dc4e8bfb80b056b01c92391 Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 11:50:51 -0600 Subject: [PATCH 3/9] oopz --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 41d8114..a556426 100644 --- a/client.go +++ b/client.go @@ -38,7 +38,7 @@ func (p *Provider) getCloudDNSZone(zone string) (string, error) { for _, zone := range response.ManagedZones { if zone.Visibility == "public" { p.zoneMap[zone.DnsName] = zone.Name - if zone.DnsName[len(zone.DnsName)-2:len(zone.DnsName)-1] == "." { + if zone.DnsName[len(zone.DnsName)-1:len(zone.DnsName)] == "." { p.zoneMap[zone.DnsName[:len(zone.DnsName)-1]] = zone.Name } } From 8429c0980e8f68b2c650da1b9f07193a652bebec Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 12:34:25 -0600 Subject: [PATCH 4/9] normalize zone --- client-delete.go | 2 ++ client-get.go | 3 +++ client-post.go | 1 + client.go | 4 +--- util.go | 6 ++++++ 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/client-delete.go b/client-delete.go index 7d131fd..a6b8fe9 100644 --- a/client-delete.go +++ b/client-delete.go @@ -13,6 +13,8 @@ func (p *Provider) deleteCloudDNSRecord(ctx context.Context, zone, name, recordT if err := p.newService(ctx); err != nil { return nil, err } + zone = normalizeZone(zone); + fullName := libdns.AbsoluteName(name, zone) gcdZone, err := p.getCloudDNSZone(zone) if err != nil { diff --git a/client-get.go b/client-get.go index c8fb91e..7d95443 100644 --- a/client-get.go +++ b/client-get.go @@ -13,6 +13,7 @@ func (p *Provider) getCloudDNSRecords(ctx context.Context, zone string) ([]libdn if err := p.newService(ctx); err != nil { return nil, err } + zone = normalizeZone(zone); gcdZone, err := p.getCloudDNSZone(zone) if err != nil { @@ -37,6 +38,8 @@ func (p *Provider) getCloudDNSRecord(ctx context.Context, zone, name, recordType if err := p.newService(ctx); err != nil { return nil, err } + zone = normalizeZone(zone); + gcdZone, err := p.getCloudDNSZone(zone) if err != nil { return nil, err diff --git a/client-post.go b/client-post.go index 4c78d87..2b63b6a 100644 --- a/client-post.go +++ b/client-post.go @@ -15,6 +15,7 @@ func (p *Provider) postCloudDNSRecord(ctx context.Context, zone string, recordsT if err := p.newService(ctx); err != nil { return nil, err } + zone = normalizeZone(zone); gcdZone, err := p.getCloudDNSZone(zone) if err != nil { return nil, err diff --git a/client.go b/client.go index a556426..6edd44a 100644 --- a/client.go +++ b/client.go @@ -31,6 +31,7 @@ func (p *Provider) newService(ctx context.Context) error { // getCloudDNSZone will return the Google Cloud DNS zone name for the specified zone. The data is cached // for five minutes to avoid repeated calls to the GCP API servers. func (p *Provider) getCloudDNSZone(zone string) (string, error) { + normalizeZone(zone) if p.zoneMap == nil || time.Since(p.zoneMapLastUpdated) > zoneMapTTL { p.zoneMap = make(map[string]string) zonesLister := p.service.ManagedZones.List(p.Project) @@ -38,9 +39,6 @@ func (p *Provider) getCloudDNSZone(zone string) (string, error) { for _, zone := range response.ManagedZones { if zone.Visibility == "public" { p.zoneMap[zone.DnsName] = zone.Name - if zone.DnsName[len(zone.DnsName)-1:len(zone.DnsName)] == "." { - p.zoneMap[zone.DnsName[:len(zone.DnsName)-1]] = zone.Name - } } } return nil diff --git a/util.go b/util.go index 986f601..6ac1e77 100644 --- a/util.go +++ b/util.go @@ -87,3 +87,9 @@ func convertToLibDNS(googleRecord *dns.ResourceRecordSet, zone string) libdnsRec } return records } +func normalizeZone(zone string) string { + if zone[len(zone)-1:len(zone)] === '.' { + return zone + } + return zone + "." +} \ No newline at end of file From 4c821983ce38326105b4d6f2b44b4b19eba865a4 Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 12:38:44 -0600 Subject: [PATCH 5/9] patch syntax --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 6ac1e77..617e201 100644 --- a/util.go +++ b/util.go @@ -88,7 +88,7 @@ func convertToLibDNS(googleRecord *dns.ResourceRecordSet, zone string) libdnsRec return records } func normalizeZone(zone string) string { - if zone[len(zone)-1:len(zone)] === '.' { + if zone[len(zone)-1:len(zone)] == "." { return zone } return zone + "." From 5750dd014b2ebf0dc31548a57d80e1ef271e62d9 Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 12:48:35 -0600 Subject: [PATCH 6/9] ofc --- client-delete.go | 1 - client-get.go | 2 -- client-post.go | 3 +-- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/client-delete.go b/client-delete.go index a6b8fe9..98c6b25 100644 --- a/client-delete.go +++ b/client-delete.go @@ -13,7 +13,6 @@ func (p *Provider) deleteCloudDNSRecord(ctx context.Context, zone, name, recordT if err := p.newService(ctx); err != nil { return nil, err } - zone = normalizeZone(zone); fullName := libdns.AbsoluteName(name, zone) gcdZone, err := p.getCloudDNSZone(zone) diff --git a/client-get.go b/client-get.go index 7d95443..c4326aa 100644 --- a/client-get.go +++ b/client-get.go @@ -13,7 +13,6 @@ func (p *Provider) getCloudDNSRecords(ctx context.Context, zone string) ([]libdn if err := p.newService(ctx); err != nil { return nil, err } - zone = normalizeZone(zone); gcdZone, err := p.getCloudDNSZone(zone) if err != nil { @@ -38,7 +37,6 @@ func (p *Provider) getCloudDNSRecord(ctx context.Context, zone, name, recordType if err := p.newService(ctx); err != nil { return nil, err } - zone = normalizeZone(zone); gcdZone, err := p.getCloudDNSZone(zone) if err != nil { diff --git a/client-post.go b/client-post.go index 2b63b6a..d49b128 100644 --- a/client-post.go +++ b/client-post.go @@ -15,7 +15,6 @@ func (p *Provider) postCloudDNSRecord(ctx context.Context, zone string, recordsT if err := p.newService(ctx); err != nil { return nil, err } - zone = normalizeZone(zone); gcdZone, err := p.getCloudDNSZone(zone) if err != nil { return nil, err @@ -26,7 +25,7 @@ func (p *Provider) postCloudDNSRecord(ctx context.Context, zone string, recordsT name := recordsToSend[0].Name fullName := libdns.AbsoluteName(name, zone) rrs := dns.ResourceRecordSet{ - Name: fullName, + Name: normalizeZone(fullName), Rrdatas: make([]string, 0), Ttl: int64(recordsToSend[0].TTL / time.Second), Type: recordsToSend[0].Type, From 59409300fe331c3649faa007820b3c66a94ad331 Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 12:54:54 -0600 Subject: [PATCH 7/9] actually assign param --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 6edd44a..a8d6373 100644 --- a/client.go +++ b/client.go @@ -31,7 +31,7 @@ func (p *Provider) newService(ctx context.Context) error { // getCloudDNSZone will return the Google Cloud DNS zone name for the specified zone. The data is cached // for five minutes to avoid repeated calls to the GCP API servers. func (p *Provider) getCloudDNSZone(zone string) (string, error) { - normalizeZone(zone) + zone = normalizeZone(zone) if p.zoneMap == nil || time.Since(p.zoneMapLastUpdated) > zoneMapTTL { p.zoneMap = make(map[string]string) zonesLister := p.service.ManagedZones.List(p.Project) From ff04339e8243eda71f45b88254f17cf421b61a89 Mon Sep 17 00:00:00 2001 From: topboy Date: Thu, 14 Nov 2024 13:02:40 -0600 Subject: [PATCH 8/9] normalize host... --- client-get.go | 2 +- util.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/client-get.go b/client-get.go index c4326aa..153ffdf 100644 --- a/client-get.go +++ b/client-get.go @@ -42,7 +42,7 @@ func (p *Provider) getCloudDNSRecord(ctx context.Context, zone, name, recordType if err != nil { return nil, err } - fullName := libdns.AbsoluteName(name, zone) + fullName := normalizeHost(libdns.AbsoluteName(name, zone)) rrs, err := p.service.ResourceRecordSets.Get(p.Project, gcdZone, fullName, recordType).Context(ctx).Do() if err != nil { return nil, err diff --git a/util.go b/util.go index 617e201..274745d 100644 --- a/util.go +++ b/util.go @@ -92,4 +92,10 @@ func normalizeZone(zone string) string { return zone } return zone + "." +} +func normalizeHost(host string) string { + if host[len(host)-1:len(host)] == "." { + return host[:len(host)-1] + } + return host } \ No newline at end of file From f0e45cf73452f0c4a3da0f0b7c995d6d62ad7e06 Mon Sep 17 00:00:00 2001 From: topboy Date: Fri, 15 Nov 2024 13:13:41 -0600 Subject: [PATCH 9/9] dedupe record.values --- client-post.go | 3 ++- util.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/client-post.go b/client-post.go index d49b128..1798c82 100644 --- a/client-post.go +++ b/client-post.go @@ -15,6 +15,7 @@ func (p *Provider) postCloudDNSRecord(ctx context.Context, zone string, recordsT if err := p.newService(ctx); err != nil { return nil, err } + zone = normalizeZone(zone) gcdZone, err := p.getCloudDNSZone(zone) if err != nil { return nil, err @@ -25,7 +26,7 @@ func (p *Provider) postCloudDNSRecord(ctx context.Context, zone string, recordsT name := recordsToSend[0].Name fullName := libdns.AbsoluteName(name, zone) rrs := dns.ResourceRecordSet{ - Name: normalizeZone(fullName), + Name: fullName, Rrdatas: make([]string, 0), Ttl: int64(recordsToSend[0].TTL / time.Second), Type: recordsToSend[0].Type, diff --git a/util.go b/util.go index 274745d..cc32d83 100644 --- a/util.go +++ b/util.go @@ -46,7 +46,14 @@ func (l libdnsRecords) hasRecord(record libdns.Record) bool { } return false } - +func contains(arr []string, value string) bool { + for _, v := range arr { + if v == value { + return true + } + } + return false +} // doesNotHaveRecords returns true if this set of records does not contain the specified // record. Only the name, type, and value are compared; the TTL is ignored. func (l libdnsRecords) doesNotHaveRecord(record libdns.Record) bool { @@ -64,7 +71,9 @@ func (l libdnsRecords) prepValuesForCloudDNS() []string { //ensure we quote a value with spaces but do not double quote value = fmt.Sprintf(`"%s"`, strings.Trim(value, `"`)) } - values = append(values, value) + if !contains(values, value) { + values = append(values, value) + } } return values