Skip to content

Commit 1a1eec2

Browse files
committed
Fix NPE on updating security groups for an instance
1 parent b92fd17 commit 1a1eec2

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,42 +3105,6 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31053105
}
31063106
}
31073107

3108-
boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
3109-
3110-
if (securityGroupIdList != null && isVMware) {
3111-
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3112-
} else {
3113-
// Get default guest network in Basic zone
3114-
Network defaultNetwork = null;
3115-
try {
3116-
DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
3117-
if (zone.getNetworkType() == NetworkType.Basic) {
3118-
// Get default guest network in Basic zone
3119-
defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
3120-
} else if (_networkModel.checkSecurityGroupSupportForNetwork(_accountMgr.getActiveAccountById(vm.getAccountId()), zone, Collections.emptyList(), securityGroupIdList)) {
3121-
NicVO defaultNic = _nicDao.findDefaultNicForVM(vm.getId());
3122-
if (defaultNic != null) {
3123-
defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
3124-
}
3125-
}
3126-
} catch (InvalidParameterValueException e) {
3127-
if(logger.isDebugEnabled()) {
3128-
logger.debug(e.getMessage(),e);
3129-
}
3130-
defaultNetwork = _networkModel.getDefaultNetworkForVm(id);
3131-
}
3132-
3133-
if (securityGroupIdList != null && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
3134-
if (vm.getState() == State.Stopped) {
3135-
// Remove instance from security groups
3136-
_securityGroupMgr.removeInstanceFromGroups(vm);
3137-
// Add instance in provided groups
3138-
_securityGroupMgr.addInstanceToGroups(vm, securityGroupIdList);
3139-
} else {
3140-
throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
3141-
}
3142-
}
3143-
}
31443108
List<? extends Nic> nics = _nicDao.listByVmId(vm.getId());
31453109
if (hostName != null) {
31463110
// Check is hostName is RFC compliant
@@ -3173,6 +3137,35 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31733137
.getUuid(), nic.getId(), extraDhcpOptionsMap);
31743138
}
31753139

3140+
boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
3141+
3142+
if (securityGroupIdList != null && isVMware) {
3143+
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3144+
} else if (securityGroupIdList != null){
3145+
DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
3146+
List<Long> networkIds = new ArrayList<>();
3147+
try {
3148+
if (zone.getNetworkType() == NetworkType.Basic) {
3149+
// Get default guest network in Basic zone
3150+
Network defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
3151+
networkIds.add(defaultNetwork.getId());
3152+
} else {
3153+
networkIds = networks.stream().map(Network::getId).collect(Collectors.toList());
3154+
}
3155+
} catch (InvalidParameterValueException e) {
3156+
if(logger.isDebugEnabled()) {
3157+
logger.debug(e.getMessage(),e);
3158+
}
3159+
}
3160+
3161+
if (_networkModel.checkSecurityGroupSupportForNetwork(
3162+
_accountMgr.getActiveAccountById(vm.getAccountId()),
3163+
zone, networkIds, securityGroupIdList)
3164+
) {
3165+
updateSecurityGroup(vm, securityGroupIdList);
3166+
}
3167+
}
3168+
31763169
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, userDataId,
31773170
userDataDetails, isDisplayVmEnabled, isDynamicallyScalable,
31783171
deleteProtection, customId, hostName, instanceName);
@@ -3188,6 +3181,17 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31883181
return _vmDao.findById(id);
31893182
}
31903183

3184+
private void updateSecurityGroup(UserVmVO vm, List<Long> securityGroupIdList) {
3185+
if (vm.getState() == State.Stopped) {
3186+
// Remove instance from security groups
3187+
_securityGroupMgr.removeInstanceFromGroups(vm);
3188+
// Add instance in provided groups
3189+
_securityGroupMgr.addInstanceToGroups(vm, securityGroupIdList);
3190+
} else {
3191+
throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
3192+
}
3193+
}
3194+
31913195
protected void updateUserData(UserVm vm) throws ResourceUnavailableException, InsufficientCapacityException {
31923196
boolean result = updateUserDataInternal(vm);
31933197
if (result) {

ui/src/views/compute/EditVM.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export default {
206206
zoneid: this.resource.zoneid
207207
}).then(response => {
208208
const zone = response?.listzonesresponse?.zone || []
209-
this.securityGroupsEnabled = zone?.[0]?.securitygroupsenabled
209+
this.securityGroupsEnabled = zone?.[0]?.securitygroupsenabled || this.$store.getters.showSecurityGroups
210210
})
211211
},
212212
fetchSecurityGroups () {

ui/src/views/compute/InstanceTab.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export default {
179179
vm: {},
180180
totalStorage: 0,
181181
currentTab: 'details',
182+
showUpdateSecurityGroupsModal: false,
182183
showAddVolumeModal: false,
183184
diskOfferings: [],
184185
annotations: [],

0 commit comments

Comments
 (0)