Skip to content

Commit 8f6721e

Browse files
authored
Improve some UserVmManagerImpl's methods name and docs (#8673)
Co-authored-by: Daniel Augusto Veronezi Salvador <gutoveronezi@apache.org>
1 parent 592038a commit 8f6721e

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -1217,34 +1217,35 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
12171217
}
12181218

12191219
/**
1220-
Updates the instance details map with the current values of the instance for the CPU speed, memory, and CPU number if they have not been specified.
1220+
Updates the instance details map with the current values for absent details. This only applies to details {@value VmDetailConstants#CPU_SPEED},
1221+
{@value VmDetailConstants#MEMORY}, and {@value VmDetailConstants#CPU_NUMBER}. This method only updates the map passed as parameter, not the database.
12211222
@param details Map containing the instance details.
1222-
@param vmInstance The virtual machine instance.
1223+
@param vmInstance The virtual machine instance to retrieve the current values.
12231224
@param newServiceOfferingId The ID of the new service offering.
12241225
*/
12251226

1226-
protected void updateInstanceDetails (Map<String, String> details, VirtualMachine vmInstance, Long newServiceOfferingId) {
1227+
protected void updateInstanceDetailsMapWithCurrentValuesForAbsentDetails(Map<String, String> details, VirtualMachine vmInstance, Long newServiceOfferingId) {
12271228
ServiceOfferingVO currentServiceOffering = serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
12281229
ServiceOfferingVO newServiceOffering = serviceOfferingDao.findById(newServiceOfferingId);
1229-
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getSpeed(), details, VmDetailConstants.CPU_SPEED, currentServiceOffering.getSpeed());
1230-
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getRamSize(), details, VmDetailConstants.MEMORY, currentServiceOffering.getRamSize());
1231-
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getCpu(), details, VmDetailConstants.CPU_NUMBER, currentServiceOffering.getCpu());
1230+
addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(newServiceOffering.getSpeed(), details, VmDetailConstants.CPU_SPEED, currentServiceOffering.getSpeed());
1231+
addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(newServiceOffering.getRamSize(), details, VmDetailConstants.MEMORY, currentServiceOffering.getRamSize());
1232+
addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(newServiceOffering.getCpu(), details, VmDetailConstants.CPU_NUMBER, currentServiceOffering.getCpu());
12321233
}
12331234

12341235
/**
1235-
* Updates a specific instance detail with the current instance value if the new value is null.
1236+
* Adds the current detail value to the instance details map if a new value was not specified to it.
12361237
*
1237-
* @param newValue the new value to be set
1238-
* @param details a map of instance details
1239-
* @param detailsConstant the name of the detail constant to be updated
1240-
* @param currentValue the current value of the detail constant
1238+
* @param newValue the new value to be set.
1239+
* @param details a map of instance details.
1240+
* @param detailKey the detail to be updated.
1241+
* @param currentValue the current value of the detail constant.
12411242
*/
12421243

