diff --git a/client-delete.go b/client-delete.go index 7d131fd..98c6b25 100644 --- a/client-delete.go +++ b/client-delete.go @@ -13,6 +13,7 @@ func (p *Provider) deleteCloudDNSRecord(ctx context.Context, zone, name, recordT if err := p.newService(ctx); err != nil { return nil, err } + 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..153ffdf 100644 --- a/client-get.go +++ b/client-get.go @@ -37,11 +37,12 @@ func (p *Provider) getCloudDNSRecord(ctx context.Context, zone, name, recordType if err := p.newService(ctx); err != nil { return nil, err } + gcdZone, err := p.getCloudDNSZone(zone) 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/client-post.go b/client-post.go index 4c78d87..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 diff --git a/client.go b/client.go index 907415c..a8d6373 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) { + 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) diff --git a/util.go b/util.go index 986f601..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 @@ -87,3 +96,15 @@ 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 + "." +} +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