@@ -2289,6 +2289,7 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
22892289 String hostName = cmd .getHostName ();
22902290 Map <String ,String > details = cmd .getDetails ();
22912291 Account caller = CallContext .current ().getCallingAccount ();
2292+ List <Long > securityGroupIdList = cmd .getSecurityGroupIdList ();
22922293
22932294 // Input validation and permission checks
22942295 UserVmVO vmInstance = _vmDao .findById (id );
@@ -2339,7 +2340,7 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
23392340 }
23402341
23412342 return updateVirtualMachine (id , displayName , group , ha , isDisplayVm , osTypeId , userData , isDynamicallyScalable ,
2342- cmd .getHttpMethod (), cmd .getCustomId (), hostName , cmd .getInstanceName ());
2343+ cmd .getHttpMethod (), cmd .getCustomId (), hostName , cmd .getInstanceName (), securityGroupIdList );
23432344 }
23442345
23452346 private void saveUsageEvent (UserVmVO vm ) {
@@ -2389,7 +2390,8 @@ private void generateNetworkUsageForVm(VirtualMachine vm, boolean isDisplay, Str
23892390
23902391 @ Override
23912392 public UserVm updateVirtualMachine (long id , String displayName , String group , Boolean ha , Boolean isDisplayVmEnabled , Long osTypeId , String userData ,
2392- Boolean isDynamicallyScalable , HTTPMethod httpMethod , String customId , String hostName , String instanceName ) throws ResourceUnavailableException , InsufficientCapacityException {
2393+ Boolean isDynamicallyScalable , HTTPMethod httpMethod , String customId , String hostName , String instanceName , List <Long > securityGroupIdList )
2394+ throws ResourceUnavailableException , InsufficientCapacityException {
23932395 UserVmVO vm = _vmDao .findById (id );
23942396 if (vm == null ) {
23952397 throw new CloudRuntimeException ("Unable to find virual machine with id " + id );
@@ -2451,6 +2453,25 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
24512453 isDynamicallyScalable = vm .isDynamicallyScalable ();
24522454 }
24532455
2456+ // Get default guest network in Basic zone
2457+ DataCenterVO zone = _dcDao .findById (vm .getDataCenterId ());
2458+ Network defaultNetwork = _networkModel .getExclusiveGuestNetwork (zone .getId ());
2459+
2460+ boolean isVMware = (vm .getHypervisorType () == HypervisorType .VMware );
2461+
2462+ if (securityGroupIdList != null && isVMware ) {
2463+ throw new InvalidParameterValueException ("Security group feature is not supported for vmWare hypervisor" );
2464+ } else if (securityGroupIdList != null && _networkModel .isSecurityGroupSupportedInNetwork (defaultNetwork ) && _networkModel .canAddDefaultSecurityGroup ()) {
2465+ if (vm .getState () == State .Stopped ) {
2466+ // Remove instance from security groups
2467+ _securityGroupMgr .removeInstanceFromGroups (id );
2468+ // Add instance in provided groups
2469+ _securityGroupMgr .addInstanceToGroups (id , securityGroupIdList );
2470+ } else {
2471+ throw new InvalidParameterValueException ("Virtual machine must be stopped prior to update security groups " );
2472+ }
2473+ }
2474+
24542475 if (hostName != null ) {
24552476 // Check is hostName is RFC compliant
24562477 checkNameForRFCCompliance (hostName );
0 commit comments