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 ;
@@ -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,8 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31733136 .getUuid (), nic .getId (), extraDhcpOptionsMap );
31743137 }
31753138
3139+ checkAndUpdateSecurityGroupForVM (securityGroupIdList , vm , networks );
3140+
31763141 _vmDao .updateVM (id , displayName , ha , osTypeId , userData , userDataId ,
31773142 userDataDetails , isDisplayVmEnabled , isDynamicallyScalable ,
31783143 deleteProtection , customId , hostName , instanceName );
@@ -3188,6 +3153,48 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
31883153 return _vmDao .findById (id );
31893154 }
31903155
3156+ private void checkAndUpdateSecurityGroupForVM (List <Long > securityGroupIdList , UserVmVO vm , List <NetworkVO > networks ) {
3157+ boolean isVMware = (vm .getHypervisorType () == HypervisorType .VMware );
3158+
3159+ if (securityGroupIdList != null && isVMware ) {
3160+ throw new InvalidParameterValueException ("Security group feature is not supported for VMware hypervisor" );
3161+ } else if (securityGroupIdList != null ) {
3162+ DataCenterVO zone = _dcDao .findById (vm .getDataCenterId ());
3163+ List <Long > networkIds = new ArrayList <>();
3164+ try {
3165+ if (zone .getNetworkType () == NetworkType .Basic ) {
3166+ // Get default guest network in Basic zone
3167+ Network defaultNetwork = _networkModel .getExclusiveGuestNetwork (zone .getId ());
3168+ networkIds .add (defaultNetwork .getId ());
3169+ } else {
3170+ networkIds = networks .stream ().map (Network ::getId ).collect (Collectors .toList ());
3171+ }
3172+ } catch (InvalidParameterValueException e ) {
3173+ if (logger .isDebugEnabled ()) {
3174+ logger .debug (e .getMessage (),e );
3175+ }
3176+ }
3177+
3178+ if (_networkModel .checkSecurityGroupSupportForNetwork (
3179+ _accountMgr .getActiveAccountById (vm .getAccountId ()),
3180+ zone , networkIds , securityGroupIdList )
3181+ ) {
3182+ updateSecurityGroup (vm , securityGroupIdList );
3183+ }
3184+ }
3185+ }
3186+
3187+ private void updateSecurityGroup (UserVmVO vm , List <Long > securityGroupIdList ) {
3188+ if (vm .getState () == State .Stopped ) {
3189+ // Remove instance from security groups
3190+ _securityGroupMgr .removeInstanceFromGroups (vm );
3191+ // Add instance in provided groups
3192+ _securityGroupMgr .addInstanceToGroups (vm , securityGroupIdList );
3193+ } else {
3194+ throw new InvalidParameterValueException (String .format ("VM %s must be stopped prior to update security groups" , vm .getUuid ()));
3195+ }
3196+ }
3197+
31913198 protected void updateUserData (UserVm vm ) throws ResourceUnavailableException , InsufficientCapacityException {
31923199 boolean result = updateUserDataInternal (vm );
31933200 if (result ) {
@@ -3695,7 +3702,7 @@ public UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOff
36953702 boolean isVmWare = (template .getHypervisorType () == HypervisorType .VMware || (hypervisor != null && hypervisor == HypervisorType .VMware ));
36963703
36973704 if (securityGroupIdList != null && isVmWare ) {
3698- throw new InvalidParameterValueException ("Security group feature is not supported for vmWare hypervisor" );
3705+ throw new InvalidParameterValueException ("Security group feature is not supported for VMware hypervisor" );
36993706 } else if (!isVmWare && _networkModel .isSecurityGroupSupportedInNetwork (defaultNetwork ) && _networkModel .canAddDefaultSecurityGroup ()) {
37003707 //add the default securityGroup only if no security group is specified
37013708 if (securityGroupIdList == null || securityGroupIdList .isEmpty ()) {
@@ -3755,7 +3762,7 @@ public UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, Service
37553762
37563763 } else if (securityGroupIdList != null && !securityGroupIdList .isEmpty ()) {
37573764 if (isVmWare ) {
3758- throw new InvalidParameterValueException ("Security group feature is not supported for vmWare hypervisor" );
3765+ throw new InvalidParameterValueException ("Security group feature is not supported for VMware hypervisor" );
37593766 }
37603767 // Only one network can be specified, and it should be security group enabled
37613768 if (networkIdList .size () > 1 && template .getHypervisorType () != HypervisorType .KVM && hypervisor != HypervisorType .KVM ) {
0 commit comments