use of org.apache.cloudstack.api.command.user.vm.UpdateVMCmd in project cloudstack by apache.
the class UserVmManagerImpl method updateVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_UPDATE, eventDescription = "updating Vm")
public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException {
validateInputsAndPermissionForUpdateVirtualMachineCommand(cmd);
String displayName = cmd.getDisplayName();
String group = cmd.getGroup();
Boolean ha = cmd.getHaEnable();
Boolean isDisplayVm = cmd.getDisplayVm();
Long id = cmd.getId();
Long osTypeId = cmd.getOsTypeId();
String userData = cmd.getUserData();
Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
String hostName = cmd.getHostName();
Map<String, String> details = cmd.getDetails();
List<Long> securityGroupIdList = getSecurityGroupIdList(cmd);
boolean cleanupDetails = cmd.isCleanupDetails();
String extraConfig = cmd.getExtraConfig();
UserVmVO vmInstance = _vmDao.findById(cmd.getId());
if (MapUtils.isNotEmpty(details) || cmd.isCleanupDetails()) {
VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId());
if (template != null && template.isDeployAsIs()) {
throw new CloudRuntimeException("Detail settings are read from OVA, it cannot be changed by API call.");
}
}
long accountId = vmInstance.getAccountId();
if (isDisplayVm != null && isDisplayVm != vmInstance.isDisplay()) {
updateDisplayVmFlag(isDisplayVm, id, vmInstance);
}
final Account caller = CallContext.current().getCallingAccount();
final List<String> userDenyListedSettings = Stream.of(QueryService.UserVMDeniedDetails.value().split(",")).map(item -> (item).trim()).collect(Collectors.toList());
final List<String> userReadOnlySettings = Stream.of(QueryService.UserVMReadOnlyDetails.value().split(",")).map(item -> (item).trim()).collect(Collectors.toList());
if (cleanupDetails) {
if (caller != null && caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
userVmDetailsDao.removeDetails(id);
} else {
for (final UserVmDetailVO detail : userVmDetailsDao.listDetails(id)) {
if (detail != null && !userDenyListedSettings.contains(detail.getName()) && !userReadOnlySettings.contains(detail.getName())) {
userVmDetailsDao.removeDetail(id, detail.getName());
}
}
}
} else {
if (MapUtils.isNotEmpty(details)) {
if (details.containsKey("extraconfig")) {
throw new InvalidParameterValueException("'extraconfig' should not be included in details as key");
}
if (caller != null && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
// Ensure denied or read-only detail is not passed by non-root-admin user
for (final String detailName : details.keySet()) {
if (userDenyListedSettings.contains(detailName)) {
throw new InvalidParameterValueException("You're not allowed to add or edit the restricted setting: " + detailName);
}
if (userReadOnlySettings.contains(detailName)) {
throw new InvalidParameterValueException("You're not allowed to add or edit the read-only setting: " + detailName);
}
}
// Add any hidden/denied or read-only detail
for (final UserVmDetailVO detail : userVmDetailsDao.listDetails(id)) {
if (userDenyListedSettings.contains(detail.getName()) || userReadOnlySettings.contains(detail.getName())) {
details.put(detail.getName(), detail.getValue());
}
}
}
verifyVmLimits(vmInstance, details);
vmInstance.setDetails(details);
_vmDao.saveDetails(vmInstance);
}
if (StringUtils.isNotBlank(extraConfig)) {
if (EnableAdditionalVmConfig.valueIn(accountId)) {
s_logger.info("Adding extra configuration to user vm: " + vmInstance.getUuid());
addExtraConfig(vmInstance, extraConfig);
} else {
throw new InvalidParameterValueException("attempted setting extraconfig but enable.additional.vm.configuration is disabled");
}
}
}
return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod(), cmd.getCustomId(), hostName, cmd.getInstanceName(), securityGroupIdList, cmd.getDhcpOptionsMap());
}
Aggregations