From 4e842a5eb6dcb9520e03db70cd5896f1df14b72a Mon Sep 17 00:00:00 2001 From: YAEGASHI Takeshi Date: Wed, 5 Dec 2018 05:04:39 +0900 Subject: [PATCH] Azure: Do not overwrite existing TXT records (#726) --- providers/dns/azure/azure.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/providers/dns/azure/azure.go b/providers/dns/azure/azure.go index 74a4e531..48ff4e38 100644 --- a/providers/dns/azure/azure.go +++ b/providers/dns/azure/azure.go @@ -148,11 +148,37 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { rsc.Authorizer = d.authorizer relative := toRelativeRecord(fqdn, acme.ToFqdn(zone)) + + // Get existing record set + rset, err := rsc.Get(ctx, d.config.ResourceGroup, zone, relative, dns.TXT) + if err != nil { + detailedError, ok := err.(autorest.DetailedError) + if !ok || detailedError.StatusCode != http.StatusNotFound { + return fmt.Errorf("azure: %v", err) + } + } + + // Construct unique TXT records using map + uniqRecords := map[string]struct{}{value: {}} + if rset.RecordSetProperties != nil && rset.TxtRecords != nil { + for _, txtRecord := range *rset.TxtRecords { + // Assume Value doesn't contain multiple strings + if txtRecord.Value != nil && len(*txtRecord.Value) > 0 { + uniqRecords[(*txtRecord.Value)[0]] = struct{}{} + } + } + } + + var txtRecords []dns.TxtRecord + for txt := range uniqRecords { + txtRecords = append(txtRecords, dns.TxtRecord{Value: &[]string{txt}}) + } + rec := dns.RecordSet{ Name: &relative, RecordSetProperties: &dns.RecordSetProperties{ TTL: to.Int64Ptr(int64(d.config.TTL)), - TxtRecords: &[]dns.TxtRecord{{Value: &[]string{value}}}, + TxtRecords: &txtRecords, }, }