1243-
protected void updateInstanceDetailsKeepCurrentValueIfNull(Integer newValue, Map<String, String> details, String detailsConstant, Integer currentValue) {
1244-
if (newValue == null && details.get(detailsConstant) == null) {
1244+
protected void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(Integer newValue, Map<String, String> details, String detailKey, Integer currentValue) {
1245+
if (newValue == null && details.get(detailKey) == null) {
12451246
String currentValueString = String.valueOf(currentValue);
1246-
logger.debug("{} was not specified, keeping the current value: {}.", detailsConstant, currentValueString);
1247-
details.put(detailsConstant, currentValueString);
1247+
logger.debug("{} was not specified, keeping the current value: {}.", detailKey, currentValueString);
1248+
details.put(detailKey, currentValueString);
12481249
}
12491250
}
12501251

@@ -1917,7 +1918,7 @@ public UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableEx
19171918

19181919
Map<String, String> cmdDetails = cmd.getDetails();
19191920

1920-
updateInstanceDetails(cmdDetails, vm, newServiceOfferingId);
1921+
updateInstanceDetailsMapWithCurrentValuesForAbsentDetails(cmdDetails, vm, newServiceOfferingId);
19211922

19221923
boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmdDetails);
19231924
if (result) {

server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -1454,11 +1454,11 @@ public void testRestoreVirtualMachineWithVMSnapshots() throws ResourceUnavailabl
14541454
}
14551455

14561456
@Test
1457-
public void updateInstanceDetailsKeepCurrentValueIfNullTestDetailsConstantIsNotNullDoNothing() {
1457+
public void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecifiedTestDetailsConstantIsNotNullDoNothing() {
14581458
int currentValue = 123;
14591459

14601460
for (String detailsConstant : detailsConstants) {
1461-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(null, customParameters, detailsConstant, currentValue);
1461+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(null, customParameters, detailsConstant, currentValue);
14621462
}
14631463

14641464
Assert.assertEquals(customParameters.get(VmDetailConstants.MEMORY), "2048");
@@ -1467,12 +1467,12 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestDetailsConstantIsNotN
14671467
}
14681468

14691469
@Test
1470-
public void updateInstanceDetailsKeepCurrentValueIfNullTestNewValueIsNotNullDoNothing() {
1470+
public void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecifiedTestNewValueIsNotNullDoNothing() {
14711471
Map<String, String> details = new HashMap<>();
14721472
int currentValue = 123;
14731473

14741474
for (String detailsConstant : detailsConstants) {
1475-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(321, details, detailsConstant, currentValue);
1475+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(321, details, detailsConstant, currentValue);
14761476
}
14771477

14781478
Assert.assertNull(details.get(VmDetailConstants.MEMORY));
@@ -1481,12 +1481,12 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestNewValueIsNotNullDoNo
14811481
}
14821482

14831483
@Test
1484-
public void updateInstanceDetailsKeepCurrentValueIfNullTestBothValuesAreNullKeepCurrentValue() {
1484+
public void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecifiedTestBothValuesAreNullKeepCurrentValue() {
14851485
Map<String, String> details = new HashMap<>();
14861486
int currentValue = 123;
14871487

14881488
for (String detailsConstant : detailsConstants) {
1489-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(null, details, detailsConstant, currentValue);
1489+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(null, details, detailsConstant, currentValue);
14901490
}
14911491

14921492
Assert.assertEquals(details.get(VmDetailConstants.MEMORY), String.valueOf(currentValue));
@@ -1495,11 +1495,11 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestBothValuesAreNullKeep
14951495
}
14961496

14971497
@Test
1498-
public void updateInstanceDetailsKeepCurrentValueIfNullTestNeitherValueIsNullDoNothing() {
1498+
public void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecifiedTestNeitherValueIsNullDoNothing() {
14991499
int currentValue = 123;
15001500

15011501
for (String detailsConstant : detailsConstants) {
1502-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(321, customParameters, detailsConstant, currentValue);
1502+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(321, customParameters, detailsConstant, currentValue);
15031503
}
15041504

15051505
Assert.assertEquals(customParameters.get(VmDetailConstants.MEMORY), "2048");
@@ -1508,16 +1508,16 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestNeitherValueIsNullDoN
15081508
}
15091509

15101510
@Test
1511-
public void updateInstanceDetailsTestAllConstantsAreUpdated() {
1511+
public void updateInstanceDetailsMapWithCurrentValuesForAbsentDetailsTestAllConstantsAreUpdated() {
15121512
Mockito.doReturn(serviceOffering).when(_serviceOfferingDao).findById(Mockito.anyLong());
15131513
Mockito.doReturn(1L).when(vmInstanceMock).getId();
15141514
Mockito.doReturn(1L).when(vmInstanceMock).getServiceOfferingId();
15151515
Mockito.doReturn(serviceOffering).when(_serviceOfferingDao).findByIdIncludingRemoved(Mockito.anyLong(), Mockito.anyLong());
1516-
userVmManagerImpl.updateInstanceDetails(null, vmInstanceMock, 0l);
1516+
userVmManagerImpl.updateInstanceDetailsMapWithCurrentValuesForAbsentDetails(null, vmInstanceMock, 0l);
15171517

1518-
Mockito.verify(userVmManagerImpl).updateInstanceDetailsKeepCurrentValueIfNull(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.CPU_SPEED), Mockito.any());
1519-
Mockito.verify(userVmManagerImpl).updateInstanceDetailsKeepCurrentValueIfNull(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.MEMORY), Mockito.any());
1520-
Mockito.verify(userVmManagerImpl).updateInstanceDetailsKeepCurrentValueIfNull(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.CPU_NUMBER), Mockito.any());
1518+
Mockito.verify(userVmManagerImpl).addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.CPU_SPEED), Mockito.any());
1519+
Mockito.verify(userVmManagerImpl).addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.MEMORY), Mockito.any());
1520+
Mockito.verify(userVmManagerImpl).addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(Mockito.any(), Mockito.any(), Mockito.eq(VmDetailConstants.CPU_NUMBER), Mockito.any());
15211521
}
15221522

15231523
@Test

0 commit comments

Comments
 (0)