Skip to content

Commit e3a6bff

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

2 files changed

Lines changed: 30 additions & 29 deletions

File tree

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,34 +1230,35 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
12301230
}
12311231

12321232
/**
1233-
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.
1233+
Updates the instance details map with the current values for absent details. This only applies to details {@value VmDetailConstants#CPU_SPEED},
1234+
{@value VmDetailConstants#MEMORY}, and {@value VmDetailConstants#CPU_NUMBER}. This method only updates the map passed as parameter, not the database.
12341235
@param details Map containing the instance details.
1235-
@param vmInstance The virtual machine instance.
1236+
@param vmInstance The virtual machine instance to retrieve the current values.
12361237
@param newServiceOfferingId The ID of the new service offering.
12371238
*/
12381239

1239-
protected void updateInstanceDetails (Map<String, String> details, VirtualMachine vmInstance, Long newServiceOfferingId) {
1240+
protected void updateInstanceDetailsMapWithCurrentValuesForAbsentDetails(Map<String, String> details, VirtualMachine vmInstance, Long newServiceOfferingId) {
12401241
ServiceOfferingVO currentServiceOffering = serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
12411242
ServiceOfferingVO newServiceOffering = serviceOfferingDao.findById(newServiceOfferingId);
1242-
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getSpeed(), details, VmDetailConstants.CPU_SPEED, currentServiceOffering.getSpeed());
1243-
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getRamSize(), details, VmDetailConstants.MEMORY, currentServiceOffering.getRamSize());
1244-
updateInstanceDetailsKeepCurrentValueIfNull(newServiceOffering.getCpu(), details, VmDetailConstants.CPU_NUMBER, currentServiceOffering.getCpu());
1243+
addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(newServiceOffering.getSpeed(), details, VmDetailConstants.CPU_SPEED, currentServiceOffering.getSpeed());
1244+
addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(newServiceOffering.getRamSize(), details, VmDetailConstants.MEMORY, currentServiceOffering.getRamSize());
1245+
addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(newServiceOffering.getCpu(), details, VmDetailConstants.CPU_NUMBER, currentServiceOffering.getCpu());
12451246
}
12461247

12471248
/**
1248-
* Updates a specific instance detail with the current instance value if the new value is null.
1249+
* Adds the current detail value to the instance details map if a new value was not specified to it.
12491250
*
1250-
* @param newValue the new value to be set
1251-
* @param details a map of instance details
1252-
* @param detailsConstant the name of the detail constant to be updated
1253-
* @param currentValue the current value of the detail constant
1251+
* @param newValue the new value to be set.
1252+
* @param details a map of instance details.
1253+
* @param detailKey the detail to be updated.
1254+
* @param currentValue the current value of the detail constant.
12541255
*/
12551256

1256-
protected void updateInstanceDetailsKeepCurrentValueIfNull(Integer newValue, Map<String, String> details, String detailsConstant, Integer currentValue) {
1257-
if (newValue == null && details.get(detailsConstant) == null) {
1257+
protected void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(Integer newValue, Map<String, String> details, String detailKey, Integer currentValue) {
1258+
if (newValue == null && details.get(detailKey) == null) {
12581259
String currentValueString = String.valueOf(currentValue);
1259-
logger.debug("{} was not specified, keeping the current value: {}.", detailsConstant, currentValueString);
1260-
details.put(detailsConstant, currentValueString);
1260+
logger.debug("{} was not specified, keeping the current value: {}.", detailKey, currentValueString);
1261+
details.put(detailKey, currentValueString);
12611262
}
12621263
}
12631264

@@ -1930,7 +1931,7 @@ public UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableEx
19301931

19311932
Map<String, String> cmdDetails = cmd.getDetails();
19321933

1933-
updateInstanceDetails(cmdDetails, vm, newServiceOfferingId);
1934+
updateInstanceDetailsMapWithCurrentValuesForAbsentDetails(cmdDetails, vm, newServiceOfferingId);
19341935

19351936
boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmdDetails);
19361937
if (result) {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,11 +1455,11 @@ public void testRestoreVirtualMachineWithVMSnapshots() throws ResourceUnavailabl
14551455
}
14561456

14571457
@Test
1458-
public void updateInstanceDetailsKeepCurrentValueIfNullTestDetailsConstantIsNotNullDoNothing() {
1458+
public void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecifiedTestDetailsConstantIsNotNullDoNothing() {
14591459
int currentValue = 123;
14601460

14611461
for (String detailsConstant : detailsConstants) {
1462-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(null, customParameters, detailsConstant, currentValue);
1462+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(null, customParameters, detailsConstant, currentValue);
14631463
}
14641464

14651465
Assert.assertEquals(customParameters.get(VmDetailConstants.MEMORY), "2048");
@@ -1468,12 +1468,12 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestDetailsConstantIsNotN
14681468
}
14691469

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

14751475
for (String detailsConstant : detailsConstants) {
1476-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(321, details, detailsConstant, currentValue);
1476+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(321, details, detailsConstant, currentValue);
14771477
}
14781478

14791479
Assert.assertNull(details.get(VmDetailConstants.MEMORY));
@@ -1482,12 +1482,12 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestNewValueIsNotNullDoNo
14821482
}
14831483

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

14891489
for (String detailsConstant : detailsConstants) {
1490-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(null, details, detailsConstant, currentValue);
1490+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(null, details, detailsConstant, currentValue);
14911491
}
14921492

14931493
Assert.assertEquals(details.get(VmDetailConstants.MEMORY), String.valueOf(currentValue));
@@ -1496,11 +1496,11 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestBothValuesAreNullKeep
14961496
}
14971497

14981498
@Test
1499-
public void updateInstanceDetailsKeepCurrentValueIfNullTestNeitherValueIsNullDoNothing() {
1499+
public void addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecifiedTestNeitherValueIsNullDoNothing() {
15001500
int currentValue = 123;
15011501

15021502
for (String detailsConstant : detailsConstants) {
1503-
userVmManagerImpl.updateInstanceDetailsKeepCurrentValueIfNull(321, customParameters, detailsConstant, currentValue);
1503+
userVmManagerImpl.addCurrentDetailValueToInstanceDetailsMapIfNewValueWasNotSpecified(321, customParameters, detailsConstant, currentValue);
15041504
}
15051505

15061506
Assert.assertEquals(customParameters.get(VmDetailConstants.MEMORY), "2048");
@@ -1509,16 +1509,16 @@ public void updateInstanceDetailsKeepCurrentValueIfNullTestNeitherValueIsNullDoN
15091509
}
15101510

15111511
@Test
1512-
public void updateInstanceDetailsTestAllConstantsAreUpdated() {
1512+
public void updateInstanceDetailsMapWithCurrentValuesForAbsentDetailsTestAllConstantsAreUpdated() {
15131513
Mockito.doReturn(serviceOffering).when(_serviceOfferingDao).findById(Mockito.anyLong());
15141514
Mockito.doReturn(1L).when(vmInstanceMock).getId();
15151515
Mockito.doReturn(1L).when(vmInstanceMock).getServiceOfferingId();
15161516
Mockito.doReturn(serviceOffering).when(_serviceOfferingDao).findByIdIncludingRemoved(Mockito.anyLong(), Mockito.anyLong());
1517-
userVmManagerImpl.updateInstanceDetails(null, vmInstanceMock, 0l);
1517+
userVmManagerImpl.updateInstanceDetailsMapWithCurrentValuesForAbsentDetails(null, vmInstanceMock, 0l);
15181518

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

15241524
@Test

0 commit comments

Comments
 (0)