@@ -3,6 +3,7 @@ package cloudflare
33import (
44 "context"
55 "fmt"
6+ "log"
67 "net/http"
78 "sync"
89
@@ -42,8 +43,13 @@ func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record
4243
4344 recs := make ([]libdns.Record , 0 , len (result ))
4445 for _ , rec := range result {
45- recs = append (recs , rec .libdnsRecord (zone ))
46+ libdnsRec , err := rec .libdnsRecord (zone )
47+ if err != nil {
48+ return nil , fmt .Errorf ("parsing Cloudflare DNS record %+v: %v" , rec , err )
49+ }
50+ recs = append (recs , libdnsRec )
4651 }
52+ log .Printf ("GOT RECORDS: %#v" , recs )
4753
4854 return recs , nil
4955}
@@ -61,7 +67,11 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
6167 if err != nil {
6268 return nil , err
6369 }
64- created = append (created , result .libdnsRecord (zone ))
70+ libdnsRec , err := result .libdnsRecord (zone )
71+ if err != nil {
72+ return nil , fmt .Errorf ("parsing Cloudflare DNS record %+v: %v" , rec , err )
73+ }
74+ created = append (created , libdnsRec )
6575 }
6676
6777 return created , nil
@@ -77,29 +87,14 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
7787
7888 var recs []libdns.Record
7989 for _ , rec := range records {
80- // we create a "delete queue" for each record
81- // requested for deletion; if the record ID
82- // is known, that is the only one to fill the
83- // queue, but if it's not known, we try to find
84- // a match theoretically there could be more
85- // than one
86- var deleteQueue []libdns.Record
87-
88- if rec .ID == "" {
89- // record ID is required; try to find it with what was provided
90- exactMatches , err := p .getDNSRecords (ctx , zoneInfo , rec , true )
91- if err != nil {
92- return nil , err
93- }
94- for _ , rec := range exactMatches {
95- deleteQueue = append (deleteQueue , rec .libdnsRecord (zone ))
96- }
97- } else {
98- deleteQueue = []libdns.Record {rec }
90+ // record ID is required; try to find it with what was provided
91+ exactMatches , err := p .getDNSRecords (ctx , zoneInfo , rec , true )
92+ if err != nil {
93+ return nil , err
9994 }
10095
101- for _ , delRec := range deleteQueue {
102- reqURL := fmt .Sprintf ("%s/zones/%s/dns_records/%s" , baseURL , zoneInfo .ID , delRec .ID )
96+ for _ , cfRec := range exactMatches {
97+ reqURL := fmt .Sprintf ("%s/zones/%s/dns_records/%s" , baseURL , zoneInfo .ID , cfRec .ID )
10398 req , err := http .NewRequestWithContext (ctx , "DELETE" , reqURL , nil )
10499 if err != nil {
105100 return nil , err
@@ -111,7 +106,11 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
111106 return nil , err
112107 }
113108
114- recs = append (recs , result .libdnsRecord (zone ))
109+ libdnsRec , err := result .libdnsRecord (zone )
110+ if err != nil {
111+ return nil , fmt .Errorf ("parsing Cloudflare DNS record %+v: %v" , rec , err )
112+ }
113+ recs = append (recs , libdnsRec )
115114 }
116115
117116 }
@@ -134,27 +133,31 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
134133 return nil , err
135134 }
136135 oldRec .ZoneID = zoneInfo .ID
137- if rec .ID == "" {
138- // the record might already exist, even if we don't know the ID yet
139- matches , err := p .getDNSRecords (ctx , zoneInfo , rec , false )
136+
137+ // the record might already exist, even if we don't know the ID yet
138+ matches , err := p .getDNSRecords (ctx , zoneInfo , rec , false )
139+ if err != nil {
140+ return nil , err
141+ }
142+ if len (matches ) == 0 {
143+ // record doesn't exist; create it
144+ result , err := p .createRecord (ctx , zoneInfo , rec )
140145 if err != nil {
141146 return nil , err
142147 }
143- if len (matches ) == 0 {
144- // record doesn't exist; create it
145- result , err := p .createRecord (ctx , zoneInfo , rec )
146- if err != nil {
147- return nil , err
148- }
149- results = append (results , result .libdnsRecord (zone ))
150- continue
151- }
152- if len (matches ) > 1 {
153- return nil , fmt .Errorf ("unexpectedly found more than 1 record for %v" , rec )
148+ libdnsRec , err := result .libdnsRecord (zone )
149+ if err != nil {
150+ return nil , fmt .Errorf ("parsing Cloudflare DNS record %+v: %v" , rec , err )
154151 }
155- // record does exist, fill in the ID so that we can update it
156- oldRec .ID = matches [0 ].ID
152+ results = append (results , libdnsRec )
153+ continue
154+ }
155+ if len (matches ) > 1 {
156+ return nil , fmt .Errorf ("unexpectedly found more than 1 record for %v" , rec )
157157 }
158+ // record does exist, fill in the ID so that we can update it
159+ oldRec .ID = matches [0 ].ID
160+
158161 // record exists; update it
159162 cfRec , err := cloudflareRecord (rec )
160163 if err != nil {
@@ -164,7 +167,11 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
164167 if err != nil {
165168 return nil , err
166169 }
167- results = append (results , result .libdnsRecord (zone ))
170+ libdnsRec , err := result .libdnsRecord (zone )
171+ if err != nil {
172+ return nil , fmt .Errorf ("parsing Cloudflare DNS record %+v: %v" , rec , err )
173+ }
174+ results = append (results , libdnsRec )
168175 }
169176
170177 return results , nil
0 commit comments