use of org.apache.cloudstack.api.BaseCmd.HTTPMethod in project cloudstack by apache.
the class UserVmManagerImpl method updateVirtualMachine.
@Override
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData, Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName, List<Long> securityGroupIdList, Map<String, Map<Integer, String>> extraDhcpOptionsMap) throws ResourceUnavailableException, InsufficientCapacityException {
UserVmVO vm = _vmDao.findById(id);
if (vm == null) {
throw new CloudRuntimeException("Unable to find virtual machine with id " + id);
}
if (instanceName != null) {
VMInstanceVO vmInstance = _vmInstanceDao.findVMByInstanceName(instanceName);
if (vmInstance != null && vmInstance.getId() != id) {
throw new CloudRuntimeException("Instance name : " + instanceName + " is not unique");
}
}
if (vm.getState() == State.Error || vm.getState() == State.Expunging) {
s_logger.error("vm is not in the right state: " + id);
throw new InvalidParameterValueException("Vm with id " + id + " is not in the right state");
}
if (displayName == null) {
displayName = vm.getDisplayName();
}
if (ha == null) {
ha = vm.isHaEnabled();
}
ServiceOffering offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
if (!offering.isOfferHA() && ha) {
throw new InvalidParameterValueException("Can't enable ha for the vm as it's created from the Service offering having HA disabled");
}
if (isDisplayVmEnabled == null) {
isDisplayVmEnabled = vm.isDisplayVm();
}
boolean updateUserdata = false;
if (userData != null) {
// check and replace newlines
userData = userData.replace("\\n", "");
userData = validateUserData(userData, httpMethod);
// update userData on domain router.
updateUserdata = true;
} else {
userData = vm.getUserData();
}
if (osTypeId == null) {
osTypeId = vm.getGuestOSId();
}
if (group != null) {
addInstanceToGroup(id, group);
}
if (isDynamicallyScalable == null) {
isDynamicallyScalable = vm.isDynamicallyScalable();
} else {
if (isDynamicallyScalable == true) {
VMTemplateVO template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId());
if (!template.isDynamicallyScalable()) {
throw new InvalidParameterValueException("Dynamic Scaling cannot be enabled for the VM since its template does not have dynamic scaling enabled");
}
if (!offering.isDynamicScalingEnabled()) {
throw new InvalidParameterValueException("Dynamic Scaling cannot be enabled for the VM since its service offering does not have dynamic scaling enabled");
}
if (!UserVmManager.EnableDynamicallyScaleVm.valueIn(vm.getDataCenterId())) {
s_logger.debug(String.format("Dynamic Scaling cannot be enabled for the VM %s since the global setting enable.dynamic.scale.vm is set to false", vm.getUuid()));
throw new InvalidParameterValueException("Dynamic Scaling cannot be enabled for the VM since corresponding global setting is set to false");
}
}
}
boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
if (securityGroupIdList != null && isVMware) {
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
} else {
// Get default guest network in Basic zone
Network defaultNetwork = null;
try {
DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
if (zone.getNetworkType() == NetworkType.Basic) {
// Get default guest network in Basic zone
defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
} else if (zone.isSecurityGroupEnabled()) {
NicVO defaultNic = _nicDao.findDefaultNicForVM(vm.getId());
if (defaultNic != null) {
defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
}
}
} catch (InvalidParameterValueException e) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(e.getMessage(), e);
}
defaultNetwork = _networkModel.getDefaultNetworkForVm(id);
}
if (securityGroupIdList != null && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
if (vm.getState() == State.Stopped) {
// Remove instance from security groups
_securityGroupMgr.removeInstanceFromGroups(id);
// Add instance in provided groups
_securityGroupMgr.addInstanceToGroups(id, securityGroupIdList);
} else {
throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
}
}
}
List<? extends Nic> nics = _nicDao.listByVmId(vm.getId());
if (hostName != null) {
// Check is hostName is RFC compliant
checkNameForRFCCompliance(hostName);
if (vm.getHostName().equalsIgnoreCase(hostName)) {
s_logger.debug("Vm " + vm + " is already set with the hostName specified: " + hostName);
hostName = null;
}
// Verify that vm's hostName is unique
List<NetworkVO> vmNtwks = new ArrayList<NetworkVO>(nics.size());
for (Nic nic : nics) {
vmNtwks.add(_networkDao.findById(nic.getNetworkId()));
}
checkIfHostNameUniqueInNtwkDomain(hostName, vmNtwks);
}
List<NetworkVO> networks = nics.stream().map(nic -> _networkDao.findById(nic.getNetworkId())).collect(Collectors.toList());
verifyExtraDhcpOptionsNetwork(extraDhcpOptionsMap, networks);
for (Nic nic : nics) {
_networkMgr.saveExtraDhcpOptions(networks.stream().filter(network -> network.getId() == nic.getNetworkId()).findFirst().get().getUuid(), nic.getId(), extraDhcpOptionsMap);
}
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName);
if (updateUserdata) {
updateUserData(vm);
}
if (State.Running == vm.getState()) {
updateDns(vm, hostName);
}
return _vmDao.findById(id);
}
Aggregations