@@ -269,20 +269,59 @@ resource "google_compute_disk" "${this.sanitizeResourceName(disk.id)}" {
269269 const zone = vm . properties . zone || "asia-northeast3-a" ;
270270 const networkTags = vm . properties . networkTags || [ ] ;
271271
272- const subnetRef = subnets . length > 0 ?
273- `google_compute_subnetwork.${ this . sanitizeResourceName ( subnets [ 0 ] . id ) } .id` :
274- '"default"' ;
272+ // VM-Subnet 연결 찾기 (스태킹 관계)
273+ const subnetConnection = connections . find ( conn =>
274+ ( conn . fromBlockId === vm . id || conn . toBlockId === vm . id ) &&
275+ conn . properties ?. stackConnection === true &&
276+ subnets . some ( s => s . id === conn . fromBlockId || s . id === conn . toBlockId )
277+ ) ;
278+
279+ let subnetRef = '"default"' ;
280+ if ( subnetConnection ) {
281+ // 연결된 Subnet ID 찾기
282+ const connectedSubnetId = subnetConnection . fromBlockId === vm . id
283+ ? subnetConnection . toBlockId
284+ : subnetConnection . fromBlockId ;
285+ const connectedSubnet = subnets . find ( s => s . id === connectedSubnetId ) ;
286+
287+ if ( connectedSubnet ) {
288+ subnetRef = `google_compute_subnetwork.${ this . sanitizeResourceName ( connectedSubnet . id ) } .id` ;
289+ console . log ( `[GCP CodeGen] VM ${ vm . id . substring ( 0 , 8 ) } → Subnet ${ connectedSubnet . id . substring ( 0 , 8 ) } ` ) ;
290+ }
291+ } else if ( subnets . length > 0 ) {
292+ // 연결이 없으면 첫 번째 Subnet 사용 (fallback)
293+ subnetRef = `google_compute_subnetwork.${ this . sanitizeResourceName ( subnets [ 0 ] . id ) } .id` ;
294+ console . warn ( `[GCP CodeGen] No subnet connection found for VM ${ vm . id . substring ( 0 , 8 ) } , using first subnet` ) ;
295+ }
275296
276297 // 부트디스크 찾기 (스태킹된 Persistent Disk)
298+ console . log ( `[GCP CodeGen] VM ${ vm . id . substring ( 0 , 8 ) } - 부트디스크 검색 중...` ) ;
299+ console . log ( `[GCP CodeGen] 전체 연결 수: ${ connections . length } , Disk 수: ${ persistentDisks . length } ` ) ;
300+
277301 const bootDisk = persistentDisks . find ( ( disk ) => {
278302 const diskConnection = connections . find ( conn =>
279303 ( conn . fromBlockId === vm . id && conn . toBlockId === disk . id ) ||
280304 ( conn . fromBlockId === disk . id && conn . toBlockId === vm . id )
281305 ) ;
306+
307+ if ( diskConnection ) {
308+ console . log ( `[GCP CodeGen] Disk ${ disk . id . substring ( 0 , 8 ) } 연결 발견:` , {
309+ stackConnection : diskConnection . properties ?. stackConnection ,
310+ volumeType : diskConnection . properties ?. volumeType ,
311+ fromTo : `${ diskConnection . fromBlockId . substring ( 0 , 8 ) } → ${ diskConnection . toBlockId . substring ( 0 , 8 ) } `
312+ } ) ;
313+ }
314+
282315 return diskConnection ?. properties ?. stackConnection === true &&
283316 diskConnection ?. properties ?. volumeType === 'boot' ;
284317 } ) ;
285318
319+ if ( bootDisk ) {
320+ console . log ( `✅ [GCP CodeGen] 부트디스크 찾음: ${ bootDisk . id . substring ( 0 , 8 ) } (${ bootDisk . properties . name } )` ) ;
321+ } else {
322+ console . log ( `❌ [GCP CodeGen] 부트디스크 없음` ) ;
323+ }
324+
286325 let code = `# Compute Engine: ${ name }
287326resource "google_compute_instance" "${ this . sanitizeResourceName ( vm . id ) } " {
288327 name = "${ name } "
0 commit comments