@@ -336,8 +336,9 @@ public ComputerSystem DeployVirtualMachine(dynamic jsonObj, string systemVmIso)
336336 }
337337
338338 String publicIpAddress = "" ;
339+ int nicCount = 0 ;
339340 // Add the Nics to the VM in the deviceId order.
340- for ( int i = 0 ; i <= 2 ; i ++ )
341+ foreach ( var nc in nicInfo )
341342 {
342343 foreach ( var nic in nicInfo )
343344 {
@@ -360,18 +361,23 @@ public ComputerSystem DeployVirtualMachine(dynamic jsonObj, string systemVmIso)
360361 }
361362 }
362363
363- if ( i == 2 )
364+ if ( nicCount == 2 )
364365 {
365366 publicIpAddress = nic . ip ;
366367 }
367368
368- if ( nicid == i )
369+ if ( nicid == nicCount )
369370 {
370371 // Create network adapter
371372 var newAdapter = CreateNICforVm ( newVm , mac ) ;
373+ String switchName = "" ;
374+ if ( nic . name != null )
375+ {
376+ switchName = nic . name ;
377+ }
372378
373379 // connection to vswitch
374- var portSettings = AttachNicToPort ( newVm , newAdapter ) ;
380+ var portSettings = AttachNicToPort ( newVm , newAdapter , switchName ) ;
375381
376382 // set vlan
377383 if ( vlan != null )
@@ -383,6 +389,7 @@ public ComputerSystem DeployVirtualMachine(dynamic jsonObj, string systemVmIso)
383389 newAdapter . Path , portSettings . Path , ( vlan == null ? "No VLAN" : "VLAN " + vlan ) ) ;
384390 }
385391 }
392+ nicCount ++ ;
386393 }
387394
388395 // pass the boot args for the VM using KVP component.
@@ -459,10 +466,18 @@ public static Boolean pingResource(String ip)
459466 return false ;
460467 }
461468
462- private EthernetPortAllocationSettingData AttachNicToPort ( ComputerSystem newVm , SyntheticEthernetPortSettingData newAdapter )
469+ private EthernetPortAllocationSettingData AttachNicToPort ( ComputerSystem newVm , SyntheticEthernetPortSettingData newAdapter , String vSwitchName )
463470 {
464471 // Get the virtual switch
465- VirtualEthernetSwitch vSwitch = GetExternalVirtSwitch ( ) ;
472+ VirtualEthernetSwitch vSwitch = GetExternalVirtSwitch ( vSwitchName ) ;
473+ //check the the recevied vSwitch is the same as vSwitchName.
474+ if ( ! vSwitchName . Equals ( "" ) && ! vSwitch . ElementName . Equals ( vSwitchName ) )
475+ {
476+ var errMsg = string . Format ( "Internal error, coudl not find Virtual Switch with the name : " + vSwitchName ) ;
477+ var ex = new WmiException ( errMsg ) ;
478+ logger . Error ( errMsg , ex ) ;
479+ throw ex ;
480+ }
466481
467482 // Create port for adapter
468483 var defaultEthernetPortSettings = EthernetPortAllocationSettingData . GetInstances ( vSwitch . Scope , "InstanceID LIKE \" %Default\" " ) ;
@@ -1157,7 +1172,7 @@ public static VirtualEthernetSwitch GetExternalVirtSwitch()
11571172 {
11581173 // Work back from the first *bound* external NIC we find.
11591174 var externNICs = ExternalEthernetPort . GetInstances ( "IsBound = TRUE" ) ;
1160-
1175+ VirtualEthernetSwitch vSwitch = null ;
11611176 // Assert
11621177 if ( externNICs . Count == 0 )
11631178 {
@@ -1166,8 +1181,7 @@ public static VirtualEthernetSwitch GetExternalVirtSwitch()
11661181 logger . Error ( errMsg , ex ) ;
11671182 throw ex ;
11681183 }
1169-
1170- ExternalEthernetPort externNIC = externNICs . OfType < ExternalEthernetPort > ( ) . First ( ) ;
1184+ foreach ( ExternalEthernetPort externNIC in externNICs . OfType < ExternalEthernetPort > ( ) ) {
11711185 // A sequence of ASSOCIATOR objects need to be traversed to get from external NIC the vswitch.
11721186 // We use ManagementObjectSearcher objects to execute this sequence of questions
11731187 // NB: default scope of ManagementObjectSearcher is '\\.\root\cimv2', which does not contain
@@ -1217,7 +1231,7 @@ public static VirtualEthernetSwitch GetExternalVirtSwitch()
12171231 var vSwitchQuery = new RelatedObjectQuery ( switchPort . Path . Path , VirtualEthernetSwitch . CreatedClassName ) ;
12181232 var vSwitchSearch = new ManagementObjectSearcher ( externNIC . Scope , vSwitchQuery ) ;
12191233 var vSwitchCollection = new VirtualEthernetSwitch . VirtualEthernetSwitchCollection ( vSwitchSearch . Get ( ) ) ;
1220-
1234+
12211235 // assert
12221236 if ( vSwitchCollection . Count < 1 )
12231237 {
@@ -1226,7 +1240,12 @@ public static VirtualEthernetSwitch GetExternalVirtSwitch()
12261240 logger . Error ( errMsg , ex ) ;
12271241 throw ex ;
12281242 }
1229- VirtualEthernetSwitch vSwitch = vSwitchCollection . OfType < VirtualEthernetSwitch > ( ) . First ( ) ;
1243+ vSwitch = vSwitchCollection . OfType < VirtualEthernetSwitch > ( ) . First ( ) ;
1244+ if ( vSwitch . ElementName . Equals ( vSwitchName ) == true )
1245+ {
1246+ return vSwitch ;
1247+ }
1248+ }
12301249 return vSwitch ;
12311250 }
12321251
0 commit comments