@@ -2110,34 +2110,26 @@ public UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, Service
2110
2110
}
2111
2111
}
2112
2112
2113
- // if network is security group enabled, and default security group is not present in the list of groups specified, add it automatically
2113
+ // if network is security group enabled, and no security group is specified, then add the default security group automatically
2114
2114
if (isSecurityGroupEnabledNetworkUsed && !isVmWare && _networkMgr .canAddDefaultSecurityGroup ()) {
2115
- if (securityGroupIdList == null ) {
2116
- securityGroupIdList = new ArrayList <Long >();
2117
- }
2118
-
2119
- SecurityGroup defaultGroup = _securityGroupMgr .getDefaultSecurityGroup (owner .getId ());
2120
- if (defaultGroup != null ) {
2121
- //check if security group id list already contains Default security group, and if not - add it
2122
- boolean defaultGroupPresent = false ;
2123
- for (Long securityGroupId : securityGroupIdList ) {
2124
- if (securityGroupId .longValue () == defaultGroup .getId ()) {
2125
- defaultGroupPresent = true ;
2126
- break ;
2127
- }
2115
+
2116
+ //add the default securityGroup only if no security group is specified
2117
+ if (securityGroupIdList == null || securityGroupIdList .isEmpty ()){
2118
+ if (securityGroupIdList == null ) {
2119
+ securityGroupIdList = new ArrayList <Long >();
2128
2120
}
2129
-
2130
- if (!defaultGroupPresent ) {
2121
+
2122
+ SecurityGroup defaultGroup = _securityGroupMgr .getDefaultSecurityGroup (owner .getId ());
2123
+ if (defaultGroup != null ) {
2124
+ securityGroupIdList .add (defaultGroup .getId ());
2125
+ } else {
2126
+ //create default security group for the account
2127
+ if (s_logger .isDebugEnabled ()) {
2128
+ s_logger .debug ("Couldn't find default security group for the account " + owner + " so creating a new one" );
2129
+ }
2130
+ defaultGroup = _securityGroupMgr .createSecurityGroup (SecurityGroupManager .DEFAULT_GROUP_NAME , SecurityGroupManager .DEFAULT_GROUP_DESCRIPTION , owner .getDomainId (), owner .getId (), owner .getAccountName ());
2131
2131
securityGroupIdList .add (defaultGroup .getId ());
2132
2132
}
2133
-
2134
- } else {
2135
- //create default security group for the account
2136
- if (s_logger .isDebugEnabled ()) {
2137
- s_logger .debug ("Couldn't find default security group for the account " + owner + " so creating a new one" );
2138
- }
2139
- defaultGroup = _securityGroupMgr .createSecurityGroup (SecurityGroupManager .DEFAULT_GROUP_NAME , SecurityGroupManager .DEFAULT_GROUP_DESCRIPTION , owner .getDomainId (), owner .getId (), owner .getAccountName ());
2140
- securityGroupIdList .add (defaultGroup .getId ());
2141
2133
}
2142
2134
}
2143
2135
@@ -2759,7 +2751,7 @@ public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMach
2759
2751
}
2760
2752
2761
2753
//check if vm is security group enabled
2762
- if (_securityGroupMgr .isVmSecurityGroupEnabled (vmId ) && !_securityGroupMgr .isVmMappedToDefaultSecurityGroup (vmId ) && _networkMgr .canAddDefaultSecurityGroup ()) {
2754
+ if (_securityGroupMgr .isVmSecurityGroupEnabled (vmId ) && _securityGroupMgr . getSecurityGroupsForVm ( vmId ). isEmpty () && !_securityGroupMgr .isVmMappedToDefaultSecurityGroup (vmId ) && _networkMgr .canAddDefaultSecurityGroup ()) {
2763
2755
//if vm is not mapped to security group, create a mapping
2764
2756
if (s_logger .isDebugEnabled ()) {
2765
2757
s_logger .debug ("Vm " + vm + " is security group enabled, but not mapped to default security group; creating the mapping automatically" );
0 commit comments