Skip to content

Commit b253d95

Browse files
author
Alena Prokharchyk
committed
UpdateVirtualMachine: code cleanup - got rid of warnings and unused code from the method; extracted part of the code to its separate method.
1 parent 1a23d6e commit b253d95

2 files changed

Lines changed: 41 additions & 46 deletions

File tree

‎server/src/com/cloud/vm/UserVmManager.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222

23+
import org.apache.cloudstack.api.BaseCmd.HTTPMethod;
2324
import org.apache.cloudstack.framework.config.ConfigKey;
2425

2526
import com.cloud.agent.api.VmDiskStatsEntry;
@@ -110,4 +111,7 @@ public interface UserVmManager extends UserVmService {
110111
boolean setupVmForPvlan(boolean add, Long hostId, NicProfile nic);
111112

112113
void collectVmDiskStatistics (UserVmVO userVm);
114+
115+
UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData,
116+
Boolean isDynamicallyScalable, HTTPMethod httpMethod)throws ResourceUnavailableException, InsufficientCapacityException;
113117
}

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

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,43 +1768,29 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd)
17681768
Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
17691769
Account caller = CallContext.current().getCallingAccount();
17701770

1771-
// Input validation
1772-
UserVmVO vmInstance = null;
1773-
1774-
// Verify input parameters
1775-
vmInstance = _vmDao.findById(id.longValue());
1776-
1771+
// Input validation and permission checks
1772+
UserVmVO vmInstance = _vmDao.findById(id.longValue());
17771773
if (vmInstance == null) {
17781774
throw new InvalidParameterValueException(
17791775
"unable to find virtual machine with id " + id);
17801776
}
17811777

1782-
ServiceOffering offering = _serviceOfferingDao.findById(vmInstance
1783-
.getServiceOfferingId());
1784-
if (!offering.getOfferHA() && ha != null && ha) {
1785-
throw new InvalidParameterValueException(
1786-
"Can't enable ha for the vm as it's created from the Service offering having HA disabled");
1787-
}
1788-
17891778
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true,
17901779
vmInstance);
1791-
1792-
if (displayName == null) {
1793-
displayName = vmInstance.getDisplayName();
1794-
}
1795-
1796-
if (ha == null) {
1797-
ha = vmInstance.isHaEnabled();
1798-
}
1799-
1800-
if (isDisplayVmEnabled == null) {
1801-
isDisplayVmEnabled = vmInstance.isDisplayVm();
1802-
} else{
1780+
1781+
if (isDisplayVmEnabled != null) {
18031782
if(!_accountMgr.isRootAdmin(caller.getType())){
18041783
throw new PermissionDeniedException( "Cannot update parameter displayvm, only admin permitted ");
18051784
}
18061785
}
18071786

1787+
return updateVirtualMachine(id, displayName, group, ha, isDisplayVmEnabled, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod());
1788+
}
1789+
1790+
@Override
1791+
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha,
1792+
Boolean isDisplayVmEnabled, Long osTypeId, String userData, Boolean isDynamicallyScalable, HTTPMethod httpMethod)
1793+
throws ResourceUnavailableException, InsufficientCapacityException {
18081794
UserVmVO vm = _vmDao.findById(id);
18091795
if (vm == null) {
18101796
throw new CloudRuntimeException(
@@ -1816,45 +1802,50 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd)
18161802
throw new InvalidParameterValueException("Vm with id " + id
18171803
+ " is not in the right state");
18181804
}
1805+
1806+
if (displayName == null) {
1807+
displayName = vm.getDisplayName();
1808+
}
1809+
1810+
if (ha == null) {
1811+
ha = vm.isHaEnabled();
1812+
}
1813+
1814+
ServiceOffering offering = _serviceOfferingDao.findById(vm.getServiceOfferingId());
1815+
if (!offering.getOfferHA() && ha) {
1816+
throw new InvalidParameterValueException(
1817+
"Can't enable ha for the vm as it's created from the Service offering having HA disabled");
1818+
}
1819+
1820+
if (isDisplayVmEnabled == null) {
1821+
isDisplayVmEnabled = vm.isDisplayVm();
1822+
}
18191823

18201824
boolean updateUserdata = false;
18211825
if (userData != null) {
18221826
// check and replace newlines
18231827
userData = userData.replace("\\n", "");
1824-
validateUserData(userData, cmd.getHttpMethod());
1828+
validateUserData(userData, httpMethod);
18251829
// update userData on domain router.
18261830
updateUserdata = true;
18271831
} else {
1828-
userData = vmInstance.getUserData();
1829-
}
1830-
1831-
String description = "";
1832-
1833-
if (displayName != null && !displayName.equals(vmInstance.getDisplayName())) {
1834-
description += "New display name: " + displayName + ". ";
1832+
userData = vm.getUserData();
18351833
}
18361834

1837-
if (ha != vmInstance.isHaEnabled()) {
1838-
if (ha) {
1839-
description += "Enabled HA. ";
1840-
} else {
1841-
description += "Disabled HA. ";
1842-
}
1835+
if (isDynamicallyScalable == null) {
1836+
isDynamicallyScalable = vm.isDynamicallyScalable();
18431837
}
1838+
18441839
if (osTypeId == null) {
1845-
osTypeId = vmInstance.getGuestOSId();
1846-
} else {
1847-
description += "Changed Guest OS Type to " + osTypeId + ". ";
1840+
osTypeId = vm.getGuestOSId();
18481841
}
18491842

18501843
if (group != null) {
1851-
if (addInstanceToGroup(id, group)) {
1852-
description += "Added to group: " + group + ".";
1853-
}
1844+
addInstanceToGroup(id, group);
18541845
}
18551846

18561847
if (isDynamicallyScalable == null) {
1857-
isDynamicallyScalable = vmInstance.isDynamicallyScalable();
1848+
isDynamicallyScalable = vm.isDynamicallyScalable();
18581849
}
18591850

18601851
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable);

0 commit comments

Comments
 (0)