Skip to content

Commit 351b012

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

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

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

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.net.URLDecoder;
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
31-
import java.util.Collections;
3231
import java.util.Date;
3332
import java.util.HashMap;
3433
import java.util.HashSet;
@@ -3105,42 +3104,6 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31053104
}
31063105
}
31073106

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-
}
31443107
List<? extends Nic> nics = _nicDao.listByVmId(vm.getId());
31453108
if (hostName != null) {
31463109
// Check is hostName is RFC compliant
@@ -3173,6 +3136,35 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31733136
.getUuid(), nic.getId(), extraDhcpOptionsMap);
31743137
}
31753138

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

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