2828import java.net.URLDecoder;
2929import java.util.ArrayList;
3030import java.util.Arrays;
31- import java.util.Collections;
3231import java.util.Date;
3332import java.util.HashMap;
3433import java.util.HashSet;
@@ -3205,42 +3204,6 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
32053204 }
32063205 }
32073206
3208- boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
3209-
3210- if (securityGroupIdList != null && isVMware) {
3211- throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3212- } else {
3213- // Get default guest network in Basic zone
3214- Network defaultNetwork = null;
3215- try {
3216- DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
3217- if (zone.getNetworkType() == NetworkType.Basic) {
3218- // Get default guest network in Basic zone
3219- defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
3220- } else if (_networkModel.checkSecurityGroupSupportForNetwork(_accountMgr.getActiveAccountById(vm.getAccountId()), zone, Collections.emptyList(), securityGroupIdList)) {
3221- NicVO defaultNic = _nicDao.findDefaultNicForVM(vm.getId());
3222- if (defaultNic != null) {
3223- defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
3224- }
3225- }
3226- } catch (InvalidParameterValueException e) {
3227- if(logger.isDebugEnabled()) {
3228- logger.debug(e.getMessage(),e);
3229- }
3230- defaultNetwork = _networkModel.getDefaultNetworkForVm(id);
3231- }
3232-
3233- if (securityGroupIdList != null && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
3234- if (vm.getState() == State.Stopped) {
3235- // Remove instance from security groups
3236- _securityGroupMgr.removeInstanceFromGroups(vm);
3237- // Add instance in provided groups
3238- _securityGroupMgr.addInstanceToGroups(vm, securityGroupIdList);
3239- } else {
3240- throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
3241- }
3242- }
3243- }
32443207 List<? extends Nic> nics = _nicDao.listByVmId(vm.getId());
32453208 if (hostName != null) {
32463209 // Check is hostName is RFC compliant
@@ -3273,6 +3236,35 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
32733236 .getUuid(), nic.getId(), extraDhcpOptionsMap);
32743237 }
32753238
3239+ boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
3240+
3241+ if (securityGroupIdList != null && isVMware) {
3242+ throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
3243+ } else if (securityGroupIdList != null){
3244+ DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
3245+ List<Long> networkIds = new ArrayList<>();
3246+ try {
3247+ if (zone.getNetworkType() == NetworkType.Basic) {
3248+ // Get default guest network in Basic zone
3249+ Network defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
3250+ networkIds.add(defaultNetwork.getId());
3251+ } else {
3252+ networkIds = networks.stream().map(Network::getId).collect(Collectors.toList());
3253+ }
3254+ } catch (InvalidParameterValueException e) {
3255+ if(logger.isDebugEnabled()) {
3256+ logger.debug(e.getMessage(),e);
3257+ }
3258+ }
3259+
3260+ if (_networkModel.checkSecurityGroupSupportForNetwork(
3261+ _accountMgr.getActiveAccountById(vm.getAccountId()),
3262+ zone, networkIds, securityGroupIdList)
3263+ ) {
3264+ updateSecurityGroup(vm, securityGroupIdList);
3265+ }
3266+ }
3267+
32763268 _vmDao.updateVM(id, displayName, ha, osTypeId, userData, userDataId,
32773269 userDataDetails, isDisplayVmEnabled, isDynamicallyScalable,
32783270 deleteProtection, customId, hostName, instanceName);
@@ -3288,6 +3280,17 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
32883280 return _vmDao.findById(id);
32893281 }
32903282
3283+ private void updateSecurityGroup(UserVmVO vm, List<Long> securityGroupIdList) {
3284+ if (vm.getState() == State.Stopped) {
3285+ // Remove instance from security groups
3286+ _securityGroupMgr.removeInstanceFromGroups(vm);
3287+ // Add instance in provided groups
3288+ _securityGroupMgr.addInstanceToGroups(vm, securityGroupIdList);
3289+ } else {
3290+ throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
3291+ }
3292+ }
3293+
32913294 protected void updateUserData(UserVm vm) throws ResourceUnavailableException, InsufficientCapacityException {
32923295 boolean result = updateUserDataInternal(vm);
32933296 if (result) {
0 commit comments