Skip to content

Commit 745a7b3

Browse files
authored
[4.9.1] Explicitly controlling VMware VM hardware version (#18)
* Explicitly controlling VMware VM hardware version * Fix unit test
1 parent 32b5062 commit 745a7b3

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public ClusterDasConfigInfo getDasConfig() throws Exception {
8282
return configInfo.getDasConfig();
8383
}
8484

85+
public ClusterConfigInfoEx getClusterConfigInfo() throws Exception {
86+
ClusterConfigInfoEx configInfo = (ClusterConfigInfoEx)_context.getVimClient().getDynamicProperty(_mor, "configurationEx");
87+
return configInfo;
88+
}
89+
8590
@Override
8691
public ManagedObjectReference getHyperHostDatacenter() throws Exception {
8792
Pair<DatacenterMO, String> dcPair = DatacenterMO.getOwnerDatacenter(getContext(), getMor());

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28+
import com.cloud.utils.StringUtils;
2829
import org.apache.log4j.Logger;
2930

3031
import com.cloud.exception.CloudException;
@@ -46,7 +47,9 @@
4647
import com.cloud.utils.nicira.nvp.plugin.NiciraNvpApiVersion;
4748
import com.vmware.vim25.AlreadyExistsFaultMsg;
4849
import com.vmware.vim25.BoolPolicy;
50+
import com.vmware.vim25.ClusterConfigInfoEx;
4951
import com.vmware.vim25.CustomFieldStringValue;
52+
import com.vmware.vim25.DatacenterConfigInfo;
5053
import com.vmware.vim25.DVPortSetting;
5154
import com.vmware.vim25.DVPortgroupConfigInfo;
5255
import com.vmware.vim25.DVPortgroupConfigSpec;
@@ -1321,6 +1324,11 @@ public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, St
13211324
videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
13221325

13231326
vmConfig.getDeviceChange().add(videoDeviceSpec);
1327+
1328+
ClusterMO clusterMo = new ClusterMO(host.getContext(), host.getHyperHostCluster());
1329+
DatacenterMO dataCenterMo = new DatacenterMO(host.getContext(), host.getHyperHostDatacenter());
1330+
setVMHardwareVersion(vmConfig, clusterMo, dataCenterMo);
1331+
13241332
if (host.createVm(vmConfig)) {
13251333
// Here, when attempting to find the VM, we need to use the name
13261334
// with which we created it. This is the only such place where
@@ -1349,6 +1357,28 @@ public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, St
13491357
return false;
13501358
}
13511359

1360+
/**
1361+
* Set the VM hardware version based on the information retrieved by the cluster and datacenter:
1362+
* - If the cluster hardware version is set, then it is set to this hardware version on vmConfig
1363+
* - If the cluster hardware version is not set, check datacenter hardware version. If it is set, then it is set to vmConfig
1364+
* - In case both cluster and datacenter hardware version are not set, hardware version is not set to vmConfig
1365+
*/
1366+
protected static void setVMHardwareVersion(VirtualMachineConfigSpec vmConfig, ClusterMO clusterMO, DatacenterMO datacenterMO) throws Exception {
1367+
ClusterConfigInfoEx clusterConfigInfo = clusterMO != null ? clusterMO.getClusterConfigInfo() : null;
1368+
String clusterHardwareVersion = clusterConfigInfo != null ? clusterConfigInfo.getDefaultHardwareVersionKey() : null;
1369+
if (StringUtils.isNotBlank(clusterHardwareVersion)) {
1370+
s_logger.debug("Cluster hardware version found: " + clusterHardwareVersion + ". Creating VM with this hardware version");
1371+
vmConfig.setVersion(clusterHardwareVersion);
1372+
} else {
1373+
DatacenterConfigInfo datacenterConfigInfo = datacenterMO != null ? datacenterMO.getDatacenterConfigInfo() : null;
1374+
String datacenterHardwareVersion = datacenterConfigInfo != null ? datacenterConfigInfo.getDefaultHardwareVersionKey() : null;
1375+
if (StringUtils.isNotBlank(datacenterHardwareVersion)) {
1376+
s_logger.debug("Datacenter hardware version found: " + datacenterHardwareVersion + ". Creating VM with this hardware version");
1377+
vmConfig.setVersion(datacenterHardwareVersion);
1378+
}
1379+
}
1380+
}
1381+
13521382
private static VirtualDeviceConfigSpec getControllerSpec(String diskController, int busNum) {
13531383
VirtualDeviceConfigSpec controllerSpec = new VirtualDeviceConfigSpec();
13541384
VirtualController controller = null;

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertTrue;
2222
import static org.junit.Assert.assertNull;
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;
@@ -34,11 +36,14 @@
3436

3537
import com.vmware.vim25.AboutInfo;
3638
import com.vmware.vim25.BoolPolicy;
39+
import com.vmware.vim25.ClusterConfigInfoEx;
40+
import com.vmware.vim25.DatacenterConfigInfo;
3741
import com.vmware.vim25.DVPortgroupConfigInfo;
3842
import com.vmware.vim25.DVPortgroupConfigSpec;
3943
import com.vmware.vim25.DVSTrafficShapingPolicy;
4044
import com.vmware.vim25.LongPolicy;
4145
import com.vmware.vim25.ServiceContent;
46+
import com.vmware.vim25.VirtualMachineConfigSpec;
4247
import com.vmware.vim25.VMwareDVSPortSetting;
4348
import com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec;
4449

@@ -55,6 +60,16 @@ public class HypervisorHostHelperTest {
5560
ServiceContent serviceContent;
5661
@Mock
5762
AboutInfo aboutInfo;
63+
@Mock
64+
private VirtualMachineConfigSpec vmSpec;
65+
@Mock
66+
private ClusterMO clusterMO;
67+
@Mock
68+
private DatacenterMO datacenterMO;
69+
@Mock
70+
private ClusterConfigInfoEx clusterConfigInfo;
71+
@Mock
72+
private DatacenterConfigInfo datacenterConfigInfo;
5873

5974
String vSwitchName;
6075
Integer networkRateMbps;
@@ -79,6 +94,8 @@ public static void tearDownAfterClass() throws Exception {
7994

8095
@Before
8196
public void setUp() throws Exception {
97+
when(clusterMO.getClusterConfigInfo()).thenReturn(clusterConfigInfo);
98+
when(datacenterMO.getDatacenterConfigInfo()).thenReturn(datacenterConfigInfo);
8299
}
83100

84101
@After
@@ -557,4 +574,29 @@ public void testComposeCloudNetworkNameUnTaggedVlanGuestTraffic() throws Excepti
557574
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName);
558575
assertEquals("cloud.guest.400.s123.512.1-vSwitch2", cloudNetworkName);
559576
}
577+
578+
@Test
579+
public void testSetVMHardwareVersionClusterLevel() throws Exception {
580+
when(clusterConfigInfo.getDefaultHardwareVersionKey()).thenReturn("vmx-11");
581+
when(datacenterConfigInfo.getDefaultHardwareVersionKey()).thenReturn("vmx-9");
582+
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
583+
verify(vmSpec).setVersion("vmx-11");
584+
verify(vmSpec, never()).setVersion("vmx-9");
585+
}
586+
587+
@Test
588+
public void testSetVMHardwareVersionDatacenterLevel() throws Exception {
589+
when(clusterConfigInfo.getDefaultHardwareVersionKey()).thenReturn(null);
590+
when(datacenterConfigInfo.getDefaultHardwareVersionKey()).thenReturn("vmx-9");
591+
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
592+
verify(vmSpec).setVersion("vmx-9");
593+
}
594+
595+
@Test
596+
public void testSetVMHardwareVersionUnset() throws Exception {
597+
when(clusterConfigInfo.getDefaultHardwareVersionKey()).thenReturn(null);
598+
when(datacenterConfigInfo.getDefaultHardwareVersionKey()).thenReturn(null);
599+
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
600+
verify(vmSpec, never()).setVersion(any(String.class));
601+
}
560602
}

0 commit comments

Comments
 (0)