Skip to content

Commit 1f93087

Browse files
authored
Merge pull request #1978 from Accelerite/ReleaseNicSecondaryIP
CLOUDSTACK-9779 : Releasing secondary guest IP fails with error VM nic Ip x.x.x.x is mapped to load balancing rule
2 parents f0dd599 + feb5fa9 commit 1f93087

File tree

5 files changed

+71
-20
lines changed

5 files changed

+71
-20
lines changed

engine/schema/src/com/cloud/network/dao/LoadBalancerDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ public interface LoadBalancerDao extends GenericDao<LoadBalancerVO, Long> {
2929

3030
List<LoadBalancerVO> listInTransitionStateByNetworkIdAndScheme(long networkId, Scheme scheme);
3131

32+
boolean isLoadBalancerRulesMappedToVmGuestIp(long instanceId, String instanceIp, long networkId);
33+
3234
}

engine/schema/src/com/cloud/network/dao/LoadBalancerDaoImpl.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import javax.inject.Inject;
2222

23+
import com.cloud.network.rules.FirewallRule;
24+
import com.cloud.utils.db.JoinBuilder;
2325
import org.springframework.stereotype.Component;
2426

2527
import com.cloud.network.rules.FirewallRule.State;
@@ -36,6 +38,8 @@ public class LoadBalancerDaoImpl extends GenericDaoBase<LoadBalancerVO, Long> im
3638

3739
@Inject
3840
protected FirewallRulesCidrsDao _portForwardingRulesCidrsDao;
41+
@Inject
42+
LoadBalancerVMMapDao _loadBalancerVMMapDao;
3943

