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,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 ) {
0 commit comments