@@ -64,9 +64,15 @@ func (p *Provider) caller() string {
64
64
65
65
// GetRecords lists all the records in the zone.
66
66
func (p * Provider ) GetRecords (ctx context.Context , zone string ) ([]libdns.Record , error ) {
67
- zone = strings .TrimSuffix (zone , "." )
67
+ p .getLogger ().Debug ("GetRecords called" ,
68
+ zap .String ("zone" , zone ))
68
69
69
- records , err := p .getZoneRecords (ctx , zone )
70
+ managedZone , err := p .findManageableZone (ctx , zone )
71
+ if err != nil {
72
+ return nil , err
73
+ }
74
+
75
+ records , err := p .getZoneRecords (ctx , managedZone )
70
76
if err != nil {
71
77
return nil , err
72
78
}
@@ -76,11 +82,36 @@ func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record
76
82
77
83
// AppendRecords adds records to the zone. It returns the records that were added.
78
84
func (p * Provider ) AppendRecords (ctx context.Context , zone string , records []libdns.Record ) ([]libdns.Record , error ) {
79
- zone = strings .TrimSuffix (zone , "." )
85
+ p .getLogger ().Debug ("AppendRecords called" ,
86
+ zap .String ("zone" , zone ),
87
+ zap .Int ("record_count" , len (records )))
88
+
89
+ managedZone , err := p .findManageableZone (ctx , zone )
90
+ if err != nil {
91
+ return nil , err
92
+ }
93
+
94
+ if zone != managedZone {
95
+ p .getLogger ().Debug ("Using managed zone" ,
96
+ zap .String ("managed_zone" , managedZone ),
97
+ zap .String ("requested_zone" , zone ))
98
+ }
80
99
81
100
var created []libdns.Record
82
101
for _ , rec := range records {
83
- result , err := p .appendZoneRecord (ctx , zone , rec )
102
+ // Adjust record name if managedZone differs from requested zone
103
+ adjustedRecord := rec
104
+ if managedZone != strings .TrimSuffix (zone , "." ) {
105
+ adjustedRecord = p .adjustRecordForZone (rec , zone , managedZone )
106
+ }
107
+
108
+ adjustedRR := adjustedRecord .RR ()
109
+ p .getLogger ().Debug ("Creating record" ,
110
+ zap .String ("name" , adjustedRR .Name ),
111
+ zap .String ("type" , adjustedRR .Type ),
112
+ zap .String ("value" , adjustedRR .Data ))
113
+
114
+ result , err := p .appendZoneRecord (ctx , managedZone , adjustedRecord )
84
115
if err != nil {
85
116
return nil , err
86
117
}
@@ -93,13 +124,38 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
93
124
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
94
125
// It returns the updated records.
95
126
func (p * Provider ) SetRecords (ctx context.Context , zone string , records []libdns.Record ) ([]libdns.Record , error ) {
96
- zone = strings .TrimSuffix (zone , "." )
127
+ p .getLogger ().Debug ("SetRecords called" ,
128
+ zap .String ("zone" , zone ),
129
+ zap .Int ("record_count" , len (records )))
130
+
131
+ managedZone , err := p .findManageableZone (ctx , zone )
132
+ if err != nil {
133
+ return nil , err
134
+ }
135
+
136
+ if zone != managedZone {
137
+ p .getLogger ().Debug ("Using managed zone" ,
138
+ zap .String ("managed_zone" , managedZone ),
139
+ zap .String ("requested_zone" , zone ))
140
+ }
97
141
98
142
var updated []libdns.Record
99
143
var errors []error
100
144
101
145
for _ , rec := range records {
102
- result , err := p .setZoneRecord (ctx , zone , rec )
146
+ // Adjust record name if managedZone differs from requested zone
147
+ adjustedRecord := rec
148
+ if managedZone != strings .TrimSuffix (zone , "." ) {
149
+ adjustedRecord = p .adjustRecordForZone (rec , zone , managedZone )
150
+ }
151
+
152
+ adjustedRR := adjustedRecord .RR ()
153
+ p .getLogger ().Debug ("Creating record" ,
154
+ zap .String ("name" , adjustedRR .Name ),
155
+ zap .String ("type" , adjustedRR .Type ),
156
+ zap .String ("value" , adjustedRR .Data ))
157
+
158
+ result , err := p .setZoneRecord (ctx , managedZone , adjustedRecord )
103
159
if err != nil {
104
160
errors = append (errors , err )
105
161
continue
@@ -121,11 +177,36 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
121
177
122
178
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
123
179
func (p * Provider ) DeleteRecords (ctx context.Context , zone string , records []libdns.Record ) ([]libdns.Record , error ) {
124
- zone = strings .TrimSuffix (zone , "." )
180
+ p .getLogger ().Debug ("DeleteRecords called" ,
181
+ zap .String ("zone" , zone ),
182
+ zap .Int ("record_count" , len (records )))
183
+
184
+ managedZone , err := p .findManageableZone (ctx , zone )
185
+ if err != nil {
186
+ return nil , err
187
+ }
188
+
189
+ if zone != managedZone {
190
+ p .getLogger ().Debug ("Using managed zone" ,
191
+ zap .String ("managed_zone" , managedZone ),
192
+ zap .String ("requested_zone" , zone ))
193
+ }
125
194
126
195
var deleted []libdns.Record
127
196
for _ , rec := range records {
128
- result , err := p .deleteZoneRecord (ctx , zone , rec )
197
+ // Adjust record name if managedZone differs from requested zone
198
+ adjustedRecord := rec
199
+ if managedZone != strings .TrimSuffix (zone , "." ) {
200
+ adjustedRecord = p .adjustRecordForZone (rec , zone , managedZone )
201
+ }
202
+
203
+ adjustedRR := adjustedRecord .RR ()
204
+ p .getLogger ().Debug ("Deleting record" ,
205
+ zap .String ("name" , adjustedRR .Name ),
206
+ zap .String ("type" , adjustedRR .Type ),
207
+ zap .String ("value" , adjustedRR .Data ))
208
+
209
+ result , err := p .deleteZoneRecord (ctx , managedZone , adjustedRecord )
129
210
if err != nil {
130
211
return nil , err
131
212
}
@@ -135,6 +216,57 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
135
216
return deleted , nil
136
217
}
137
218
219
+ // adjustRecordForZone adjusts the record name when the managed zone differs from the requested zone
220
+ func (p * Provider ) adjustRecordForZone (record libdns.Record , requestedZone , managedZone string ) libdns.Record {
221
+ requestedZone = strings .TrimSuffix (requestedZone , "." )
222
+ managedZone = strings .TrimSuffix (managedZone , "." )
223
+
224
+ // Calculate the subdomain portion that was stripped during zone detection
225
+ // Example: requestedZone="test.domain.com", managedZone="domain.com" -> subdomain="test"
226
+ if ! strings .HasSuffix (requestedZone , managedZone ) {
227
+ return record // Safety check - shouldn't happen with proper zone detection
228
+ }
229
+
230
+ var subdomain string
231
+ if requestedZone == managedZone {
232
+ subdomain = ""
233
+ } else {
234
+ subdomain = strings .TrimSuffix (requestedZone , "." + managedZone )
235
+ }
236
+
237
+ if subdomain == "" {
238
+ return record
239
+ }
240
+
241
+ rr := record .RR ()
242
+
243
+ // Check if the record name has already been adjusted by seeing if it already ends with the subdomain
244
+ if strings .HasSuffix (rr .Name , "." + subdomain ) {
245
+ p .getLogger ().Debug ("Record name already adjusted, skipping" ,
246
+ zap .String ("name" , rr .Name ),
247
+ zap .String ("subdomain" , subdomain ))
248
+ return record
249
+ }
250
+
251
+ // Adjust the record name to include the subdomain
252
+ // Example: "_acme-challenge.libdns" -> "_acme-challenge.libdns.test"
253
+ adjustedName := rr .Name + "." + subdomain
254
+
255
+ p .getLogger ().Debug ("Adjusting record name" ,
256
+ zap .String ("original_name" , rr .Name ),
257
+ zap .String ("adjusted_name" , adjustedName ),
258
+ zap .String ("subdomain" , subdomain ))
259
+
260
+ adjustedRR := & libdns.RR {
261
+ Type : rr .Type ,
262
+ Name : adjustedName ,
263
+ Data : rr .Data ,
264
+ TTL : rr .TTL ,
265
+ }
266
+
267
+ return adjustedRR
268
+ }
269
+
138
270
// Interface guards
139
271
var (
140
272
_ libdns.RecordGetter = (* Provider )(nil )
0 commit comments