@@ -51,6 +51,17 @@ public async Task<bool> CreateRecordAsync(string recordName, string txtValue)
5151 {
5252 var cleanName = recordName . TrimEnd ( '.' ) ;
5353
54+ // Extract the zone from the record name
55+ var zoneName = ExtractZoneFromRecord ( cleanName ) ;
56+ Console . WriteLine ( $ "[Infoblox] Extracted zone: { zoneName } from record: { cleanName } ") ;
57+
58+ // Verify zone exists first
59+ var zoneExists = await VerifyZoneExistsAsync ( zoneName ) ;
60+ if ( ! zoneExists )
61+ {
62+ Console . WriteLine ( $ "[Infoblox] WARNING: Zone { zoneName } may not exist or is not accessible") ;
63+ }
64+
5465 // Delete any existing records with the same name first to ensure only one record exists
5566 var searchUrl = $ "record:txt?name={ Uri . EscapeDataString ( cleanName ) } ";
5667 var searchResponse = await _httpClient . GetAsync ( searchUrl ) ;
@@ -72,12 +83,13 @@ public async Task<bool> CreateRecordAsync(string recordName, string txtValue)
7283 }
7384 }
7485
75- // Create new record
86+ // Create new record with zone specified
7687 var payload = new
7788 {
7889 name = cleanName ,
7990 text = txtValue ,
8091 ttl = 60 ,
92+ zone = zoneName ,
8193 view = "default"
8294 } ;
8395
@@ -147,6 +159,46 @@ public async Task<bool> DeleteRecordAsync(string recordName)
147159 }
148160 }
149161
162+ private string ExtractZoneFromRecord ( string recordName )
163+ {
164+ if ( string . IsNullOrWhiteSpace ( recordName ) )
165+ return string . Empty ;
166+
167+ var parts = recordName . TrimEnd ( '.' ) . Split ( '.' ) ;
168+ if ( parts . Length < 2 )
169+ return recordName ;
170+
171+ // Use last two labels as default zone: e.g., "keyfactortestb.com"
172+ return string . Join ( "." , parts . Skip ( parts . Length - 2 ) ) ;
173+ }
174+
175+ private async Task < bool > VerifyZoneExistsAsync ( string zoneName )
176+ {
177+ try
178+ {
179+ var zoneUrl = $ "zone_auth?fqdn={ Uri . EscapeDataString ( zoneName ) } ";
180+ var response = await _httpClient . GetAsync ( zoneUrl ) ;
181+
182+ if ( ! response . IsSuccessStatusCode )
183+ {
184+ Console . WriteLine ( $ "[Infoblox] Zone lookup failed: { response . StatusCode } ") ;
185+ return false ;
186+ }
187+
188+ var json = await response . Content . ReadAsStringAsync ( ) ;
189+ var zones = JsonDocument . Parse ( json ) . RootElement ;
190+ var zoneExists = zones . GetArrayLength ( ) > 0 ;
191+
192+ Console . WriteLine ( $ "[Infoblox] Zone { zoneName } exists: { zoneExists } ") ;
193+ return zoneExists ;
194+ }
195+ catch ( Exception ex )
196+ {
197+ Console . WriteLine ( $ "[Infoblox] Error verifying zone: { ex . Message } ") ;
198+ return false ;
199+ }
200+ }
201+
150202 public void Dispose ( )
151203 {
152204 _httpClient ? . Dispose ( ) ;
0 commit comments