Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix NPE on updating security groups for an instance#10493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -28,7 +28,6 @@ | ||
| 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 UserVm updateVirtualMachine(long id, String displayName, String group, Bo | ||
| } | ||
| } | ||
| 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 UserVm updateVirtualMachine(long id, String displayName, String group, Bo | ||
| .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 UserVm updateVirtualMachine(long id, String displayName, String group, Bo | ||
| 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); | ||
| } | ||
| } | ||
harikrishna-patnala marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| 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 UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOff | ||
| 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 UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, Service | ||
| } 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) { | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.