@@ -77,7 +77,8 @@ public async Task<bool> CreateRecordAsync(string recordName, string txtValue)
7777 if ( searchResponse . IsSuccessStatusCode )
7878 {
7979 var searchJson = await searchResponse . Content . ReadAsStringAsync ( ) ;
80- var records = JsonDocument . Parse ( searchJson ) . RootElement ;
80+ using var searchDoc = JsonDocument . Parse ( searchJson ) ;
81+ var records = searchDoc . RootElement ;
8182 var recordCount = records . GetArrayLength ( ) ;
8283 _logger ? . LogDebug ( "[Infoblox] Found {RecordCount} existing records" , recordCount ) ;
8384
@@ -135,7 +136,7 @@ public async Task<bool> CreateRecordAsync(string recordName, string txtValue)
135136 view = "default"
136137 } ;
137138
138- var json = JsonSerializer . Serialize ( payload ) ;
139+ var json = JsonSerializer . Serialize ( payload , _jsonOptions ) ;
139140 _logger ? . LogDebug ( "[Infoblox] Creating new TXT record. Payload: {Payload}" , json ) ;
140141
141142 var request = new HttpRequestMessage ( HttpMethod . Post , "./record:txt" ) ;
@@ -190,7 +191,7 @@ public async Task<bool> DeleteRecordAsync(string recordName)
190191 try
191192 {
192193 var cleanName = recordName . TrimEnd ( '.' ) ;
193- var searchUrl = $ "record:txt?name={ Uri . EscapeDataString ( cleanName ) } ";
194+ var searchUrl = $ "./ record:txt?name={ Uri . EscapeDataString ( cleanName ) } ";
194195
195196 var searchResponse = await _httpClient . GetAsync ( searchUrl ) ;
196197 if ( ! searchResponse . IsSuccessStatusCode )
@@ -200,26 +201,45 @@ public async Task<bool> DeleteRecordAsync(string recordName)
200201 }
201202
202203 var searchJson = await searchResponse . Content . ReadAsStringAsync ( ) ;
203- var records = JsonDocument . Parse ( searchJson ) . RootElement ;
204+ using var searchDoc = JsonDocument . Parse ( searchJson ) ;
205+ var records = searchDoc . RootElement ;
204206
205207 if ( records . GetArrayLength ( ) == 0 )
206208 {
207209 _logger ? . LogDebug ( "[Infoblox] No TXT records found for {RecordName}" , cleanName ) ;
208210 return false ;
209211 }
210212
211- var recordRef = records [ 0 ] . GetProperty ( "_ref" ) . GetString ( ) ;
212- if ( string . IsNullOrEmpty ( recordRef ) )
213+ var allDeleted = true ;
214+ foreach ( var record in records . EnumerateArray ( ) )
213215 {
214- _logger ? . LogDebug ( "[Infoblox] Record reference is null or empty" ) ;
215- return false ;
216- }
216+ if ( ! record . TryGetProperty ( "_ref" , out var refProperty ) )
217+ {
218+ _logger ? . LogWarning ( "[Infoblox] Record does not have _ref property" ) ;
219+ allDeleted = false ;
220+ continue ;
221+ }
222+
223+ var recordRef = "./" + refProperty . GetString ( ) ;
224+ if ( string . IsNullOrEmpty ( recordRef ) || recordRef == "./" )
225+ {
226+ _logger ? . LogWarning ( "[Infoblox] Record _ref is null or empty" ) ;
227+ allDeleted = false ;
228+ continue ;
229+ }
230+
231+ var deleteResponse = await _httpClient . DeleteAsync ( recordRef ) ;
232+ var result = await deleteResponse . Content . ReadAsStringAsync ( ) ;
217233
218- var deleteResponse = await _httpClient . DeleteAsync ( recordRef ) ;
219- var result = await deleteResponse . Content . ReadAsStringAsync ( ) ;
234+ _logger ? . LogDebug ( "[Infoblox] Delete TXT: {StatusCode} - {Result}" , deleteResponse . StatusCode , result ) ;
235+
236+ if ( ! deleteResponse . IsSuccessStatusCode )
237+ {
238+ allDeleted = false ;
239+ }
240+ }
220241
221- _logger ? . LogDebug ( "[Infoblox] Delete TXT: {StatusCode} - {Result}" , deleteResponse . StatusCode , result ) ;
222- return deleteResponse . IsSuccessStatusCode ;
242+ return allDeleted ;
223243 }
224244 catch ( Exception ex )
225245 {
@@ -274,7 +294,8 @@ private async Task<bool> VerifyZoneExistsAsync(string zoneName)
274294 }
275295
276296 var json = await response . Content . ReadAsStringAsync ( ) ;
277- var zones = JsonDocument . Parse ( json ) . RootElement ;
297+ using var zoneDoc = JsonDocument . Parse ( json ) ;
298+ var zones = zoneDoc . RootElement ;
278299 var zoneExists = zones . GetArrayLength ( ) > 0 ;
279300
280301 _logger ? . LogDebug ( "[Infoblox] Zone {ZoneName} exists: {ZoneExists}" , zoneName , zoneExists ) ;
@@ -300,14 +321,18 @@ private async Task<bool> VerifyRecordExists(string recordName, string expectedVa
300321 }
301322
302323 var json = await response . Content . ReadAsStringAsync ( ) ;
303- var records = JsonDocument . Parse ( json ) . RootElement ;
324+ using var recordDoc = JsonDocument . Parse ( json ) ;
325+ var records = recordDoc . RootElement ;
304326
305327 foreach ( var record in records . EnumerateArray ( ) )
306328 {
307- var text = record . GetProperty ( "text" ) . GetString ( ) ;
308- if ( text == expectedValue )
329+ if ( record . TryGetProperty ( "text" , out var textProperty ) )
309330 {
310- return true ;
331+ var text = textProperty . GetString ( ) ;
332+ if ( text == expectedValue )
333+ {
334+ return true ;
335+ }
311336 }
312337 }
313338
0 commit comments