Skip to content

Commit 4668ebe

Browse files
vishesh92harikrishna-patnala
authored andcommitted
Fix NPE on updating security groups for an instance (apache#10493)
* Fix NPE on updating security groups for an instance * addressed review comments * Method refactoring --------- Co-authored-by: Harikrishna Patnala <[email protected]>
1 parent ced6628 commit 4668ebe

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3236,11 +3236,29 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
32363236
.getUuid(), nic.getId(), extraDhcpOptionsMap);
32373237
}
32383238

3239+
checkAndUpdateSecurityGroupForVM(securityGroupIdList, vm, networks);
3240+
3241+
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, userDataId,
3242+
userDataDetails, isDisplayVmEnabled, isDynamicallyScalable,
3243+
deleteProtection, customId, hostName, instanceName);
3244+
3245+
if (updateUserdata) {
3246+
updateUserData(vm);
3247+
}
3248+
3249+
if (State.Running == vm.getState()) {
3250+
updateDns(vm, hostName);
3251+
}
3252+
3253+
return _vmDao.findById(id);
3254+
}
3255+
3256+
private void checkAndUpdateSecurityGroupForVM(List<Long> securityGroupIdList, UserVmVO vm, List<NetworkVO> networks) {
32393257
boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
32403258

32413259
if (securityGroupIdList != null && isVMware) {
3242-
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3243-
} else if (securityGroupIdList != null){
3260+
throw new InvalidParameterValueException("Security group feature is not supported for VMware hypervisor");
3261+
} else if (securityGroupIdList != null) {
32443262
DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
32453263
List<Long> networkIds = new ArrayList<>();
32463264
try {
@@ -3264,20 +3282,6 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
32643282
updateSecurityGroup(vm, securityGroupIdList);
32653283
}
32663284
}
3267-
3268-
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, userDataId,
3269-
userDataDetails, isDisplayVmEnabled, isDynamicallyScalable,
3270-
deleteProtection, customId, hostName, instanceName);
3271-
3272-
if (updateUserdata) {
3273-
updateUserData(vm);
3274-
}
3275-
3276-
if (State.Running == vm.getState()) {
3277-
updateDns(vm, hostName);
3278-
}
3279-
3280-
return _vmDao.findById(id);
32813285
}
32823286

32833287
private void updateSecurityGroup(UserVmVO vm, List<Long> securityGroupIdList) {
@@ -3287,7 +3291,7 @@ private void updateSecurityGroup(UserVmVO vm, List<Long> securityGroupIdList) {
32873291
// Add instance in provided groups
32883292
_securityGroupMgr.addInstanceToGroups(vm, securityGroupIdList);
32893293
} else {
3290-
throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
3294+
throw new InvalidParameterValueException(String.format("VM %s must be stopped prior to update security groups", vm.getUuid()));
32913295
}
32923296
}
32933297

@@ -3831,7 +3835,7 @@ public UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOff
38313835
boolean isVmWare = (template.getHypervisorType() == HypervisorType.VMware || (hypervisor != null && hypervisor == HypervisorType.VMware));
38323836

38333837
if (securityGroupIdList != null && isVmWare) {
3834-
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3838+
throw new InvalidParameterValueException("Security group feature is not supported for VMware hypervisor");
38353839
} else if (!isVmWare && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
38363840
//add the default securityGroup only if no security group is specified
38373841
if (securityGroupIdList == null || securityGroupIdList.isEmpty()) {
@@ -3891,7 +3895,7 @@ public UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, Service
38913895

38923896
} else if (securityGroupIdList != null && !securityGroupIdList.isEmpty()) {
38933897
if (isVmWare) {
3894-
throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3898+
throw new InvalidParameterValueException("Security group feature is not supported for VMware hypervisor");
38953899
}
38963900
// Only one network can be specified, and it should be security group enabled
38973901
if (networkIdList.size() > 1 && template.getHypervisorType() != HypervisorType.KVM && hypervisor != HypervisorType.KVM) {

ui/src/views/compute/EditVM.vue

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,27 +207,6 @@ export default {
207207
}).then(response => {
208208
const zone = response?.listzonesresponse?.zone || []
209209
this.securityGroupsEnabled = zone?.[0]?.securitygroupsenabled || this.$store.getters.showSecurityGroups
210-
if (this.securityGroupsEnabled) {
211-
api('listNetworks', { supportedservices: 'SecurityGroup' }).then(json => {
212-
if (json.listnetworksresponse && json.listnetworksresponse.network) {
213-
for (const net of json.listnetworksresponse.network) {
214-
if (this.securityGroupNetworkProviderUseThisVM) {
215-
break
216-
}
217-
const listVmParams = {
218-
id: this.resource.id,
219-
networkid: net.id,
220-
listall: true
221-
}
222-
api('listVirtualMachines', listVmParams).then(json => {
223-
if (json.listvirtualmachinesresponse && json.listvirtualmachinesresponse?.virtualmachine?.length > 0) {
224-
this.securityGroupNetworkProviderUseThisVM = true
225-
}
226-
})
227-
}
228-
}
229-
})
230-
}
231210
})
232211
},
233212
fetchSecurityGroups () {

ui/src/views/compute/InstanceTab.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export default {
249249
vm: {},
250250
totalStorage: 0,
251251
currentTab: 'details',
252+
showUpdateSecurityGroupsModal: false,
252253
showAddVolumeModal: false,
253254
showUpdateSecurityGroupsModal: false,
254255
diskOfferings: [],

0 commit comments

Comments
 (0)