4044
protected LoadBalancerDaoImpl() {
4145
ListByIp = createSearchBuilder();
@@ -75,4 +79,40 @@ public List<LoadBalancerVO> listInTransitionStateByNetworkIdAndScheme(long netwo
7579
return listBy(sc);
7680
}
7781

82+
@Override
83+
public boolean isLoadBalancerRulesMappedToVmGuestIp(long instanceId, String instanceIp, long networkId)
84+
{
85+
SearchBuilder<LoadBalancerVMMapVO> lbVmMapSearch = _loadBalancerVMMapDao.createSearchBuilder();
86+
lbVmMapSearch.and("instanceIp", lbVmMapSearch.entity().getInstanceIp(),SearchCriteria.Op.EQ);
87+
lbVmMapSearch.and("instanceId", lbVmMapSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
88+
89+
SearchBuilder<LoadBalancerVO> firewallRuleIdSearch = createSearchBuilder();
90+
firewallRuleIdSearch.selectFields(firewallRuleIdSearch.entity().getId());
91+
firewallRuleIdSearch.and("networkId",firewallRuleIdSearch.entity().getNetworkId(),Op.EQ);
92+
firewallRuleIdSearch.and("purpose",firewallRuleIdSearch.entity().getPurpose(),Op.EQ);
93+
firewallRuleIdSearch.and("state",firewallRuleIdSearch.entity().getState(),Op.NEQ);
94+
firewallRuleIdSearch.join("LoadBalancerRuleList", lbVmMapSearch, lbVmMapSearch.entity().getLoadBalancerId(), firewallRuleIdSearch.entity().getId(), JoinBuilder.JoinType.INNER);
95+
96+
firewallRuleIdSearch.done();
97+
lbVmMapSearch.done();
98+
99+
SearchCriteria<LoadBalancerVO> sc = firewallRuleIdSearch.create();
100+
sc.setParameters("state", State.Revoke);
101+
sc.setParameters("networkId", networkId);
102+
sc.setParameters("purpose", FirewallRule.Purpose.LoadBalancing);
103+
104+
sc.setJoinParameters("LoadBalancerRuleList", "instanceIp", instanceIp);
105+
sc.setJoinParameters("LoadBalancerRuleList", "instanceId", instanceId);
106+
107+
List<LoadBalancerVO> lbRuleList = customSearch(sc, null);
108+
109+
if(lbRuleList == null || lbRuleList.size() > 0) {
110+
return true;
111+
}
112+
113+
return false;
114+
}
115+
116+
117+
78118
}

server/src/com/cloud/network/NetworkServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
import com.cloud.vm.dao.NicSecondaryIpVO;
198198
import com.cloud.vm.dao.UserVmDao;
199199
import com.cloud.vm.dao.VMInstanceDao;
200+
import com.cloud.network.dao.LoadBalancerDao;
200201

201202
/**
202203
* NetworkServiceImpl implements NetworkService.
@@ -335,6 +336,9 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
335336
@Inject
336337
NetworkDetailsDao _networkDetailsDao;
337338

339+
@Inject
340+
LoadBalancerDao _loadBalancerDao;
341+
338342
int _cidrLimit;
339343
boolean _allowSubdomainNetworkAccess;
340344

@@ -852,7 +856,7 @@ public boolean releaseSecondaryIpFromNic(long ipAddressId) {
852856
throw new InvalidParameterValueException("Can' remove the ip " + secondaryIp + "is associate with static NAT rule public IP address id " + publicIpVO.getId());
853857
}
854858

855-
if (_lbService.isLbRuleMappedToVmGuestIp(secondaryIp)) {
859+
if (_loadBalancerDao.isLoadBalancerRulesMappedToVmGuestIp(vm.getId(), secondaryIp, network.getId())) {
856860
s_logger.debug("VM nic IP " + secondaryIp + " is mapped to load balancing rule");
857861
throw new InvalidParameterValueException("Can't remove the secondary ip " + secondaryIp + " is mapped to load balancing rule");
858862
}

server/test/org/apache/cloudstack/networkoffering/CreateNetworkOfferingTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import javax.inject.Inject;
2626

27+
import com.cloud.network.dao.LoadBalancerVMMapDao;
2728
import junit.framework.TestCase;
2829

2930
import org.apache.cloudstack.context.CallContext;
@@ -93,6 +94,9 @@ public class CreateNetworkOfferingTest extends TestCase {
9394
@Inject
9495
UserIpAddressDetailsDao userIpAddressDetailsDao;
9596

97+
@Inject
98+
LoadBalancerVMMapDao _loadBalancerVMMapDao;
99+
96100
@Override
97101
@Before
98102
public void setUp() {

server/test/resources/createNetworkOffering.xml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
1313
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
1414
xsi:schemaLocation="http://www.springframework.org/schema/beans
15-
http://www.springframework.org/schema/beans/spring-beans.xsd
15+
http://www.springframework.org/schema/beans/spring-beans.xsd
1616
http://www.springframework.org/schema/tx
17-
http://www.springframework.org/schema/tx/spring-tx.xsd
17+
http://www.springframework.org/schema/tx/spring-tx.xsd
1818
http://www.springframework.org/schema/aop
19-
http://www.springframework.org/schema/aop/spring-aop.xsd
19+
http://www.springframework.org/schema/aop/spring-aop.xsd
2020
http://www.springframework.org/schema/context
21-
http://www.springframework.org/schema/context/spring-context.xsd">
21+
http://www.springframework.org/schema/context/spring-context.xsd">
2222

2323
<context:annotation-config />
2424

@@ -34,23 +34,24 @@
3434
<ref bean="transactionContextBuilder" />
3535
<ref bean="actionEventInterceptor" />
3636
</list>
37-
</property>
38-
</bean>
39-
40-
<bean id="nicIpAliasDaoImpl" class="com.cloud.vm.dao.NicIpAliasDaoImpl" />
41-
<bean id="ConfigurationManager" class="com.cloud.configuration.ConfigurationManagerImpl">
42-
<property name="name" value="ConfigurationManager"/>
43-
</bean>
44-
45-
<bean class="org.apache.cloudstack.networkoffering.ChildTestConfiguration" />
37+
</property>
38+
</bean>
39+
40+
<bean id="nicIpAliasDaoImpl" class="com.cloud.vm.dao.NicIpAliasDaoImpl" />
41+
<bean id="ConfigurationManager" class="com.cloud.configuration.ConfigurationManagerImpl">
42+
<property name="name" value="ConfigurationManager"/>
43+
</bean>
44+
45+
<bean class="org.apache.cloudstack.networkoffering.ChildTestConfiguration" />
4646
<bean id="UservmDetailsDaoImpl" class="com.cloud.vm.dao.UserVmDetailsDaoImpl" />
4747
<bean id="hostGpuGroupsDaoImpl" class="com.cloud.gpu.dao.HostGpuGroupsDaoImpl" />
48-
<bean id="vGPUTypesDaoImpl" class="com.cloud.gpu.dao.VGPUTypesDaoImpl" />
49-
<bean id="usageEventDaoImpl" class="com.cloud.event.dao.UsageEventDaoImpl" />
50-
<bean id="usageEventDetailsDaoImpl" class="com.cloud.event.dao.UsageEventDetailsDaoImpl" />
48+
<bean id="vGPUTypesDaoImpl" class="com.cloud.gpu.dao.VGPUTypesDaoImpl" />
49+
<bean id="usageEventDaoImpl" class="com.cloud.event.dao.UsageEventDaoImpl" />
50+
<bean id="usageEventDetailsDaoImpl" class="com.cloud.event.dao.UsageEventDetailsDaoImpl" />
5151
<bean id="storagePoolHostDaoImpl" class="com.cloud.storage.dao.StoragePoolHostDaoImpl" />
52-
<bean id="storagePoolTagsDaoImpl" class="com.cloud.storage.dao.StoragePoolTagsDaoImpl" />
52+
<bean id="storagePoolTagsDaoImpl" class="com.cloud.storage.dao.StoragePoolTagsDaoImpl" />
5353
<bean id="primaryDataStoreDaoImpl" class="org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDaoImpl" />
5454
<bean id="userIpAddressDetailsDao" class="org.apache.cloudstack.resourcedetail.dao.UserIpAddressDetailsDaoImpl" />
55-
56-
</beans>
55+
<bean id="loadBalancerVMMapDaoImpl" class="com.cloud.network.dao.LoadBalancerVMMapDaoImpl" />
56+
57+
</beans>

0 commit comments

Comments
 (0)