Skip to content

Commit db5aa33

Browse files
authored
[4.11.3] Explicitly controlling VMware VM hardware version (#17)
* Explicitly controlling VMware VM hardware version * Add unit tests and add defensive checks
1 parent 0dacb2c commit db5aa33

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

vmware-base/src/com/cloud/hypervisor/vmware/mo/ClusterMO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public ClusterDasConfigInfo getDasConfig() throws Exception {
9494
return null;
9595
}
9696

97-
private ClusterConfigInfoEx getClusterConfigInfo() throws Exception {
97+
public ClusterConfigInfoEx getClusterConfigInfo() throws Exception {
9898
ClusterConfigInfoEx configInfo = (ClusterConfigInfoEx)_context.getVimClient().getDynamicProperty(_mor, "configurationEx");
9999
return configInfo;
100100
}

vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323

24+
import com.vmware.vim25.DatacenterConfigInfo;
2425
import org.apache.log4j.Logger;
2526

2627
import com.vmware.vim25.CustomFieldStringValue;
@@ -508,4 +509,9 @@ public boolean ensureCustomFieldDef(String fieldName) throws Exception {
508509
CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
509510
return cfmMo.ensureCustomFieldDef("Datacenter", fieldName) > 0;
510511
}
512+
513+
public DatacenterConfigInfo getDatacenterConfigInfo() throws Exception {
514+
DatacenterConfigInfo configInfo = (DatacenterConfigInfo)_context.getVimClient().getDynamicProperty(_mor, "configuration");
515+
return configInfo;
516+
}
511517
}

vmware-base/src/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import javax.xml.transform.dom.DOMSource;
3838
import javax.xml.transform.stream.StreamResult;
3939

40+
import com.vmware.vim25.ClusterConfigInfoEx;
41+
import com.vmware.vim25.DatacenterConfigInfo;
4042
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
4143
import org.apache.commons.lang.StringUtils;
4244
import org.apache.log4j.Logger;
@@ -1515,6 +1517,11 @@ public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, St
15151517
videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
15161518

15171519
vmConfig.getDeviceChange().add(videoDeviceSpec);
1520+
1521+
ClusterMO clusterMo = new ClusterMO(host.getContext(), host.getHyperHostCluster());
1522+
DatacenterMO dataCenterMo = new DatacenterMO(host.getContext(), host.getHyperHostDatacenter());
1523+
setVMHardwareVersion(vmConfig, clusterMo, dataCenterMo);
1524+
15181525
if (host.createVm(vmConfig)) {
15191526
// Here, when attempting to find the VM, we need to use the name
15201527
// with which we created it. This is the only such place where
@@ -1543,6 +1550,28 @@ public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, St
15431550
return false;
15441551
}
15451552

1553+
/**
1554+
* Set the VM hardware version based on the information retrieved by the cluster and datacenter:
1555+
* - If the cluster hardware version is set, then it is set to this hardware version on vmConfig
1556+
* - If the cluster hardware version is not set, check datacenter hardware version. If it is set, then it is set to vmConfig
1557+
* - In case both cluster and datacenter hardware version are not set, hardware version is not set to vmConfig
1558+
*/
1559+
protected static void setVMHardwareVersion(VirtualMachineConfigSpec vmConfig, ClusterMO clusterMO, DatacenterMO datacenterMO) throws Exception {
1560+
ClusterConfigInfoEx clusterConfigInfo = clusterMO != null ? clusterMO.getClusterConfigInfo() : null;
1561+
String clusterHardwareVersion = clusterConfigInfo != null ? clusterConfigInfo.getDefaultHardwareVersionKey() : null;
1562+
if (StringUtils.isNotBlank(clusterHardwareVersion)) {
1563+
s_logger.debug("Cluster hardware version found: " + clusterHardwareVersion + ". Creating VM with this hardware version");
1564+
vmConfig.setVersion(clusterHardwareVersion);
1565+
} else {
1566+
DatacenterConfigInfo datacenterConfigInfo = datacenterMO != null ? datacenterMO.getDatacenterConfigInfo() : null;
1567+
String datacenterHardwareVersion = datacenterConfigInfo != null ? datacenterConfigInfo.getDefaultHardwareVersionKey() : null;
1568+
if (StringUtils.isNotBlank(datacenterHardwareVersion)) {
1569+
s_logger.debug("Datacenter hardware version found: " + datacenterHardwareVersion + ". Creating VM with this hardware version");
1570+
vmConfig.setVersion(datacenterHardwareVersion);
1571+
}
1572+
}
1573+
}
1574+
15461575
private static VirtualDeviceConfigSpec getControllerSpec(String diskController, int busNum) {
15471576
VirtualDeviceConfigSpec controllerSpec = new VirtualDeviceConfigSpec();
15481577
VirtualController controller = null;

vmware-base/test/com/cloud/hypervisor/vmware/mo/HypervisorHostHelperTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertNull;
2222
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.Matchers.any;
24+
import static org.mockito.Mockito.never;
2325
import static org.mockito.Mockito.verify;
2426
import static org.mockito.Mockito.verifyZeroInteractions;
2527
import static org.mockito.Mockito.when;
2628

2729
import java.util.HashMap;
2830
import java.util.Map;
2931

32+
import com.vmware.vim25.ClusterConfigInfoEx;
33+
import com.vmware.vim25.DatacenterConfigInfo;
34+
import com.vmware.vim25.VirtualMachineConfigSpec;
3035
import org.junit.After;
3136
import org.junit.AfterClass;
3237
import org.junit.Before;
@@ -62,6 +67,16 @@ public class HypervisorHostHelperTest {
6267
ServiceContent serviceContent;
6368
@Mock
6469
AboutInfo aboutInfo;
70+
@Mock
71+
private VirtualMachineConfigSpec vmSpec;
72+
@Mock
73+
private ClusterMO clusterMO;
74+
@Mock
75+
private DatacenterMO datacenterMO;
76+
@Mock
77+
private ClusterConfigInfoEx clusterConfigInfo;
78+
@Mock
79+
private DatacenterConfigInfo datacenterConfigInfo;
6580

6681
String vSwitchName;
6782
Integer networkRateMbps;
@@ -70,10 +85,12 @@ public class HypervisorHostHelperTest {
7085
String svlanId = null;
7186

7287
@Before
73-
public void setup() {
88+
public void setup() throws Exception {
7489
MockitoAnnotations.initMocks(this);
7590
when(context.getServiceContent()).thenReturn(serviceContent);
7691
when(serviceContent.getAbout()).thenReturn(aboutInfo);
92+
when(clusterMO.getClusterConfigInfo()).thenReturn(clusterConfigInfo);
93+
when(datacenterMO.getDatacenterConfigInfo()).thenReturn(datacenterConfigInfo);
7794
}
7895

7996
@BeforeClass
@@ -883,4 +900,29 @@ public void testCreateDVPortVlanSpecInvalidInputVlanRange() {
883900
assertTrue(((VmwareDistributedVirtualSwitchTrunkVlanSpec) spec).getVlanId().get(0).getStart() == 0);
884901
assertTrue(((VmwareDistributedVirtualSwitchTrunkVlanSpec) spec).getVlanId().get(0).getEnd() == 0);
885902
}
903+
904+
@Test
905+
public void testSetVMHardwareVersionClusterLevel() throws Exception {
906+
when(clusterConfigInfo.getDefaultHardwareVersionKey()).thenReturn("vmx-11");
907+
when(datacenterConfigInfo.getDefaultHardwareVersionKey()).thenReturn("vmx-9");
908+
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
909+
verify(vmSpec).setVersion("vmx-11");
910+
verify(vmSpec, never()).setVersion("vmx-9");
911+
}
912+
913+
@Test
914+
public void testSetVMHardwareVersionDatacenterLevel() throws Exception {
915+
when(clusterConfigInfo.getDefaultHardwareVersionKey()).thenReturn(null);
916+
when(datacenterConfigInfo.getDefaultHardwareVersionKey()).thenReturn("vmx-9");
917+
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
918+
verify(vmSpec).setVersion("vmx-9");
919+
}
920+
921+
@Test
922+
public void testSetVMHardwareVersionUnset() throws Exception {
923+
when(clusterConfigInfo.getDefaultHardwareVersionKey()).thenReturn(null);
924+
when(datacenterConfigInfo.getDefaultHardwareVersionKey()).thenReturn(null);
925+
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
926+
verify(vmSpec, never()).setVersion(any());
927+
}
886928
}

0 commit comments

Comments
 (0)