mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Fix NPE on updating security groups for an instance (#10493)
* Fix NPE on updating security groups for an instance * addressed review comments * Method refactoring --------- Co-authored-by: Harikrishna Patnala <harikrishna.patnala@gmail.com>
This commit is contained in:
parent
0da243d660
commit
45bac89b83
@ -28,7 +28,6 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@ -3105,42 +3104,6 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
}
|
||||
}
|
||||
|
||||
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 (_networkModel.checkSecurityGroupSupportForNetwork(_accountMgr.getActiveAccountById(vm.getAccountId()), zone, Collections.emptyList(), securityGroupIdList)) {
|
||||
NicVO defaultNic = _nicDao.findDefaultNicForVM(vm.getId());
|
||||
if (defaultNic != null) {
|
||||
defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
|
||||
}
|
||||
}
|
||||
} catch (InvalidParameterValueException e) {
|
||||
if(logger.isDebugEnabled()) {
|
||||
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(vm);
|
||||
// Add instance in provided groups
|
||||
_securityGroupMgr.addInstanceToGroups(vm, 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
|
||||
@ -3173,6 +3136,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
.getUuid(), nic.getId(), extraDhcpOptionsMap);
|
||||
}
|
||||
|
||||
checkAndUpdateSecurityGroupForVM(securityGroupIdList, vm, networks);
|
||||
|
||||
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, userDataId,
|
||||
userDataDetails, isDisplayVmEnabled, isDynamicallyScalable,
|
||||
deleteProtection, customId, hostName, instanceName);
|
||||
@ -3188,6 +3153,48 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
return _vmDao.findById(id);
|
||||
}
|
||||
|
||||
private void checkAndUpdateSecurityGroupForVM(List<Long> securityGroupIdList, UserVmVO vm, List<NetworkVO> networks) {
|
||||
boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
|
||||
|
||||
if (securityGroupIdList != null && isVMware) {
|
||||
throw new InvalidParameterValueException("Security group feature is not supported for VMware hypervisor");
|
||||
} else if (securityGroupIdList != null) {
|
||||
DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
|
||||
List<Long> networkIds = new ArrayList<>();
|
||||
try {
|
||||
if (zone.getNetworkType() == NetworkType.Basic) {
|
||||
// Get default guest network in Basic zone
|
||||
Network defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
|
||||
networkIds.add(defaultNetwork.getId());
|
||||
} else {
|
||||
networkIds = networks.stream().map(Network::getId).collect(Collectors.toList());
|
||||
}
|
||||
} catch (InvalidParameterValueException e) {
|
||||
if(logger.isDebugEnabled()) {
|
||||
logger.debug(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
if (_networkModel.checkSecurityGroupSupportForNetwork(
|
||||
_accountMgr.getActiveAccountById(vm.getAccountId()),
|
||||
zone, networkIds, securityGroupIdList)
|
||||
) {
|
||||
updateSecurityGroup(vm, securityGroupIdList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSecurityGroup(UserVmVO vm, List<Long> securityGroupIdList) {
|
||||
if (vm.getState() == State.Stopped) {
|
||||
// Remove instance from security groups
|
||||
_securityGroupMgr.removeInstanceFromGroups(vm);
|
||||
// Add instance in provided groups
|
||||
_securityGroupMgr.addInstanceToGroups(vm, securityGroupIdList);
|
||||
} else {
|
||||
throw new InvalidParameterValueException(String.format("VM %s must be stopped prior to update security groups", vm.getUuid()));
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateUserData(UserVm vm) throws ResourceUnavailableException, InsufficientCapacityException {
|
||||
boolean result = updateUserDataInternal(vm);
|
||||
if (result) {
|
||||
@ -3695,7 +3702,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
boolean isVmWare = (template.getHypervisorType() == HypervisorType.VMware || (hypervisor != null && hypervisor == HypervisorType.VMware));
|
||||
|
||||
if (securityGroupIdList != null && isVmWare) {
|
||||
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
|
||||
throw new InvalidParameterValueException("Security group feature is not supported for VMware hypervisor");
|
||||
} else if (!isVmWare && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
|
||||
//add the default securityGroup only if no security group is specified
|
||||
if (securityGroupIdList == null || securityGroupIdList.isEmpty()) {
|
||||
@ -3755,7 +3762,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
|
||||
} else if (securityGroupIdList != null && !securityGroupIdList.isEmpty()) {
|
||||
if (isVmWare) {
|
||||
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
|
||||
throw new InvalidParameterValueException("Security group feature is not supported for VMware hypervisor");
|
||||
}
|
||||
// Only one network can be specified, and it should be security group enabled
|
||||
if (networkIdList.size() > 1 && template.getHypervisorType() != HypervisorType.KVM && hypervisor != HypervisorType.KVM) {
|
||||
|
||||
@ -206,7 +206,7 @@ export default {
|
||||
zoneid: this.resource.zoneid
|
||||
}).then(response => {
|
||||
const zone = response?.listzonesresponse?.zone || []
|
||||
this.securityGroupsEnabled = zone?.[0]?.securitygroupsenabled
|
||||
this.securityGroupsEnabled = zone?.[0]?.securitygroupsenabled || this.$store.getters.showSecurityGroups
|
||||
})
|
||||
},
|
||||
fetchSecurityGroups () {
|
||||
|
||||
@ -179,6 +179,7 @@ export default {
|
||||
vm: {},
|
||||
totalStorage: 0,
|
||||
currentTab: 'details',
|
||||
showUpdateSecurityGroupsModal: false,
|
||||
showAddVolumeModal: false,
|
||||
diskOfferings: [],
|
||||
annotations: [],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user